I'm trying to record the audio transferred by a WebRTC PeerConnection
MediaStream
. I added a sink to the audio track which implements AudioTrackSinkInterface
. It implements the OnData
method:
void TestAudioTrackSink::OnData(const void* audio_data, int bits_per_sample, int sample_rate, size_t number_of_channels, size_t number_of_frames) {
size_t valueCount = number_of_channels * number_of_frames;
int16_t *_data = (int16_t*)audio_data;
f.write((char*)&_data, sizeof(int16_t) * valueCount);
f.flush();
}
f
is an ofstream
. Bits per sample is 16, sample rate is 16000, channels is 1, frames is 160.
But when I open the created file with AudaCity raw import (signed 16bit PCM, little endian, mono, sample rate 16000) I am not getting meaningful audio.
How to I correctly write that raw audio date?
Turned out in the end I access the data where the pointer itself was stored and not where it was pointing to, a classic. The correct implementation of my method would look like:
void TestAudioTrackSink::OnData(const void* audio_data, int bits_per_sample, int sample_rate, size_t number_of_channels, size_t number_of_frames) {
size_t number_of_bytes = number_of_channels * number_of_frames * sizeof(int16_t); //assuming bits_per_sample is 16
f.write(reinterpret_cast<const char*>(audio_data), number_of_bytes);
f.flush();
}
Note: For more processing of webrtc native retrieved and sent audio data I am checking out a custom AudioDeviceModule now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With