AudioMixerController.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include "audio/android/utils/Errors.h"
  23. #include <thread>
  24. #include <mutex>
  25. #include <condition_variable>
  26. #include <atomic>
  27. #include <vector>
  28. namespace cocos2d {
  29. class Track;
  30. class AudioMixer;
  31. class AudioMixerController
  32. {
  33. public:
  34. struct OutputBuffer
  35. {
  36. void* buf;
  37. size_t size;
  38. };
  39. AudioMixerController(int bufferSizeInFrames, int sampleRate, int channelCount);
  40. ~AudioMixerController();
  41. bool init();
  42. bool addTrack(Track* track);
  43. bool hasPlayingTacks();
  44. void pause();
  45. void resume();
  46. inline bool isPaused() const { return _isPaused; };
  47. void mixOneFrame();
  48. inline OutputBuffer* current() { return &_mixingBuffer; }
  49. private:
  50. void destroy();
  51. void initTrack(Track* track, std::vector<Track*>& tracksToRemove);
  52. private:
  53. int _bufferSizeInFrames;
  54. int _sampleRate;
  55. int _channelCount;
  56. AudioMixer* _mixer;
  57. std::mutex _activeTracksMutex;
  58. std::vector<Track*> _activeTracks;
  59. OutputBuffer _mixingBuffer;
  60. std::atomic_bool _isPaused;
  61. std::atomic_bool _isMixingFrame;
  62. };
  63. } // namespace cocos2d {