本文整理汇总了C++中IDirectSoundBuffer::Release方法的典型用法代码示例。如果您正苦于以下问题:C++ IDirectSoundBuffer::Release方法的具体用法?C++ IDirectSoundBuffer::Release怎么用?C++ IDirectSoundBuffer::Release使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDirectSoundBuffer
的用法示例。
在下文中一共展示了IDirectSoundBuffer::Release方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SetPrimaryBufferMode
void DSound::SetPrimaryBufferMode()
{
#ifndef _XBOX
DSBUFFERDESC format;
memset( &format, 0, sizeof(format) );
format.dwSize = sizeof(format);
format.dwFlags = DSBCAPS_PRIMARYBUFFER;
format.dwBufferBytes = 0;
format.lpwfxFormat = NULL;
IDirectSoundBuffer *pBuffer;
HRESULT hr = this->GetDS()->CreateSoundBuffer( &format, &pBuffer, NULL );
/* hr */
if( FAILED(hr) )
{
LOG->Warn(hr_ssprintf(hr, "Couldn't create primary buffer"));
return;
}
WAVEFORMATEX waveformat;
memset( &waveformat, 0, sizeof(waveformat) );
waveformat.cbSize = 0;
waveformat.wFormatTag = WAVE_FORMAT_PCM;
waveformat.wBitsPerSample = 16;
waveformat.nChannels = 2;
waveformat.nSamplesPerSec = 44100;
waveformat.nBlockAlign = 4;
waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
// Set the primary buffer's format
hr = IDirectSoundBuffer_SetFormat( pBuffer, &waveformat );
if( FAILED(hr) )
LOG->Warn( hr_ssprintf(hr, "SetFormat on primary buffer") );
DWORD got;
hr = pBuffer->GetFormat( &waveformat, sizeof(waveformat), &got );
if( FAILED(hr) )
LOG->Warn( hr_ssprintf(hr, "GetFormat on primary buffer") );
else if( waveformat.nSamplesPerSec != 44100 )
LOG->Warn( "Primary buffer set to %i instead of 44100", waveformat.nSamplesPerSec );
/* MS docs:
*
* When there are no sounds playing, DirectSound stops the mixer engine and halts DMA
* (direct memory access) activity. If your application has frequent short intervals of
* silence, the overhead of starting and stopping the mixer each time a sound is played
* may be worse than the DMA overhead if you kept the mixer active. Also, some sound
* hardware or drivers may produce unwanted audible artifacts from frequent starting and
* stopping of playback. If your application is playing audio almost continuously with only
* short breaks of silence, you can force the mixer engine to remain active by calling the
* IDirectSoundBuffer::Play method for the primary buffer. The mixer will continue to run
* silently.
*
* However, I just added the above code and I don't want to change more until it's tested.
*/
// pBuffer->Play( 0, 0, DSBPLAY_LOOPING );
pBuffer->Release();
#endif
}
示例2:
bool VDAudioOutputDirectSoundW32::InitPlayback() {
tWAVEFORMATEX *wf = &*mInitFormat;
mMillisecsPerByte = 1000.0 * (double)wf->nBlockAlign / (double)wf->nAvgBytesPerSec;
// create looping secondary buffer
DSBUFFERDESC dsd={sizeof(DSBUFFERDESC)};
dsd.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
dsd.dwBufferBytes = mDSBufferSize;
dsd.lpwfxFormat = (WAVEFORMATEX *)wf;
dsd.guid3DAlgorithm = DS3DALG_DEFAULT;
IDirectSoundBuffer *pDSB;
HRESULT hr = mpDS8->CreateSoundBuffer(&dsd, &pDSB, NULL);
if (FAILED(hr)) {
VDDEBUG("VDAudioOutputDirectSound: Failed to create secondary buffer! hr=%08x\n", hr);
return false;
}
// query to IDirectSoundBuffer8
hr = pDSB->QueryInterface(IID_IDirectSoundBuffer8, (void **)&mpDSBuffer);
pDSB->Release();
if (FAILED(hr)) {
VDDEBUG("VDAudioOutputDirectSound: Failed to obtain IDirectSoundBuffer8 interface! hr=%08x\n", hr);
return false;
}
// all done!
mDSWriteCursor = 0;
return true;
}
示例3: LoadSoundWave
HRESULT NatsumeSound::LoadSoundWave(TCHAR *_FileName)
{
// Waveファイルオープン
WAVEFORMATEX wFmt;
char *pWaveData = 0;
DWORD WaveSize = 0;
if(FAILED(
OpenWave(_FileName,wFmt,&pWaveData,WaveSize)
))
{
Error("Waveファイルのロードに失敗しました。");
return E_FAIL;
}
DSBUFFERDESC DSBufferDesc;
DSBufferDesc.dwSize = sizeof(DSBUFFERDESC);
DSBufferDesc.dwFlags = 0;
DSBufferDesc.dwBufferBytes = WaveSize;
DSBufferDesc.dwReserved = 0;
DSBufferDesc.lpwfxFormat = &wFmt;
DSBufferDesc.guid3DAlgorithm = GUID_NULL;
IDirectSoundBuffer *ptmpBuf = 0;
LpDirectSound->CreateSoundBuffer( &DSBufferDesc, &ptmpBuf, NULL );
ptmpBuf->QueryInterface( IID_IDirectSoundBuffer8 ,(void**)&SoundDataBuffer[SoundDataBufferNextKey] );
ptmpBuf->Release();
if ( SoundDataBuffer[SoundDataBufferNextKey] == 0 )
{
//pDS8->Release();
return E_FAIL;
}
// セカンダリバッファにWaveデータ書き込み
LPVOID lpvWrite = 0;
DWORD dwLength = 0;
if ( DS_OK == SoundDataBuffer[SoundDataBufferNextKey]->Lock( 0, 0, &lpvWrite, &dwLength, NULL, NULL, DSBLOCK_ENTIREBUFFER ) )
{
memcpy( lpvWrite, pWaveData, dwLength);
SoundDataBuffer[SoundDataBufferNextKey]->Unlock( lpvWrite, dwLength, NULL, 0);
}
delete[] pWaveData; // 元音はもういらない
return S_OK;
}
示例4: CreateSoundBuffer
void Window::CreateSoundBuffer(void)
{
//Fill in structures describing the sound buffer
WAVEFORMATEX bufferFormat = {0};
bufferFormat.wFormatTag = WAVE_FORMAT_PCM;
bufferFormat.nChannels = 2;
bufferFormat.nSamplesPerSec = 44100;
bufferFormat.wBitsPerSample = 16;
bufferFormat.nBlockAlign = bufferFormat.nChannels * bufferFormat.wBitsPerSample / 8;
bufferFormat.nAvgBytesPerSec= bufferFormat.nSamplesPerSec * bufferFormat.nBlockAlign;
DSBUFFERDESC bufferDesc = {0};
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_GLOBALFOCUS;
bufferDesc.dwBufferBytes = bufferFormat.nAvgBytesPerSec;
bufferDesc.lpwfxFormat = &bufferFormat;
//Create an IDirectSoundBuffer
IDirectSoundBuffer * tempSoundBuffer;
if(FAILED(directSound->CreateSoundBuffer(&bufferDesc, &tempSoundBuffer, 0)))
throw Ex("Window Error: directSound->CreateSoundBuffer failed");
//Promote to an IDirectSoundBuffer8
if(FAILED(tempSoundBuffer->QueryInterface( IID_IDirectSoundBuffer8,
reinterpret_cast<LPVOID *>(&soundBuffer))))
{
throw Ex("Window Error: tempSoundBuffer->QueryInterface failed");
}
//Release the temporary sound buffer
tempSoundBuffer->Release();
//Zero the contents of the sound buffer
Dword numSoundBufferBytes;
void * soundBufferDataPtr;
if(FAILED(soundBuffer->Lock(0, 0, &soundBufferDataPtr, &numSoundBufferBytes, 0, 0, DSBLOCK_ENTIREBUFFER)))
throw Ex("Window Error: soundBuffer->Lock failed");
ZeroMemory(soundBufferDataPtr, numSoundBufferBytes);
if(FAILED(soundBuffer->Unlock(soundBufferDataPtr, numSoundBufferBytes, 0, 0)))
throw Ex("Window Error: soundBuffer->Unlock failed");
}
示例5: Init
// Initialise DirectSound and buffers
void Loudspeaker::Init(HWND hwnd)
{
// Direct sound
DirectSoundCreate8(NULL, &directSound, NULL);
directSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY);
// Primary (soundcard) buffer
DSBUFFERDESC bufferDesc;
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = 0;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = NULL;
bufferDesc.guid3DAlgorithm = GUID_NULL;
directSound->CreateSoundBuffer(&bufferDesc, &primaryBuffer, NULL);
WAVEFORMATEX waveFormat;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nSamplesPerSec = 44100;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 1;
waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
waveFormat.cbSize = 0;
primaryBuffer->SetFormat(&waveFormat);
// Secondary (CPU) buffer
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = 1764;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = &waveFormat;
bufferDesc.guid3DAlgorithm = GUID_NULL;
IDirectSoundBuffer* temp;
directSound->CreateSoundBuffer(&bufferDesc, &temp, NULL);
temp->QueryInterface(IID_IDirectSoundBuffer8, (void**)&secondaryBuffer);
temp->Release();
temp = 0;
}
示例6: CreateSound
// must be 44.1k 16bit Stereo PCM Wave
Sound DSound::CreateSound( char* wavFileName )
{
int error;
FILE* filePtr;
unsigned int count;
WaveHeaderType waveFileHeader;
WAVEFORMATEX waveFormat;
DSBUFFERDESC bufferDesc;
HRESULT result;
IDirectSoundBuffer* tempBuffer;
IDirectSoundBuffer8* pSecondaryBuffer;
unsigned char* waveData;
unsigned char* bufferPtr;
unsigned long bufferSize;
// Open the wave file in binary.
error = fopen_s( &filePtr,wavFileName,"rb" );
assert( error == 0 );
// Read in the wave file header.
count = fread( &waveFileHeader,sizeof( waveFileHeader ),1,filePtr );
assert( count == 1 );
// Check that the chunk ID is the RIFF format.
assert( (waveFileHeader.chunkId[0] == 'R') &&
(waveFileHeader.chunkId[1] == 'I') &&
(waveFileHeader.chunkId[2] == 'F') &&
(waveFileHeader.chunkId[3] == 'F') );
// Check that the file format is the WAVE format.
assert( (waveFileHeader.format[0] == 'W') &&
(waveFileHeader.format[1] == 'A') &&
(waveFileHeader.format[2] == 'V') &&
(waveFileHeader.format[3] == 'E') );
// Check that the sub chunk ID is the fmt format.
assert( (waveFileHeader.subChunkId[0] == 'f') &&
(waveFileHeader.subChunkId[1] == 'm') &&
(waveFileHeader.subChunkId[2] == 't') &&
(waveFileHeader.subChunkId[3] == ' ') );
// Check that the audio format is WAVE_FORMAT_PCM.
assert( waveFileHeader.audioFormat == WAVE_FORMAT_PCM );
// Check that the wave file was recorded in stereo format.
assert( waveFileHeader.numChannels == 2 );
// Check that the wave file was recorded at a sample rate of 44.1 KHz.
assert( waveFileHeader.sampleRate == 44100 );
// Ensure that the wave file was recorded in 16 bit format.
assert( waveFileHeader.bitsPerSample == 16 );
// Check for the data chunk header.
assert( (waveFileHeader.dataChunkId[0] == 'd') &&
(waveFileHeader.dataChunkId[1] == 'a') &&
(waveFileHeader.dataChunkId[2] == 't') &&
(waveFileHeader.dataChunkId[3] == 'a') );
// Set the wave format of secondary buffer that this wave file will be loaded onto.
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nSamplesPerSec = 44100;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 2;
waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
waveFormat.cbSize = 0;
// Set the buffer description of the secondary sound buffer that the wave file will be loaded onto.
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = waveFileHeader.dataSize;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = &waveFormat;
bufferDesc.guid3DAlgorithm = GUID_NULL;
// Create a temporary sound buffer with the specific buffer settings.
result = pDirectSound->CreateSoundBuffer( &bufferDesc,&tempBuffer,NULL );
assert( !FAILED( result ) );
// Test the buffer format against the direct sound 8 interface and create the secondary buffer.
result = tempBuffer->QueryInterface( IID_IDirectSoundBuffer8,(void**)&pSecondaryBuffer );
assert( !FAILED( result ) );
// Release the temporary buffer.
tempBuffer->Release();
tempBuffer = 0;
// Move to the beginning of the wave data which starts at the end of the data chunk header.
fseek( filePtr,sizeof(WaveHeaderType),SEEK_SET );
// Create a temporary buffer to hold the wave file data.
waveData = new unsigned char[ waveFileHeader.dataSize ];
assert( waveData );
// Read in the wave file data into the newly created buffer.
count = fread( waveData,1,waveFileHeader.dataSize,filePtr );
assert( count == waveFileHeader.dataSize);
//.........这里部分代码省略.........
示例7: GetSampleSize
OutputStream*
DSAudioDevice::openBuffer(
void* samples, int frame_count,
int channel_count, int sample_rate, SampleFormat sample_format)
{
ADR_GUARD("DSAudioDevice::openBuffer");
const int frame_size = channel_count * GetSampleSize(sample_format);
WAVEFORMATEX wfx;
memset(&wfx, 0, sizeof(wfx));
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = channel_count;
wfx.nSamplesPerSec = sample_rate;
wfx.nAvgBytesPerSec = sample_rate * frame_size;
wfx.nBlockAlign = frame_size;
wfx.wBitsPerSample = GetSampleSize(sample_format) * 8;
wfx.cbSize = sizeof(wfx);
DSBUFFERDESC dsbd;
memset(&dsbd, 0, sizeof(dsbd));
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPAN |
DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY |
DSBCAPS_STATIC | DSBCAPS_CTRLPOSITIONNOTIFY;
if (m_global_focus) {
dsbd.dwFlags |= DSBCAPS_GLOBALFOCUS;
}
const int buffer_frame_count = std::max(m_min_buffer_length, frame_count);
const int buffer_size = buffer_frame_count * frame_size;
dsbd.dwBufferBytes = buffer_size;
dsbd.lpwfxFormat = &wfx;
// create the DS buffer
IDirectSoundBuffer* buffer;
HRESULT result = m_direct_sound->CreateSoundBuffer(
&dsbd, &buffer, NULL);
if (FAILED(result) || !buffer) {
return 0;
}
ADR_IF_DEBUG {
DSBCAPS caps;
caps.dwSize = sizeof(caps);
result = buffer->GetCaps(&caps);
if (FAILED(result)) {
buffer->Release();
return 0;
} else {
std::ostringstream ss;
ss << "actual buffer size: " << caps.dwBufferBytes << std::endl
<< "buffer_size: " << buffer_size;
ADR_LOG(ss.str().c_str());
}
}
void* data;
DWORD data_size;
result = buffer->Lock(0, buffer_size, &data, &data_size, 0, 0, 0);
if (FAILED(result)) {
buffer->Release();
return 0;
}
ADR_IF_DEBUG {
std::ostringstream ss;
ss << "buffer size: " << buffer_size << std::endl
<< "data size: " << data_size << std::endl
<< "frame count: " << frame_count;
ADR_LOG(ss.str().c_str());
}
const int actual_size = frame_count * frame_size;
memcpy(data, samples, actual_size);
memset((u8*)data + actual_size, 0, buffer_size - actual_size);
buffer->Unlock(data, data_size, 0, 0);
DSOutputBuffer* b = new DSOutputBuffer(
this, buffer, buffer_frame_count, frame_size);
SYNCHRONIZED(this);
m_open_buffers.push_back(b);
return b;
}
示例8: mmioOpen
TDDSDGenWave::TDDSDGenWave( TDDSD* OWner, const wstring& fname, bool is3D, bool useFX )
{
FIsStream = false;
FOwner = OWner;
FUseFX = useFX;
//サウンドカードが無いなら、何もしない
if( ! FOwner->Initialized() )
return;
HMMIO hm = mmioOpen((LPWSTR)fname.c_str(), NULL, MMIO_READ | MMIO_ALLOCBUF);
if( hm == 0 ) {
//PutDebugMessage(fname + 'が、見つかりません');
return;
}
//WAVEに入る
MMCKINFO mckP; //親チャンクの情報
mckP.fccType = MakeFourCC('W','A','V','E');
if( (mmioDescend(hm, &mckP, NULL, MMIO_FINDRIFF)) != 0 ) {
mmioClose(hm, 0);
//PutDebugMessage(fname + 'は、WAVEファイルではありません');
return;
}
//fmtチャンクに入る
MMCKINFO mckC; //子チャンクの情報
mckC.ckid = MakeFourCC('f','m','t',' ');
if( mmioDescend(hm, &mckC, &mckP, MMIO_FINDCHUNK) != 0 ) {
mmioClose(hm, 0);
//PutDebugMessage(fname + 'には、fmtチャンクが有りません');
return;
}
//fmtチャンクを読み取る
u32 fmtSize = mckC.cksize;
if( mmioRead(hm, (HPSTR)&FWaveFormat, fmtSize) != fmtSize ) {
mmioClose(hm, 0);
//PutDebugMessage(fname + 'のfmtチャンクが不正です');
return;
}
//fmtチャンクを抜け、dataチャンクに入る
mmioAscend(hm, &mckC, 0);
mckC.ckid = MakeFourCC('d','a','t','a');
if( mmioDescend(hm, &mckC, &mckP, MMIO_FINDCHUNK) != 0 ) {
mmioClose(hm, 0);
//PutDebugMessage(fname + 'には、dataチャンクが有りません');
return;
}
u32 dataSize = mckC.cksize;
//二次バッファの作成
DSBUFFERDESC dsbd;
ZeroMemory(&dsbd, sizeof(dsbd));
dsbd.dwSize = sizeof(dsbd);
if( is3D )
dsbd.dwFlags = DSBCAPS_CTRLVOLUME + DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRL3D;
else
dsbd.dwFlags = DSBCAPS_CTRLPAN + DSBCAPS_CTRLVOLUME + DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN;
if( FUseFX )
dsbd.dwFlags = DSBCAPS_CTRLPAN + DSBCAPS_CTRLVOLUME + DSBCAPS_CTRLFREQUENCY + DSBCAPS_CTRLFX;
if( FOwner->FStickyFocus )
dsbd.dwFlags = dsbd.dwFlags | DSBCAPS_STICKYFOCUS;
dsbd.dwBufferBytes = dataSize;
dsbd.lpwfxFormat = &FWaveFormat;
FLength = dataSize;
IDirectSoundBuffer* dsb;
FOwner->DSound()->CreateSoundBuffer(&dsbd, &dsb, NULL);
dsb->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)&FSoundBuffer);
dsb->Release();
//二次バッファのロック
LPVOID lpBuf1, lpBuf2;
DWORD dwBuf1, dwBuf2;
FSoundBuffer->Lock(0,dataSize, &lpBuf1, &dwBuf1, &lpBuf2, &dwBuf2, 0);
//音データのロード(dataチャンクを読み取る)
mmioRead(hm, (HPSTR)lpBuf1, dwBuf1);
if( dwBuf2 != 0 ) {
mmioRead(hm, (HPSTR)lpBuf2, dwBuf2);
}
FSoundBuffer->Unlock(lpBuf1,dwBuf1,lpBuf2,dwBuf2);
FSoundBuffer->SetFrequency(FWaveFormat.nSamplesPerSec);
mmioClose(hm, 0);
}
示例9: LoadWaveFile
bool SoundClass::LoadWaveFile(char* filename, IDirectSoundBuffer8** secondaryBuffer)
{
int error;
FILE* filePtr;
unsigned int count;
WaveHeaderType waveFileHeader;
WAVEFORMATEX waveFormat;
DSBUFFERDESC bufferDesc;
HRESULT result;
IDirectSoundBuffer* tempBuffer;
unsigned char* waveData;
unsigned char* bufferPtr;
unsigned long bufferSize;
error = fopen_s(&filePtr, filename, "rb");
if (error != 0)
{
return false;
}
count = fread(&waveFileHeader, sizeof(waveFileHeader), 1, filePtr);
if (count != 1)
{
return false;
}
if ((waveFileHeader.chunkId[0] != 'R') || (waveFileHeader.chunkId[1] != 'I') ||
(waveFileHeader.chunkId[2] != 'F') || (waveFileHeader.chunkId[3] != 'F'))
{
return false;
}
if ((waveFileHeader.format[0] != 'W') || (waveFileHeader.format[1] != 'A') ||
(waveFileHeader.format[2] != 'V') || (waveFileHeader.format[3] != 'E'))
{
return false;
}
if ((waveFileHeader.subChunkId[0] != 'f') || (waveFileHeader.subChunkId[1] != 'm') ||
(waveFileHeader.subChunkId[2] != 't') || (waveFileHeader.subChunkId[3] != ' '))
{
return false;
}
if (waveFileHeader.audioFormat != WAVE_FORMAT_PCM)
{
return false;
}
if (waveFileHeader.numChannels != 2)
{
return false;
}
if (waveFileHeader.sampleRate != 44100)
{
return false;
}
if (waveFileHeader.bitsPerSample != 16)
{
return false;
}
if ((waveFileHeader.dataChunkId[0] != 'd') || (waveFileHeader.dataChunkId[1] != 'a') ||
(waveFileHeader.dataChunkId[2] != 't') || (waveFileHeader.dataChunkId[3] != 'a'))
{
return false;
}
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nSamplesPerSec = 44100;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 2;
waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
waveFormat.cbSize = 0;
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = waveFileHeader.dataSize;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = &waveFormat;
bufferDesc.guid3DAlgorithm = GUID_NULL;
result = m_DirectSound->CreateSoundBuffer(&bufferDesc, &tempBuffer, NULL);
if (FAILED(result))
{
return false;
}
result = tempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&*secondaryBuffer);
if (FAILED(result))
{
return false;
}
tempBuffer->Release();
tempBuffer = 0;
//.........这里部分代码省略.........
示例10: loadWav
bool cSound::loadWav(char* fileName, IDirectSoundBuffer8**secondaryBuffer)
{
int error;
FILE *filePtr;
unsigned int count=0;
wavFileHeader waveFileHeader; //Stores the header of the wav file type
WAVEFORMATEX waveFormat; //Stores the buffer wav format
DSBUFFERDESC bufferDesc; //To sotre the details of the buffer
IDirectSoundBuffer* tmpBuffer;
unsigned char *waveData; //To load the wav file temporarily
unsigned char *bufferPtr;
unsigned long bufferSize;
error = fopen_s(&filePtr, fileName, "rb");
if (error!=0)
return false;
if (filePtr)
count = fread(&waveFileHeader, sizeof(wavFileHeader), 1, filePtr);
if (count != 1)
return false;
if ((waveFileHeader.chunkId[0] != 'R') || (waveFileHeader.chunkId[1] != 'I') ||
(waveFileHeader.chunkId[2] != 'F') || (waveFileHeader.chunkId[3] != 'F'))
{
return false;// Check that the chunk ID is the RIFF format.
}
if ((waveFileHeader.format[0] != 'W') || (waveFileHeader.format[1] != 'A') ||
(waveFileHeader.format[2] != 'V') || (waveFileHeader.format[3] != 'E'))
{
return false;// Check that the file format is the WAVE format.
}
if ((waveFileHeader.subChunkId[0] != 'f') || (waveFileHeader.subChunkId[1] != 'm') ||
(waveFileHeader.subChunkId[2] != 't') || (waveFileHeader.subChunkId[3] != ' '))
{
return false;// Check that the sub chunk ID is the fmt format.
}
if (waveFileHeader.audioFormat != WAVE_FORMAT_PCM)
{
return false;// Check that the audio format is WAVE_FORMAT_PCM.
}
int numChannels = waveFileHeader.numChannels;
int sampleRate = waveFileHeader.sampleRate;
int bitsPerSample = waveFileHeader.bitsPerSample;
if ((waveFileHeader.dataChunkId[0] != 'd') || (waveFileHeader.dataChunkId[1] != 'a') ||
(waveFileHeader.dataChunkId[2] != 't') || (waveFileHeader.dataChunkId[3] != 'a'))
return false;// Check for the data chunk header.
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nSamplesPerSec = sampleRate;
waveFormat.nChannels = numChannels;
waveFormat.wBitsPerSample = bitsPerSample;
waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8)*waveFormat.nChannels;
waveFormat.nAvgBytesPerSec = waveFormat.nBlockAlign*waveFormat.nSamplesPerSec;
waveFormat.cbSize = 0;
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = waveFileHeader.dataSize;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = &waveFormat;
bufferDesc.guid3DAlgorithm = GUID_NULL;
HRESULT result;
result = intSound->CreateSoundBuffer(&bufferDesc, &tmpBuffer, NULL);
if (FAILED(result))return false;
result = tmpBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&*secondaryBuffer);
if (FAILED(result))return false;
tmpBuffer->Release();
tmpBuffer = NULL;
//R E A D I N G T H E W A V F I L E S T A R T S H E R E
fseek(filePtr, sizeof(waveFileHeader), SEEK_SET);
waveData = new unsigned char[waveFileHeader.dataSize];
memset(waveData, 0, waveFileHeader.dataSize);
if(!waveData) return false;
while (count<waveFileHeader.dataSize)
count = fread(waveData, 1, waveFileHeader.dataSize, filePtr);
error = fclose(filePtr);
if (error != 0)return false;
result = (*secondaryBuffer)->Lock(0,waveFileHeader.dataSize,(void**)&bufferPtr,(DWORD*)&bufferSize,NULL,0,0);
if (FAILED(result))return false;
memcpy(bufferPtr, waveData, waveFileHeader.dataSize);
result = (*secondaryBuffer)->Unlock((void**)bufferPtr, bufferSize, NULL, 0);
if (FAILED(result))return false;
delete[]waveData;
return true;
}
示例11: initialize
bool OutputDirectSound::initialize(quint32 freq, ChannelMap map, Qmmp::AudioFormat format)
{
Q_UNUSED(format);
DSBUFFERDESC bufferDesc;
HRESULT result = DirectSoundCreate8(0, &m_ds, 0);
if(result != DS_OK)
{
qWarning("OutputDirectSound: DirectSoundCreate8 failed, error code = 0x%lx", result);
m_ds = 0;
return false;
}
if((result = m_ds->SetCooperativeLevel(GetDesktopWindow(), DSSCL_PRIORITY)) != DS_OK)
{
qWarning("OutputDirectSound: SetCooperativeLevel failed, error code = 0x%lx", result);
return false;
}
ZeroMemory(&bufferDesc, sizeof(DSBUFFERDESC));
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = 0;
bufferDesc.lpwfxFormat = NULL;
if((result = m_ds->CreateSoundBuffer(&bufferDesc, &m_primaryBuffer, NULL)) != DS_OK)
{
m_primaryBuffer = 0;
qWarning("OutputDirectSound: CreateSoundBuffer failed, error code = 0x%lx", result);
return false;
}
WAVEFORMATEX wfex;
ZeroMemory(&wfex, sizeof(WAVEFORMATEX));
wfex.wFormatTag = WAVE_FORMAT_PCM;
wfex.nChannels = map.count();
wfex.nSamplesPerSec = freq;
wfex.wBitsPerSample = 16;
wfex.nBlockAlign = (wfex.wBitsPerSample / 8) * wfex.nChannels;
wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
if((result = m_primaryBuffer->SetFormat(&wfex)) != DS_OK)
{
qWarning("OutputDirectSound: SetFormat failed, error code = 0x%lx", result);
return false;
}
if((result = m_primaryBuffer->Play(0, 0, DSBPLAY_LOOPING)) != DS_OK)
{
qWarning("OutputDirectSound: Play failed, error code = 0x%lx", result);
return false;
}
ZeroMemory(&wfex, sizeof(WAVEFORMATEX));
wfex.wFormatTag = WAVE_FORMAT_PCM;
wfex.nChannels = map.count();
wfex.nSamplesPerSec = freq;
wfex.wBitsPerSample = 16;
wfex.nBlockAlign = (wfex.wBitsPerSample / 8) * wfex.nChannels;
wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
ZeroMemory(&bufferDesc, sizeof(DSBUFFERDESC));
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME |
DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPOSITIONNOTIFY;
bufferDesc.lpwfxFormat = &wfex;
bufferDesc.dwBufferBytes = DS_BUFSIZE; // buffer size
IDirectSoundBuffer *pDSB;
if((result = m_ds->CreateSoundBuffer(&bufferDesc, &pDSB, NULL)) != DS_OK)
{
qWarning("OutputDirectSound: CreateSoundBuffer failed, error code = 0x%lx", result);
return false;
}
if((result = pDSB->QueryInterface(IID_IDirectSoundBuffer8, (void**)&m_dsBuffer)) != DS_OK)
{
m_dsBuffer = 0;
qWarning("OutputDirectSound: QueryInterface failed, error code = 0x%lx", result);
pDSB->Release();
return false;
}
m_dsBuffer->SetCurrentPosition(0);
m_dsBuffer->Play(0,0,DSBPLAY_LOOPING);
m_dsBufferAt = 0;
configure(freq, map, Qmmp::PCM_S16LE);
if(volumeControl)
volumeControl->restore();
return true;
}
示例12: sSetSoundHandler
sBool sSetSoundHandler(sInt freq,sSoundHandler handler,sInt latency,sInt flags)
{
HRESULT hr;
DSBUFFERDESC dsbd;
IDirectSoundBuffer *buffer = 0;
void *p1,*p2;
DWORD c1,c2;
WAVEFORMATEXTENSIBLE sformat = { { WAVE_FORMAT_PCM,2,freq,freq*4,4,16,0 }, {0}, {3}, {0} };
sClearSoundHandler();
if(!DXSThread) return 0;
sLockSound();
DXSOSamples = latency;
DXSORate = freq;
DXSOChannels = 2;
if (flags & sSOF_5POINT1)
{
DXSOChannels=6;
sformat.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;
sformat.Format.cbSize=22;
sformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
}
sformat.Format.nChannels=DXSOChannels;
sformat.Format.nBlockAlign=2*DXSOChannels;
sformat.Format.nAvgBytesPerSec=freq*sformat.Format.nBlockAlign;
sformat.Samples.wValidBitsPerSample=16;
sformat.dwChannelMask=(1<<DXSOChannels)-1; // very space opera
// open device
hr = DirectSoundCreate8(0,&DXSO,0);
if(FAILED(hr)) goto error;
hr = DXSO->SetCooperativeLevel(sHWND,DSSCL_PRIORITY);
if(FAILED(hr)) goto error;
// set primary buffer sample rate
sSetMem(&dsbd,0,sizeof(dsbd));
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwBufferBytes = 0;
dsbd.lpwfxFormat = 0;
hr = DXSO->CreateSoundBuffer(&dsbd,&DXSOPrimary,0);
if(SUCCEEDED(hr))
DXSOPrimary->SetFormat(&sformat.Format);
// create streaming buffer
sSetMem(&dsbd,0,sizeof(dsbd));
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
if(flags & sSOF_GLOBALFOCUS)
dsbd.dwFlags |= DSBCAPS_GLOBALFOCUS;
dsbd.dwBufferBytes = latency*DXSOChannels*2;
dsbd.lpwfxFormat = &sformat.Format;
hr = DXSO->CreateSoundBuffer(&dsbd,&buffer,0);
if(FAILED(hr)) goto error;
hr = buffer->QueryInterface(IID_IDirectSoundBuffer8,(void**)&DXSOBuffer);
if(FAILED(hr)) goto error;
buffer->Release(); buffer = 0;
hr = DXSOBuffer->Lock(0,0,&p1,&c1,&p2,&c2,DSBLOCK_ENTIREBUFFER);
if(!FAILED(hr))
{
if (p1) sSetMem(p1,0,c1);
if (p2) sSetMem(p2,0,c2);
DXSOBuffer->Unlock(p1,c1,p2,c2);
}
DXSOHandler = handler;
DXSOBuffer->Play(0,0,DSBPLAY_LOOPING);
sUnlockSound();
sPingSound();
return sTRUE;
error:
sClearSoundHandler();
sUnlockSound();
return sFALSE;
}
示例13: Acquire
bool BufferCache::Acquire(const DSBUFFERDESC& desc, IDirectSoundBuffer8*& pBuffer, bool bUseCache)
{
// If buffer caching is enabled, try to find a
// buffer with a matching description structure
if(DXAudioMgr()->GetInit()->m_bCacheBuffers && bUseCache)
{
BufferInfoVector::iterator itr;
for(itr = m_Free.begin(); itr != m_Free.end(); ++itr)
{
if((*itr)->m_Desc == desc)
{
pBuffer = (*itr)->m_pBuffer;
pBuffer->SetCurrentPosition(0);
pBuffer->AddRef();
m_Used.push_back(*itr);
m_Free.erase(itr);
return true;
}
}
}
// Create a buffer normally
IDirectSoundBuffer* pDSBuffer;
HRESULT hr = DXAudioMgr()->DirectSound()->CreateSoundBuffer(&desc, &pDSBuffer, NULL);
if(FAILED(hr))
{
DebugOut(3, "First attempt at sound buffer creation failed. Error = %s. Attempting again...", DXGetErrorString(hr));
if(desc.dwFlags & DSBCAPS_CTRL3D)
{
DXAudioMgr()->ResetSound3DLimit();
if(!DXAudioMgr()->RemoveSound3D(0))
return Error::Handle("Could not create sound buffer. Error = %s", DXGetErrorString(hr));
}
else
{
DXAudioMgr()->ResetSoundLimit();
if(!DXAudioMgr()->RemoveSound(0))
return Error::Handle("Could not create sound buffer. Error = %s", DXGetErrorString(hr));
}
hr = DXAudioMgr()->DirectSound()->CreateSoundBuffer(&desc, &pDSBuffer, NULL);
if(FAILED(hr))
return Error::Handle("Could not create sound buffer. Error = %s", DXGetErrorString(hr));
}
// Get the IDirectSoundBuffer8 interface
hr = pDSBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&pBuffer);
if(FAILED(hr))
return Error::Handle("Could not obtain DirectSoundBuffer8 interface. Error = %s", DXGetErrorString(hr));
// Release the temporary DirectSoundBuffer interface
pDSBuffer->Release();
// If buffer caching is enabled,
if(DXAudioMgr()->GetInit()->m_bCacheBuffers && bUseCache)
{
BufferInfo* pInfo = new BufferInfo;
pInfo->m_pBuffer = pBuffer;
pInfo->m_pBuffer->AddRef();
memcpy(&pInfo->m_Format, desc.lpwfxFormat, sizeof(WAVEFORMATEX));
memcpy(&pInfo->m_Desc, &desc, sizeof(DSBUFFERDESC));
pInfo->m_Desc.lpwfxFormat = &pInfo->m_Format;
m_Used.push_back(pInfo);
}
return true;
}
示例14: LoadWav
HRESULT DirectSoundDevice::LoadWav(char* filename, SGE::Sound::Sound *sound){
//This method is based of an example featured on http://www.rastertek.com/dx11tut14.html
struct WaveHeaderType
{
char chunkId[4];
unsigned long chunkSize;
char format[4];
char subChunkId[4];
unsigned long subChunkSize;
unsigned short audioFormat;
unsigned short numChannels;
unsigned long sampleRate;
unsigned long bytesPerSecond;
unsigned short blockAlign;
unsigned short bitsPerSample;
char dataChunkId[4];
unsigned long dataSize;
};
sound->index = -1;
int error;
FILE* filePtr = NULL;
unsigned int count;
WaveHeaderType waveFileHeader;
WAVEFORMATEX waveFormat;
DSBUFFERDESC bufferDesc;
HRESULT result;
IDirectSoundBuffer* tempBuffer = NULL;
unsigned char* waveData = NULL;
unsigned char *bufferPtr = NULL;
unsigned long bufferSize = NULL;
if(fopen_s(&filePtr, filename, "rb") != 0)
return E_FAIL;
count = fread(&waveFileHeader, sizeof(waveFileHeader), 1, filePtr);
if(count != 1)
{
return E_FAIL;
}
// Check that the chunk ID is the RIFF format.
if((waveFileHeader.chunkId[0] != 'R') || (waveFileHeader.chunkId[1] != 'I') ||
(waveFileHeader.chunkId[2] != 'F') || (waveFileHeader.chunkId[3] != 'F'))
{
return E_FAIL;
}
// Check that the file format is the WAVE format.
if((waveFileHeader.format[0] != 'W') || (waveFileHeader.format[1] != 'A') ||
(waveFileHeader.format[2] != 'V') || (waveFileHeader.format[3] != 'E'))
{
return E_FAIL;
}
// Check that the sub chunk ID is the fmt format.
if((waveFileHeader.subChunkId[0] != 'f') || (waveFileHeader.subChunkId[1] != 'm') ||
(waveFileHeader.subChunkId[2] != 't') || (waveFileHeader.subChunkId[3] != ' '))
{
return E_FAIL;
}
// Check for the data chunk header.
if((waveFileHeader.dataChunkId[0] != 'd') || (waveFileHeader.dataChunkId[1] != 'a') ||
(waveFileHeader.dataChunkId[2] != 't') || (waveFileHeader.dataChunkId[3] != 'a'))
{
return E_FAIL;
}
// Set the wave format of secondary buffer that this wave file will be loaded onto.
waveFormat.wFormatTag = waveFileHeader.audioFormat;
waveFormat.nSamplesPerSec = waveFileHeader.sampleRate;
waveFormat.wBitsPerSample = waveFileHeader.bitsPerSample;
waveFormat.nChannels = waveFileHeader.numChannels;
waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
waveFormat.cbSize = 0;
// Set the buffer description of the secondary sound buffer that the wave file will be loaded onto.
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = waveFileHeader.dataSize;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = &waveFormat;
bufferDesc.guid3DAlgorithm = GUID_NULL;
IDirectSoundBuffer8 * soundBuffer = NULL;
result = lpds->CreateSoundBuffer(&bufferDesc, &tempBuffer, NULL);
if(FAILED(result)) return result;
result = tempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&soundBuffer);
if(FAILED(result)) return result;
tempBuffer->Release();
tempBuffer = 0;
fseek(filePtr, sizeof(WaveHeaderType), SEEK_SET);
//.........这里部分代码省略.........
示例15: LoadWaveFile
bool Sound::LoadWaveFile(const char* filename, IDirectSoundBuffer8** secondaryBuffer)
{
int error;
FILE* filePtr;
unsigned int count;
WaveHeaderType waveFileHeader;
WAVEFORMATEX waveFormat;
DSBUFFERDESC bufferDesc;
HRESULT result;
IDirectSoundBuffer* tempBuffer;
unsigned char* waveData;
unsigned char* bufferPtr;
unsigned long bufferSize;
error = fopen_s(&filePtr, filename, "rb");
if(error != 0)
{
return false;
}
count = fread(&waveFileHeader, sizeof(waveFileHeader), 1, filePtr);
if(count != 1)
{
return false;
}
//check through the header to make sure that it is a non-corrupted WAV file
if((waveFileHeader.chunkId[0] != 'R') || (waveFileHeader.chunkId[1] != 'I') ||
(waveFileHeader.chunkId[2] != 'F') || (waveFileHeader.chunkId[3] != 'F'))
{
return false;
}
if((waveFileHeader.format[0] != 'W') || (waveFileHeader.format[1] != 'A') ||
(waveFileHeader.format[2] != 'V') || (waveFileHeader.format[3] != 'E'))
{
return false;
}
if((waveFileHeader.subChunkId[0] != 'f') || (waveFileHeader.subChunkId[1] != 'm') ||
(waveFileHeader.subChunkId[2] != 't') || (waveFileHeader.subChunkId[3] != ' '))
{
return false;
}
if(waveFileHeader.audioFormat != WAVE_FORMAT_PCM)
{
return false;
}
if(waveFileHeader.numChannels != 2)
{
return false;
}
if(waveFileHeader.sampleRate != 44100)
{
return false;
}
if(waveFileHeader.bitsPerSample != 16)
{
return false;
}
if((waveFileHeader.dataChunkId[0] != 'd') || (waveFileHeader.dataChunkId[1] != 'a') ||
(waveFileHeader.dataChunkId[2] != 't') || (waveFileHeader.dataChunkId[3] != 'a'))
{
return false;
}
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nSamplesPerSec = 44100;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 2;
waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
waveFormat.cbSize = 0;
bufferDesc.dwSize = sizeof(DSBUFFERDESC);
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME;
bufferDesc.dwBufferBytes = waveFileHeader.dataSize;
bufferDesc.dwReserved = 0;
bufferDesc.lpwfxFormat = &waveFormat;
bufferDesc.guid3DAlgorithm = GUID_NULL;
result = m_DirectSound->CreateSoundBuffer(&bufferDesc, &tempBuffer, NULL);
if(FAILED(result))
{
return false;
}
result = tempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&*secondaryBuffer);
if(FAILED(result))
{
return false;
}
tempBuffer->Release();
tempBuffer = 0;
//.........这里部分代码省略.........