diff --git a/AV.cc b/AV.cc index 6acd312..ea0aeef 100644 --- a/AV.cc +++ b/AV.cc @@ -5,6 +5,7 @@ #include #include #include /* INT_MAX */ +#include #include @@ -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::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) { diff --git a/AV.h b/AV.h index 2e9e73a..da50ba5 100644 --- a/AV.h +++ b/AV.h @@ -4,6 +4,7 @@ #include #include +#include #include @@ -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