| 533 | } |
| 534 | |
| 535 | std::weak_ptr<Effect> Xaudio2Implementation::playSample(IXAudio2SubmixVoice * submix, EffectDesc const & desc) |
| 536 | { |
| 537 | std::weak_ptr<Effect> result; |
| 538 | |
| 539 | if (!canPlaySample(desc.sample)) |
| 540 | return result; |
| 541 | |
| 542 | uint32_t key = makeKey(desc.sample); |
| 543 | |
| 544 | WAVEFORMATEX wfx; |
| 545 | getFormatEX(desc.sample, &wfx); |
| 546 | |
| 547 | std::lock_guard<std::mutex> guard(m_voicePoolMutex); |
| 548 | |
| 549 | if (IXAudio2SourceVoice * voice = allocateVoice(key, wfx)) |
| 550 | { |
| 551 | HRESULT hr; |
| 552 | if (FAILED(hr = voice->SetSourceSampleRate(desc.sample->sampleRate))) |
| 553 | { |
| 554 | #ifdef _DEBUG |
| 555 | // manuelk : not sure why sometimes Xaudio2 returns XAUDIO2_E_INVALID_CALL here |
| 556 | // when queuing many effects too quickly - setting to fail silently in release mode |
| 557 | log::warning("AudioEngine : failed to set sample rate for audio sample (%08x)", hr); |
| 558 | #endif |
| 559 | return result; |
| 560 | } |
| 561 | |
| 562 | if (!setOutputVoice(submix, voice)) |
| 563 | return result; |
| 564 | |
| 565 | XAUDIO2_BUFFER buffer = { 0 }; |
| 566 | buffer.pAudioData = (BYTE const *)desc.sample->samples; |
| 567 | buffer.Flags = XAUDIO2_END_OF_STREAM; |
| 568 | buffer.AudioBytes = desc.sample->samplesSize; |
| 569 | buffer.LoopCount = desc.loop == 1 ? XAUDIO2_NO_LOOP_REGION : std::min(desc.loop, (uint32_t)XAUDIO2_LOOP_INFINITE); |
| 570 | |
| 571 | if (voice->SubmitSourceBuffer(&buffer) != S_OK) { |
| 572 | log::warning("AudioEngine : error SubmitSourceBuffer"); |
| 573 | return result; |
| 574 | } |
| 575 | |
| 576 | if (desc.volume!=1.f) |
| 577 | voice->SetVolume(desc.volume); |
| 578 | if (desc.pitch!=1.f) |
| 579 | voice->SetFrequencyRatio(desc.pitch); |
| 580 | if (desc.pan != 0.f) |
| 581 | applyPan(desc.pan, voice, desc.sample->nchannels); |
| 582 | |
| 583 | if (FAILED(hr = voice->Start(0))) |
| 584 | { |
| 585 | log::warning("Error starting voice for audio sample"); |
| 586 | return result; |
| 587 | } |
| 588 | |
| 589 | std::shared_ptr<Xaudio2Effect> effect; |
| 590 | |
| 591 | if (!m_options.use3D || !desc.transform) |
| 592 | effect = std::make_shared<Xaudio2Effect>(); |
nothing calls this directly
no test coverage detected