Merge pull request #12472 from MSOpenTech/v3-winrt-audio

V3: WinRT Fix for issue 12448 cpp-tests[ win8.1-universal--window 8.1]:Audio - CocosDenshion:Crash
This commit is contained in:
pandamicro 2015-06-23 20:01:18 +08:00
commit 1d4a496aaa
2 changed files with 59 additions and 43 deletions

View File

@ -61,11 +61,13 @@ void Audio::Initialize()
void Audio::CreateResources()
{
try
do
{
ThrowIfFailed(
XAudio2Create(&m_musicEngine)
);
if (FAILED(XAudio2Create(&m_musicEngine)))
{
m_engineExperiencedCriticalError = true;
break;
}
#if defined(_DEBUG)
XAUDIO2_DEBUG_CONFIGURATION debugConfig = {0};
@ -83,31 +85,33 @@ void Audio::CreateResources()
// decode the data then we feed it through the XAudio2 pipeline as a separate Mastering Voice, so that we can tag it
// as Game Media.
// We default the mastering voice to 2 channels to simplify the reverb logic.
ThrowIfFailed(
m_musicEngine->CreateMasteringVoice(&m_musicMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameMedia)
);
if(FAILED(m_musicEngine->CreateMasteringVoice(&m_musicMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameMedia)))
{
m_engineExperiencedCriticalError = true;
break;
}
// Create a separate engine and mastering voice for sound effects in the sample
// Games will use many voices in a complex graph for audio, mixing all effects down to a
// single mastering voice.
// We are creating an entirely new engine instance and mastering voice in order to tag
// our sound effects with the audio category AudioCategory_GameEffects.
ThrowIfFailed(
XAudio2Create(&m_soundEffectEngine)
);
if(FAILED(XAudio2Create(&m_soundEffectEngine)))
{
m_engineExperiencedCriticalError = true;
break;
}
m_soundEffectEngineCallback.Initialize(this);
m_soundEffectEngine->RegisterForCallbacks(&m_soundEffectEngineCallback);
// We default the mastering voice to 2 channels to simplify the reverb logic.
ThrowIfFailed(
m_soundEffectEngine->CreateMasteringVoice(&m_soundEffectMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameEffects)
);
}
catch (...)
{
m_engineExperiencedCriticalError = true;
}
if(FAILED(m_soundEffectEngine->CreateMasteringVoice(&m_soundEffectMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameEffects)))
{
m_engineExperiencedCriticalError = true;
break;
}
} while (false);
}
unsigned int Audio::Hash(const char *key)
@ -309,9 +313,10 @@ void Audio::PlaySoundEffect(unsigned int sound)
StopSoundEffect(sound);
ThrowIfFailed(
m_soundEffects[sound].m_soundEffectSourceVoice->SubmitSourceBuffer(&m_soundEffects[sound].m_audioBuffer)
);
if (FAILED(m_soundEffects[sound].m_soundEffectSourceVoice->SubmitSourceBuffer(&m_soundEffects[sound].m_audioBuffer)))
{
m_engineExperiencedCriticalError = true;
}
if (m_engineExperiencedCriticalError) {
// If there's an error, then we'll recreate the engine on the next render pass
@ -560,10 +565,11 @@ void Audio::PreloadSoundEffect(const char* pszFilePath, bool isMusic)
sends.SendCount = 1;
sends.pSends = descriptors;
ThrowIfFailed(
m_musicEngine->CreateSourceVoice(&m_soundEffects[sound].m_soundEffectSourceVoice,
&wfx, 0, 1.0f, &m_voiceContext, &sends)
);
if (FAILED(m_musicEngine->CreateSourceVoice(&m_soundEffects[sound].m_soundEffectSourceVoice,
&wfx, 0, 1.0f, &m_voiceContext, &sends)))
{
m_engineExperiencedCriticalError = true;
}
//fix bug: set a initial volume
m_soundEffects[sound].m_soundEffectSourceVoice->SetVolume(m_backgroundMusicVolume);
} else
@ -575,10 +581,11 @@ void Audio::PreloadSoundEffect(const char* pszFilePath, bool isMusic)
sends.SendCount = 1;
sends.pSends = descriptors;
ThrowIfFailed(
m_soundEffectEngine->CreateSourceVoice(&m_soundEffects[sound].m_soundEffectSourceVoice,
&wfx, 0, 1.0f, &m_voiceContext, &sends, nullptr)
);
if(FAILED(m_soundEffectEngine->CreateSourceVoice(&m_soundEffects[sound].m_soundEffectSourceVoice,
&wfx, 0, 1.0f, &m_voiceContext, &sends, nullptr)))
{
m_engineExperiencedCriticalError = true;
}
//fix bug: set a initial volume
m_soundEffects[sound].m_soundEffectSourceVoice->SetVolume(m_soundEffctVolume);
}

View File

@ -311,6 +311,7 @@ void AudioPlayer::setVolume(float volume)
bool AudioPlayer::play2d(AudioCache* cache)
{
bool ret = false;
HRESULT hr = S_OK;
if (cache != nullptr)
{
@ -323,12 +324,14 @@ bool AudioPlayer::play2d(AudioCache* cache)
XAUDIO2_VOICE_SENDS sends = { 0 };
sends.SendCount = 1;
sends.pSends = descriptors;
ThrowIfFailed(_xaEngine->CreateSourceVoice(&_xaSourceVoice, &cache->_audInfo._wfx, 0, 1.0, this, &sends));
hr = _xaEngine->CreateSourceVoice(&_xaSourceVoice, &cache->_audInfo._wfx, 0, 1.0, this, &sends);
}
_isStreaming = _cache->isStreamingSource();
_duration = getDuration();
ret = _play();
if (SUCCEEDED(hr)) {
_isStreaming = _cache->isStreamingSource();
_duration = getDuration();
ret = _play();
}
}
return ret;
@ -336,20 +339,26 @@ bool AudioPlayer::play2d(AudioCache* cache)
void AudioPlayer::init()
{
memset(&_xaBuffer, 0, sizeof(_xaBuffer));
ThrowIfFailed(XAudio2Create(_xaEngine.ReleaseAndGetAddressOf()));
do {
memset(&_xaBuffer, 0, sizeof(_xaBuffer));
if (FAILED(XAudio2Create(_xaEngine.ReleaseAndGetAddressOf()))) {
break;
}
#if defined(_DEBUG)
XAUDIO2_DEBUG_CONFIGURATION debugConfig = { 0 };
debugConfig.BreakMask = XAUDIO2_LOG_ERRORS;
debugConfig.TraceMask = XAUDIO2_LOG_ERRORS;
_xaEngine->SetDebugConfiguration(&debugConfig);
XAUDIO2_DEBUG_CONFIGURATION debugConfig = { 0 };
debugConfig.BreakMask = XAUDIO2_LOG_ERRORS;
debugConfig.TraceMask = XAUDIO2_LOG_ERRORS;
_xaEngine->SetDebugConfiguration(&debugConfig);
#endif
_xaEngine->RegisterForCallbacks(this);
ThrowIfFailed(_xaEngine->CreateMasteringVoice(&_xaMasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameMedia));
_ready = true;
_state = AudioPlayerState::READY;
_xaEngine->RegisterForCallbacks(this);
if (FAILED(_xaEngine->CreateMasteringVoice(&_xaMasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameMedia))) {
break;
}
_ready = true;
_state = AudioPlayerState::READY;
} while (false);
}
void AudioPlayer::free()