| 
									
										
										
										
											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-12-30 14:24:29 +08:00
										 |  |  | #include <kiwano/platform/win32/helper.h>  // win32::ThrowIfFailed
 | 
					
						
							|  |  |  | #include <kiwano/core/Logger.h>
 | 
					
						
							|  |  |  | #include <kiwano-audio/libraries.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-11 21:55:29 +08:00
										 |  |  | #include <kiwano-audio/AudioEngine.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 	namespace audio | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 		AudioEngine::AudioEngine() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			: x_audio2_(nullptr) | 
					
						
							|  |  |  | 			, mastering_voice_(nullptr) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 		AudioEngine::~AudioEngine() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 		void AudioEngine::SetupComponent() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			KGE_SYS_LOG(L"Creating audio resources"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			HRESULT hr = dlls::MediaFoundation::Get().MFStartup(MF_VERSION, MFSTARTUP_FULL); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 				hr = dlls::XAudio2::Get().XAudio2Create(&x_audio2_, 0, XAUDIO2_DEFAULT_PROCESSOR); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				hr = x_audio2_->CreateMasteringVoice(&mastering_voice_); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			win32::ThrowIfFailed(hr); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 		void AudioEngine::DestroyComponent() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			KGE_SYS_LOG(L"Destroying audio resources"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			if (mastering_voice_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				mastering_voice_->DestroyVoice(); | 
					
						
							|  |  |  | 				mastering_voice_ = nullptr; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			if (x_audio2_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				x_audio2_->Release(); | 
					
						
							|  |  |  | 				x_audio2_ = nullptr; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			dlls::MediaFoundation::Get().MFShutdown(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 10:37:29 +08:00
										 |  |  | 		bool AudioEngine::CreateSound(Sound& sound, const Transcoder::Buffer& buffer) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 			KGE_ASSERT(x_audio2_ && "AudioEngine hasn't been initialized!"); | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 10:37:29 +08:00
										 |  |  | 			HRESULT hr = S_OK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (buffer.format == nullptr) | 
					
						
							|  |  |  | 				hr = E_INVALIDARG; | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 10:37:29 +08:00
										 |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-12-31 10:37:29 +08:00
										 |  |  | 				IXAudio2SourceVoice* voice = nullptr; | 
					
						
							|  |  |  | 				hr = x_audio2_->CreateSourceVoice(&voice, buffer.format, 0, XAUDIO2_DEFAULT_FREQ_RATIO); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (SUCCEEDED(hr)) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					IXAudio2SourceVoice* old = sound.GetXAudio2Voice(); | 
					
						
							|  |  |  | 					if (old) | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						old->DestroyVoice(); | 
					
						
							|  |  |  | 						old = nullptr; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					sound.SetXAudio2Voice(voice); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 10:37:29 +08:00
										 |  |  | 			win32::WarnIfFailed(hr); | 
					
						
							|  |  |  | 			return SUCCEEDED(hr); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 		void AudioEngine::Open() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 			KGE_ASSERT(x_audio2_ && "AudioEngine hasn't been initialized!"); | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 10:37:29 +08:00
										 |  |  | 			if (x_audio2_) | 
					
						
							|  |  |  | 				x_audio2_->StartEngine(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 		void AudioEngine::Close() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-09-30 11:12:25 +08:00
										 |  |  | 			KGE_ASSERT(x_audio2_ && "AudioEngine hasn't been initialized!"); | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 10:37:29 +08:00
										 |  |  | 			if (x_audio2_) | 
					
						
							|  |  |  | 				x_audio2_->StopEngine(); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-06 01:08:09 +08:00
										 |  |  | } |