我在音频信号处理方面是新手。
目前,我已将设备连接到我的电脑,该电脑从麦克风/播放轨道向我发送音频数据。我已经使用 Steinberg ASIO SDK 2.3 创建了主机应用程序,该应用程序连接到设备并在重复回调中返回原始数据。信号是 24 位,频率可以选择任何我喜欢的,比如说 44100 hZ、2pan's、单通道。我已将此信号也转换为 double 因为我正在对其进行一些信号处理。
我现在想做的是为我的主机添加录音功能。例如,单击按钮时,传入的数据会不断转换为 WAV 文件,当我单击其他按钮时,它会停止并保存。
我已经阅读过有关 WAV 文件、文件格式、比特流格式 (RIFF) 的内容,并且对 WAV 文件的外观有一个总体了解。我还检查了很多论坛帖子、stackoverflow 的帖子或代码项目帖子,到处都找到了与主题相关的内容,但我不知道如何实时进行持续录制。我发现的很多代码都是关于在对其进行修改后将数据数组转换为 WAV 的。我想进行持续的转换并使 WAV 文件附加/扩展,直到我告诉它停止。
例如,我可以以某种方式修改它吗?
#include <fstream>
template <typename T>
void write(std::ofstream& stream, const T& t) {
stream.write((const char*)&t, sizeof(T));
}
template <typename T>
void writeFormat(std::ofstream& stream) {
write<short>(stream, 1);
}
template <>
void writeFormat<float>(std::ofstream& stream) {
write<short>(stream, 3);
}
template <typename SampleType>
void writeWAVData(
char const* outFile,
SampleType* buf,
size_t bufSize,
int sampleRate,
short channels)
{
std::ofstream stream(outFile, std::ios::binary);
stream.write("RIFF", 4);
write<int>(stream, 36 + bufSize);
stream.write("WAVE", 4);
stream.write("fmt ", 4);
write<int>(stream, 16);
writeFormat<SampleType>(stream); // Format
write<short>(stream, channels); // Channels
write<int>(stream, sampleRate); // Sample Rate
write<int>(stream, sampleRate * channels * sizeof(SampleType)); // Byterate
write<short>(stream, channels * sizeof(SampleType)); // Frame size
write<short>(stream, 8 * sizeof(SampleType)); // Bits per sample
stream.write("data", 4);
stream.write((const char*)&bufSize, 4);
stream.write((const char*)buf, bufSize);
}
并以某种方式回调:
writeWAVData("mySound.wav", mySampleBuffer, mySampleBufferSize, 44100, 1);
我很感激任何提示/链接/建议/形式的帮助。