AudioSourceReader.h 4.3 KB

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