2018-11-08 21:39:26 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Easy2D - Nomango
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
|
|
#include "Music.h"
|
2018-11-12 20:46:54 +08:00
|
|
|
|
#include "../utils/Transcoder.h"
|
|
|
|
|
|
#include "../utils/File.h"
|
|
|
|
|
|
#include "modules.h"
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
|
|
#include "logs.h"
|
2018-11-08 21:39:26 +08:00
|
|
|
|
|
|
|
|
|
|
namespace easy2d
|
|
|
|
|
|
{
|
|
|
|
|
|
Music::Music()
|
|
|
|
|
|
: opened_(false)
|
|
|
|
|
|
, playing_(false)
|
|
|
|
|
|
, wave_data_(nullptr)
|
|
|
|
|
|
, size_(0)
|
|
|
|
|
|
, voice_(nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Music::Music(const String& file_path)
|
|
|
|
|
|
: opened_(false)
|
|
|
|
|
|
, playing_(false)
|
|
|
|
|
|
, wave_data_(nullptr)
|
|
|
|
|
|
, size_(0)
|
|
|
|
|
|
, voice_(nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
Load(file_path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Music::Music(Resource& res)
|
|
|
|
|
|
: opened_(false)
|
|
|
|
|
|
, playing_(false)
|
|
|
|
|
|
, wave_data_(nullptr)
|
|
|
|
|
|
, size_(0)
|
|
|
|
|
|
, voice_(nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
Load(res);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Music::~Music()
|
|
|
|
|
|
{
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Music::Load(const String & file_path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (opened_)
|
|
|
|
|
|
{
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
File music_file;
|
|
|
|
|
|
if (!music_file.Open(file_path))
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
logs::Trace(L"Media file not found.");
|
2018-11-08 21:39:26 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8> File::AddSearchPath <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
// Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫͨ<D2AA><CDA8> File::GetPath <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
|
|
|
|
|
String music_file_path = music_file.GetPath();
|
|
|
|
|
|
|
|
|
|
|
|
Transcoder transcoder;
|
2018-11-12 20:46:54 +08:00
|
|
|
|
HRESULT hr = transcoder.LoadMediaFile(music_file_path.c_str(), &wave_data_, &size_);
|
|
|
|
|
|
if (FAILED(hr))
|
2018-11-08 21:39:26 +08:00
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
logs::Trace(L"Load media from file failed.", hr);
|
2018-11-08 21:39:26 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-12 20:46:54 +08:00
|
|
|
|
hr = devices::Audio::Instance().CreateVoice(&voice_, transcoder.GetWaveFormatEx());
|
2018-11-08 21:39:26 +08:00
|
|
|
|
if (FAILED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (wave_data_)
|
|
|
|
|
|
{
|
|
|
|
|
|
delete[] wave_data_;
|
|
|
|
|
|
wave_data_ = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
logs::Trace(L"Create source voice error", hr);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
opened_ = true;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Music::Load(Resource& res)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (opened_)
|
|
|
|
|
|
{
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Transcoder transcoder;
|
2018-11-12 20:46:54 +08:00
|
|
|
|
HRESULT hr = transcoder.LoadMediaResource(res, &wave_data_, &size_);
|
|
|
|
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
2018-11-08 21:39:26 +08:00
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
logs::Trace(L"Load media from resource failed.", hr);
|
2018-11-08 21:39:26 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-12 20:46:54 +08:00
|
|
|
|
hr = devices::Audio::Instance().CreateVoice(&voice_, transcoder.GetWaveFormatEx());
|
2018-11-08 21:39:26 +08:00
|
|
|
|
if (FAILED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (wave_data_)
|
|
|
|
|
|
{
|
|
|
|
|
|
delete[] wave_data_;
|
|
|
|
|
|
wave_data_ = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
logs::Trace(L"Create source voice error", hr);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
opened_ = true;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Music::Play(int loop_count)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!opened_)
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
logs::Trace(L"Music must be opened first!");
|
2018-11-08 21:39:26 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-12 20:46:54 +08:00
|
|
|
|
UINT32 buffers_queued = 0;
|
|
|
|
|
|
voice_.GetBuffersQueued(&buffers_queued);
|
|
|
|
|
|
if (buffers_queued)
|
2018-11-08 21:39:26 +08:00
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
|
|
|
|
if (loop_count < 0)
|
|
|
|
|
|
loop_count = XAUDIO2_LOOP_INFINITE;
|
|
|
|
|
|
else
|
|
|
|
|
|
loop_count = std::min(loop_count, XAUDIO2_LOOP_INFINITE - 1);
|
|
|
|
|
|
|
2018-11-12 20:46:54 +08:00
|
|
|
|
HRESULT hr = voice_.Play(wave_data_, size_, static_cast<UINT32>(loop_count));
|
|
|
|
|
|
if (FAILED(hr))
|
2018-11-08 21:39:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
logs::Trace(L"Submitting source buffer error", hr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
playing_ = SUCCEEDED(hr);
|
|
|
|
|
|
|
|
|
|
|
|
return playing_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Music::Pause()
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
if (SUCCEEDED(voice_.Pause()))
|
|
|
|
|
|
playing_ = false;
|
2018-11-08 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Music::Resume()
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
if (SUCCEEDED(voice_.Resume()))
|
|
|
|
|
|
playing_ = true;
|
2018-11-08 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Music::Stop()
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
if (SUCCEEDED(voice_.Stop()))
|
|
|
|
|
|
playing_ = false;
|
2018-11-08 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Music::Close()
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
voice_.Destroy();
|
2018-11-08 21:39:26 +08:00
|
|
|
|
|
|
|
|
|
|
if (wave_data_)
|
|
|
|
|
|
{
|
|
|
|
|
|
delete[] wave_data_;
|
|
|
|
|
|
wave_data_ = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
opened_ = false;
|
|
|
|
|
|
playing_ = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Music::IsPlaying() const
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
if (opened_)
|
2018-11-08 21:39:26 +08:00
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
UINT32 buffers_queued = 0;
|
|
|
|
|
|
voice_.GetBuffersQueued(&buffers_queued);
|
|
|
|
|
|
if (buffers_queued && playing_)
|
2018-11-08 21:39:26 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float Music::GetVolume() const
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
float volume = 0.f;
|
|
|
|
|
|
voice_.GetVolume(&volume);
|
|
|
|
|
|
return volume;
|
2018-11-08 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Music::SetVolume(float volume)
|
|
|
|
|
|
{
|
2018-11-12 20:46:54 +08:00
|
|
|
|
return SUCCEEDED(voice_.SetVolume(volume));
|
2018-11-08 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|