SoundList.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 SoundList.h
  19. * @ingroup fx
  20. * The SoundList class.
  21. */
  22. #include "ISound.h"
  23. #include <vector>
  24. #include <memory>
  25. #include <mutex>
  26. AUD_NAMESPACE_BEGIN
  27. /**
  28. * This class allows to have a list of sound that will play sequentially or randomly with each playback.
  29. */
  30. class AUD_API SoundList : public ISound
  31. {
  32. private:
  33. /**
  34. * The list of sounds that will play.
  35. */
  36. std::vector<std::shared_ptr<ISound>> m_list;
  37. /**
  38. * Flag for random playback
  39. */
  40. bool m_random = false;
  41. /**
  42. * Current sound index. -1 if no reader has been created.
  43. */
  44. int m_index = -1;
  45. /**
  46. * Mutex to prevent multithreading crashes.
  47. */
  48. std::recursive_mutex m_mutex;
  49. // delete copy constructor and operator=
  50. SoundList(const SoundList&) = delete;
  51. SoundList& operator=(const SoundList&) = delete;
  52. public:
  53. /**
  54. * Creates a new, empty sound list.
  55. * Sounds must be added to the list using the addSound() method.
  56. * \param random False if the sounds int he list must be played sequentially. True if random.
  57. */
  58. SoundList(bool random = false);
  59. /**
  60. * Creates a new sound list and initializes it.
  61. * \param list A vector with sounds to initialize the list.
  62. * \param random False if the sounds int he list must be played sequentially. True if random.
  63. */
  64. SoundList(std::vector<std::shared_ptr<ISound>>& list, bool random = false);
  65. virtual std::shared_ptr<IReader> createReader();
  66. /**
  67. * Adds a sound to the list.
  68. * The added sounds can be played sequentially or randomly dependig
  69. * on the m_random flag
  70. * \param sound A shared_ptr to the sound.
  71. */
  72. void addSound(std::shared_ptr<ISound> sound);
  73. /**
  74. * Sets the playback mode of the sound list.
  75. * There are two posible modes, random and sequential.
  76. * \param random True to activate the random mode, false to activate sequential mode.
  77. */
  78. void setRandomMode(bool random);
  79. /**
  80. * Returns the playback mode of the sound list.
  81. * The two posible modes are random and sequential.
  82. * \return True if the random mode is activated, false otherwise.
  83. */
  84. bool getRandomMode();
  85. /**
  86. * Returns the amount of sounds in the list.
  87. * \return The amount of sounds in the list.
  88. */
  89. int getSize();
  90. };
  91. AUD_NAMESPACE_END