AV now decodes sounds directly into a new buffer and then mixes that buffer into the final audio stream

git-svn-id: svn://anubis/anaglym/trunk@292 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-06-25 20:14:36 +00:00
parent 5bed8a2dfd
commit 8b8a7eee1f
2 changed files with 21 additions and 1 deletions

20
AV.cc
View File

@ -5,6 +5,7 @@
#include <SDL_sound.h>
#include <math.h>
#include <limits.h> /* INT_MAX */
#include <stdint.h>
#include <iostream>
@ -60,6 +61,7 @@ AV::AV()
cerr << "SDL_OpenAudio() error." << endl;
exit(3);
}
m_sound_buffer = new int16_t[CALLBACK_SAMPLES * CHANNELS];
m_active_sounds_mutex = SDL_CreateMutex();
}
@ -71,6 +73,7 @@ AV::~AV()
SDL_CloseAudio();
Sound_Quit();
SDL_Quit();
delete[] m_sound_buffer;
}
void AV::start(int width, int height, bool fullscreen, bool grab_input,
@ -159,14 +162,29 @@ void AV::playCallback(Uint8 * stream, int len)
{
memset(stream, 0, len);
SDL_mutexP(m_active_sounds_mutex);
if (len > CALLBACK_SAMPLES * CHANNELS * BYTES_PER_SAMPLE)
{
cerr << "anaglym bug: callback buffer length = " << len
<< ", sound buffer size = "
<< CALLBACK_SAMPLES * CHANNELS * BYTES_PER_SAMPLE << endl;
exit(38);
}
memset(stream, 0, len);
for (std::set<Sound *>::iterator it = m_active_sounds.begin();
it != m_active_sounds.end();
it++)
{
if ((*it)->decode(stream, len) == 0)
int bytes_decoded = (*it)->decode((Uint8 *)m_sound_buffer, len);
if (bytes_decoded == 0)
{
m_active_sounds.erase(it);
}
for (int i = 0, samples_decoded = bytes_decoded / 2;
i < samples_decoded;
i++)
{
stream[i] = min(INT_MAX, stream[i] + m_sound_buffer[i]);
}
}
if (m_active_sounds.size() == 0)
{

2
AV.h
View File

@ -4,6 +4,7 @@
#include <SDL.h>
#include <SDL_sound.h>
#include <stdint.h>
#include <set>
@ -83,6 +84,7 @@ class AV
#endif
std::set< Sound * > m_active_sounds;
SDL_mutex * m_active_sounds_mutex;
int16_t * m_sound_buffer;
};
#endif