PlaybackManager.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #pragma once
  17. /**
  18. * @file PlaybackManager.h
  19. * @ingroup fx
  20. * The PlaybackManager class.
  21. */
  22. #include "PlaybackCategory.h"
  23. #include "devices/IDevice.h"
  24. #include "ISound.h"
  25. #include <unordered_map>
  26. #include <memory>
  27. AUD_NAMESPACE_BEGIN
  28. /**
  29. * This class allows to control groups of playing sounds easily.
  30. * The sounds are part of categories.
  31. */
  32. class AUD_API PlaybackManager
  33. {
  34. private:
  35. /**
  36. * Unordered map of categories, each category has different name.
  37. */
  38. std::unordered_map<unsigned int, std::shared_ptr<PlaybackCategory>> m_categories;
  39. /**
  40. * Device used for playback.
  41. */
  42. std::shared_ptr<IDevice> m_device;
  43. /**
  44. * The current key used for new categories.
  45. */
  46. unsigned int m_currentKey;
  47. // delete copy constructor and operator=
  48. PlaybackManager(const PlaybackManager&) = delete;
  49. PlaybackManager& operator=(const PlaybackManager&) = delete;
  50. public:
  51. /**
  52. * Creates a new PlaybackManager.
  53. * \param device A shared pointer to the device which will be used for playback.
  54. */
  55. PlaybackManager(std::shared_ptr<IDevice> device);
  56. /**
  57. * Adds an existent category to the manager and returns a key to access it.
  58. * \param category The category to be added.
  59. * \return The category key.
  60. */
  61. unsigned int addCategory(std::shared_ptr<PlaybackCategory> category);
  62. /**
  63. * Adds an existent category to the manager and returns a key to access it.
  64. * \param volume The volume of the new category.
  65. * \return The category key.
  66. */
  67. unsigned int addCategory(float volume);
  68. /**
  69. * Plays a sound and adds it to a new or existent category.
  70. * \param sound The sound to be played and added to a category.
  71. * \param catKey Key of the category.
  72. * \return The handle of the playback; nullptr if the sound couldn't be played.
  73. */
  74. std::shared_ptr<IHandle> play(std::shared_ptr<ISound> sound, unsigned int catKey);
  75. /**
  76. * Resumes all the paused sounds of a category.
  77. * \param catKey Key of the category.
  78. * \return
  79. * - true if succesful.
  80. * - false if the category doesn't exist.
  81. */
  82. bool resume(unsigned int catKey);
  83. /**
  84. * Pauses all current playing sounds of a category.
  85. * \param catKey Key of the category.
  86. * \return
  87. * - true if succesful.
  88. * - false if the category doesn't exist.
  89. */
  90. bool pause(unsigned int catKey);
  91. /**
  92. * Retrieves the volume of a category.
  93. * \param catKey Key of the category.
  94. * \return The volume value of the category. If the category doesn't exist it returns a negative number.
  95. */
  96. float getVolume(unsigned int catKey);
  97. /**
  98. * Sets the volume for a category.
  99. * \param volume The volume.
  100. * \param catKey Key of the category.
  101. * \return
  102. * - true if succesful.
  103. * - false if the category doesn't exist.
  104. */
  105. bool setVolume(float volume, unsigned int catKey);
  106. /**
  107. * Stops and erases a category of sounds.
  108. * \param catKey Key of the category.
  109. * \return
  110. * - true if succesful.
  111. * - false if the category doesn't exist.
  112. */
  113. bool stop(unsigned int catKey);
  114. /**
  115. * Removes all the invalid handles of all the categories.
  116. * Only needed if individual sounds are stopped with their handles.
  117. */
  118. void clean();
  119. /**
  120. * Removes all the invalid handles of a category.
  121. * Only needed if individual sounds are stopped with their handles.
  122. * \param catKey Key of the category.
  123. * \return
  124. * - true if succesful.
  125. * - false if the category doesn't exist.
  126. */
  127. bool clean(unsigned int catKey);
  128. /**
  129. * Retrieves the device of the PlaybackManager.
  130. * \return A shared pointer to the device used by the playback manager.
  131. */
  132. std::shared_ptr<IDevice> getDevice();
  133. };
  134. AUD_NAMESPACE_END