1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Corrige carregamento de SounfEffect

This commit is contained in:
Danilo 2024-05-29 15:58:37 -03:00
parent ce95aac19c
commit a2e23283c6
6 changed files with 62 additions and 23 deletions

View File

@ -124,7 +124,7 @@ namespace xna {
if (_byte == 'w') if (_byte == 'w')
num1 = binaryReader.ReadUInt16(); num1 = binaryReader.ReadUInt16();
else else
return nullptr; throw std::runtime_error("ContentReader::PrepareStream: Bad xbn file.");
graphicsProfile = (num1 & XnbVersionProfileMask) >> XnbVersionProfileShift; graphicsProfile = (num1 & XnbVersionProfileMask) >> XnbVersionProfileShift;
bool flag = false; bool flag = false;

View File

@ -48,7 +48,7 @@ namespace xna {
insertRegisteredReader<Vector3Reader>("Vector3Reader"); insertRegisteredReader<Vector3Reader>("Vector3Reader");
insertRegisteredReader<Vector4Reader>("Vector4Reader"); insertRegisteredReader<Vector4Reader>("Vector4Reader");
insertRegisteredReader<Texture2DReader>("Texture2DReader"); insertRegisteredReader<Texture2DReader>("Texture2DReader");
insertRegisteredReader<SoundEffect>("SoundEffectReader"); insertRegisteredReader<SoundEffectReader>("SoundEffectReader");
} }
void PlatformInit::InitActivadors() void PlatformInit::InitActivadors()
@ -76,6 +76,6 @@ namespace xna {
insertActivadorReader<Vector3Reader>(); insertActivadorReader<Vector3Reader>();
insertActivadorReader<Vector4Reader>(); insertActivadorReader<Vector4Reader>();
insertActivadorReader<Texture2DReader>(); insertActivadorReader<Texture2DReader>();
insertActivadorReader<SoundEffect>(); insertActivadorReader<SoundEffectReader>();
} }
} }

View File

@ -1,16 +1,18 @@
#include "platform-dx/implementations.hpp" #include "platform-dx/implementations.hpp"
#include "csharp/stream.hpp"
using DxSoundEffect = DirectX::SoundEffect; using DxSoundEffect = DirectX::SoundEffect;
namespace xna { namespace xna {
SoundEffectInstance::SoundEffectInstance() { SoundEffectInstance::SoundEffectInstance() {
impl = unew<PlatformImplementation>(); impl = unew<PlatformImplementation>();
} }
SoundEffectInstance::~SoundEffectInstance() { SoundEffectInstance::~SoundEffectInstance() {
impl = nullptr; impl = nullptr;
} }
//Remover posteriormente
SoundEffect::SoundEffect() { SoundEffect::SoundEffect() {
} }
@ -23,36 +25,64 @@ namespace xna {
} }
SoundEffect::SoundEffect( SoundEffect::SoundEffect(
std::vector<Byte> format, std::vector<Byte> const& format,
std::vector<Byte> data, std::vector<Byte> const& data,
Int loopStart, Int loopStart,
Int loopLength, Int loopLength,
//We must evaluate how to use the time duration
TimeSpan const& duration) { TimeSpan const& duration) {
if (!AudioEngine::impl || !AudioEngine::impl->_dxAudioEngine) 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<Byte*>(&word);
auto bDword = reinterpret_cast<Byte*>(&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<Byte[]>(data.size()); auto wavData = unew<Byte[]>(data.size());
for (size_t i = 0; i < data.size(); ++i) for (size_t i = 0; i < data.size(); ++i)
wavData[i] = data[i]; wavData[i] = data[i];
auto wavFormat = reinterpret_cast<WAVEFORMATEX*>(format.data()); auto wfx = reinterpret_cast<WAVEFORMATEX*>(wavData.get());
auto startAudio = wavData.get() + sizeof(WAVEFORMATEX); wfx->wFormatTag = tag;
/*auto se = new DxSoundEffect( 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<DxSoundEffect>(
AudioEngine::impl->_dxAudioEngine.get(), AudioEngine::impl->_dxAudioEngine.get(),
wavData, wavData,
wavFormat, wfx,
startAudio,
data.size(),
loopStart,
loopLength);*/
impl->_dxSoundEffect = unew<DxSoundEffect>(
AudioEngine::impl->_dxAudioEngine.get(),
wavData,
wavFormat,
startAudio, startAudio,
data.size(), data.size(),
loopStart, loopStart,
loopLength); loopLength);
impl = unew<PlatformImplementation>();
impl->_dxSoundEffect = std::move(se);
} }
void SoundEffect::Play() { void SoundEffect::Play() {

View File

@ -5,6 +5,7 @@
#include "content/reader.hpp" #include "content/reader.hpp"
#include "csharp/type.hpp" #include "csharp/type.hpp"
#include "audio/soundeffect.hpp" #include "audio/soundeffect.hpp"
#include "csharp/timespan.hpp"
namespace xna { namespace xna {
class SoundEffectReader : public ContentTypeReaderT<PSoundEffect> { class SoundEffectReader : public ContentTypeReaderT<PSoundEffect> {
@ -19,6 +20,9 @@ namespace xna {
auto loopStart = input.ReadInt32(); auto loopStart = input.ReadInt32();
auto loopLength = input.ReadInt32(); auto loopLength = input.ReadInt32();
auto num = input.ReadInt32(); auto num = input.ReadInt32();
auto sf = snew<SoundEffect>(format, data, loopStart, loopLength, TimeSpan::FromMilliseconds((double)num));
return sf;
} }
}; };
} }

View File

@ -25,11 +25,12 @@ namespace xna {
class SoundEffect { class SoundEffect {
public: public:
//Remover posteriormente ou implementar funções de carregamento dos dados
SoundEffect(); SoundEffect();
SoundEffect(String const& fileName); SoundEffect(String const& fileName);
SoundEffect( SoundEffect(
std::vector<Byte> format, std::vector<Byte> const& format,
std::vector<Byte> data, std::vector<Byte> const& data,
Int loopStart, Int loopStart,
Int loopLength, Int loopLength,
TimeSpan const& duration); TimeSpan const& duration);

View File

@ -45,6 +45,9 @@ namespace xna {
//A simplified port of the System.IO.MemoryStream. //A simplified port of the System.IO.MemoryStream.
class MemoryStream : public Stream { class MemoryStream : public Stream {
public: public:
constexpr MemoryStream(std::vector<Byte> const& bytes):
_buffer(bytes), _length(bytes.size()){}
constexpr MemoryStream(Int capacity) : constexpr MemoryStream(Int capacity) :
_buffer(static_cast<size_t>(capacity)), _buffer(static_cast<size_t>(capacity)),
_length(capacity > 0 ? capacity : 0){} _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(Byte const* buffer, Int bufferLength, Int offset, Int count) override;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override; virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
virtual void WriteByte(Byte value) override; virtual void WriteByte(Byte value) override;
public: public:
std::vector<Byte> _buffer;
private:
Int _position{ 0 }; Int _position{ 0 };
Int _origin{ 0 }; Int _origin{ 0 };
Int _length{ 0 }; Int _length{ 0 };
std::vector<Byte> _buffer;
bool _closed{ false }; bool _closed{ false };
}; };