dasp icon indicating copy to clipboard operation
dasp copied to clipboard

Current best practice for the type of audio data in callback function?

Open Dicklessgreat opened this issue 1 year ago • 0 comments

Hi! So nice to post here first time. I want to ask current best practice for the signature of an audio callback function with dasp.

I'm currently working on embedded system with ADDA connected with stm32H7 via SAI. Now I'm making a synthesizer and it doesn't know how much audio block length is, and how much channels are. But my BSP crate provide me an audio callback like that.

fn bsp_audio_callback<const CHAN: usize, const LEN: usize>(block: [[f32: CHAN]; LEN]) {
    call_my_impl_func(block);
}

If I implement synthesizer's callback function as generics, should I implement like that?

impl<T: dasp_sample::FloatSample> Synth<T> {
    fn synth_block<U: dasp_frame::Frame>(&mut self, block: &mut [U]) 
    where
    <U as Frame>::Sample: dasp_sample::FromSample<T>,
    {
        let tick = ...;
        for frame in block {
            for mut chan in frame.channels_mut() {
                let v: T = self.oscillator.sin(tick);
                *chan = <U as bare_synth::dasp_frame::Frame>::Sample::from_sample(
                    v.to_sample(),
                );
            }
        }
    }
}

Or, should I use dasp_slice trait for the block? Any comment, suggestion, example, or snippet much appreciated. Thanks.

Dicklessgreat avatar Mar 31 '24 13:03 Dicklessgreat