Skip to content

Commit 48cc442

Browse files
committed
Fix WAV sample conversion on f32 samples
- Fix broken playback of float WAV samples - Simplify bit depth handling by using hound's native conversions where possible - Maintain fallback handling for non-standard bit depths
1 parent a1cf45a commit 48cc442

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

src/decoder/wav.rs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -79,51 +79,47 @@ where
7979
match (spec.sample_format, spec.bits_per_sample as u32) {
8080
(SampleFormat::Float, bits) => {
8181
if bits == 32 {
82-
self.reader.samples().next().and_then(|value| value.ok())
82+
let next_f32: Option<Result<f32, _>> = self.reader.samples().next();
83+
next_f32.and_then(|value| value.ok().map(|value| value.to_sample()))
8384
} else {
8485
// > 32 bits we cannot handle, so we'll just return equilibrium
8586
// and let the iterator continue
8687
Some(Self::Item::EQUILIBRIUM)
8788
}
8889
}
8990

91+
(SampleFormat::Int, 8) => {
92+
let next_i8: Option<Result<i8, _>> = self.reader.samples().next();
93+
next_i8.and_then(|value| value.ok().map(|value| value.to_sample()))
94+
}
95+
(SampleFormat::Int, 16) => {
96+
let next_i16: Option<Result<i16, _>> = self.reader.samples().next();
97+
next_i16.and_then(|value| value.ok().map(|value| value.to_sample()))
98+
}
99+
(SampleFormat::Int, 24) => {
100+
let next_i24_in_i32: Option<Result<i32, _>> = self.reader.samples().next();
101+
next_i24_in_i32.and_then(|value| {
102+
value.ok().and_then(I24::new).map(|value| value.to_sample())
103+
})
104+
}
105+
(SampleFormat::Int, 32) => {
106+
let next_i32: Option<Result<i32, _>> = self.reader.samples().next();
107+
next_i32.and_then(|value| value.ok().map(|value| value.to_sample()))
108+
}
90109
(SampleFormat::Int, bits) => {
91-
let next_i32 = self.reader.samples().next();
92-
match bits {
93-
8 => next_i32.and_then(|value| {
94-
value
95-
.ok()
96-
.map(|value| (value as i8).to_sample::<Self::Item>())
97-
}),
98-
16 => next_i32.and_then(|value| {
99-
value
100-
.ok()
101-
.map(|value| (value as i16).to_sample::<Self::Item>())
102-
}),
103-
24 => next_i32.and_then(|value| {
104-
value
105-
.ok()
106-
.and_then(I24::new)
107-
.map(|value| value.to_sample::<Self::Item>())
108-
}),
109-
32 => next_i32.and_then(|value| {
110-
value.ok().map(|value| value.to_sample::<Self::Item>())
111-
}),
112-
_ => {
113-
// Unofficial WAV integer bit depth, try to handle it anyway
114-
next_i32.and_then(|value| {
115-
value.ok().map(|value| {
116-
if bits <= 32 {
117-
(value << (32 - bits)).to_sample::<Self::Item>()
118-
} else {
119-
// > 32 bits we cannot handle, so we'll just return
120-
// equilibrium and let the iterator continue
121-
Self::Item::EQUILIBRIUM
122-
}
123-
})
124-
})
125-
}
126-
}
110+
// Unofficial WAV integer bit depth, try to handle it anyway
111+
let next_i32: Option<Result<i32, _>> = self.reader.samples().next();
112+
next_i32.and_then(|value| {
113+
value.ok().map(|value| {
114+
if bits <= 32 {
115+
(value << (32 - bits)).to_sample()
116+
} else {
117+
// > 32 bits we cannot handle, so we'll just return
118+
// equilibrium and let the iterator continue
119+
Self::Item::EQUILIBRIUM
120+
}
121+
})
122+
})
127123
}
128124
};
129125
next_sample

0 commit comments

Comments
 (0)