编写WebRTC (AudioTrackSinkInterface)原始音频到光盘
Writing WebRTC (AudioTrackSinkInterface) raw audio to disc
我正在尝试记录由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,通道为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();
}
注意:对于更多的处理webtc本地检索和发送的音频数据,我现在检查一个自定义AudioDeviceModule。
为了给@ZoolWay添加更多的细节已经正确的答案,我在Windows平台上以二进制模式打开文件时遇到了文件损坏问题。简而言之,确保文件具有ios_base::binary
标志:
std::ofstream stream(LR"(D:test.pcm)", ios_base::binary);
[...]
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
stream.write(reinterpret_cast<const char*>(audio_data), number_of_bytes);
stream.flush();
}
相关文章:
- 用c++阅读音频
- 为什么这个音频包络不能通过开关的情况?
- FFMpeg库:如何在音频文件中精确查找
- 需要反转音频步进公式
- 如何在音频处理中使用超能力时间拉伸类
- C++新手,想知道如何使用VS code 2019播放音频文件
- 如何暂停插孔音频客户端
- 如何以编程方式将音频从任何录制设备路由到任何播放设备
- 将音频从浏览器流式传输到WebRTC本机C++应用程序
- 使用IMFSourceReader(Microsoft媒体基金会)进行音频流传输
- 大声音频无法播放
- 让 iaxClient 将音频发送到缓冲区而不是音频设备/从缓冲区获取音频
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 波形音频 - waveOutWrite 发出断断续续的声音
- WASAPI 共享模式:音频引擎预期的振幅是多少?
- 在Win7上,在默认端点上轮询音频会话有时会崩溃
- 媒体基金会:WavSink以比实时消耗率更快的速度处理音频-是否可以实时限制后台处理
- PJSUA在两个或多个不同的音频声卡之间切换
- 当直接渲染到WASAPI时,两个流中的一个流没有音频输出
- 编写WebRTC (AudioTrackSinkInterface)原始音频到光盘