1 /*******************************************************************************
2 * Copyright 2015-2016 Juan Francisco Crespo Galán
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16 
17 #include "fx/PlaybackManager.h"
18 #include "fx/VolumeSound.h"
19 
20 #include <stdexcept>
21 
22 AUD_NAMESPACE_BEGIN
PlaybackManager(std::shared_ptr<IDevice> device)23 PlaybackManager::PlaybackManager(std::shared_ptr<IDevice> device) :
24 	m_device(device), m_currentKey(0)
25 {
26 }
27 
addCategory(std::shared_ptr<PlaybackCategory> category)28 unsigned int PlaybackManager::addCategory(std::shared_ptr<PlaybackCategory> category)
29 {
30 	bool flag = true;
31 	unsigned int k = -1;
32 	do {
33 		auto iter = m_categories.find(m_currentKey);
34 		if(iter == m_categories.end())
35 		{
36 			m_categories[m_currentKey] = category;
37 			k = m_currentKey;
38 			m_currentKey++;
39 			flag = false;
40 		}
41 		else
42 			m_currentKey++;
43 	} while(flag);
44 
45 	return k;
46 }
47 
addCategory(float volume)48 unsigned int PlaybackManager::addCategory(float volume)
49 {
50 	std::shared_ptr<PlaybackCategory> category = std::make_shared<PlaybackCategory>(m_device);
51 	category->setVolume(volume);
52 	bool flag = true;
53 	unsigned int k = -1;
54 	do {
55 		auto iter = m_categories.find(m_currentKey);
56 		if(iter == m_categories.end())
57 		{
58 			m_categories[m_currentKey] = category;
59 			k = m_currentKey;
60 			m_currentKey++;
61 			flag = false;
62 		}
63 		else
64 			m_currentKey++;
65 	} while(flag);
66 
67 	return k;
68 }
69 
play(std::shared_ptr<ISound> sound,unsigned int catKey)70 std::shared_ptr<IHandle> PlaybackManager::play(std::shared_ptr<ISound> sound, unsigned int catKey)
71 {
72 	auto iter = m_categories.find(catKey);
73 	std::shared_ptr<PlaybackCategory> category;
74 
75 	if(iter != m_categories.end())
76 	{
77 		category = iter->second;
78 	}
79 	else
80 	{
81 		category = std::make_shared<PlaybackCategory>(m_device);
82 		m_categories[catKey] = category;
83 	}
84 	return category->play(sound);
85 }
86 
resume(unsigned int catKey)87 bool PlaybackManager::resume(unsigned int catKey)
88 {
89 	auto iter = m_categories.find(catKey);
90 
91 	if(iter != m_categories.end())
92 	{
93 		iter->second->resume();
94 		return true;
95 	}
96 	else
97 	{
98 		return false;
99 	}
100 }
101 
pause(unsigned int catKey)102 bool PlaybackManager::pause(unsigned int catKey)
103 {
104 	auto iter = m_categories.find(catKey);
105 
106 	if(iter != m_categories.end())
107 	{
108 		iter->second->pause();
109 		return true;
110 	}
111 	else
112 	{
113 		return false;
114 	}
115 }
116 
getVolume(unsigned int catKey)117 float PlaybackManager::getVolume(unsigned int catKey)
118 {
119 	auto iter = m_categories.find(catKey);
120 
121 	if(iter != m_categories.end())
122 	{
123 		return iter->second->getVolume();
124 	}
125 	else
126 	{
127 		return -1.0;
128 	}
129 }
130 
setVolume(float volume,unsigned int catKey)131 bool PlaybackManager::setVolume(float volume, unsigned int catKey)
132 {
133 	auto iter = m_categories.find(catKey);
134 
135 	if(iter != m_categories.end())
136 	{
137 		iter->second->setVolume(volume);
138 		return true;
139 	}
140 	else
141 	{
142 		return false;
143 	}
144 }
145 
stop(unsigned int catKey)146 bool PlaybackManager::stop(unsigned int catKey)
147 {
148 	auto iter = m_categories.find(catKey);
149 
150 	if(iter != m_categories.end())
151 	{
152 		iter->second->stop();
153 		return true;
154 	}
155 	else
156 	{
157 		return false;
158 	}
159 }
160 
clean()161 void PlaybackManager::clean()
162 {
163 	for(auto cat : m_categories)
164 		cat.second->cleanHandles();
165 }
166 
clean(unsigned int catKey)167 bool PlaybackManager::clean(unsigned int catKey)
168 {
169 	auto iter = m_categories.find(catKey);
170 
171 	if(iter != m_categories.end())
172 	{
173 		iter->second->cleanHandles();
174 		return true;
175 	}
176 	else
177 	{
178 		return false;
179 	}
180 }
181 
getDevice()182 std::shared_ptr<IDevice> PlaybackManager::getDevice()
183 {
184 	return m_device;
185 }
186 AUD_NAMESPACE_END
187