1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <windows.h>
#include <iostream>
#pragma comment(lib, "winmm.lib")
#include "WavReader.h"
/*
Open up a .wav file and read it to a buffer.
*/
BUFFER WavReader::CreateBuffer()
{
// Open the file for reading
HMMIO handle = mmioOpen(L"C:\\WINDOWS\\MEDIA\\TADA.wav", 0, MMIO_READ);
memset(&ChunkInfo,0, sizeof(MMCKINFO));
// Read the necessary information from the open file.
mmioDescend(handle, &ChunkInfo, 0, MMIO_FINDRIFF);
FormatChunkInfo.ckid = mmioStringToFOURCC(L"fmt ", 0);
mmioDescend(handle, &FormatChunkInfo, &ChunkInfo, MMIO_FINDCHUNK);
mmioRead(handle, (char*)&waveFmt, sizeof(WAVEFORMATEX));
mmioAscend(handle, &FormatChunkInfo, 0);
DataChunkInfo.ckid = mmioStringToFOURCC(L"data", 0);
mmioDescend(handle, &DataChunkInfo, &ChunkInfo, MMIO_FINDCHUNK);
// Get the number of channels and number of samples.
readNumChannels = waveFmt.nChannels;
readNumSamples = DataChunkInfo.cksize / (readNumChannels * waveFmt.wBitsPerSample/8);
// Allocate memory for the samples per channel.
readBuffers = new short *[readNumChannels];
for (int i = 0; i < readNumChannels; i++) readBuffers[i] = new short[readNumSamples];
// Read samples per channel into the memory allocated and pointed at by readBuffers.
for (int j = 0; j < readNumSamples; j++)
for (int i = 0; i < readNumChannels; i++)
mmioRead(handle, (HPSTR)&readBuffers[i][j], sizeof(short));
// Close the file.
mmioClose(handle, 0);
// Assign data to be returned in the passedData struct.
passedData.passedChannels = readNumChannels;
passedData.passedSamples = readNumSamples;
passedData.passedBuffers = readBuffers;
// Return the struct passedData.
return passedData;
}
/*
Constructor Destructor
*/
WavReader::WavReader()
{
// Do something here
}
WavReader::~WavReader()
{
for (int i = 0; i < readNumChannels; i++) delete[] readBuffers[i];
delete[] readBuffers;
// Do something here
}
|