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
This commit is contained in:
Josh Holtrop 2010-06-16 20:51:17 +00:00
parent 1c2221e2f6
commit d376d7a5b0
2 changed files with 38 additions and 14 deletions

50
AV.cc
View File

@ -1,13 +1,22 @@
#include "AV.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL.h>
#include <iostream>
#include <SDL_sound.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#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<Sound *>::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;
}

2
AV.h
View File

@ -10,8 +10,6 @@
#include "FileLoader/FileLoader.h"
#include "refptr/refptr.h"
#define AV_SOUND_RATE 44100
class AV
{
public: