I am working on a project where I need to take input from a microphone in c++, and create a wav file that stores the recorded microphone. Right now, I made a simple function that just reads from the microphone, and dumps it into a bin file. I can open it in audacity and hear what I said, but I wish to convert it to sound through my program in lets say a function. Is there anyway I can create a function to convert a binary file into a wav format? It doesn't have to be wav, it can be any popular sound format such as mp3. This is my function for getting the sound --
#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);
}
UPDATE: Hello again, I have done some more research and I have made my own headers. I have another question, how do I implement the headers into the wav file? I tried doing it in the traditional ofstream method, but that doesn't work at the moment. This is the function I made for it --
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;
}