2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2019-03-31 01:37:06 +08:00
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
#include "../kiwano-audio.h"
|
2019-04-11 16:00:57 +08:00
|
|
|
#include "Sound.h"
|
2019-03-31 01:37:06 +08:00
|
|
|
#include "Transcoder.h"
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
Sound::Sound()
|
2019-03-31 01:37:06 +08:00
|
|
|
: opened_(false)
|
|
|
|
|
, playing_(false)
|
|
|
|
|
, size_(0)
|
2019-04-11 16:00:57 +08:00
|
|
|
, wave_data_(nullptr)
|
2019-03-31 01:37:06 +08:00
|
|
|
, voice_(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
Sound::Sound(Resource const& res)
|
|
|
|
|
: Sound()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
Load(res);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
Sound::~Sound()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
bool Sound::Load(Resource const& res)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (opened_)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
|
Transcoder transcoder;
|
|
|
|
|
|
|
|
|
|
if (res.IsFileType())
|
|
|
|
|
{
|
|
|
|
|
if (!modules::Shlwapi::Get().PathFileExistsW(res.GetFileName().c_str()))
|
|
|
|
|
{
|
2019-04-11 14:40:54 +08:00
|
|
|
KGE_WARNING_LOG(L"Media file '%s' not found", res.GetFileName().c_str());
|
2019-03-31 01:37:06 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
hr = transcoder.LoadMediaFile(res.GetFileName(), &wave_data_, &size_);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hr = transcoder.LoadMediaResource(res, &wave_data_, &size_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
|
{
|
2019-04-11 14:40:54 +08:00
|
|
|
KGE_ERROR_LOG(L"Load media file failed with HRESULT of %08X", hr);
|
2019-03-31 01:37:06 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
hr = Audio::Instance().CreateVoice(&voice_, transcoder.GetWaveFormatEx());
|
2019-03-31 01:37:06 +08:00
|
|
|
if (FAILED(hr))
|
|
|
|
|
{
|
|
|
|
|
if (wave_data_)
|
|
|
|
|
{
|
|
|
|
|
delete[] wave_data_;
|
|
|
|
|
wave_data_ = nullptr;
|
|
|
|
|
}
|
2019-04-11 14:40:54 +08:00
|
|
|
KGE_ERROR_LOG(L"Create source voice failed with HRESULT of %08X", hr);
|
2019-03-31 01:37:06 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opened_ = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
void Sound::Play(int loop_count)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (!opened_)
|
|
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
KGE_ERROR_LOG(L"Sound must be opened first!");
|
|
|
|
|
return;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL");
|
|
|
|
|
|
|
|
|
|
// if sound stream is not empty, stop() will clear it
|
|
|
|
|
XAUDIO2_VOICE_STATE state;
|
|
|
|
|
voice_->GetState(&state);
|
|
|
|
|
if (state.BuffersQueued)
|
2019-03-31 01:37:06 +08:00
|
|
|
Stop();
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
// clamp loop count
|
|
|
|
|
loop_count = (loop_count < 0) ? XAUDIO2_LOOP_INFINITE : std::min(loop_count, XAUDIO2_LOOP_INFINITE - 1);
|
|
|
|
|
|
|
|
|
|
XAUDIO2_BUFFER buffer = { 0 };
|
|
|
|
|
buffer.pAudioData = wave_data_;
|
|
|
|
|
buffer.Flags = XAUDIO2_END_OF_STREAM;
|
|
|
|
|
buffer.AudioBytes = size_;
|
|
|
|
|
buffer.LoopCount = static_cast<UINT32>(loop_count);
|
|
|
|
|
|
|
|
|
|
HRESULT hr = voice_->SubmitSourceBuffer(&buffer);
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
{
|
|
|
|
|
hr = voice_->Start();
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
|
{
|
2019-04-11 14:40:54 +08:00
|
|
|
KGE_ERROR_LOG(L"Submitting source buffer failed with HRESULT of %08X", hr);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playing_ = SUCCEEDED(hr);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
void Sound::Pause()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL");
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(voice_->Stop()))
|
2019-03-31 01:37:06 +08:00
|
|
|
playing_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
void Sound::Resume()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL");
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(voice_->Start()))
|
2019-03-31 01:37:06 +08:00
|
|
|
playing_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
void Sound::Stop()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL");
|
|
|
|
|
|
|
|
|
|
HRESULT hr = voice_->Stop();
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
hr = voice_->ExitLoop();
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
hr = voice_->FlushSourceBuffers();
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
2019-03-31 01:37:06 +08:00
|
|
|
playing_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
void Sound::Close()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
if (voice_)
|
|
|
|
|
{
|
|
|
|
|
voice_->Stop();
|
|
|
|
|
voice_->FlushSourceBuffers();
|
|
|
|
|
voice_->DestroyVoice();
|
|
|
|
|
voice_ = nullptr;
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
|
|
|
if (wave_data_)
|
|
|
|
|
{
|
|
|
|
|
delete[] wave_data_;
|
|
|
|
|
wave_data_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opened_ = false;
|
|
|
|
|
playing_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
bool Sound::IsPlaying() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (opened_)
|
|
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
if (!voice_)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
XAUDIO2_VOICE_STATE state;
|
|
|
|
|
voice_->GetState(&state);
|
|
|
|
|
UINT32 buffers_queued = state.BuffersQueued;
|
|
|
|
|
|
2019-03-31 01:37:06 +08:00
|
|
|
if (buffers_queued && playing_)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
float Sound::GetVolume() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL");
|
|
|
|
|
|
|
|
|
|
float volume = 0.0f;
|
|
|
|
|
voice_->GetVolume(&volume);
|
2019-03-31 01:37:06 +08:00
|
|
|
return volume;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 16:00:57 +08:00
|
|
|
void Sound::SetVolume(float volume)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-04-11 16:00:57 +08:00
|
|
|
KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL");
|
|
|
|
|
|
|
|
|
|
volume = std::min(std::max(volume, -224.f), 224.f);
|
|
|
|
|
voice_->SetVolume(volume);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|