本文整理汇总了C++中IDirectSoundBuffer类的典型用法代码示例。如果您正苦于以下问题:C++ IDirectSoundBuffer类的具体用法?C++ IDirectSoundBuffer怎么用?C++ IDirectSoundBuffer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IDirectSoundBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: memset
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: AppDebugAssert
void SoundLibrary3dDirectSound::SetChannelVolume( int _channel, float _volume )
{
DirectSoundChannel *channel;
if (_channel == m_musicChannelId)
channel = m_musicChannel;
else channel = &m_channels[_channel];
if (!NearlyEquals(_volume, channel->m_volume) || m_forceVolumeUpdate )
{
AppDebugAssert(_volume >= 0.0f && _volume <= 10.0f);
channel->m_volume = _volume;
// long calculatedVolume = expf(10.0f - _volume);
// calculatedVolume += m_masterVolume;
// calculatedVolume *= -1.0f;
float calculatedVolume = -(5.0f - _volume * 0.5f);
calculatedVolume *= 1000.0f;
calculatedVolume += m_masterVolume;
if( calculatedVolume < -10000.0f ) calculatedVolume = -10000.0f;
if( calculatedVolume > 0.0f ) calculatedVolume = 0.0f;
IDirectSoundBuffer *buffer = channel->m_bufferInterface;
int errCode = buffer->SetVolume( calculatedVolume );
if (errCode == DSERR_BUFFERLOST)
buffer->Restore();
else
SOUNDASSERT( errCode, "Direct sound couldn't set buffer volume" );
}
}
示例3: GetFreeBuffer
//-----------------------------------------------------------------------------
// Name: CSound::Play3D()
// Desc: Plays the sound using voice management flags. Pass in DSBPLAY_LOOPING
// in the dwFlags to loop the sound
//-----------------------------------------------------------------------------
HRESULT CSound::Play3D( LPDS3DBUFFER p3DBuffer, DWORD dwPriority, DWORD dwFlags, LONG lFrequency )
{
HRESULT hr;
BOOL bRestored;
DWORD dwBaseFrequency;
if( m_apDSBuffer == NULL )
return CO_E_NOTINITIALIZED;
IDirectSoundBuffer* pDSB = GetFreeBuffer();
if( pDSB == NULL )
return DXTRACE_ERR( TEXT("GetFreeBuffer"), E_FAIL );
// Restore the buffer if it was lost
if( FAILED( hr = RestoreBuffer( pDSB, &bRestored ) ) )
return DXTRACE_ERR( TEXT("RestoreBuffer"), hr );
if( bRestored )
{
// The buffer was restored, so we need to fill it with new data
if( FAILED( hr = FillBufferWithSound( pDSB, FALSE ) ) )
return DXTRACE_ERR( TEXT("FillBufferWithSound"), hr );
}
if( m_dwCreationFlags & DSBCAPS_CTRLFREQUENCY )
{
pDSB->GetFrequency( &dwBaseFrequency );
pDSB->SetFrequency( dwBaseFrequency + lFrequency );
}
// QI for the 3D buffer
IDirectSound3DBuffer* pDS3DBuffer;
hr = pDSB->QueryInterface( IID_IDirectSound3DBuffer, (VOID**) &pDS3DBuffer );
if( SUCCEEDED( hr ) )
{
hr = pDS3DBuffer->SetAllParameters( p3DBuffer, DS3D_IMMEDIATE );
if( SUCCEEDED( hr ) )
{
hr = pDSB->Play( 0, dwPriority, dwFlags );
}
pDS3DBuffer->Release();
}
return hr;
}
示例4: OpenWave
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;
}
示例5: sizeof
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");
}
示例6:
void SoundLibrary3dDirectSound::SetChannelFrequency( int _channel, int _frequency )
{
DirectSoundChannel *channel;
if (_channel == m_musicChannelId) channel = m_musicChannel;
else channel = &m_channels[_channel];
if (channel->m_freq != _frequency)
{
channel->m_freq = _frequency;
IDirectSoundBuffer *buffer = channel->m_bufferInterface;
int errCode = buffer->SetFrequency( _frequency );
if (errCode == DSERR_BUFFERLOST)
buffer->Restore();
else
SOUNDASSERT( errCode, "Direct sound couldn't set channel frequency" );
}
}
示例7: AddLogMsg
void as_Sound::SetPitch(float pitch)
{
// set volume in hundredth of Decibel from 0 (full volume) to -9600 (silence)
long volume;
HRESULT hr;
// Get the 3D buffer in the audio path.
IDirectSoundBuffer *myBuffer = NULL;
hr = this->pAudioPath->GetObjectInPath(
0,
DMUS_PATH_BUFFER,
0,
GUID_NULL,
0,
IID_IDirectSoundBuffer,
(LPVOID *)&myBuffer);
if (FAILED(hr))
{
AddLogMsg("Error: GetObjectInPath");
return;
}
//this->pDS3DBuffer->QueryInterface(IID_IDirectSoundBuffer,(LPVOID *)&myBuffer);
if (myBuffer)
{
myBuffer->SetFrequency(pitch);
}
/*
if (true == looping)
{
MUSIC_TIME mtLength;
hr = pSegment->GetLength(&mtLength);
hr = pSegment->SetLoopPoints(0, mtLength/44000*22000);
if (S_OK != hr) {
char msg[128];
sprintf(msg, "SetLoopPoints error 0x%08lX", hr);
AddLogMsg(msg);
}
this->pSegment->SetRepeats(DMUS_SEG_REPEAT_INFINITE);
}
else
this->pSegment->SetRepeats(0);*/
}
示例8: PostThreadMessage
int CSound::Play(DWORD position_ms, bool isLoop)
{
if(!m_pSecondaryBuffer) return CS_E_NULL_SECONDARY;
m_isLoop = isLoop;
if(m_isStreamFile){
//OpenStreamThread();
//if(IsPlaying()){
//PostThreadMessage(m_dwThreadId, CSL_MSG_SEEK_AND_PLAY, (DWORD)(position_ms * ((double)m_wfx.nAvgBytesPerSec/1000.0)), 0);
PostThreadMessage(m_dwThreadId, CSL_MSG_SEEK_AND_PLAY, position_ms, 0);
WaitForSingleObject(m_hThreadMessageDispatchEvent, INFINITE);
//this->SetStreamCurosr((DWORD)(position_ms * ((double)m_wfx.nAvgBytesPerSec/1000.0)));
//}
//m_pSecondaryBuffer->Play(0, 0, DSBPLAY_LOOPING);
}else{
IDirectSoundBuffer* pBuffer = NULL;
if(m_nDuplicateLimit > 0){ //セカンダリバッファのコピーを使う
int id = GetInactiveBufferNo();//使用中でないバッファのインデックスを取得
if(id == -1) return CS_E_NOCANDO;
pBuffer = m_ppDuplicatedBuffer[id];
}else{
if( !m_isAllowRapidAccess ){
DWORD dwStatus;
m_pSecondaryBuffer->GetStatus( &dwStatus );
bool isSecondaryPlaying = (dwStatus & DSBSTATUS_PLAYING);
if( isSecondaryPlaying ){
/* nop */
}else{
pBuffer = m_pSecondaryBuffer;
}
}else{
pBuffer = m_pSecondaryBuffer;
}
}
if( !pBuffer ){
/* nop */
}else{
pBuffer->SetCurrentPosition( (DWORD)((double)position_ms * (double)(m_wfx.nAvgBytesPerSec/1000.0)) );
pBuffer->Play(0, 0, isLoop ? DSBPLAY_LOOPING : 0);
//m_pSecondaryBuffer->Play(0, 0, m_isStreamFile ? DSBPLAY_LOOPING : isLoop ? DSBPLAY_LOOPING : 0);
}
}
return CS_E_OK;
}
示例9: assert
int CSoundResource::play( int sndID, DWORD priority, DWORD flags, LONG volume, LONG freq, LONG pan )
{
assert( mDSBuffers );
int bufferIdx = getFreeBufferIndex();
IDirectSoundBuffer* buffer = mDSBuffers[bufferIdx];
if( !restoreBufferAndFill( buffer, false ) )
return -1;
if( mCreationFlags & DSBCAPS_CTRLVOLUME )
buffer->SetVolume( volume );
if( freq != -1 && (mCreationFlags & DSBCAPS_CTRLFREQUENCY) )
buffer->SetFrequency( freq );
if( mCreationFlags & DSBCAPS_CTRLPAN )
buffer->SetPan( pan );
mPlayingIDs[bufferIdx] = sndID;
buffer->Play( 0, priority, flags );
return bufferIdx;
}
示例10: DirectSoundCreate8
// 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;
}
示例11: SetPrimaryBufferFormat
//-----------------------------------------------------------------------------
// Name: CSoundManager::SetPrimaryBufferFormat()
// Desc: Set primary buffer to a specified format
// !WARNING! - Setting the primary buffer format and then using this
// same DirectSound object for DirectMusic messes up
// DirectMusic!
// For example, to set the primary buffer format to 22kHz stereo, 16-bit
// then: dwPrimaryChannels = 2
// dwPrimaryFreq = 22050,
// dwPrimaryBitRate = 16
//-----------------------------------------------------------------------------
HRESULT CSoundManager::SetPrimaryBufferFormat( DWORD dwPrimaryChannels,
DWORD dwPrimaryFreq,
DWORD dwPrimaryBitRate )
{
HRESULT hr;
IDirectSoundBuffer* pDSBPrimary = NULL;
if( m_pDS == NULL )
return CO_E_NOTINITIALIZED;
// Get the primary buffer
DSBUFFERDESC dsbd;
ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwBufferBytes = 0;
dsbd.lpwfxFormat = NULL;
if( FAILED( hr = m_pDS->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL ) ) )
return DXTRACE_ERR( TEXT("CreateSoundBuffer"), hr );
WAVEFORMATEX wfx;
ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM;
wfx.nChannels = (WORD) dwPrimaryChannels;
wfx.nSamplesPerSec = (DWORD) dwPrimaryFreq;
wfx.wBitsPerSample = (WORD) dwPrimaryBitRate;
wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
wfx.nAvgBytesPerSec = (DWORD) (wfx.nSamplesPerSec * wfx.nBlockAlign);
if( FAILED( hr = pDSBPrimary->SetFormat(&wfx) ) )
return DXTRACE_ERR( TEXT("SetFormat"), hr );
SAFE_RELEASE( pDSBPrimary );
return S_OK;
}
示例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: fopen_s
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;
//.........这里部分代码省略.........
示例14: fread
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: ADR_GUARD
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;
}