Track.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /****************************************************************************
  2. Copyright (c) 2016-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "audio/android/PcmData.h"
  22. #include "audio/android/IVolumeProvider.h"
  23. #include "audio/android/PcmBufferProvider.h"
  24. #include <functional>
  25. #include <mutex>
  26. namespace cocos2d { namespace experimental {
  27. class Track : public PcmBufferProvider, public IVolumeProvider
  28. {
  29. public:
  30. enum class State
  31. {
  32. IDLE,
  33. PLAYING,
  34. RESUMED,
  35. PAUSED,
  36. STOPPED,
  37. OVER,
  38. DESTROYED
  39. };
  40. Track(const PcmData &pcmData);
  41. virtual ~Track();
  42. inline State getState() const { return _state; };
  43. void setState(State state);
  44. inline State getPrevState() const { return _prevState; };
  45. inline bool isPlayOver() const { return _state == State::PLAYING && _nextFrame >= _numFrames;};
  46. inline void setName(int name) { _name = name; };
  47. inline int getName() const { return _name; };
  48. void setVolume(float volume);
  49. float getVolume() const;
  50. bool setPosition(float pos);
  51. float getPosition() const;
  52. virtual gain_minifloat_packed_t getVolumeLR() override ;
  53. inline void setLoop(bool isLoop) { _isLoop = isLoop; };
  54. inline bool isLoop() const { return _isLoop; };
  55. std::function<void(State)> onStateChanged;
  56. private:
  57. inline bool isVolumeDirty() const
  58. { return _isVolumeDirty; };
  59. inline void setVolumeDirty(bool isDirty)
  60. { _isVolumeDirty = isDirty; };
  61. inline bool isInitialized() const
  62. { return _isInitialized; };
  63. inline void setInitialized(bool isInitialized)
  64. { _isInitialized = isInitialized; };
  65. private:
  66. PcmData _pcmData;
  67. State _prevState;
  68. State _state;
  69. std::mutex _stateMutex;
  70. int _name;
  71. float _volume;
  72. bool _isVolumeDirty;
  73. std::mutex _volumeDirtyMutex;
  74. bool _isLoop;
  75. bool _isInitialized;
  76. friend class AudioMixerController;
  77. };
  78. }} // namespace cocos2d { namespace experimental {