how to read f32 audio samples from microphone in rust cpal library? trying to decode DTMF signals in realtime

407 Views Asked by At

I am trying to decode dtmf signals using rust in real time, and I have a working example that decodes them from wav files. But I am trying to do it in real time from the soundcard, by providing data, that came from microphone, to function (Decoder::process(&data)), but I think it receives F32 vector of samples. Can you provide an example on how to properly read sound from microphone in a needed way? Here is my example from https://gitlab.scd31.com/stephen/dtmf add cargo dependency dtmf = "0.1.5"

use dtmf::decoder::Decoder;
use rodio::{Decoder as RDecoder, Source};
use std::fs::File;
use std::io::BufReader;

fn main() {
    // Load in our audio samples
    // This can also be done in real time from the sound card
    let file = BufReader::new(File::open("data/dtmf_test.wav").unwrap());
    let source = RDecoder::new(file).unwrap();
    let samples = source.convert_samples();
    let sample_rate = samples.sample_rate();
    let data: Vec<f32> = samples.collect();

    // set up our decoder
    let mut decoder = Decoder::new(sample_rate, |tone, state| {
        println!("{:?}: {:?}", tone, state);
    });

    // can process all samples at once, or in smaller batches
    decoder.process(&data);
}

This thing takes wav file and decodes it. but I am trying to do it real time. Thank you.

0

There are 0 best solutions below