录音/音频转换数据为WAV实时 [英] Record/Convert AUDIO data to WAV in Real-time

查看:269
本文介绍了录音/音频转换数据为WAV实时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的,当涉及到音频信号处理。

I am new when it comes to audio signal processing.

这是送我的麦克风/播放曲目的音频数据目前,我已经连接的设备到我的电脑。我已经斯坦伯格ASIO SDK 2.3它连接到该设备,并在反复回调返回原始数据的使用创造了主机应用程序。信号是24位和频率可以选择任何我喜欢的,让我们说44100赫兹,2pan的,单一的渠道。我已经转换该信号也将翻一番< -1.0,1.0>因为我在上面做一些信号处理。

Currently I have connected device to my PC that sends me audio data from mic/playback track. I have already created host application with usage of Steinberg ASIO SDK 2.3 which connects to the device and in repeating callback returns raw data. Signal is 24bit and frequency can be chosen whatever I like, let's say 44100 hZ, 2pan's, single channel. I have converted this signal also to double <-1.0, 1.0> because I am doing some signal processing on it.

我想现在要做的就是录音功能添加到我的主机。例如按钮点击,输入数据正在不断转化为WAV文件,当我点击另一个按钮停止并保存。

What I would like to do now is to add recording functionality to my host. For example on button click, incoming data is being continuously converted to WAV file and when I click other button it stops and saves.

我已阅读已经对WAV文件,文件格式,码流格式(RIFF),并以某种方式有一个总体思路WAV文件的样子。我也查了很多论坛帖子,计算器的​​线程或code-项目职位,到处找我相关主题的东西,但我不能得到一个想法,我怎么能做出实时录制正在进行。很多code的我发现是有关转换数据数组做修改后为WAV。我想使正在进行的转换,使WAV文件添加/扩大,直到我告诉它停下来。

I have read already about WAV files, file formats, bitstream formats (RIFF), and somehow have an overall idea how the WAV file looks like. I also checked a lot of forum threads, stackoverflow's threads or code-projects posts and everywhere I find something related to topic but I can't get an idea how can I make ongoing recording in real time. A lot of code I had found is about converting data array to WAV after doing modifications to it. I would like to make ongoing conversion and make WAV file appending/expanding till I tell it to stop.

例如可我莫名其妙地修改此?

For example could I somehow modify this?

#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);

我对任何提示/链接/建议/形式的帮助表示感谢。

I am grateful for any hint / link / suggestion / form of help.

推荐答案

您的使用情况和你见过上线code之间的区别在于,在您的使用情况下,你不预先知道长文件即将结束的存在,因为你不知道什么时候用户将preSS停止按钮。

The difference between your use case and the code you've seen on line is that in your use case, you don't know in advance how long the file is going to end up being, since you don't know when the user will press the stop button.

来处理这个问题的方法是通过编写出来的WAV头部像往常一样启动,但现在不用担心了你写的文件大小特定字段中的值(即RIFF和后场之后,数据)字段。你可以离开设置为零这些领域现在。

The way to handle this is to start by writing out the WAV header as usual, but don't worry for now about the values you write for the file-size-specific fields (i.e. the field after "RIFF" and the field after "data"). You can leave those fields set to zero for now.

然后写出来的音频样本,你接受他们,他们即附加到文件末尾。

Then write out the audio samples as you receive them, i.e. appending them to the end of the file.

最后,用户拥有pressed站,您要关闭该文件后,你需要回去并覆盖正确的值这两个标题字段。因为此时你知道你写到文件的音频数据的多少字节,你现在可以做到这一点。一旦你做到这一点,该文件应该是良好和使用。您可以使用例如的ofstream :: seekp(fieldOffset,的ios_bas​​e ::求)寻求回从需要修改的字段的文件的顶部相应的偏移。

Finally, after the user has pressed stop and you are about to close the file, you'll need to go back and overwrite those two header-fields with the correct values. You can do this now because at this point you know how many bytes of audio data you wrote into the file. Once you've done that, the file should be well-formed and usable. You can use e.g. ofstream::seekp(fieldOffset, ios_base::beg) to seek back to the appropriate offsets from the top of the file for the fields you need to modify.

这篇关于录音/音频转换数据为WAV实时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆