2015-05-16 09:17:52 +08:00
|
|
|
/*
|
|
|
|
* cocos2d-x http://www.cocos2d-x.org
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010-2011 - cocos2d-x community
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) Microsoft Open Technologies, Inc.
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-03 09:49:41 +08:00
|
|
|
#include "base/ccMacros.h"
|
2015-05-16 09:17:52 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
|
|
|
|
|
|
|
|
#ifndef __AUDIO_SOURCE_READER_H_
|
|
|
|
#define __AUDIO_SOURCE_READER_H_
|
|
|
|
|
|
|
|
#define NEAR near
|
2015-05-24 10:13:12 +08:00
|
|
|
#include <mfapi.h>
|
|
|
|
#include <mfidl.h>
|
|
|
|
#include <mfreadwrite.h>
|
2015-05-28 01:57:26 +08:00
|
|
|
#include <queue>
|
|
|
|
#include <mutex>
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/winrt/MediaStreamer.h"
|
2015-06-04 10:50:57 +08:00
|
|
|
#include "ogg/ogg.h"
|
|
|
|
#include "vorbis/vorbisfile.h"
|
2015-05-16 09:17:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
namespace experimental{
|
|
|
|
|
|
|
|
const UINT PCMDATA_CACHEMAXSIZE = 2621440;
|
|
|
|
const UINT QUEUEBUFFER_NUM = 4;
|
|
|
|
const UINT CHUNK_SIZE_MAX = PCMDATA_CACHEMAXSIZE / QUEUEBUFFER_NUM;
|
|
|
|
|
2015-05-28 01:57:26 +08:00
|
|
|
typedef std::vector<BYTE> PCMBuffer;
|
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
enum class FileFormat
|
|
|
|
{
|
|
|
|
UNKNOWN,
|
|
|
|
WAV,
|
|
|
|
MP3,
|
|
|
|
OGG
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct AudioDataChunk
|
|
|
|
{
|
2015-05-28 01:57:26 +08:00
|
|
|
std::shared_ptr<PCMBuffer> _data;
|
2015-05-16 09:17:52 +08:00
|
|
|
size_t _dataSize;
|
|
|
|
bool _endOfStream;
|
|
|
|
int _seqNo;
|
|
|
|
} AudioDataChunk;
|
|
|
|
|
|
|
|
|
|
|
|
class AudioSourceReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioSourceReader();
|
|
|
|
virtual ~AudioSourceReader();
|
|
|
|
|
|
|
|
bool isStreamingSource() { return _isStreaming; }
|
|
|
|
std::string getFilePath() { return _filePath; }
|
|
|
|
virtual size_t getTotalAudioBytes() { return _audioSize; }
|
|
|
|
|
|
|
|
virtual bool initialize(const std::string& filePath) = 0;
|
|
|
|
virtual FileFormat getFileFormat() = 0;
|
|
|
|
virtual bool consumeChunk(AudioDataChunk& chunk) = 0;
|
|
|
|
virtual void produceChunk() = 0;
|
|
|
|
virtual void seekTo(const float ratio) = 0;
|
|
|
|
virtual const WAVEFORMATEX& getWaveFormatInfo() { return _wfx; }
|
|
|
|
|
|
|
|
protected:
|
2015-05-24 10:13:12 +08:00
|
|
|
void flushChunks();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool _isDirty;
|
|
|
|
size_t _bytesRead;
|
2015-05-16 09:17:52 +08:00
|
|
|
bool _isStreaming;
|
|
|
|
std::string _filePath;
|
|
|
|
size_t _audioSize;
|
|
|
|
WAVEFORMATEX _wfx;
|
2015-05-24 10:13:12 +08:00
|
|
|
std::mutex _rwMutex;
|
|
|
|
std::queue<AudioDataChunk> _chnkQ;
|
2015-05-16 09:17:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WAVReader : public AudioSourceReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WAVReader();
|
2015-05-28 01:57:26 +08:00
|
|
|
virtual ~WAVReader();
|
2015-05-16 09:17:52 +08:00
|
|
|
|
2015-05-28 01:57:26 +08:00
|
|
|
virtual bool initialize(const std::string& filePath) override;
|
|
|
|
virtual FileFormat getFileFormat() override { return FileFormat::WAV; }
|
|
|
|
virtual bool consumeChunk(AudioDataChunk& chunk) override;
|
|
|
|
virtual void produceChunk() override;
|
|
|
|
virtual void seekTo(const float ratio) override;
|
2015-05-16 09:17:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
MediaStreamer^ _streamer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MP3Reader : public AudioSourceReader
|
|
|
|
{
|
2015-05-24 10:13:12 +08:00
|
|
|
public:
|
|
|
|
MP3Reader();
|
2015-05-28 01:57:26 +08:00
|
|
|
virtual ~MP3Reader();
|
|
|
|
|
|
|
|
virtual bool initialize(const std::string& filePath) override;
|
|
|
|
virtual FileFormat getFileFormat() override { return FileFormat::WAV; }
|
|
|
|
virtual bool consumeChunk(AudioDataChunk& chunk) override;
|
|
|
|
virtual void produceChunk() override;
|
|
|
|
virtual void seekTo(const float ratio) override;
|
2015-05-24 10:13:12 +08:00
|
|
|
|
2015-05-28 01:57:26 +08:00
|
|
|
// for backward compatibility with simple audio engine
|
|
|
|
void doLargeFileSupport(bool action) { _largeFileSupport = action; }
|
2015-05-24 10:13:12 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
HRESULT configureSourceReader(IMFSourceReader* pReader, IMFMediaType** ppDecomprsdAudioType);
|
|
|
|
HRESULT readAudioData(IMFSourceReader* pReader);
|
2015-05-28 01:57:26 +08:00
|
|
|
void chunkify(PCMBuffer& buffer);
|
|
|
|
bool appendToMappedWavFile(PCMBuffer& buffer);
|
2015-05-24 10:13:12 +08:00
|
|
|
void readFromMappedWavFile(BYTE *data, size_t offset, int size, UINT *pRetSize);
|
|
|
|
Microsoft::WRL::Wrappers::FileHandle openFile(const std::string& path, bool append = false);
|
|
|
|
|
|
|
|
private:
|
2015-05-28 01:57:26 +08:00
|
|
|
bool _largeFileSupport;
|
2015-05-24 10:13:12 +08:00
|
|
|
std::string _mappedWavFile;
|
2015-05-16 09:17:52 +08:00
|
|
|
};
|
|
|
|
|
2015-06-04 10:50:57 +08:00
|
|
|
class OGGReader : public AudioSourceReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OGGReader();
|
|
|
|
virtual ~OGGReader();
|
|
|
|
|
|
|
|
virtual bool initialize(const std::string& filePath) override;
|
|
|
|
virtual FileFormat getFileFormat() override { return FileFormat::WAV; }
|
|
|
|
virtual bool consumeChunk(AudioDataChunk& chunk) override;
|
|
|
|
virtual void produceChunk() override;
|
|
|
|
virtual void seekTo(const float ratio) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<OggVorbis_File> _vorbisFd;
|
|
|
|
};
|
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
}
|
|
|
|
NS_CC_END
|
|
|
|
#endif // __AUDIO_SOURCE_READER_H_
|
|
|
|
#endif
|
|
|
|
|