- Home /
Copying buffer data in real time
I am trying to send incoming microphone audio from Unity to Wwise in real time. I have a "producer thread" and a "consumer thread". I am trying to copy my incoming buffer from the producer to the consumer.
// Unity callback on microphone input (“producer thread“)
void OnAudioFilterRead(float[] data, int channels)
{
// acquire ownership of mutex and buffer
mutex.WaitOne();
// copy samples to buffer (de–interleave channels)
for (int i = 0; i < data.Length / channels; i++)
{
buffer.Add(data[i * channels]);
}
// release ownership of mutex and buffer
mutex.ReleaseMutex();
}
// Wwise callback that sends buffered samples to Wwise (“consumer thread“)
bool AudioSamplesDelegate(uint playingID, uint channelIndex, float[] samples)
{
// acquire ownership of mutex and buffer
mutex.WaitOne();
// copy samples from buffer to temporary block
int blockSize = Math.Min(buffer.Count, samples.Length);
List<float> block = buffer.GetRange(0, blockSize);
buffer.RemoveRange(0, blockSize);
// release ownership of mutex and buffer (release mutex as quickly as possible)
mutex.ReleaseMutex();
// copy samples from temporary block to output array
block.CopyTo(samples);
// Return false to indicate that there is no more data to provide. This will also stop the associated event.
return IsPlaying;
}
This works but the audio I get from Wwise is glitchy. Any inputs on the best method to do this/improve this? Are circular buffers the way to go
Comment
Your answer
Follow this Question
Related Questions
Voice Chat Prototype 0 Answers
how to downsampling audio data 0 Answers
How can I set audio to play as if it was into the recording device? 1 Answer
Get the device audio volume 0 Answers