Skip to content

Commit 6498406

Browse files
committed
wav: return None instead of EQUILIBRIUM for unsupported bit depths
Instead of returning EQUILIBRIUM (0.0) when encountering WAV bit depths > 32, return None to terminate the iterator. Also add error logging through tracing or stdout to indicate the unsupported format.
1 parent f076a25 commit 6498406

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/conversions/sample.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ impl Sample for f32 {
144144
fn to_f32(self) -> f32 {
145145
self
146146
}
147+
148+
#[inline]
149+
fn is_zero(self) -> bool {
150+
2.0 * (self - Self::ZERO_VALUE).abs()
151+
<= f32::EPSILON * (self.abs() + Self::ZERO_VALUE.abs())
152+
}
147153
}
148154

149155
#[cfg(test)]

src/decoder/wav.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ where
9494
value
9595
})
9696
} else {
97-
// > 32 bits we cannot handle, so we'll just return equilibrium
98-
// and let the iterator continue
99-
Some(Self::Item::EQUILIBRIUM)
97+
#[cfg(feature = "tracing")]
98+
tracing::error!("Unsupported WAV float bit depth: {}", bits);
99+
#[cfg(not(feature = "tracing"))]
100+
println!("Unsupported WAV float bit depth: {}", bits);
101+
None
100102
}
101103
}
102104

@@ -126,17 +128,17 @@ where
126128
(SampleFormat::Int, bits) => {
127129
// Unofficial WAV integer bit depth, try to handle it anyway
128130
let next_i32: Option<Result<i32, _>> = self.reader.samples().next();
129-
next_i32.and_then(|value| {
130-
value.ok().map(|value| {
131-
if bits <= 32 {
132-
(value << (32 - bits)).to_sample()
133-
} else {
134-
// > 32 bits we cannot handle, so we'll just return
135-
// equilibrium and let the iterator continue
136-
Self::Item::EQUILIBRIUM
137-
}
131+
if bits <= 32 {
132+
next_i32.and_then(|value| {
133+
value.ok().map(|value| (value << (32 - bits)).to_sample())
138134
})
139-
})
135+
} else {
136+
#[cfg(feature = "tracing")]
137+
tracing::error!("Unsupported WAV integer bit depth: {}", bits);
138+
#[cfg(not(feature = "tracing"))]
139+
println!("Unsupported WAV integer bit depth: {}", bits);
140+
None
141+
}
140142
}
141143
};
142144
next_sample

0 commit comments

Comments
 (0)