From b294dffaa1962133febb5248faefa5e66c423f98 Mon Sep 17 00:00:00 2001 From: "anniruddh.koppal" Date: Wed, 3 Jun 2015 19:50:57 -0700 Subject: [PATCH] Added ogg support to audio engine. Fixed issues #150 and #151 --- .../libcocos2d_8_1.Windows.vcxproj | 8 +- .../libcocos2d_8_1.WindowsPhone.vcxproj | 8 +- .../winrt_8.1_props/cocos2d_winrt_8.1.props | 4 +- .../cocos2d_winrt_8.1_app.props | 10 + cocos/audio/winrt/AudioCachePlayer.cpp | 41 ++-- cocos/audio/winrt/AudioCachePlayer.h | 1 + cocos/audio/winrt/AudioEngine-winrt.cpp | 3 +- cocos/audio/winrt/AudioSourceReader.cpp | 195 ++++++++++++++---- cocos/audio/winrt/AudioSourceReader.h | 18 ++ 9 files changed, 221 insertions(+), 67 deletions(-) diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Windows/libcocos2d_8_1.Windows.vcxproj b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Windows/libcocos2d_8_1.Windows.vcxproj index aa50019dda..b416a13c74 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Windows/libcocos2d_8_1.Windows.vcxproj +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Windows/libcocos2d_8_1.Windows.vcxproj @@ -159,7 +159,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\winrt_8.1-specific\OggDecoder\include;$(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) _USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;_DEBUG;COCOS2D_DEBUG=1;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) @@ -179,7 +179,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\winrt_8.1-specific\OggDecoder\include;$(EngineRoot)external\winrt_8.1-specific\OggDecoder\include;$(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) _USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;NDEBUG;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) @@ -199,7 +199,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\winrt_8.1-specific\OggDecoder\include;$(EngineRoot)external\winrt_8.1-specific\OggDecoder\include;$(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) _USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;_DEBUG;COCOS2D_DEBUG=1;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) @@ -219,7 +219,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\winrt_8.1-specific\OggDecoder\include;$(EngineRoot)external\winrt_8.1-specific\OggDecoder\include;$(EngineRoot)external\winrt_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\winrt_8.1\freetype2;$(EngineRoot)external\websockets\include\winrt_8.1;$(EngineRoot)external\curl\include\winrt_8.1;$(EngineRoot)external\tiff\include\winrt_8.1;$(EngineRoot)external\jpeg\include\winrt_8.1;$(EngineRoot)external\png\include\winrt_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) _USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;NDEBUG;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.WindowsPhone/libcocos2d_8_1.WindowsPhone.vcxproj b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.WindowsPhone/libcocos2d_8_1.WindowsPhone.vcxproj index d6f4970a79..4c5c0b69d3 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.WindowsPhone/libcocos2d_8_1.WindowsPhone.vcxproj +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.WindowsPhone/libcocos2d_8_1.WindowsPhone.vcxproj @@ -113,7 +113,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\wp_8.1-specific\OggDecoder\include;$(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) CC_WINDOWS_PHONE_8_1;_USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;_DEBUG;COCOS2D_DEBUG=1;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) @@ -133,7 +133,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\wp_8.1-specific\OggDecoder\include;$(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) CC_WINDOWS_PHONE_8_1;_USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;NDEBUG;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) @@ -153,7 +153,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\wp_8.1-specific\OggDecoder\include;$(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) CC_WINDOWS_PHONE_8_1;_USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;_DEBUG;COCOS2D_DEBUG=1;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) @@ -173,7 +173,7 @@ true /Zm384 /bigobj %(AdditionalOptions) pch.h - $(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) + $(EngineRoot)external\wp_8.1-specific\OggDecoder\include;$(EngineRoot)external\wp_8.1-specific\zlib\include;$(EngineRoot)external\freetype2\include\wp_8.1\freetype2;$(EngineRoot)external\websockets\include\wp_8.1;$(EngineRoot)external\curl\include\wp_8.1;$(EngineRoot)external\tiff\include\wp_8.1;$(EngineRoot)external\jpeg\include\wp_8.1;$(EngineRoot)external\png\include\wp_8.1;$(EngineRoot)external\protobuf-lite\src;$(EngineRoot)external\protobuf-lite\win32;%(AdditionalIncludeDirectories) CC_WINDOWS_PHONE_8_1;_USRDLL;_LIB;COCOS2DXWIN32_EXPORTS;_USE3DDLL;_EXPORT_DLL_;_USRSTUDIODLL;_USREXDLL;_USEGUIDLL;CC_ENABLE_CHIPMUNK_INTEGRATION=1;CC_ENABLE_BULLET_INTEGRATION=1;NDEBUG;%(PreprocessorDefinitions) false %(DisableSpecificWarnings) diff --git a/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1.props b/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1.props index 3e56e02d37..a8e48dc02c 100644 --- a/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1.props +++ b/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1.props @@ -18,8 +18,8 @@ 4056;4244;4251;4756;4453;28204;4099; - libGLESv2.lib;libEGL.lib;ws2_32.lib;libwebsockets.lib;libcurl.lib;chipmunk.lib;zlib.lib;freetype.lib;sqlite3.lib;d2d1.lib;d3d11.lib;dxgi.lib;windowscodecs.lib;dwrite.lib;dxguid.lib;xaudio2.lib;mfcore.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;%(AdditionalDependencies) - $(EngineRoot)external\$(COCOS2D_PLATFORM)-specific\angle\prebuilt\$(Platform);$(EngineRoot)external\curl\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\websockets\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\chipmunk\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\$(COCOS2D_PLATFORM)-specific\zlib\prebuilt\$(Platform);$(EngineRoot)external\sqlite3\libraries\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\freetype2\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\poly2tri;$(EngineRoot)external\poly2tri\common;$(EngineRoot)external\poly2tri\sweep;%(AdditionalLibraryDirectories); + libogg.lib;libvorbis.lib;libvorbisfile.lib;libGLESv2.lib;libEGL.lib;ws2_32.lib;libwebsockets.lib;libcurl.lib;chipmunk.lib;zlib.lib;freetype.lib;sqlite3.lib;d2d1.lib;d3d11.lib;dxgi.lib;windowscodecs.lib;dwrite.lib;dxguid.lib;xaudio2.lib;mfcore.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;%(AdditionalDependencies) + $(EngineRoot)external\$(COCOS2D_PLATFORM)-specific\OggDecoder\prebuilt\$(Platform);$(EngineRoot)external\$(COCOS2D_PLATFORM)-specific\angle\prebuilt\$(Platform);$(EngineRoot)external\curl\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\websockets\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\chipmunk\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\$(COCOS2D_PLATFORM)-specific\zlib\prebuilt\$(Platform);$(EngineRoot)external\sqlite3\libraries\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\freetype2\prebuilt\$(COCOS2D_PLATFORM)\$(Platform);$(EngineRoot)external\poly2tri;$(EngineRoot)external\poly2tri\common;$(EngineRoot)external\poly2tri\sweep;%(AdditionalLibraryDirectories); /IGNORE:4264 %(AdditionalOptions) diff --git a/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1_app.props b/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1_app.props index 56a1e35697..88ce0623ee 100644 --- a/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1_app.props +++ b/cocos/2d/winrt_8.1_props/cocos2d_winrt_8.1_app.props @@ -6,6 +6,7 @@ $(EngineRoot)external\curl\prebuilt\$(COCOS2D_PLATFORM)\$(Platform)\ $(EngineRoot)external\websockets\prebuilt\$(COCOS2D_PLATFORM)\$(Platform)\ $(EngineRoot)external\sqlite3\libraries\$(COCOS2D_PLATFORM)\$(Platform)\ + $(EngineRoot)external\$(COCOS2D_PLATFORM)-specific\OggDecoder\prebuilt\$(Platform)\ @@ -29,5 +30,14 @@ true + + true + + + true + + + true + \ No newline at end of file diff --git a/cocos/audio/winrt/AudioCachePlayer.cpp b/cocos/audio/winrt/AudioCachePlayer.cpp index 19dab3147c..0be13433dd 100644 --- a/cocos/audio/winrt/AudioCachePlayer.cpp +++ b/cocos/audio/winrt/AudioCachePlayer.cpp @@ -37,6 +37,7 @@ inline void ThrowIfFailed(HRESULT hr) // AudioCache AudioCache::AudioCache() : _isReady(false) + , _retry(false) , _fileFullPath("") , _srcReader(nullptr) , _fileFormat(FileFormat::UNKNOWN) @@ -57,6 +58,10 @@ AudioCache::~AudioCache() void AudioCache::readDataTask() { + if (_isReady) { + return; + } + std::wstring path(_fileFullPath.begin(), _fileFullPath.end()); if (nullptr != _srcReader) { @@ -68,31 +73,34 @@ void AudioCache::readDataTask() { case FileFormat::WAV: _srcReader = new (std::nothrow) WAVReader(); - if (_srcReader && _srcReader->initialize(_fileFullPath)) { - _audInfo._totalAudioBytes = _srcReader->getTotalAudioBytes(); - _audInfo._wfx = _srcReader->getWaveFormatInfo(); - _isReady = true; - invokeCallbacks(); - } break; case FileFormat::OGG: + _srcReader = new (std::nothrow) OGGReader(); break; case FileFormat::MP3: _srcReader = new (std::nothrow) MP3Reader(); - if (_srcReader && _srcReader->initialize(_fileFullPath)) { - _audInfo._totalAudioBytes = _srcReader->getTotalAudioBytes(); - _audInfo._wfx = _srcReader->getWaveFormatInfo(); - _isReady = true; - invokeCallbacks(); - } + break; case FileFormat::UNKNOWN: default: break; } + + if (_srcReader && _srcReader->initialize(_fileFullPath)) { + _audInfo._totalAudioBytes = _srcReader->getTotalAudioBytes(); + _audInfo._wfx = _srcReader->getWaveFormatInfo(); + _isReady = true; + _retry = false; + invokeCallbacks(); + } + + if (!_isReady) { + _retry = true; + log("Failed to read input file: %s.\n", _fileFullPath.c_str()); + } } void AudioCache::addCallback(const std::function &callback) @@ -105,6 +113,10 @@ void AudioCache::addCallback(const std::function &callback) _callbacks.push_back(callback); } _cbMutex.unlock(); + + if (_retry) { + readDataTask(); + } } void AudioCache::invokeCallbacks() @@ -290,6 +302,9 @@ void AudioPlayer::setVolume(float volume) if (FAILED(_xaMasterVoice->SetVolume(volume))) { error(); } + else { + _volume = volume; + } } } @@ -371,7 +386,7 @@ bool AudioPlayer::_play(bool resume) if (_state == AudioPlayerState::PAUSED && !resume || nullptr == _xaSourceVoice) break; - if (FAILED(_xaSourceVoice->Start())) { + if (FAILED(_xaMasterVoice->SetVolume(_volume)) || FAILED(_xaSourceVoice->Start())) { error(); } else { diff --git a/cocos/audio/winrt/AudioCachePlayer.h b/cocos/audio/winrt/AudioCachePlayer.h index f6911f47c6..2439c4d582 100644 --- a/cocos/audio/winrt/AudioCachePlayer.h +++ b/cocos/audio/winrt/AudioCachePlayer.h @@ -65,6 +65,7 @@ private: AudioCache& operator=(const AudioCache&); private: + bool _retry; bool _isReady; AudioInfo _audInfo; std::mutex _cbMutex; diff --git a/cocos/audio/winrt/AudioEngine-winrt.cpp b/cocos/audio/winrt/AudioEngine-winrt.cpp index bb6f1d5ece..22a322171e 100644 --- a/cocos/audio/winrt/AudioEngine-winrt.cpp +++ b/cocos/audio/winrt/AudioEngine-winrt.cpp @@ -160,7 +160,7 @@ int AudioEngineImpl::play2d(const std::string &filePath, bool loop, float volume } else if (ext.compare(".ogg") == 0){ audioCache->_fileFormat = FileFormat::OGG; - //eraseCache = false; //TODO add support for OGG + eraseCache = false; } else if (ext.compare(".mp3") == 0){ audioCache->_fileFormat = FileFormat::MP3; @@ -168,7 +168,6 @@ int AudioEngineImpl::play2d(const std::string &filePath, bool loop, float volume } else{ log("unsupported media type:%s\n", ext.c_str()); - eraseCache = false; } if (eraseCache){ diff --git a/cocos/audio/winrt/AudioSourceReader.cpp b/cocos/audio/winrt/AudioSourceReader.cpp index dcca1804f1..4c64b09578 100644 --- a/cocos/audio/winrt/AudioSourceReader.cpp +++ b/cocos/audio/winrt/AudioSourceReader.cpp @@ -53,6 +53,24 @@ void AudioSourceReader::flushChunks() _rwMutex.unlock(); } +void AudioSourceReader::seekTo(const float ratio) +{ + if (_isStreaming) { + auto newPos = ratio * _audioSize; + + if (!newPos && !_isDirty && _chnkQ.size()) // already in 0.0 position + return; + + _bytesRead = newPos; + flushChunks(); + auto alignment = _wfx.nChannels * _wfx.nBlockAlign; + _bytesRead = _bytesRead >= _audioSize ? (_audioSize - alignment) : _bytesRead - (_bytesRead % alignment); + + for (int i = 0; i < QUEUEBUFFER_NUM; i++) { + produceChunk(); + } + } +} // WAVFileReader WAVReader::WAVReader() : @@ -78,10 +96,8 @@ bool WAVReader::initialize(const std::string& filePath) flushChunks(); - _rwMutex.lock(); _streamer = ref new MediaStreamer; _streamer->Initialize(std::wstring(_filePath.begin(), _filePath.end()).c_str(), true); - _rwMutex.unlock(); _wfx = _streamer->GetOutputWaveFormatEx(); UINT32 dataSize = _streamer->GetMaxStreamLengthInBytes(); @@ -162,31 +178,7 @@ void WAVReader::produceChunk() void WAVReader::seekTo(const float ratio) { - if (_isStreaming) { - auto newPos = ratio * _audioSize; - - if (!newPos && !_isDirty && _chnkQ.size()) // already in 0.0 position - return; - - _bytesRead = newPos; - flushChunks(); - - switch (_wfx.wFormatTag) - { - case WAVE_FORMAT_PCM: - case WAVE_FORMAT_ADPCM: { - auto alignment = _wfx.nChannels * _wfx.nBlockAlign; - _bytesRead = _bytesRead >= _audioSize ? (_audioSize - alignment) : _bytesRead - (_bytesRead % alignment); - } break; - - default: - break; - } - - for (int i = 0; i < QUEUEBUFFER_NUM; i++) { - produceChunk(); - } - } + AudioSourceReader::seekTo(ratio); } @@ -299,21 +291,7 @@ void MP3Reader::produceChunk() void MP3Reader::seekTo(const float ratio) { - if (_isStreaming) { - auto newPos = ratio * _audioSize; - - if (!newPos && !_isDirty && _chnkQ.size()) // already in 0.0 position - return; - - _bytesRead = newPos; - flushChunks(); - auto alignment = _wfx.nChannels * _wfx.nBlockAlign; - _bytesRead = _bytesRead >= _audioSize ? (_audioSize - alignment) : _bytesRead - (_bytesRead % alignment); - - for (int i = 0; i < QUEUEBUFFER_NUM; i++) { - produceChunk(); - } - } + AudioSourceReader::seekTo(ratio); } HRESULT MP3Reader::configureSourceReader(IMFSourceReader* pReader, IMFMediaType** ppDecomprsdAudioType) @@ -520,4 +498,137 @@ Wrappers::FileHandle MP3Reader::openFile(const std::string& path, bool append) return Microsoft::WRL::Wrappers::FileHandle(CreateFile2(std::wstring(path.begin(), path.end()).c_str(), access, FILE_SHARE_READ, creation, &extParams)); } + +// OGGReader +OGGReader::OGGReader() +{ +} + +OGGReader::~OGGReader() +{ + if (_vorbisFd) { + ov_clear(_vorbisFd.get()); + } +} + +bool OGGReader::initialize(const std::string& filePath) +{ + bool ret = false; + _filePath = filePath; + + do { + _vorbisFd = std::make_unique(); + if (ov_fopen(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str(), _vorbisFd.get())){ + break; + } + + auto vi = ov_info(_vorbisFd.get(), -1); + + if (!vi) { + break; + } + + auto totalFrames = ov_pcm_total(_vorbisFd.get(), -1); + auto bytesPerFrame = vi->channels * 2; + _audioSize = totalFrames * bytesPerFrame; + + _wfx.wFormatTag = WAVE_FORMAT_PCM; + _wfx.nChannels = vi->channels; + _wfx.nSamplesPerSec = vi->rate; + _wfx.nAvgBytesPerSec = vi->rate * bytesPerFrame; + _wfx.nBlockAlign = bytesPerFrame; + _wfx.wBitsPerSample = (bytesPerFrame / vi->channels) * 8; + _wfx.cbSize = 0; + + if (_audioSize <= PCMDATA_CACHEMAXSIZE) { + produceChunk(); + } + else { + _isStreaming = true; + for (int i = 0; i < QUEUEBUFFER_NUM; i++) { + produceChunk(); + } + } + + ret = true; + } while (false); + + return ret; +} + +bool OGGReader::consumeChunk(AudioDataChunk& chunk) +{ + bool ret = false; + _isDirty = true; + + _rwMutex.lock(); + if (_chnkQ.size() > 0) { + chunk = _chnkQ.front(); + if (_isStreaming) { + _chnkQ.pop(); + } + ret = true; + } + _rwMutex.unlock(); + + return ret; +} + +void OGGReader::produceChunk() +{ + _rwMutex.lock(); + int chunkSize = _audioSize; + + do { + if (!_isStreaming && _chnkQ.size() || _chnkQ.size() >= QUEUEBUFFER_NUM) { + break; + } + + if (_isStreaming) { + chunkSize = std::min(CHUNK_SIZE_MAX, _audioSize - _bytesRead); + } + + if (!chunkSize && !_chnkQ.size()) { + auto alignment = _wfx.nChannels * _wfx.nBlockAlign; + _bytesRead -= alignment; + chunkSize = alignment; + } + + if (!chunkSize) { + break; + } + + int retSize = 0; + AudioDataChunk chunk = { 0 }; + chunk._data = std::make_shared(chunkSize); + + auto newPos = (1.0f * _bytesRead / _audioSize) * ov_time_total(_vorbisFd.get(), -1); + if (ov_time_seek(_vorbisFd.get(), newPos)){ + break; + } + + do + { + long br = 0; + int current_section = 0; + if ((br = ov_read(_vorbisFd.get(), (char*)chunk._data->data() + retSize, chunkSize - retSize, 0, 2, 1, ¤t_section)) == 0) { + break; + } + retSize += br; + } while (retSize < chunkSize); + + _bytesRead += retSize; + chunk._dataSize = retSize; + chunk._seqNo = ((float)_bytesRead / _audioSize) * ((float)_audioSize / CHUNK_SIZE_MAX); + chunk._endOfStream = (_bytesRead >= _audioSize); + _chnkQ.push(chunk); + } while (false); + _rwMutex.unlock(); +} + +void OGGReader::seekTo(const float ratio) +{ + AudioSourceReader::seekTo(ratio); +} + #endif diff --git a/cocos/audio/winrt/AudioSourceReader.h b/cocos/audio/winrt/AudioSourceReader.h index e6d5f37a05..d835d22cf8 100644 --- a/cocos/audio/winrt/AudioSourceReader.h +++ b/cocos/audio/winrt/AudioSourceReader.h @@ -30,6 +30,8 @@ #include #include #include "MediaStreamer.h" +#include "ogg/ogg.h" +#include "vorbis/vorbisfile.h" NS_CC_BEGIN namespace experimental{ @@ -132,6 +134,22 @@ class MP3Reader : public AudioSourceReader std::string _mappedWavFile; }; +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 _vorbisFd; +}; + } NS_CC_END #endif // __AUDIO_SOURCE_READER_H_