Sign in to follow this  
jf_wangfeng

How to fill Secondary DS Buffer with wav data?

Recommended Posts

How to fill Secondary DS Buffer with wav data? I hope someone can show a simple process about it. And I wonder if I use FILE class to open a wav file and then use memcpy(SecBuffer,pfile,dwSize) to fill buffer can work?

Share this post


Link to post
Share on other sites
Basically you are right, but you need to read and check the .wav file header.
	struct WaveFileHeader
{
char RiffSig[4]; // 'RIFF'
long WaveformChunkSize; // 8
char WaveSig[4]; // 'WAVE'
char FormatSig[4]; // 'fmt ' (notice space after)
long FormatChunkSize; // 16
short FormatTag; // WAVE_FORMAT_PCM
short Channels; // # of channels
long SampleRate; // sampling rate
long BytesPerSec; // bytes per second
short BlockAlign; // sample block alignment
short BitsPerSample; // bits per second
char DataSig[4]; // 'data'
long DataSize; // size of waveform data
};


I actually store the sound once it's read in an object of this type:
	class SoundRawData
{
public:
short mChannels;
long mFrequency;
short mBitsPerSample;
long mDataSize;
void *mData;
SoundRawData(short bitsPerSample,short channels,long size,long frequency)
: mFrequency(frequency),mChannels(channels),mBitsPerSample(bitsPerSample),mDataSize(size),mData(0)
{
mData=new char[mDataSize];
}
~SoundRawData()
{
if(mData)
delete [] mData;
}
};

The method I have to load the .wav file (ripped from my project) is shown below. Note that I pass in the path, and a unique name for the sound - wen the data is loaded I enter the SoundRawData object into a std::map, mapped to the name field.
HRESULT DirectSoundManager::LoadWaveFileToMemory(const char *filename,const char *name)
{
if(!filename || !name)
{
Logger::OutputLine("DirectSoundManager::LoadWaveFileToMemory : missing filename or name");
return E_FAIL;
}

FILE *fp = fopen(filename,"rb");
if(!fp)
{
Logger::OutputLine("DirectSoundManager::LoadWaveFileToMemory : can't open file");
return E_FAIL;
}

//read the wave file header
static WaveFileHeader header;
ZeroMemory(&header,sizeof(header));
if(fread( &header, 1, sizeof(header), fp ) < sizeof(header))
{
Logger::OutputLine("DirectSoundManager::LoadWaveFileToMemory : couldn't read header");
fclose(fp);
return E_FAIL;
}
// Check the contents of the header
if(memcmp(header.RiffSig,"RIFF",4) || memcmp(header.WaveSig,"WAVE",4) || memcmp(header.FormatSig,"fmt ",4) )// || memcmp( hdr.DataSig, "data", 4 ) ) //some wave files don't supply the DataSig field
{
Logger::OutputLine("DirectSoundManager::LoadWaveFileToMemory : invalid file format");
fclose(fp);
return E_FAIL;
}

// Fill sound info struct
SoundRawData *data = new SoundRawData(header.BitsPerSample,header.Channels,header.DataSize,header.SampleRate);

//Load the sound data itself
if(fread( data->mData, 1, data->mDataSize, fp ) < data->mDataSize)
{
Logger::OutputLine("DirectSoundManager::LoadWaveFileToMemory : couldn't read data");
delete data;
fclose(fp);
return E_FAIL;
}

//insert the sound and it's name to the map
std::string str(name);
mRawSounds.insert(SoundRawDataMap::value_type(str,data));

return TRUE;
}

From this you should be able to figure it out, if you take out the bits about the std::map etc.

Good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this