Open a *.wav file and write it to a ASCII text file

Hello all! Am new to c++ programming. I was given an assignment to convert a *.wav audio file into an ASCII text file that contains all the numbers corresponding to the sampled audio. Kindly tell me a program to convert the *.wav file into ASCII text file.

Thanks in advance!
Do you have to code it from scratch, or are you allowed to use system calls? If so, what system are you working on?

If you want to output just the values of the sampled audio yourself, the first thing you need to do is reasd the WAVE header, to find out the offset to the data (and how it is encoded?).

Windows does provide multimedia support, including the "old school" MMIO functions, which can be used to load a wave file and access the data.

Andy
Thanks Andy for replying. Actually i have the following code that records a sound from the microphone and saves it as a .wav file. What i want is, to get the sample values written in a text file rather than saving it as .wav file. Kindly help me with this. Am using MS visual studio 2010. Here goes the code:

************************************************


// check1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include <CommDlg.h>
#include<iostream>
#include <MMSystem.h>

#define NSEC 5;



using namespace std;



HWAVEIN hWaveIn;

WAVEHDR WaveInHdr;

MMRESULT result;

WAVEFORMATEX pFormat;



void CheckMMIOError(DWORD code);

void SaveWaveFile();



void main()

{



//Declare local varibles

int samplesperchan;

int sampleRate;

int *waveIn;



cout << "*********************************************\n";

cout << "Configuring the Sound Hardware:\n";

cout << "*********************************************\n";

cout << "Enter the number of Samples/Channel:\n";

cin >> samplesperchan;

cout << "Enter the Sampling Rate:\n";

cin >> sampleRate;



pFormat.wFormatTag = WAVE_FORMAT_PCM; // simple, uncompressed format

pFormat.nChannels = 1; // 1=mono, 2=stereo

pFormat.nSamplesPerSec = sampleRate; // 44100

pFormat.wBitsPerSample = 16; // 16 for high quality, 8 for telephone-grade

pFormat.nBlockAlign = pFormat.nChannels*pFormat.wBitsPerSample/8;

pFormat.nAvgBytesPerSec = pFormat.nChannels*pFormat.wBitsPerSample/8;

pFormat.cbSize=0;



result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,

0L, 0L, WAVE_FORMAT_DIRECT);

if (result)

{

LPTSTR fault;

waveInGetErrorText(result, fault, 256);

MessageBoxW(NULL,L"Failed to open waveform input device.",NULL,NULL);

return;

}

int nSec = NSEC;

waveIn = new int[sampleRate*nSec];

WaveInHdr.lpData = (LPSTR)waveIn;

WaveInHdr.dwBufferLength = sampleRate*nSec*pFormat.nBlockAlign;

WaveInHdr.dwBytesRecorded=0;

WaveInHdr.dwUser = 0L;

WaveInHdr.dwFlags = 0L;

WaveInHdr.dwLoops = 0L;

waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));



result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

if (result)

{

MessageBoxW(NULL,L"Failed to read block from device",NULL,NULL);

return;

}



result = waveInStart(hWaveIn);

if (result)

{

MessageBoxW(NULL,L"Failed to start recording",NULL,NULL);

return;

}

cout << "Start Recording...........\n";

do

{



}

while (waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR))== WAVERR_STILLPLAYING);

SaveWaveFile();

waveInStop(hWaveIn);

cout << "Stop Recording.\n";

waveInClose(hWaveIn);



if(!waveIn)

delete [] waveIn;

}

void SaveWaveFile()

{

MMCKINFO ChunkInfo;

MMCKINFO FormatChunkInfo;

MMCKINFO DataChunkInfo;



HMMIO handle = mmioOpen(

L"test.wav", 0, MMIO_CREATE | MMIO_WRITE);

if (!handle) {

MessageBox(0, L"Error creating file.", L"Error Message", 0);

return;

}

memset(&ChunkInfo, 0, sizeof(MMCKINFO));

ChunkInfo.fccType = mmioStringToFOURCC(L"WAVE", 0);

DWORD Res = mmioCreateChunk(

handle, &ChunkInfo, MMIO_CREATERIFF);

CheckMMIOError(Res);



FormatChunkInfo.ckid = mmioStringToFOURCC(L"fmt ", 0);

FormatChunkInfo.cksize = sizeof(WAVEFORMATEX);

Res = mmioCreateChunk(handle, &FormatChunkInfo, 0);

CheckMMIOError(Res);

// Write the wave format data.

mmioWrite(handle, (char*)&pFormat, sizeof(pFormat));



Res = mmioAscend(handle, &FormatChunkInfo, 0);

CheckMMIOError(Res);

DataChunkInfo.ckid = mmioStringToFOURCC(L"data", 0);

DWORD DataSize = WaveInHdr.dwBytesRecorded;

DataChunkInfo.cksize = DataSize;

Res = mmioCreateChunk(handle, &DataChunkInfo, 0);

CheckMMIOError(Res);



mmioWrite(handle, (char*)WaveInHdr.lpData, DataSize);

// Ascend out of the data chunk.

mmioAscend(handle, &DataChunkInfo, 0);



// Ascend out of the RIFF chunk (the main chunk). Failure to do

// this will result in a file that is unreadable by Windows95

// Sound Recorder.

mmioAscend(handle, &ChunkInfo, 0);

mmioClose(handle, 0);

}

void CheckMMIOError(DWORD code)

{

if (code == 0) return;

LPWSTR buff;

wsprintf(buff,

L"MMIO Error. Error Code: %d", code);

MessageBox(NULL,buff, L"MMIO Error", 0);

}
It's just a case of writing your data to a file in the required format.

This is where you're writing you data chunk

mmioWrite(handle, (char*)WaveInHdr.lpData, DataSize);

Instead you want to write the data pointed to by WaveInHdr.lpData out using (e.g) ofstream.

As you're using uncompressed 16-bit mono audio, you could just walk the data with a WORD pointer. (Unless you want the raw bytes?)
Last edited on
I didn't get you. Can you please post the code for writing that data into a text file? I would be really grateful to you.
I could, but given the code you posted it should be easy enough for you.

I you have a serious go at coding it, I will give you a review!
But i need it really urgently. Please let me know how to convert the data to ASCII format.
:-/

You just write the numbers out using ofstream. This snippet writes out the 16 bit values, one per line. ofs is an ostream object which has been successfully opened.

Andy

1
2
3
4
5
6
7
WORD* pOut = reinterpret_cast<WORD*>(WaveInHdr.lpData);
const WORD* pEnd = reinterpret_cast<const WORD*>(WaveInHdr.lpData + DataSize);

for( ; pEnd > pOut; ++pOut)
{
    ofs << *pOut << endl;
}


P.S. This code is assuming that the data block is an even number of bytes in size. For safety you might want to check this, even though it should be the csse (as you're using a 16-bit audio format)
Last edited on
Topic archived. No new replies allowed.