| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - 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-10-11 21:55:29 +08:00
										 |  |  | #include <kiwano-audio/SoundPlayer.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	namespace audio | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 		SoundPlayer::SoundPlayer() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			: volume_(1.f) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 		SoundPlayer::~SoundPlayer() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			ClearCache(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 		size_t SoundPlayer::Load(String const& file_path) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-09-29 22:23:13 +08:00
										 |  |  | 			int hash_code = static_cast<int>(file_path.hash()); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			if (sound_cache_.end() != sound_cache_.find(hash_code)) | 
					
						
							| 
									
										
										
										
											2019-09-10 11:50:19 +08:00
										 |  |  | 				return hash_code; | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 			SoundPtr sound = new (std::nothrow) Sound; | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (sound) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 				if (sound->Load(file_path)) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					sound->SetVolume(volume_); | 
					
						
							|  |  |  | 					sound_cache_.insert(std::make_pair(hash_code, sound)); | 
					
						
							| 
									
										
										
										
											2019-09-10 11:50:19 +08:00
										 |  |  | 					return hash_code; | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-09-10 11:50:19 +08:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 		size_t SoundPlayer::Load(Resource const& res) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 			size_t hash_code = static_cast<size_t>(res.GetId()); | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 			if (sound_cache_.end() != sound_cache_.find(hash_code)) | 
					
						
							| 
									
										
										
										
											2019-09-10 11:50:19 +08:00
										 |  |  | 				return hash_code; | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			SoundPtr sound = new (std::nothrow) Sound; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (sound) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 				if (sound->Load(res)) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					sound->SetVolume(volume_); | 
					
						
							|  |  |  | 					sound_cache_.insert(std::make_pair(hash_code, sound)); | 
					
						
							| 
									
										
										
										
											2019-09-10 11:50:19 +08:00
										 |  |  | 					return hash_code; | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-09-10 11:50:19 +08:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 		void SoundPlayer::Play(size_t id, int loop_count) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 			auto iter = sound_cache_.find(id); | 
					
						
							|  |  |  | 			if (sound_cache_.end() != iter) | 
					
						
							|  |  |  | 				iter->second->Play(loop_count); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 		void SoundPlayer::Pause(size_t id) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 			auto iter = sound_cache_.find(id); | 
					
						
							|  |  |  | 			if (sound_cache_.end() != iter) | 
					
						
							|  |  |  | 				iter->second->Pause(); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 		void SoundPlayer::Resume(size_t id) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 			auto iter = sound_cache_.find(id); | 
					
						
							|  |  |  | 			if (sound_cache_.end() != iter) | 
					
						
							|  |  |  | 				iter->second->Resume(); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 		void SoundPlayer::Stop(size_t id) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 			auto iter = sound_cache_.find(id); | 
					
						
							|  |  |  | 			if (sound_cache_.end() != iter) | 
					
						
							|  |  |  | 				iter->second->Stop(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:26:41 +08:00
										 |  |  | 		bool SoundPlayer::IsPlaying(size_t id) | 
					
						
							| 
									
										
										
										
											2019-08-18 17:49:13 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			auto iter = sound_cache_.find(id); | 
					
						
							|  |  |  | 			if (sound_cache_.end() != iter) | 
					
						
							|  |  |  | 				return iter->second->IsPlaying(); | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:23:13 +08:00
										 |  |  | 		float SoundPlayer::GetVolume() const | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return volume_; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:23:13 +08:00
										 |  |  | 		void SoundPlayer::SetVolume(float volume) | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			volume_ = std::min(std::max(volume, -224.f), 224.f); | 
					
						
							|  |  |  | 			for (const auto& pair : sound_cache_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				pair.second->SetVolume(volume_); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 		void SoundPlayer::PauseAll() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			for (const auto& pair : sound_cache_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				pair.second->Pause(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 		void SoundPlayer::ResumeAll() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			for (const auto& pair : sound_cache_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				pair.second->Resume(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 		void SoundPlayer::StopAll() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			for (const auto& pair : sound_cache_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				pair.second->Stop(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 09:28:59 +08:00
										 |  |  | 		void SoundPlayer::ClearCache() | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			sound_cache_.clear(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-06 01:08:09 +08:00
										 |  |  | } |