twang icon indicating copy to clipboard operation
twang copied to clipboard

sample rate of 48000 in streams

Open m4b opened this issue 3 years ago • 3 comments

I'm hitting this assert here: https://docs.rs/twang/latest/src/twang/synth.rs.html#54

Is this intended? I have a sample rate of 44100 coming from default cpal config, is it a requirement the sample rate is 48000, if i want to do, e.g.:

        let mut audio = Audio::<Ch32, 1>::with_silence(self.sample_rate.0, nsamples);
        synth.stream(audio.sink());

m4b avatar Feb 05 '22 06:02 m4b

@m4b Currently, that's how it works (48000 only) but I'm starting to re-think that decision (it should work at any sample rate). In the meantime, fon::Stream can be used to resample the audio if it must be 44100.

AldaronLau avatar Feb 05 '22 18:02 AldaronLau

Ah interesting; yea I can try resampling with Stream; just curious, what is the reason the sample rate is hardcoded?

m4b avatar Feb 05 '22 18:02 m4b

@m4b It was a simplification I did when changing the API. In the new twang, all of the oscillators are independent of each other and do not share state with the Synth, like they did in the previous version. The Synth used to store sample rate, and decide how much to move the frequency counter based on that. Now there's no frequency counter, so no way for the oscillators to know what the sample rate is, so I made them all assume 48000. I should be able to change it, once I figure out #16. There might be another way to support other sample rates before that, but it needs some thought.

AldaronLau avatar Feb 05 '22 18:02 AldaronLau