gpt4 book ai didi

c++ - 如何在C++中将二进制文件转换为WAV文件

转载 作者:行者123 更新时间:2023-12-03 01:08:02 24 4
gpt4 key购买 nike

我正在一个项目中,我需要从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);
}
更新:再次,您好,我做了更多的研究,并制作了自己的标题。我还有另一个问题,如何将 header 实现到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/。另外,这是另一个站点,它确实对我为什么需要这些 header 有很大帮助-http://soundfile.sapp.org/doc/WaveFormat/

关于c++ - 如何在C++中将二进制文件转换为WAV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64407963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com