diff --git a/framework/content/reader.cpp b/framework/content/reader.cpp index 36cbc50..cdb5d3d 100644 --- a/framework/content/reader.cpp +++ b/framework/content/reader.cpp @@ -124,7 +124,7 @@ namespace xna { if (_byte == 'w') num1 = binaryReader.ReadUInt16(); else - return nullptr; + throw std::runtime_error("ContentReader::PrepareStream: Bad xbn file."); graphicsProfile = (num1 & XnbVersionProfileMask) >> XnbVersionProfileShift; bool flag = false; diff --git a/framework/platform-dx/init.cpp b/framework/platform-dx/init.cpp index 3d65c9d..dd43441 100644 --- a/framework/platform-dx/init.cpp +++ b/framework/platform-dx/init.cpp @@ -48,7 +48,7 @@ namespace xna { insertRegisteredReader("Vector3Reader"); insertRegisteredReader("Vector4Reader"); insertRegisteredReader("Texture2DReader"); - insertRegisteredReader("SoundEffectReader"); + insertRegisteredReader("SoundEffectReader"); } void PlatformInit::InitActivadors() @@ -76,6 +76,6 @@ namespace xna { insertActivadorReader(); insertActivadorReader(); insertActivadorReader(); - insertActivadorReader(); + insertActivadorReader(); } } \ No newline at end of file diff --git a/framework/platform-dx/soundeffect.cpp b/framework/platform-dx/soundeffect.cpp index 2a1439b..6ddb870 100644 --- a/framework/platform-dx/soundeffect.cpp +++ b/framework/platform-dx/soundeffect.cpp @@ -1,16 +1,18 @@ #include "platform-dx/implementations.hpp" +#include "csharp/stream.hpp" using DxSoundEffect = DirectX::SoundEffect; namespace xna { SoundEffectInstance::SoundEffectInstance() { - impl = unew(); + impl = unew(); } SoundEffectInstance::~SoundEffectInstance() { impl = nullptr; } + //Remover posteriormente SoundEffect::SoundEffect() { } @@ -23,36 +25,64 @@ namespace xna { } SoundEffect::SoundEffect( - std::vector format, - std::vector data, + std::vector const& format, + std::vector const& data, Int loopStart, Int loopLength, + //We must evaluate how to use the time duration TimeSpan const& duration) { if (!AudioEngine::impl || !AudioEngine::impl->_dxAudioEngine) - return; + return; + + //We expect 'format' to always be 16 bytes + MemoryStream stream(format); + WORD word = 0; + DWORD dword = 0; + + auto bWord = reinterpret_cast(&word); + auto bDword = reinterpret_cast(&dword); + + stream.Read(bWord, 2, 0, 2); + auto tag = word; + stream.Read(bWord, 2, 0, 2); + auto channels = word; + stream.Read(bDword, 4, 0, 4); + auto samplesPerSec = dword; + stream.Read(bDword, 4, 0, 4); + auto bytesPerSec = dword; + stream.Read(bWord, 2, 0, 2); + auto blockAlign = word; + stream.Read(bWord, 2, 0, 2); + auto bitsPerSample = word; + stream.Read(bWord, 2, 0, 2); + auto cbSize = word; auto wavData = unew(data.size()); for (size_t i = 0; i < data.size(); ++i) wavData[i] = data[i]; - auto wavFormat = reinterpret_cast(format.data()); - auto startAudio = wavData.get() + sizeof(WAVEFORMATEX); - /*auto se = new DxSoundEffect( + auto wfx = reinterpret_cast(wavData.get()); + wfx->wFormatTag = tag; + wfx->nChannels = channels; + wfx->nSamplesPerSec = samplesPerSec; + wfx->nAvgBytesPerSec = bytesPerSec; + wfx->nBlockAlign = blockAlign; + wfx->wBitsPerSample = bitsPerSample; + wfx->cbSize = cbSize; + + auto startAudio = wavData.get(); + + auto se = unew( AudioEngine::impl->_dxAudioEngine.get(), wavData, - wavFormat, - startAudio, - data.size(), - loopStart, - loopLength);*/ - impl->_dxSoundEffect = unew( - AudioEngine::impl->_dxAudioEngine.get(), - wavData, - wavFormat, + wfx, startAudio, data.size(), loopStart, loopLength); + + impl = unew(); + impl->_dxSoundEffect = std::move(se); } void SoundEffect::Play() { diff --git a/inc/audio/readers/soundeffect.hpp b/inc/audio/readers/soundeffect.hpp index dc2df2c..0ac3482 100644 --- a/inc/audio/readers/soundeffect.hpp +++ b/inc/audio/readers/soundeffect.hpp @@ -5,6 +5,7 @@ #include "content/reader.hpp" #include "csharp/type.hpp" #include "audio/soundeffect.hpp" +#include "csharp/timespan.hpp" namespace xna { class SoundEffectReader : public ContentTypeReaderT { @@ -19,6 +20,9 @@ namespace xna { auto loopStart = input.ReadInt32(); auto loopLength = input.ReadInt32(); auto num = input.ReadInt32(); + + auto sf = snew(format, data, loopStart, loopLength, TimeSpan::FromMilliseconds((double)num)); + return sf; } }; } diff --git a/inc/audio/soundeffect.hpp b/inc/audio/soundeffect.hpp index db4fbb3..8ccc046 100644 --- a/inc/audio/soundeffect.hpp +++ b/inc/audio/soundeffect.hpp @@ -25,11 +25,12 @@ namespace xna { class SoundEffect { public: + //Remover posteriormente ou implementar funções de carregamento dos dados SoundEffect(); SoundEffect(String const& fileName); SoundEffect( - std::vector format, - std::vector data, + std::vector const& format, + std::vector const& data, Int loopStart, Int loopLength, TimeSpan const& duration); diff --git a/inc/csharp/stream.hpp b/inc/csharp/stream.hpp index d335383..54b3086 100644 --- a/inc/csharp/stream.hpp +++ b/inc/csharp/stream.hpp @@ -45,6 +45,9 @@ namespace xna { //A simplified port of the System.IO.MemoryStream. class MemoryStream : public Stream { public: + constexpr MemoryStream(std::vector const& bytes): + _buffer(bytes), _length(bytes.size()){} + constexpr MemoryStream(Int capacity) : _buffer(static_cast(capacity)), _length(capacity > 0 ? capacity : 0){} @@ -79,12 +82,13 @@ namespace xna { virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override; virtual void Write(std::vector const& buffer, Int offset, Int count) override; virtual void WriteByte(Byte value) override; - + public: + std::vector _buffer; + private: Int _position{ 0 }; Int _origin{ 0 }; Int _length{ 0 }; - std::vector _buffer; bool _closed{ false }; };