FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
soundclipmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <map>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/log/logger.h"
33 #include "util/resource/resource.h"
34 
35 #include "soundclipmanager.h"
36 
37 namespace FIFE {
38  static Logger _log(LM_RESMGR);
39 
41 
42  }
43 
45  size_t totalSize = 0;
46 
48  itend = m_sclipHandleMap.end();
49 
50  for ( ; it != itend; ++it) {
51  totalSize += it->second->getSize();
52  }
53 
54  return totalSize;
55  }
56 
59  itend = m_sclipHandleMap.end();
60  size_t count = 0;
61 
62  for ( ; it != itend; ++it) {
63  if ( it->second->getState() == IResource::RES_NOT_LOADED ) {
64  count++;
65  }
66  }
67 
68  return count;
69  }
70 
73  itend = m_sclipHandleMap.end();
74  size_t count = 0;
75 
76  for ( ; it != itend; ++it) {
77  if ( it->second->getState() == IResource::RES_LOADED ) {
78  count++;
79  }
80  }
81 
82  return count;
83  }
84 
86  return m_sclipHandleMap.size();
87  }
88 
90  SoundClip* ptr = new SoundClip(loader);
91  return add(ptr);
92  }
93 
94  SoundClipPtr SoundClipManager::create(const std::string& name, IResourceLoader* loader){
95  if (exists(name)) {
96  FL_WARN(_log, LMsg("SoundClipManager::create(std::string, IResourceLoader* loader) - ") << "Resource name " << name << " was previously created. Returning original SoundClip...");
97  return get(name);
98  }
99 
100  SoundClip* ptr = new SoundClip(name, loader);
101  return add(ptr);
102  }
103 
104  SoundClipPtr SoundClipManager::load(const std::string& name, IResourceLoader* loader) {
105  SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
106 
107  if (nit != m_sclipNameMap.end()) {
108  if ( nit->second->getState() == IResource::RES_NOT_LOADED ) {
109  nit->second->load();
110  }
111 
112  return nit->second;
113  }
114 
115  //was not found so create and load resource
116  SoundClipPtr ptr = create(name, loader);
117  ptr->load();
118 
119  if (ptr->getState() == IResource::RES_NOT_LOADED){
120  FL_WARN(_log, LMsg("SoundClipManager::load(std::string) - ") << "Resource name " << name << " was not found and could not be loaded.");
121  remove(name);
122  }
123 
124  return ptr;
125  }
126 
128  assert(res);
129  assert(!(exists(res->getHandle()) || exists(res->getName())));
130 
131  SoundClipPtr resptr(res);
132 
133  std::pair<SoundClipHandleMapIterator, bool> returnValue;
134  returnValue = m_sclipHandleMap.insert ( SoundClipHandleMapPair(res->getHandle(), resptr));
135 
136  if (returnValue.second) {
137  m_sclipNameMap.insert ( SoundClipNameMapPair(returnValue.first->second->getName(), returnValue.first->second) );
138  }
139  else {
140  FL_WARN(_log, LMsg("SoundClipManager::add(IResource*) - ") << "Resource " << res->getName() << " already exists.... ignoring.");
141  }
142 
143  return returnValue.first->second;
144  }
145 
146  bool SoundClipManager::exists(const std::string& name) {
148  if (it != m_sclipNameMap.end()) {
149  return true;
150  }
151 
152  return false;
153  }
154 
157  if (it != m_sclipHandleMap.end()) {
158  return true;
159  }
160 
161  return false;
162  }
163 
164  void SoundClipManager::reload(const std::string& name) {
165  SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
166 
167  if (nit != m_sclipNameMap.end()) {
168  if ( nit->second->getState() == IResource::RES_LOADED) {
169  nit->second->free();
170  }
171  nit->second->load();
172  return;
173  }
174 
175  FL_WARN(_log, LMsg("SoundClipManager::reload(std::string) - ") << "Resource name " << name << " not found.");
176  }
177 
180 
181  if ( it != m_sclipHandleMap.end()) {
182  if ( it->second->getState() == IResource::RES_LOADED) {
183  it->second->free();
184  }
185  it->second->load();
186  return;
187  }
188 
189  FL_WARN(_log, LMsg("SoundClipManager::reload(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
190 
191  }
192 
195  itend = m_sclipHandleMap.end();
196 
197  for ( ; it != itend; ++it) {
198  if ( it->second->getState() == IResource::RES_LOADED) {
199  it->second->free();
200  }
201  it->second->load();
202  }
203  }
204 
207  itend = m_sclipHandleMap.end();
208 
209  int32_t count = 0;
210  for ( ; it != itend; ++it) {
211  if (it->second.useCount() == 2 && it->second->getState() != IResource::RES_LOADED){
212  it->second->load();
213  count++;
214  }
215  }
216  FL_DBG(_log, LMsg("SoundClipManager::loadUnreferenced() - ") << "Loaded " << count << " unreferenced resources.");
217  }
218 
219  void SoundClipManager::free(const std::string& name) {
220  SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
221 
222  if (nit != m_sclipNameMap.end()) {
223  if ( nit->second->getState() == IResource::RES_LOADED) {
224  nit->second->free();
225  }
226  return;
227  }
228 
229  FL_WARN(_log, LMsg("SoundClipManager::free(std::string) - ") << "Resource name " << name << " not found.");
230  }
231 
234  if (it != m_sclipHandleMap.end()) {
235  if ( it->second->getState() == IResource::RES_LOADED) {
236  it->second->free();
237  }
238  return;
239  }
240 
241  FL_WARN(_log, LMsg("SoundClipManager::free(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
242  }
243 
246  itend = m_sclipHandleMap.end();
247 
248  int32_t count = 0;
249 
250  for ( ; it != itend; ++it) {
251  if ( it->second->getState() == IResource::RES_LOADED) {
252  it->second->free();
253  count++;
254  }
255  }
256 
257  FL_DBG(_log, LMsg("SoundClipManager::freeAll() - ") << "Freed all " << count << " resources.");
258  }
259 
262  itend = m_sclipHandleMap.end();
263 
264  int32_t count = 0;
265  for ( ; it != itend; ++it) {
266  if (it->second.useCount() == 2 && it->second->getState() == IResource::RES_LOADED ){
267  it->second->free();
268  count++;
269  }
270  }
271 
272  FL_DBG(_log, LMsg("SoundClipManager::freeUnreferenced() - ") << "Freed " << count << " unreferenced resources.");
273  }
274 
277  SoundClipNameMapIterator nit = m_sclipNameMap.find(resource->getName());
278 
279  if (it != m_sclipHandleMap.end()) {
280  m_sclipHandleMap.erase(it);
281 
282  if (nit != m_sclipNameMap.end()) {
283  m_sclipNameMap.erase(nit);
284  return;
285  }
286  assert(false); //should never get here
287  }
288 
289  FL_WARN(_log, LMsg("SoundClipManager::remove(ResourcePtr&) - ") << "Resource " << resource->getName() << " was not found.");
290  }
291 
292  void SoundClipManager::remove(const std::string& name) {
293  std::size_t handle;
294 
295  SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
296  if (nit != m_sclipNameMap.end()) {
297  handle = nit->second->getHandle();
298  m_sclipNameMap.erase(nit);
299  }
300  else {
301  FL_WARN(_log, LMsg("SoundClipManager::remove(std::string) - ") << "Resource " << name << " was not found.");
302  return;
303  }
304 
306  if ( it != m_sclipHandleMap.end()) {
307  m_sclipHandleMap.erase(it);
308  return;
309  }
310 
311  assert(false); //should never get here
312  }
313 
315  std::string name;
316 
318 
319  if (it != m_sclipHandleMap.end()) {
320  name = it->second->getName();
321  m_sclipHandleMap.erase(it);
322  }
323  else {
324  FL_WARN(_log, LMsg("SoundClipManager::remove(ResourceHandle) - ") << "Resource handle " << handle << " was not found.");
325  return;
326  }
327 
328  SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
329  if ( nit != m_sclipNameMap.end() ) {
330  m_sclipNameMap.erase(nit);
331  return;
332  }
333 
334  assert(false); //should never get here
335  }
336 
338  //should always be equal
339  assert (m_sclipHandleMap.size() == m_sclipNameMap.size());
340 
341  size_t count = m_sclipHandleMap.size();
342 
343  m_sclipHandleMap.clear();
344  m_sclipNameMap.clear();
345 
346  FL_DBG(_log, LMsg("SoundClipManager::removeAll() - ") << "Removed all " << count << " resources.");
347  }
348 
351  itend = m_sclipHandleMap.end();
352 
353  int32_t count = 0;
354  for ( ; it != itend; ++it) {
355  if ( it->second.useCount() == 2) {
356  remove(it->second->getHandle());
357  count++;
358  }
359  }
360 
361  FL_DBG(_log, LMsg("SoundClipManager::removeUnreferenced() - ") << "Removed " << count << " unreferenced resources.");
362  }
363 
364  SoundClipPtr SoundClipManager::get(const std::string& name) {
365  SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
366 
367  if (nit != m_sclipNameMap.end()) {
368  if (nit->second->getState() != IResource::RES_LOADED){
369  //resource is not loaded so load it
370  nit->second->load();
371  }
372  return nit->second;
373  }
374 
375  //not found so attempt to create and load the resource
376  SoundClipPtr ptr = load(name);
377  return ptr;
378  }
379 
382  if (it != m_sclipHandleMap.end()) {
383  if (it->second->getState() != IResource::RES_LOADED){
384  //resource is not loaded so load it
385  it->second->load();
386  }
387  return it->second;
388  }
389 
390  FL_WARN(_log, LMsg("SoundClipManager::get(ResourceHandle) - ") << "Resource handle " << handle << " is undefined.");
391 
392  return SoundClipPtr();
393  }
394 
396  SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
397  if (nit != m_sclipNameMap.end()) {
398  return nit->second->getHandle();
399  }
400 
401  FL_WARN(_log, LMsg("SoundClipManager::getResourceHandle(std::string) - ") << "Resource " << name << " is undefined.");
402 
403  return 0;
404  }
405 
406 } //FIFE
#define FL_WARN(logger, msg)
Definition: logger.h:72
virtual ~SoundClipManager()
Destructor.
SharedPtr< SoundClip > SoundClipPtr
Definition: soundclip.h:149
virtual ResourceState getState()
Definition: resource.h:70
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
std::pair< ResourceHandle, SoundClipPtr > SoundClipHandleMapPair
virtual SoundClipPtr load(const std::string &name, IResourceLoader *loader=0)
Creates a blank resource and loads it from disk.
static Logger _log(LM_AUDIO)
virtual void freeAll()
Frees all SoundClips.
virtual void free(const std::string &name)
Frees a SoundClip from memory.
virtual void load()
Definition: soundclip.cpp:67
virtual size_t getTotalResourcesCreated() const
Returns the number of unloaded resources.
std::size_t ResourceHandle
Definition: resource.h:38
virtual size_t getTotalResources() const
Returns the number of defined resources.
std::map< std::string, SoundClipPtr >::iterator SoundClipNameMapIterator
virtual ResourceHandle getResourceHandle(const std::string &name)
Gets a SoundClip handle by name.
std::map< ResourceHandle, SoundClipPtr >::iterator SoundClipHandleMapIterator
virtual void removeAll()
Removes all SoundClips from the manager.
virtual SoundClipPtr create(IResourceLoader *loader=0)
Creates a blank SoundClip but does not load it immediately.
virtual void remove(SoundClipPtr &resource)
Removes a SoundClip from the manager.
virtual void loadUnreferenced()
Loads all unreferenced SoundClips.
SoundClipHandleMap m_sclipHandleMap
virtual void reload(const std::string &name)
Reloads a SoundClip.
virtual const std::string & getName()
Definition: resource.h:66
virtual bool exists(const std::string &name)
Checks to see if a SoundClip exists.
virtual SoundClipPtr add(SoundClip *res)
Add an Image to the manager.
virtual size_t getMemoryUsed() const
Gets the total amount of memory used by resources.
std::map< ResourceHandle, SoundClipPtr >::const_iterator SoundClipHandleMapConstIterator
virtual SoundClipPtr get(const std::string &name)
Gets a shared pointer to the SoundClip.
std::pair< std::string, SoundClipPtr > SoundClipNameMapPair
virtual void reloadAll()
Reloads all SoundClips.
SoundClipNameMap m_sclipNameMap
virtual size_t getTotalResourcesLoaded() const
Returns the number of loaded resources.
virtual void removeUnreferenced()
Removes all unreferenced SoundClips.
ResourceHandle getHandle()
Definition: resource.h:68
virtual void freeUnreferenced()
Frees all unreferenced SoundClip.
#define FL_DBG(logger, msg)
Definition: logger.h:70
Class to handle the buffers of an audio file.
Definition: soundclip.h:58