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:
parent
5bed8a2dfd
commit
8b8a7eee1f
20
AV.cc
20
AV.cc
@ -5,6 +5,7 @@
|
|||||||
#include <SDL_sound.h>
|
#include <SDL_sound.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <limits.h> /* INT_MAX */
|
#include <limits.h> /* INT_MAX */
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@ -60,6 +61,7 @@ AV::AV()
|
|||||||
cerr << "SDL_OpenAudio() error." << endl;
|
cerr << "SDL_OpenAudio() error." << endl;
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
m_sound_buffer = new int16_t[CALLBACK_SAMPLES * CHANNELS];
|
||||||
|
|
||||||
m_active_sounds_mutex = SDL_CreateMutex();
|
m_active_sounds_mutex = SDL_CreateMutex();
|
||||||
}
|
}
|
||||||
@ -71,6 +73,7 @@ AV::~AV()
|
|||||||
SDL_CloseAudio();
|
SDL_CloseAudio();
|
||||||
Sound_Quit();
|
Sound_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
delete[] m_sound_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AV::start(int width, int height, bool fullscreen, bool grab_input,
|
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);
|
memset(stream, 0, len);
|
||||||
SDL_mutexP(m_active_sounds_mutex);
|
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();
|
for (std::set<Sound *>::iterator it = m_active_sounds.begin();
|
||||||
it != m_active_sounds.end();
|
it != m_active_sounds.end();
|
||||||
it++)
|
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);
|
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)
|
if (m_active_sounds.size() == 0)
|
||||||
{
|
{
|
||||||
|
2
AV.h
2
AV.h
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_sound.h>
|
#include <SDL_sound.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
@ -83,6 +84,7 @@ class AV
|
|||||||
#endif
|
#endif
|
||||||
std::set< Sound * > m_active_sounds;
|
std::set< Sound * > m_active_sounds;
|
||||||
SDL_mutex * m_active_sounds_mutex;
|
SDL_mutex * m_active_sounds_mutex;
|
||||||
|
int16_t * m_sound_buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user