Currently, I am trying to implement sound emulation for the first time ever. However, I have no idea about the points listed below.
Here is the code that I am using at the moment. It is enough to play the garbage that is already contained in the buffer with some form of initialisation. All of the code has had the error checking removed for readability.
I then want to fill SoundData with the necessary data and then use PlaySound to Lock/Write/Unlock the buffer.
1. How do I clear the SoundData buffer so that no sounds play? Any attempt to set the array to $00 crashes.
2. Should SoundData be 8-bit?
3. Do I update SoundData 60/50 times per second like I update the graphics?
To be honest its hard for me to even put my questions into text. A straight out explanation of everything would be nice.
Here is the code that I am using at the moment. It is enough to play the garbage that is already contained in the buffer with some form of initialisation. All of the code has had the error checking removed for readability.
Code:
unsigned __int8 * SoundBuffer;
void CDirectX::CreateSound()
{
DirectSoundCreate8(NULL, &lpDS8, NULL);
lpDS8->SetCooperativeLevel(hWnd, DSSCL_NORMAL);
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwSize = sizeof(DSBUFFERDESC);
lpDS8->CreateSoundBuffer(&dsbd, &lpDSBPrimary, NULL);
lpDSBPrimary->GetFormat(&wfe, sizeof(WAVEFORMATEX), NULL);
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwBufferBytes = wfe.nAvgBytesPerSec;
dsbd.dwFlags = DSBCAPS_GLOBALFOCUS;
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.lpwfxFormat = &wfe;
lpDS8->CreateSoundBuffer(&dsbd, &lpDSBSecondary, NULL);
SoundData = new unsigned __int8[wfe.nSamplesPerSec];
ZeroMemory(&SoundData, sizeof(SoundData));
PlaySound();
lpDSBSecondary->Play(NULL, 0, DSBPLAY_LOOPING);
}
void CDirectX::PlaySound()
{
ppvAudioPtr1 = NULL;
lpDSBSecondary->Lock(0, NULL, &ppvAudioPtr1, &pdwAudioBytes1, NULL, NULL, DSBLOCK_ENTIREBUFFER);
memcpy(ppvAudioPtr1, SoundData, pdwAudioBytes1);
lpDSBSecondary->Unlock(ppvAudioPtr1, pdwAudioBytes1, NULL, NULL);
}
void CDirectX::CreateSound()
{
DirectSoundCreate8(NULL, &lpDS8, NULL);
lpDS8->SetCooperativeLevel(hWnd, DSSCL_NORMAL);
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwSize = sizeof(DSBUFFERDESC);
lpDS8->CreateSoundBuffer(&dsbd, &lpDSBPrimary, NULL);
lpDSBPrimary->GetFormat(&wfe, sizeof(WAVEFORMATEX), NULL);
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwBufferBytes = wfe.nAvgBytesPerSec;
dsbd.dwFlags = DSBCAPS_GLOBALFOCUS;
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.lpwfxFormat = &wfe;
lpDS8->CreateSoundBuffer(&dsbd, &lpDSBSecondary, NULL);
SoundData = new unsigned __int8[wfe.nSamplesPerSec];
ZeroMemory(&SoundData, sizeof(SoundData));
PlaySound();
lpDSBSecondary->Play(NULL, 0, DSBPLAY_LOOPING);
}
void CDirectX::PlaySound()
{
ppvAudioPtr1 = NULL;
lpDSBSecondary->Lock(0, NULL, &ppvAudioPtr1, &pdwAudioBytes1, NULL, NULL, DSBLOCK_ENTIREBUFFER);
memcpy(ppvAudioPtr1, SoundData, pdwAudioBytes1);
lpDSBSecondary->Unlock(ppvAudioPtr1, pdwAudioBytes1, NULL, NULL);
}
I then want to fill SoundData with the necessary data and then use PlaySound to Lock/Write/Unlock the buffer.
1. How do I clear the SoundData buffer so that no sounds play? Any attempt to set the array to $00 crashes.
2. Should SoundData be 8-bit?
3. Do I update SoundData 60/50 times per second like I update the graphics?
To be honest its hard for me to even put my questions into text. A straight out explanation of everything would be nice.