如何在C++中将二进制文件转换成wav文件
How to convert binary into a wav file in c++
我正在做一个项目,我需要用 c++ 从麦克风获取输入,并创建一个 wav 文件来存储录制的麦克风。现在,我做了一个简单的函数,它只从麦克风读取数据,并将其转储到一个 bin 文件中。我可以大胆地打开它并听到我说的话,但我希望通过我的程序将它转换为声音,让我们说一个函数。无论如何我可以创建一个将二进制文件转换为 wav 格式的函数吗?不一定非得是wav,可以是mp3等任何流行的声音格式。这是我获取声音的函数 --
#pragma comment(lib,"winmm.lib")
#include <Windows.h>
#include <mmsystem.h>
#include <fstream>
#include <iostream>
void AudioReader::AudioReader::AutioToBinary() {
WAVEFORMATEX wfx = {};
wfx.wFormatTag = WAVE_FORMAT_PCM; // PCM is standard
wfx.nChannels = 2; // 2 channels = stereo sound
wfx.nSamplesPerSec = 44100; // Samplerate. 44100 Hz
wfx.wBitsPerSample = 32; // 32 bit samples
wfx.nBlockAlign = wfx.wBitsPerSample * wfx.nChannels / 8;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
HWAVEIN wi;
waveInOpen(
&wi,
WAVE_MAPPER,
&wfx,
NULL, NULL,
CALLBACK_NULL | WAVE_FORMAT_DIRECT
);
char buffers[2][44100 * 2 * 2 / 2];
WAVEHDR headers[2] = { {},{} };
for (int i = 0; i < 2; ++i){
headers[i].lpData = buffers[i];
headers[i].dwBufferLength = 44100 * 2 * 2 / 2;
waveInPrepareHeader(wi, &headers[i], sizeof(headers[i]));
waveInAddBuffer(wi, &headers[i], sizeof(headers[i]));
}
std::ofstream outfile("Audio.bin", std::ios_base::out | std::ios_base::binary);
std::cout << "Started recording! Press escape when you're ready to stop!\n";
waveInStart(wi);
while (!(GetAsyncKeyState(VK_ESCAPE) & 0x8000)) {
for (auto& h : headers) {
if (h.dwFlags & WHDR_DONE) {
outfile.write(h.lpData, h.dwBufferLength); //dump audio binary to bin file
h.dwFlags = 0;
h.dwBytesRecorded = 0;
waveInPrepareHeader(wi, &h, sizeof(h));
waveInAddBuffer(wi, &h, sizeof(h));
}
}
}
waveInStop(wi);
for (auto& h : headers){
waveInUnprepareHeader(wi, &h, sizeof(h));
}
waveInClose(wi);
}
更新:您好,我做了更多的研究,并且自己做了 headers。我还有一个问题,如何将 headers 实现到 wav 文件中?我尝试用传统的 ofstream 方法来做,但目前不起作用。这是我为它做的功能 --
void AudioReader::AudioReader::BinaryToWav(wavehdr_tag& bin) {
std::ofstream WavFile("Audio.wav", std::ios_base::binary);
//FMT Sub-Chunk
std::string SubChunk1ID = "fmt";
int SubChunk1Size = 16;
int AudioFormat = 1;
int NumChannels = 2;
int SampleRate = 44100;
int BitsPerSample = 16;
int ByteRate = SampleRate * NumChannels * BitsPerSample / 8;
int BlockAlign = NumChannels * BitsPerSample / 8;
//Data
std::string SubChunk2ID = "data";
int SubChunk2Size = 1 * NumChannels * BitsPerSample / 8;
//RIFF
std::string ChunkID = "RIFF";
int ChunkSize = 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size);
std::string Format = "WAVE";
//dump params to file
WavFile
<< ChunkID
<< ChunkSize
<< Format
<< SubChunk1ID
<< SubChunk1Size
<< AudioFormat
<< NumChannels
<< SampleRate
<< ByteRate
<< BlockAlign
<< BitsPerSample
<< SubChunk2ID
<< SubChunk2Size
<< bin.lpData << bin.dwBufferLength;
}
好的,所以我设法找到了一篇文章来指导您完成操作。您需要有一个 bin 值才能执行此操作,因此您可以根据需要使用我的代码从您的麦克风获取 bin。这是文章,祝你好运! -- http://blog.acipo.com/generating-wave-files-in-c/. Also here is another site that did help me quite a lot on the understanding of why we need these headers -- http://soundfile.sapp.org/doc/WaveFormat/.
我正在做一个项目,我需要用 c++ 从麦克风获取输入,并创建一个 wav 文件来存储录制的麦克风。现在,我做了一个简单的函数,它只从麦克风读取数据,并将其转储到一个 bin 文件中。我可以大胆地打开它并听到我说的话,但我希望通过我的程序将它转换为声音,让我们说一个函数。无论如何我可以创建一个将二进制文件转换为 wav 格式的函数吗?不一定非得是wav,可以是mp3等任何流行的声音格式。这是我获取声音的函数 --
#pragma comment(lib,"winmm.lib")
#include <Windows.h>
#include <mmsystem.h>
#include <fstream>
#include <iostream>
void AudioReader::AudioReader::AutioToBinary() {
WAVEFORMATEX wfx = {};
wfx.wFormatTag = WAVE_FORMAT_PCM; // PCM is standard
wfx.nChannels = 2; // 2 channels = stereo sound
wfx.nSamplesPerSec = 44100; // Samplerate. 44100 Hz
wfx.wBitsPerSample = 32; // 32 bit samples
wfx.nBlockAlign = wfx.wBitsPerSample * wfx.nChannels / 8;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
HWAVEIN wi;
waveInOpen(
&wi,
WAVE_MAPPER,
&wfx,
NULL, NULL,
CALLBACK_NULL | WAVE_FORMAT_DIRECT
);
char buffers[2][44100 * 2 * 2 / 2];
WAVEHDR headers[2] = { {},{} };
for (int i = 0; i < 2; ++i){
headers[i].lpData = buffers[i];
headers[i].dwBufferLength = 44100 * 2 * 2 / 2;
waveInPrepareHeader(wi, &headers[i], sizeof(headers[i]));
waveInAddBuffer(wi, &headers[i], sizeof(headers[i]));
}
std::ofstream outfile("Audio.bin", std::ios_base::out | std::ios_base::binary);
std::cout << "Started recording! Press escape when you're ready to stop!\n";
waveInStart(wi);
while (!(GetAsyncKeyState(VK_ESCAPE) & 0x8000)) {
for (auto& h : headers) {
if (h.dwFlags & WHDR_DONE) {
outfile.write(h.lpData, h.dwBufferLength); //dump audio binary to bin file
h.dwFlags = 0;
h.dwBytesRecorded = 0;
waveInPrepareHeader(wi, &h, sizeof(h));
waveInAddBuffer(wi, &h, sizeof(h));
}
}
}
waveInStop(wi);
for (auto& h : headers){
waveInUnprepareHeader(wi, &h, sizeof(h));
}
waveInClose(wi);
}
更新:您好,我做了更多的研究,并且自己做了 headers。我还有一个问题,如何将 headers 实现到 wav 文件中?我尝试用传统的 ofstream 方法来做,但目前不起作用。这是我为它做的功能 --
void AudioReader::AudioReader::BinaryToWav(wavehdr_tag& bin) {
std::ofstream WavFile("Audio.wav", std::ios_base::binary);
//FMT Sub-Chunk
std::string SubChunk1ID = "fmt";
int SubChunk1Size = 16;
int AudioFormat = 1;
int NumChannels = 2;
int SampleRate = 44100;
int BitsPerSample = 16;
int ByteRate = SampleRate * NumChannels * BitsPerSample / 8;
int BlockAlign = NumChannels * BitsPerSample / 8;
//Data
std::string SubChunk2ID = "data";
int SubChunk2Size = 1 * NumChannels * BitsPerSample / 8;
//RIFF
std::string ChunkID = "RIFF";
int ChunkSize = 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size);
std::string Format = "WAVE";
//dump params to file
WavFile
<< ChunkID
<< ChunkSize
<< Format
<< SubChunk1ID
<< SubChunk1Size
<< AudioFormat
<< NumChannels
<< SampleRate
<< ByteRate
<< BlockAlign
<< BitsPerSample
<< SubChunk2ID
<< SubChunk2Size
<< bin.lpData << bin.dwBufferLength;
}
好的,所以我设法找到了一篇文章来指导您完成操作。您需要有一个 bin 值才能执行此操作,因此您可以根据需要使用我的代码从您的麦克风获取 bin。这是文章,祝你好运! -- http://blog.acipo.com/generating-wave-files-in-c/. Also here is another site that did help me quite a lot on the understanding of why we need these headers -- http://soundfile.sapp.org/doc/WaveFormat/.