| 
									
										
										
										
											2018-10-03 22:02:46 +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.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 13:33:39 +08:00
										 |  |  |  | #include "..\e2dtool.h"
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | #include "..\e2dmodule.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | inline bool TraceError(wchar_t* prompt) | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	WARN("Music error: %s failed!", prompt); | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | inline bool TraceError(wchar_t* prompt, HRESULT hr) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	WARN("Music error: %s (%#X)", prompt, hr); | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | namespace easy2d | 
					
						
							| 
									
										
										
										
											2018-04-24 10:35:58 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	class Transcoder | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		WAVEFORMATEX* wave_format_; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	public: | 
					
						
							|  |  |  |  | 		Transcoder() | 
					
						
							|  |  |  |  | 			: wave_format_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		~Transcoder() | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (wave_format_) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				::CoTaskMemFree(wave_format_); | 
					
						
							|  |  |  |  | 				wave_format_ = nullptr; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-27 17:07:47 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		WAVEFORMATEX* GetWaveFormatEx() | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return wave_format_; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-27 17:07:47 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		bool LoadMediaFile(LPCWSTR file_path, BYTE** wave_data, UINT32* wave_data_size) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			HRESULT hr = S_OK; | 
					
						
							| 
									
										
										
										
											2018-04-27 17:07:47 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			IMFSourceReader* reader = nullptr; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			hr = MFCreateSourceReaderFromURL( | 
					
						
							|  |  |  |  | 				file_path, | 
					
						
							|  |  |  |  | 				nullptr, | 
					
						
							|  |  |  |  | 				&reader | 
					
						
							|  |  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = ReadSource(reader, wave_data, wave_data_size); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			SafeRelease(reader); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			return SUCCEEDED(hr); | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		bool LoadMediaResource(LPCWSTR res_name, LPCWSTR res_type, BYTE** wave_data, UINT32* wave_data_size) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			HRESULT	hr = S_OK; | 
					
						
							|  |  |  |  | 			HRSRC	res_info; | 
					
						
							|  |  |  |  | 			HGLOBAL	res_data; | 
					
						
							|  |  |  |  | 			DWORD	res_size; | 
					
						
							|  |  |  |  | 			void*	res; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			IStream*			stream = nullptr; | 
					
						
							|  |  |  |  | 			IMFByteStream*		byte_stream = nullptr; | 
					
						
							|  |  |  |  | 			IMFSourceReader*	reader = nullptr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			res_info = FindResourceW(HINST_THISCOMPONENT, res_name, res_type); | 
					
						
							|  |  |  |  | 			if (res_info == nullptr) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				return TraceError(L"FindResource"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			res_data = LoadResource(HINST_THISCOMPONENT, res_info); | 
					
						
							|  |  |  |  | 			if (res_data == nullptr) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				return TraceError(L"LoadResource"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			res_size = SizeofResource(HINST_THISCOMPONENT, res_info); | 
					
						
							|  |  |  |  | 			if (res_size == 0) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				return TraceError(L"SizeofResource"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			res = LockResource(res_data); | 
					
						
							|  |  |  |  | 			if (res == nullptr) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				return TraceError(L"LockResource"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			stream = ::SHCreateMemStream( | 
					
						
							|  |  |  |  | 				static_cast<const BYTE*>(res), | 
					
						
							|  |  |  |  | 				static_cast<UINT>(res_size) | 
					
						
							|  |  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-05-24 15:47:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			if (stream == nullptr) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				return TraceError(L"SHCreateMemStream"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = MFCreateMFByteStreamOnStream(stream, &byte_stream); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = MFCreateSourceReaderFromByteStream( | 
					
						
							|  |  |  |  | 					byte_stream, | 
					
						
							|  |  |  |  | 					nullptr, | 
					
						
							|  |  |  |  | 					&reader | 
					
						
							|  |  |  |  | 				); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = ReadSource(reader, wave_data, wave_data_size); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			SafeRelease(stream); | 
					
						
							|  |  |  |  | 			SafeRelease(byte_stream); | 
					
						
							|  |  |  |  | 			SafeRelease(reader); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			return SUCCEEDED(hr); | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		HRESULT ReadSource(IMFSourceReader* reader, BYTE** wave_data, UINT32* wave_data_size) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			HRESULT hr = S_OK; | 
					
						
							|  |  |  |  | 			DWORD max_stream_size = 0; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			IMFMediaType* partial_type = nullptr; | 
					
						
							|  |  |  |  | 			IMFMediaType* uncompressed_type = nullptr; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			hr = MFCreateMediaType(&partial_type); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = partial_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = partial_type->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			// <20><><EFBFBD><EFBFBD> source reader <20><>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ú<EFBFBD><C3BA>ʵĽ<CAB5><C4BD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = reader->SetCurrentMediaType( | 
					
						
							|  |  |  |  | 					MF_SOURCE_READER_FIRST_AUDIO_STREAM, | 
					
						
							|  |  |  |  | 					0, | 
					
						
							|  |  |  |  | 					partial_type | 
					
						
							|  |  |  |  | 				); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			// <20><> IMFMediaType <20>л<EFBFBD>ȡ WAVEFORMAT <20>ṹ
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = reader->GetCurrentMediaType( | 
					
						
							|  |  |  |  | 					MF_SOURCE_READER_FIRST_AUDIO_STREAM, | 
					
						
							|  |  |  |  | 					&uncompressed_type | 
					
						
							|  |  |  |  | 				); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			// ָ<><D6B8><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				hr = reader->SetStreamSelection( | 
					
						
							|  |  |  |  | 					MF_SOURCE_READER_FIRST_AUDIO_STREAM, | 
					
						
							|  |  |  |  | 					true | 
					
						
							|  |  |  |  | 				); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			// <20><>ȡ WAVEFORMAT <20><><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				UINT32 size = 0; | 
					
						
							|  |  |  |  | 				hr = MFCreateWaveFormatExFromMFMediaType( | 
					
						
							|  |  |  |  | 					uncompressed_type, | 
					
						
							|  |  |  |  | 					&wave_format_, | 
					
						
							|  |  |  |  | 					&size | 
					
						
							|  |  |  |  | 				); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>С
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				PROPVARIANT prop; | 
					
						
							|  |  |  |  | 				PropVariantInit(&prop); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				hr = reader->GetPresentationAttribute( | 
					
						
							|  |  |  |  | 					MF_SOURCE_READER_MEDIASOURCE, | 
					
						
							|  |  |  |  | 					MF_PD_DURATION, | 
					
						
							|  |  |  |  | 					&prop | 
					
						
							|  |  |  |  | 				); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				LONGLONG duration = prop.uhVal.QuadPart; | 
					
						
							|  |  |  |  | 				max_stream_size = static_cast<DWORD>( | 
					
						
							|  |  |  |  | 					(duration * wave_format_->nAvgBytesPerSec) / 10000000 + 1 | 
					
						
							|  |  |  |  | 					); | 
					
						
							|  |  |  |  | 				PropVariantClear(&prop); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			// <20><>ȡ<EFBFBD><C8A1>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				DWORD flags = 0; | 
					
						
							|  |  |  |  | 				DWORD position = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				IMFSample*		sample = nullptr; | 
					
						
							|  |  |  |  | 				IMFMediaBuffer*	buffer = nullptr; | 
					
						
							|  |  |  |  | 				BYTE*			data = new (std::nothrow) BYTE[max_stream_size]; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				if (data == nullptr) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					TraceError(L"Low memory"); | 
					
						
							|  |  |  |  | 					hr = E_OUTOFMEMORY; | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 				else | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					while (true) | 
					
						
							|  |  |  |  | 					{ | 
					
						
							|  |  |  |  | 						hr = reader->ReadSample( | 
					
						
							|  |  |  |  | 							MF_SOURCE_READER_FIRST_AUDIO_STREAM, | 
					
						
							|  |  |  |  | 							0, | 
					
						
							|  |  |  |  | 							nullptr, | 
					
						
							|  |  |  |  | 							&flags, | 
					
						
							|  |  |  |  | 							nullptr, | 
					
						
							|  |  |  |  | 							&sample | 
					
						
							|  |  |  |  | 						); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 						if (flags & MF_SOURCE_READERF_ENDOFSTREAM) { break; } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 						if (sample == nullptr) { continue; } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 						if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 						{ | 
					
						
							|  |  |  |  | 							hr = sample->ConvertToContiguousBuffer(&buffer); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 							if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 							{ | 
					
						
							|  |  |  |  | 								BYTE *audio_data = nullptr; | 
					
						
							|  |  |  |  | 								DWORD sample_buffer_length = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 								hr = buffer->Lock( | 
					
						
							|  |  |  |  | 									&audio_data, | 
					
						
							|  |  |  |  | 									nullptr, | 
					
						
							|  |  |  |  | 									&sample_buffer_length | 
					
						
							|  |  |  |  | 								); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 								if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 								{ | 
					
						
							|  |  |  |  | 									for (DWORD i = 0; i < sample_buffer_length; i++) | 
					
						
							|  |  |  |  | 									{ | 
					
						
							|  |  |  |  | 										data[position++] = audio_data[i]; | 
					
						
							|  |  |  |  | 									} | 
					
						
							|  |  |  |  | 									hr = buffer->Unlock(); | 
					
						
							|  |  |  |  | 								} | 
					
						
							|  |  |  |  | 							} | 
					
						
							|  |  |  |  | 							SafeRelease(buffer); | 
					
						
							|  |  |  |  | 						} | 
					
						
							|  |  |  |  | 						SafeRelease(sample); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 						if (FAILED(hr)) { break; } | 
					
						
							|  |  |  |  | 					} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 					if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 					{ | 
					
						
							|  |  |  |  | 						*wave_data = data; | 
					
						
							|  |  |  |  | 						*wave_data_size = position; | 
					
						
							|  |  |  |  | 					} | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			SafeRelease(partial_type); | 
					
						
							|  |  |  |  | 			SafeRelease(uncompressed_type); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			return hr; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | easy2d::Music::Music() | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	: opened_(false) | 
					
						
							|  |  |  |  | 	, playing_(false) | 
					
						
							|  |  |  |  | 	, wave_data_(nullptr) | 
					
						
							|  |  |  |  | 	, size_(0) | 
					
						
							|  |  |  |  | 	, voice_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-27 17:27:05 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | easy2d::Music::~Music() | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	Close(); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-27 17:27:05 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | bool easy2d::Music::Load(const easy2d::String & file_path) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (opened_) | 
					
						
							| 
									
										
										
										
											2018-04-27 17:27:05 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		Close(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	File music_file; | 
					
						
							|  |  |  |  | 	if (!music_file.Open(file_path)) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		WARN("Music::Load error: File not found."); | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-04-27 17:27:05 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	// <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(); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	Transcoder transcoder; | 
					
						
							|  |  |  |  | 	if (!transcoder.LoadMediaFile((LPCWSTR)music_file_path, &wave_data_, &size_)) | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	HRESULT hr = Device::GetAudio()->CreateVoice(&voice_, transcoder.GetWaveFormatEx()); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	if (FAILED(hr)) | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		if (wave_data_) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			delete[] wave_data_; | 
					
						
							|  |  |  |  | 			wave_data_ = nullptr; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		return TraceError(L"Create source voice error", hr); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	opened_ = true; | 
					
						
							|  |  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | bool easy2d::Music::Load(const Resource& res) | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (opened_) | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		Close(); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	Transcoder transcoder; | 
					
						
							|  |  |  |  | 	if (!transcoder.LoadMediaResource(MAKEINTRESOURCE(res.id), (LPCWSTR)res.type, &wave_data_, &size_)) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	HRESULT hr = Device::GetAudio()->CreateVoice(&voice_, transcoder.GetWaveFormatEx()); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	if (FAILED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		if (wave_data_) | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 			delete[] wave_data_; | 
					
						
							|  |  |  |  | 			wave_data_ = nullptr; | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		return TraceError(L"Create source voice error", hr); | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	opened_ = true; | 
					
						
							|  |  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | bool easy2d::Music::Play(int loop_count) | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (!opened_) | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		WARN("Music::Play Failed: Music must be opened first!"); | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (voice_ == nullptr) | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		WARN("Music::Play Failed: IXAudio2SourceVoice Null pointer exception!"); | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	XAUDIO2_VOICE_STATE state; | 
					
						
							|  |  |  |  | 	voice_->GetState(&state); | 
					
						
							|  |  |  |  | 	if (state.BuffersQueued) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		Stop(); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (loop_count < 0) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		loop_count = XAUDIO2_LOOP_INFINITE; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		loop_count = std::min(loop_count, XAUDIO2_LOOP_INFINITE - 1); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	// <20>ύ wave <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	XAUDIO2_BUFFER buffer = { 0 }; | 
					
						
							|  |  |  |  | 	buffer.pAudioData = wave_data_; | 
					
						
							|  |  |  |  | 	buffer.Flags = XAUDIO2_END_OF_STREAM; | 
					
						
							|  |  |  |  | 	buffer.AudioBytes = size_; | 
					
						
							|  |  |  |  | 	buffer.LoopCount = loop_count; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	HRESULT hr; | 
					
						
							|  |  |  |  | 	if (FAILED(hr = voice_->SubmitSourceBuffer(&buffer))) | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		return TraceError(L"Submitting source buffer error", hr); | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	hr = voice_->Start(0); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	playing_ = SUCCEEDED(hr); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	return playing_; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | void easy2d::Music::Pause() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (voice_) | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		if (SUCCEEDED(voice_->Stop())) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			playing_ = false; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-01 22:07:44 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | void easy2d::Music::Resume() | 
					
						
							| 
									
										
										
										
											2018-05-22 11:58:45 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (voice_) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		if (SUCCEEDED(voice_->Start())) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			playing_ = true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | void easy2d::Music::Stop() | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (voice_) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		if (SUCCEEDED(voice_->Stop())) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			voice_->ExitLoop(); | 
					
						
							|  |  |  |  | 			voice_->FlushSourceBuffers(); | 
					
						
							|  |  |  |  | 			playing_ = false; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | void easy2d::Music::Close() | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (voice_) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		voice_->Stop(); | 
					
						
							|  |  |  |  | 		voice_->FlushSourceBuffers(); | 
					
						
							|  |  |  |  | 		voice_->DestroyVoice(); | 
					
						
							|  |  |  |  | 		voice_ = nullptr; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (wave_data_) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		delete[] wave_data_; | 
					
						
							|  |  |  |  | 		wave_data_ = nullptr; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	opened_ = false; | 
					
						
							|  |  |  |  | 	playing_ = false; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | bool easy2d::Music::IsPlaying() const | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (opened_ && voice_) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		XAUDIO2_VOICE_STATE state; | 
					
						
							|  |  |  |  | 		voice_->GetState(&state); | 
					
						
							|  |  |  |  | 		if (state.BuffersQueued && playing_) | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | float easy2d::Music::GetVolume() const | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (voice_) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		float volume = 0.f; | 
					
						
							|  |  |  |  | 		voice_->GetVolume(&volume); | 
					
						
							|  |  |  |  | 		return volume; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	return 0.f; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | bool easy2d::Music::SetVolume(float volume) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	if (voice_) | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 		volume = std::min(std::max(volume, -224.f), 224.f); | 
					
						
							|  |  |  |  | 		return SUCCEEDED(voice_->SetVolume(volume)); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  |  | IXAudio2SourceVoice * easy2d::Music::GetSourceVoice() const | 
					
						
							| 
									
										
										
										
											2018-10-11 20:00:21 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-15 20:16:08 +08:00
										 |  |  |  | 	return voice_; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } |