From d376d7a5b0b4fe326c26d07fc958f9993d314d75 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 16 Jun 2010 20:51:17 +0000 Subject: [PATCH] fixed AV::Sound::decode() to add sounds and loop over decoded length properly git-svn-id: svn://anubis/anaglym/trunk@280 99a6e188-d820-4881-8870-2d33a10e2619 --- AV.cc | 50 ++++++++++++++++++++++++++++++++++++++------------ AV.h | 2 -- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/AV.cc b/AV.cc index c5765a2..ad87690 100644 --- a/AV.cc +++ b/AV.cc @@ -1,13 +1,22 @@ -#include "AV.h" #include #include #include -#include #include #include +#include + +#include + +#include "AV.h" + using namespace std; +#define SOUND_RATE 44100 +#define CALLBACK_SAMPLES 4096 +#define CHANNELS 2 +#define BYTES_PER_SAMPLE 2 + void AV_sound_callback(void * userdata, Uint8 * stream, int len) { ((AV *)userdata)->playCallback(stream, len); @@ -40,10 +49,10 @@ AV::AV() Sound_Init(); SDL_AudioSpec desired; - desired.freq = AV_SOUND_RATE; + desired.freq = SOUND_RATE; desired.format = AUDIO_S16SYS; - desired.channels = 2; - desired.samples = 4096; + desired.channels = CHANNELS; + desired.samples = CALLBACK_SAMPLES; desired.callback = AV_sound_callback; desired.userdata = this; if (SDL_OpenAudio(&desired, NULL) < 0) @@ -130,6 +139,7 @@ void AV::playSound(AV::Sound * s) void AV::playCallback(Uint8 * stream, int len) { + memset(stream, 0, len); for (std::set::const_iterator it = m_active_sounds.begin(); it != m_active_sounds.end(); it++) @@ -165,8 +175,9 @@ bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path) Sound_AudioInfo desired; desired.channels = 2; desired.format = AUDIO_S16SYS; - desired.rate = AV_SOUND_RATE; - m_ss = Sound_NewSample(m_rwops, NULL, &desired, 4192); + desired.rate = SOUND_RATE; + m_ss = Sound_NewSample(m_rwops, NULL, &desired, + CALLBACK_SAMPLES * BYTES_PER_SAMPLE * CHANNELS); if (m_ss != NULL) { return true; @@ -205,10 +216,25 @@ void AV::Sound::loop(int times) int AV::Sound::decode(Uint8 * stream, int len) { - /* TODO: fix for len != m_ss->buffer_size */ - cerr << "AV::Sound::decode()" << endl; - Uint32 len_decoded = Sound_Decode(m_ss); - cerr << "AV::Sound::decode() len: " << len << ", len_decoded: " << len_decoded << endl; - memcpy(stream, m_ss->buffer, len_decoded); + int len_decoded = 0, len_decoded_now; + int samples_decoded = 0, samples_decoded_now; + int spos = 0; + int16_t * s16stream = (int16_t *) stream; + int16_t * decoded_stream = (int16_t *) m_ss->buffer; + do + { + len_decoded_now = Sound_Decode(m_ss); + samples_decoded_now = len_decoded_now / BYTES_PER_SAMPLE; + len_decoded += len_decoded_now; + samples_decoded += len_decoded / BYTES_PER_SAMPLE; + for (int i = 0; i < samples_decoded_now; i++) + { + int val = s16stream[spos] + decoded_stream[i]; + s16stream[spos] = (val > SHRT_MAX) ? SHRT_MAX : val; + spos++; + } + } while ( (len_decoded_now == (int) m_ss->buffer_size) + && (len_decoded < len) ); + return len_decoded; } diff --git a/AV.h b/AV.h index a39cfe2..c377fdc 100644 --- a/AV.h +++ b/AV.h @@ -10,8 +10,6 @@ #include "FileLoader/FileLoader.h" #include "refptr/refptr.h" -#define AV_SOUND_RATE 44100 - class AV { public: