From 1705af029db16065802678360cdf43dc22528849 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 23 Jun 2010 03:47:08 +0000 Subject: [PATCH] fixed audio decoding bug, sounds can play now! git-svn-id: svn://anubis/anaglym/trunk@285 99a6e188-d820-4881-8870-2d33a10e2619 --- AV.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/AV.cc b/AV.cc index 83ca517..80a0360 100644 --- a/AV.cc +++ b/AV.cc @@ -191,7 +191,7 @@ bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path) desired.format = AUDIO_S16SYS; desired.rate = SOUND_RATE; m_ss = Sound_NewSample(m_rwops, NULL, &desired, - CALLBACK_SAMPLES * BYTES_PER_SAMPLE * CHANNELS); + CALLBACK_SAMPLES * BYTES_PER_SAMPLE); if (m_ss != NULL) { return true; @@ -211,11 +211,7 @@ void AV::Sound::play() { if (m_ss != NULL) { -#if 0 - cerr << "play()" << endl; Sound_Rewind(m_ss); - cerr << "end play()" << endl; -#endif m_av.playSound(this); } } @@ -233,6 +229,7 @@ int AV::Sound::decode(Uint8 * stream, int len) int len_decoded = 0, len_decoded_now; int samples_decoded_now; int spos = 0; + int len_samples = len / BYTES_PER_SAMPLE; int16_t * s16stream = (int16_t *) stream; int16_t * decoded_stream = (int16_t *) m_ss->buffer; do @@ -240,11 +237,16 @@ int AV::Sound::decode(Uint8 * stream, int len) len_decoded_now = Sound_Decode(m_ss); samples_decoded_now = len_decoded_now / BYTES_PER_SAMPLE; len_decoded += len_decoded_now; - for (int i = 0; i < samples_decoded_now; i++) + for (int i = 0; i < samples_decoded_now; i++, spos++) { + if (spos >= len_samples) + { + cerr << "Sorry, anaglym bug in " __FILE__ ":" << __LINE__ + << ": rework sound buffer sizes" << endl; + exit(33); + } 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) );