Open jBernavaPrah opened 8 months ago
@jBernavaPrah thanks for opening an issue! If I understand right, you want to resample in realtime? I don't have an example for that, which I should probably fix.
Resampling after the fact for a 3 second audio in going to be simpler:
let writer: WavWriterHandle = todo!();
let audio: Vec<f32> = todo!();
let audio = Audio::<Ch32, 1>::with_f32_buffer(48_000, audio.as_slice());
let audio = Audio::<Ch16, 1>::with_audio(16_000, &audio);
for sample in audio.as_f32_slice().iter().cloned() {
writer.write_sample(sample).ok();
}
For realtime conversion (something like this):
pub struct MyOutput {
writer: WavWriterHandle,
}
impl fon::Sink<fon::chan::Ch16, 1> for MyOutput {
fn sample_rate(&self) -> NonZeroU32 {
NonZeroU32::try_from(16_000).unwrap()
}
fn len(&self) -> usize {
// 3 seconds times sample rate
3 * 16_000
}
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = fon::Frame<fon::chan::Ch16, 1>>) {
for sample in iter {
self.writer.write_sample(sample).ok();
}
}
}
fn write_input_data(
input: &[f32],
sink_to: &mut SinkTo<fon::chan::Ch16, fon::chan::Ch16, MyOutput, 1, 1>.
stream: &mut fon::Stream<1>,
) {
let audio = Audio::with_f32_buffer(48_000, input.to_vec());
stream.pipe(audio, sink_to);
}
let sink: SinkTo<fon::chan::Ch16, fon::chan::Ch32, MyOutput, 1, 1> = fon::SinkTo::new(MyOutput { writer });
Sorry, I don't have time to test it right now, but should be a start at least. I'll let you know when I make a more complete example later.
Hi!
Thanks for your crate. I'm not so sure if this will resolve my headache, but I will try anyway :)
I have a stream of Vec that is generated by the microphone read by the .
cpal
crate, which is a 48000hz 32bit mono channel and I need to convert it into a 16000 16bit mono channel VecI tried to understand where and how to put the code from this crate but without success.
Could you help me to achieve the goal?
Let me know if you need anything else.
Thanks a lot! Ps: I'm still new to rust so if something is a little odd to you, it's sure my fault.
Here is my minimal example: This was taken by this example