anaglym/AV.cc
Josh Holtrop d4d44dea86 added AV::Sound::pause(), added Lua interface for sounds (play/pause/stop/loop/loopForever)
git-svn-id: svn://anubis/anaglym/trunk@290 99a6e188-d820-4881-8870-2d33a10e2619
2010-06-24 02:46:28 +00:00

299 lines
6.6 KiB
C++

#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL.h>
#include <SDL_sound.h>
#include <math.h>
#include <limits.h> /* INT_MAX */
#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);
}
AV::AV()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
cerr << "SDL_Init() returned error!" << endl;
exit(3);
}
const SDL_VideoInfo * vidInfo = SDL_GetVideoInfo();
if (vidInfo != NULL)
{
m_defaultWidth = vidInfo->current_w;
m_defaultHeight = vidInfo->current_h;
}
else
{
cerr << "Warning: SDL_GetVideoInfo() returned NULL!" << endl;
m_defaultWidth = 1024;
m_defaultHeight = 768;
}
m_surface = NULL;
m_width = 0;
m_height = 0;
Sound_Init();
SDL_AudioSpec desired;
desired.freq = SOUND_RATE;
desired.format = AUDIO_S16SYS;
desired.channels = CHANNELS;
desired.samples = CALLBACK_SAMPLES;
desired.callback = AV_sound_callback;
desired.userdata = this;
if (SDL_OpenAudio(&desired, NULL) < 0)
{
cerr << "SDL_OpenAudio() error." << endl;
exit(3);
}
m_active_sounds_mutex = SDL_CreateMutex();
}
AV::~AV()
{
SDL_PauseAudio(1);
SDL_DestroyMutex(m_active_sounds_mutex);
SDL_CloseAudio();
Sound_Quit();
SDL_Quit();
}
void AV::start(int width, int height, bool fullscreen, bool grab_input,
int samples)
{
if (m_surface == NULL)
{
if (width == 0)
width = m_defaultWidth;
if (height == 0)
height = m_defaultHeight;
int flags = SDL_HWSURFACE | SDL_OPENGL;
if (fullscreen)
flags |= SDL_FULLSCREEN;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (samples > 1)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
}
m_surface = SDL_SetVideoMode(width, height, 32, flags);
if (grab_input)
{
SDL_ShowCursor(SDL_DISABLE);
SDL_WM_GrabInput(SDL_GRAB_ON);
}
SDL_WM_SetCaption("Anaglym", "Anaglym");
m_fullscreen = fullscreen;
m_width = width;
m_height = height;
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (double) width / (double) height, 0.01, 10000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
}
void AV::stop()
{
if (m_surface != NULL)
{
SDL_FreeSurface(m_surface);
m_surface = NULL;
}
}
refptr<AV::Sound> AV::createSound()
{
refptr<Sound> s = new Sound(*this);
#if 0
m_sounds.insert(s);
#endif
return s;
}
void AV::playSound(AV::Sound * s)
{
SDL_mutexP(m_active_sounds_mutex);
m_active_sounds.insert(s);
SDL_mutexV(m_active_sounds_mutex);
SDL_PauseAudio(0);
}
void AV::stopSound(AV::Sound * s)
{
SDL_mutexP(m_active_sounds_mutex);
m_active_sounds.erase(s);
if (m_active_sounds.size() == 0)
{
SDL_PauseAudio(1);
}
SDL_mutexV(m_active_sounds_mutex);
}
void AV::playCallback(Uint8 * stream, int len)
{
memset(stream, 0, len);
SDL_mutexP(m_active_sounds_mutex);
for (std::set<Sound *>::iterator it = m_active_sounds.begin();
it != m_active_sounds.end();
it++)
{
if ((*it)->decode(stream, len) == 0)
{
m_active_sounds.erase(it);
}
}
if (m_active_sounds.size() == 0)
{
SDL_PauseAudio(1);
}
SDL_mutexV(m_active_sounds_mutex);
}
AV::Sound::Sound(AV & av)
: m_av(av),
m_buff(1)
{
m_rwops = NULL;
m_ss = NULL;
m_buff_bytes_left = 0;
}
AV::Sound::~Sound()
{
if (m_ss != NULL)
{
Sound_FreeSample(m_ss);
}
}
bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path)
{
m_buff = fileLoader.load(path);
if (m_buff.size > 0)
{
m_rwops = SDL_RWFromMem(m_buff.data, m_buff.size);
Sound_AudioInfo desired;
desired.channels = CHANNELS;
desired.format = AUDIO_S16SYS;
desired.rate = SOUND_RATE;
m_ss = Sound_NewSample(m_rwops, NULL, &desired,
CALLBACK_SAMPLES * BYTES_PER_SAMPLE);
if (m_ss != NULL)
{
return true;
}
else
{
SDL_FreeRW(m_rwops);
m_rwops = NULL;
cerr << "Error loading sound " << path.toString() << ": "
<< Sound_GetError() << endl;
}
}
return false;
}
void AV::Sound::play()
{
loop(1);
}
void AV::Sound::pause()
{
if (m_ss != NULL)
{
m_av.stopSound(this);
}
}
void AV::Sound::stop()
{
if (m_ss != NULL)
{
rewind();
m_av.stopSound(this);
}
}
void AV::Sound::loop(int times)
{
if (m_ss != NULL)
{
m_loop_count = times;
m_av.playSound(this);
}
}
void AV::Sound::rewind()
{
if (m_ss != NULL)
{
Sound_Rewind(m_ss);
}
}
int AV::Sound::decode(Uint8 * stream, int len)
{
int len_decoded = 0;
int bytes_decoded_this_pass;
if (m_buff_bytes_left > 0)
{
int len_to_copy = min(len, m_buff_bytes_left);
int buff_offset = m_ss->buffer_size - m_buff_bytes_left;
memcpy(stream, ((int8_t *)m_ss->buffer) + buff_offset, len_to_copy);
m_buff_bytes_left -= len_to_copy;
len_decoded += len_to_copy;
}
while (len_decoded < len)
{
bytes_decoded_this_pass = Sound_Decode(m_ss);
if (bytes_decoded_this_pass == 0)
{
if (m_loop_count < INT_MAX)
{
m_loop_count--;
}
if (m_loop_count > 0)
{
Sound_Rewind(m_ss);
bytes_decoded_this_pass = Sound_Decode(m_ss);
}
}
if (bytes_decoded_this_pass <= 0)
{
return len_decoded;
}
int len_to_copy = min(bytes_decoded_this_pass, len - len_decoded);
memcpy(stream + len_decoded, m_ss->buffer, len_to_copy);
len_decoded += len_to_copy;
m_buff_bytes_left = bytes_decoded_this_pass - len_to_copy;
}
return len_decoded;
}