I wonder, if somebody at Unity ever tested, if that callback works properly. This is so annoying - I filed a bug report after wasting days of trying to figure out what’s going on!
I am starting the microphone with 44100 khz - same as project frequency
and playing it back over an audiosource.
_recClip = Microphone.Start(d.name, true, 2, SampleFrequency);
RecSource.clip = _recClip;
while (!(Microphone.GetPosition(null) > 1)) ;
var t = AudioSettings.dspTime;
RecSource.PlayScheduled(t + MicClipDelay);
I use
void OnAudioFilterRead(float[ ] inBuf, int channels)
(Why the heck are there no arguments which tell something about the associated timestamp and samplerate?)
to read the incoming data and store it away in some buffers on the audiothread
In the main thread I copy the samples into an other clip chunk by chunk into the correct sample positions.
This all works, when I set the microphone in the AudioMidi-Settings on Mac to 44100.
When I change it to 48000 (which is the default setting) it does not work any more.
Allthough the _recClip shows 44100 it seems not to be, because the incoming data on OnAudioFilterRead is somehow slower and pitched down.
So one could say: “just set the mac to 44100” yes. But maybe you guessed it. When I take the thing to Android it’s even more f*cked up. There frequencies differ on each device ranging from 8khz to 48000
iOS seems to work at least on my devices.
So this is more a Bug report because I think there is no solution for that. Except for interpolating the data into the correct sample rate by myself, which is impossible, because I do not know the samplerate of the incoming data.
Or is there?