kcat / openal-soft

OpenAL Soft is a software implementation of the OpenAL 3D audio API.
Other
2.18k stars 526 forks source link

Mixing two microphone streams #539

Open minghia opened 3 years ago

minghia commented 3 years ago

We have two microphones and we want to mix the two inputs before packetising and sending them around our network. It is possible to do this with OpenAl. I thought we could make each stream a source and mix using a listener and then getting the output from the listener and packetising that.

Currently since one of the microphones is not plugged in but we are getting random values from that input, that other end receiving the packets is reassembling them in a strange order so that the audio slows down when played back on the remote system.

Any help would be most appreciated.

kcat commented 3 years ago

We have two microphones and we want to mix the two inputs before packetising and sending them around our network.

Assuming then that you have two ALCdevices, one for each input, and they're using the same capture format, you can add the two buffers when you capture from them. So for example, if you have two buffers:

float Buffer1[1024];
float Buffer2[1024];

And two devices that are recording with AL_FORMAT_MONO_FLOAT32 and the same sample rate, you can capture from both devices according to how much they have:

// Max number of samples we can capture at a time
ALCint to_capture = 1024;

// Limit to the number of samples available in Device1
ALCint available;
alcGetIntegerv(Device1, ALC_CAPTURE_SAMPLES, 1, &available);
if(available < to_capture)
    to_capture = available;

// Also limit to the number of samples available in Device2
alcGetIntegerv(Device2, ALC_CAPTURE_SAMPLES, 1, &available);
if(available < to_capture)
    to_capture = available;

// Capture the same number of samples from each device, and add them together
alcCaptureSamples(Device1, Buffer1, to_capture);
alcCaptureSamples(Device2, Buffer2, to_capture);
for(int i = 0;i < to_capture;i++)
    Buffer1[i] += Buffer2[i];

// Now Buffer1 has the mix of the two inputs
minghia commented 3 years ago

The mixing may not work as the microphones readers are in two different threads. So that's why I was hoping we could have a more sophisticated way of mixing the data, like using OpenAl source and listener objects.