void Audio::CreateSourceVoice(SoundEvent sound) { // Load all data for each sound effect into a single in-memory buffer MediaStreamer soundEffectStream; switch (sound) { case Crash: soundEffectStream.Initialize(L"Assets\\Sounds\\Crash.wav"); break; case Shoot: soundEffectStream.Initialize(L"Assets\\Sounds\\Shoot.wav"); break; case Star: soundEffectStream.Initialize(L"Assets\\Sounds\\Coin1.wav"); break; case Powerup: soundEffectStream.Initialize(L"Assets\\Sounds\\GenericPowerup.wav"); break; case EnemyDead: soundEffectStream.Initialize(L"Assets\\Sounds\\EnemyDead1.wav"); break; } m_soundEffects[sound].m_soundEventType = sound; uint32 bufferLength = soundEffectStream.GetMaxStreamLengthInBytes(); m_soundEffects[sound].m_soundEffectBufferData = new byte[bufferLength]; soundEffectStream.ReadAll(m_soundEffects[sound].m_soundEffectBufferData, bufferLength, &m_soundEffects[sound].m_soundEffectBufferLength); XAUDIO2_SEND_DESCRIPTOR descriptors[2]; descriptors[0].pOutputVoice = m_soundEffectMasteringVoice; descriptors[0].Flags = 0; descriptors[1].pOutputVoice = m_soundEffectReverbVoiceSmallRoom; descriptors[1].Flags = 0; XAUDIO2_VOICE_SENDS sends = {0}; sends.SendCount = 2; sends.pSends = descriptors; // The rolling sound can have pitch shifting and a low-pass filter. ThrowIfFailed( m_soundEffectEngine->CreateSourceVoice( &m_soundEffects[sound].m_soundEffectSourceVoice, &(soundEffectStream.GetOutputWaveFormatEx()), 0, 1.0f, &m_voiceContext, &sends) ); m_soundEffects[sound].m_soundEffectSampleRate = soundEffectStream.GetOutputWaveFormatEx().nSamplesPerSec; // Queue in-memory buffer for playback ZeroMemory(&m_soundEffects[sound].m_audioBuffer, sizeof(m_soundEffects[sound].m_audioBuffer)); m_soundEffects[sound].m_audioBuffer.AudioBytes = m_soundEffects[sound].m_soundEffectBufferLength; m_soundEffects[sound].m_audioBuffer.pAudioData = m_soundEffects[sound].m_soundEffectBufferData; m_soundEffects[sound].m_audioBuffer.pContext = &m_soundEffects[sound]; m_soundEffects[sound].m_audioBuffer.Flags = XAUDIO2_END_OF_STREAM; m_soundEffects[sound].m_audioBuffer.LoopCount = 0; m_soundEffects[sound].m_soundEffectSourceVoice->SetVolume(0.4f); ThrowIfFailed( m_soundEffects[sound].m_soundEffectSourceVoice->SubmitSourceBuffer(&m_soundEffects[sound].m_audioBuffer) ); }
void Audio::PreloadSoundEffect(const char* pszFilePath, bool isMusic) { if (m_engineExperiencedCriticalError) { return; } int sound = Hash(pszFilePath); MediaStreamer mediaStreamer; mediaStreamer.Initialize(cocos2d::CCUtf8ToUnicode(pszFilePath).c_str()); m_soundEffects[sound].m_soundID = sound; uint32 bufferLength = mediaStreamer.GetMaxStreamLengthInBytes(); m_soundEffects[sound].m_soundEffectBufferData = new byte[bufferLength]; mediaStreamer.ReadAll(m_soundEffects[sound].m_soundEffectBufferData, bufferLength, &m_soundEffects[sound].m_soundEffectBufferLength); if (isMusic) { XAUDIO2_SEND_DESCRIPTOR descriptors[1]; descriptors[0].pOutputVoice = m_musicMasteringVoice; descriptors[0].Flags = 0; XAUDIO2_VOICE_SENDS sends = {0}; sends.SendCount = 1; sends.pSends = descriptors; DX::ThrowIfFailed( m_musicEngine->CreateSourceVoice(&m_soundEffects[sound].m_soundEffectSourceVoice, &(mediaStreamer.GetOutputWaveFormatEx()), 0, 1.0f, &m_voiceContext, &sends) ); } else { XAUDIO2_SEND_DESCRIPTOR descriptors[1]; descriptors[0].pOutputVoice = m_soundEffectMasteringVoice; descriptors[0].Flags = 0; XAUDIO2_VOICE_SENDS sends = {0}; sends.SendCount = 1; sends.pSends = descriptors; DX::ThrowIfFailed( m_soundEffectEngine->CreateSourceVoice(&m_soundEffects[sound].m_soundEffectSourceVoice, &(mediaStreamer.GetOutputWaveFormatEx()), 0, 1.0f, &m_voiceContext, &sends, nullptr) ); } m_soundEffects[sound].m_soundEffectSampleRate = mediaStreamer.GetOutputWaveFormatEx().nSamplesPerSec; // Queue in-memory buffer for playback ZeroMemory(&m_soundEffects[sound].m_audioBuffer, sizeof(m_soundEffects[sound].m_audioBuffer)); m_soundEffects[sound].m_audioBuffer.AudioBytes = m_soundEffects[sound].m_soundEffectBufferLength; m_soundEffects[sound].m_audioBuffer.pAudioData = m_soundEffects[sound].m_soundEffectBufferData; m_soundEffects[sound].m_audioBuffer.pContext = &m_soundEffects[sound]; m_soundEffects[sound].m_audioBuffer.Flags = XAUDIO2_END_OF_STREAM; m_soundEffects[sound].m_audioBuffer.LoopCount = 0; }
void Audio::CreateSourceVoice(SoundEvent sound) { // Load all data for each sound effect into a single in-memory buffer MediaStreamer soundEffectStream; switch (sound) { case RollingEvent: soundEffectStream.Initialize(L"Media\\Audio\\MarbleRoll.wav"); break; case FallingEvent: soundEffectStream.Initialize(L"Media\\Audio\\MarbleFall.wav"); break; case CollisionEvent: soundEffectStream.Initialize(L"Media\\Audio\\MarbleHit.wav"); break; case MenuChangeEvent: soundEffectStream.Initialize(L"Media\\Audio\\MenuChange.wav"); break; case MenuSelectedEvent: soundEffectStream.Initialize(L"Media\\Audio\\MenuSelect.wav"); break; case CheckpointEvent: soundEffectStream.Initialize(L"Media\\Audio\\Checkpoint.wav"); break; } m_soundEffects[sound].m_soundEventType = sound; uint32 bufferLength = soundEffectStream.GetMaxStreamLengthInBytes(); m_soundEffects[sound].m_soundEffectBufferData = new byte[bufferLength]; soundEffectStream.ReadAll(m_soundEffects[sound].m_soundEffectBufferData, bufferLength, &m_soundEffects[sound].m_soundEffectBufferLength); XAUDIO2_SEND_DESCRIPTOR descriptors[3]; descriptors[0].pOutputVoice = m_soundEffectMasteringVoice; descriptors[0].Flags = 0; descriptors[1].pOutputVoice = m_soundEffectReverbVoiceSmallRoom; descriptors[1].Flags = 0; descriptors[2].pOutputVoice = m_soundEffectReverbVoiceLargeRoom; descriptors[2].Flags = 0; XAUDIO2_VOICE_SENDS sends = {0}; sends.SendCount = 3; sends.pSends = descriptors; // The rolling sound can have pitch shifting and a low-pass filter. if (sound == RollingEvent) { DX::ThrowIfFailed( m_soundEffectEngine->CreateSourceVoice( &m_soundEffects[sound].m_soundEffectSourceVoice, &(soundEffectStream.GetOutputWaveFormatEx()), XAUDIO2_VOICE_USEFILTER, 2.0f, nullptr, &sends) ); } else { DX::ThrowIfFailed( m_soundEffectEngine->CreateSourceVoice( &m_soundEffects[sound].m_soundEffectSourceVoice, &(soundEffectStream.GetOutputWaveFormatEx()), 0, 1.0f, nullptr, &sends) ); } m_soundEffects[sound].m_soundEffectSampleRate = soundEffectStream.GetOutputWaveFormatEx().nSamplesPerSec; // Queue in-memory buffer for playback ZeroMemory(&m_soundEffects[sound].m_audioBuffer, sizeof(m_soundEffects[sound].m_audioBuffer)); m_soundEffects[sound].m_audioBuffer.AudioBytes = m_soundEffects[sound].m_soundEffectBufferLength; m_soundEffects[sound].m_audioBuffer.pAudioData = m_soundEffects[sound].m_soundEffectBufferData; m_soundEffects[sound].m_audioBuffer.pContext = &m_soundEffects[sound]; m_soundEffects[sound].m_audioBuffer.Flags = XAUDIO2_END_OF_STREAM; if (sound == RollingEvent) { m_soundEffects[sound].m_audioBuffer.LoopCount = XAUDIO2_LOOP_INFINITE; } else { m_soundEffects[sound].m_audioBuffer.LoopCount = 0; } if (sound == FallingEvent || sound == CheckpointEvent) { m_soundEffects[sound].m_soundEffectSourceVoice->SetVolume(0.4f); } DX::ThrowIfFailed( m_soundEffects[sound].m_soundEffectSourceVoice->SubmitSourceBuffer(&m_soundEffects[sound].m_audioBuffer) ); }