AudioSourceReader.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * cocos2d-x http://www.cocos2d-x.org
  3. *
  4. * Copyright (c) 2010-2011 - cocos2d-x community
  5. * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. *
  7. * Portions Copyright (c) Microsoft Open Technologies, Inc.
  8. * All Rights Reserved
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and limitations under the License.
  18. */
  19. #include "base/ccMacros.h"
  20. #include "platform/CCPlatformConfig.h"
  21. #if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  22. #ifndef __AUDIO_SOURCE_READER_H_
  23. #define __AUDIO_SOURCE_READER_H_
  24. #define NEAR near
  25. #include <mfapi.h>
  26. #include <mfidl.h>
  27. #include <mfreadwrite.h>
  28. #include <queue>
  29. #include <mutex>
  30. #include "audio/winrt/MediaStreamer.h"
  31. #include "ogg/ogg.h"
  32. #include "vorbis/vorbisfile.h"
  33. NS_CC_BEGIN
  34. namespace experimental{
  35. const size_t PCMDATA_CACHEMAXSIZE = 2621440;
  36. const size_t QUEUEBUFFER_NUM = 4;
  37. const size_t CHUNK_SIZE_MAX = PCMDATA_CACHEMAXSIZE / QUEUEBUFFER_NUM;
  38. typedef std::vector<BYTE> PCMBuffer;
  39. enum class FileFormat
  40. {
  41. UNKNOWN,
  42. WAV,
  43. MP3,
  44. OGG
  45. };
  46. typedef struct AudioDataChunk
  47. {
  48. std::shared_ptr<PCMBuffer> _data;
  49. size_t _dataSize;
  50. bool _endOfStream;
  51. int _seqNo;
  52. } AudioDataChunk;
  53. class AudioSourceReader
  54. {
  55. public:
  56. AudioSourceReader();
  57. virtual ~AudioSourceReader();
  58. bool isStreamingSource() { return _isStreaming; }
  59. std::string getFilePath() { return _filePath; }
  60. virtual size_t getTotalAudioBytes() { return _audioSize; }
  61. virtual bool initialize(const std::string& filePath) = 0;
  62. virtual FileFormat getFileFormat() = 0;
  63. virtual bool consumeChunk(AudioDataChunk& chunk) = 0;
  64. virtual void produceChunk() = 0;
  65. virtual void seekTo(const float ratio) = 0;
  66. virtual const WAVEFORMATEX& getWaveFormatInfo() { return _wfx; }
  67. protected:
  68. void flushChunks();
  69. protected:
  70. bool _isDirty;
  71. size_t _bytesRead;
  72. bool _isStreaming;
  73. std::string _filePath;
  74. size_t _audioSize;
  75. WAVEFORMATEX _wfx;
  76. std::mutex _rwMutex;
  77. std::queue<AudioDataChunk> _chnkQ;
  78. };
  79. class WAVReader : public AudioSourceReader
  80. {
  81. public:
  82. WAVReader();
  83. virtual ~WAVReader();
  84. virtual bool initialize(const std::string& filePath) override;
  85. virtual FileFormat getFileFormat() override { return FileFormat::WAV; }
  86. virtual bool consumeChunk(AudioDataChunk& chunk) override;
  87. virtual void produceChunk() override;
  88. virtual void seekTo(const float ratio) override;
  89. private:
  90. MediaStreamer^ _streamer;
  91. };
  92. class MP3Reader : public AudioSourceReader
  93. {
  94. public:
  95. MP3Reader();
  96. virtual ~MP3Reader();
  97. virtual bool initialize(const std::string& filePath) override;
  98. virtual FileFormat getFileFormat() override { return FileFormat::WAV; }
  99. virtual bool consumeChunk(AudioDataChunk& chunk) override;
  100. virtual void produceChunk() override;
  101. virtual void seekTo(const float ratio) override;
  102. // for backward compatibility with simple audio engine
  103. void doLargeFileSupport(bool action) { _largeFileSupport = action; }
  104. protected:
  105. HRESULT configureSourceReader(IMFSourceReader* pReader, IMFMediaType** ppDecomprsdAudioType);
  106. HRESULT readAudioData(IMFSourceReader* pReader);
  107. void chunkify(PCMBuffer& buffer);
  108. bool appendToMappedWavFile(PCMBuffer& buffer);
  109. void readFromMappedWavFile(BYTE *data, size_t offset, size_t size, UINT *pRetSize);
  110. Microsoft::WRL::Wrappers::FileHandle openFile(const std::string& path, bool append = false);
  111. private:
  112. bool _largeFileSupport;
  113. std::string _mappedWavFile;
  114. };
  115. class OGGReader : public AudioSourceReader
  116. {
  117. public:
  118. OGGReader();
  119. virtual ~OGGReader();
  120. virtual bool initialize(const std::string& filePath) override;
  121. virtual FileFormat getFileFormat() override { return FileFormat::WAV; }
  122. virtual bool consumeChunk(AudioDataChunk& chunk) override;
  123. virtual void produceChunk() override;
  124. virtual void seekTo(const float ratio) override;
  125. private:
  126. std::unique_ptr<OggVorbis_File> _vorbisFd;
  127. };
  128. }
  129. NS_CC_END
  130. #endif // __AUDIO_SOURCE_READER_H_
  131. #endif