| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-13 19:40:41 +08:00
										 |  |  | #include "easy2d-audio.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | #include "Music.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-11 17:24:11 +08:00
										 |  |  | #include "Transcoder.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace easy2d | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Music::Music() | 
					
						
							|  |  |  | 		: opened_(false) | 
					
						
							|  |  |  | 		, playing_(false) | 
					
						
							|  |  |  | 		, wave_data_(nullptr) | 
					
						
							|  |  |  | 		, size_(0) | 
					
						
							|  |  |  | 		, voice_(nullptr) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 	Music::Music(Resource const& res) | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		: Music() | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		Load(res); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Music::~Music() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		Close(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 	bool Music::Load(Resource const& res) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (opened_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Close(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		HRESULT hr = S_OK; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		Transcoder transcoder; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		if (res.IsFileType()) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-03-11 17:24:11 +08:00
										 |  |  | 			if (!modules::Shlwapi::Get().PathFileExistsW(res.GetFileName().c_str())) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-09 00:03:24 +08:00
										 |  |  | 				E2D_WARNING_LOG(L"Media file '%s' not found", res.GetFileName().c_str()); | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 			hr = transcoder.LoadMediaFile(res.GetFileName(), &wave_data_, &size_); | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 			hr = transcoder.LoadMediaResource(res, &wave_data_, &size_); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  | 		if (FAILED(hr)) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-03-14 17:55:06 +08:00
										 |  |  | 			E2D_ERROR_LOG(L"Load media file failed with HRESULT of %08X", hr); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 16:12:15 +08:00
										 |  |  | 		hr = 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; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-03-14 17:55:06 +08:00
										 |  |  | 			E2D_ERROR_LOG(L"Create source voice failed with HRESULT of %08X", hr); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		opened_ = true; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool Music::Play(int loop_count) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!opened_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-07 21:31:30 +08:00
										 |  |  | 			E2D_ERROR_LOG(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
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-03-14 17:55:06 +08:00
										 |  |  | 			E2D_ERROR_LOG(L"Submitting source buffer failed with HRESULT of %08X", hr); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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
										 |  |  | 	} | 
					
						
							|  |  |  | } |