我正在尝试录制通过 WebRTC PeerConnection
MediaStream
传输的音频。我向实现 AudioTrackSinkInterface
的音轨添加了一个接收器。它实现了 OnData
方法:
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
是一个ofstream
。 每样本位数为 16,采样率为 16000, channel 数为 1,帧数为 160。
但是当我使用 AudaCity 原始导入(带符号的 16 位 PCM、小端字节序、单声道、采样率 16000)打开创建的文件时,我没有得到有意义的音频。
如何正确写入原始音频日期?
最佳答案
最后我访问了指针本身存储的数据,而不是它指向的地方,一个经典。我的方法的正确实现如下所示:
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();
}
注意:为了对 webrtc native 检索和发送的音频数据进行更多处理,我现在正在检查自定义 AudioDeviceModule。
关于c++ - 将 WebRTC (AudioTrackSinkInterface) 原始音频写入光盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40426888/