diff --git a/AV.cc b/AV.cc index ff038ef..5c01484 100644 --- a/AV.cc +++ b/AV.cc @@ -14,7 +14,7 @@ static void AV_sound_callback(void * userdata, Uint8 * stream, int len) Sint16 * str = (Sint16 *) stream; for (int i = 0; i < len / 4; i++) { - str[i*2] = (Sint16) (32000.0 * sin(pos * 263.0 / 44100.0 * 2.0 * M_PI)); + str[i*2] = (Sint16) (32000.0 * sin(pos * 263.0 / AV_SOUND_RATE * 2.0 * M_PI)); str[i*2+1] = 0; pos++; } @@ -45,9 +45,9 @@ AV::AV() m_width = 0; m_height = 0; -// Sound_Init(); + Sound_Init(); SDL_AudioSpec desired; - desired.freq = 44100; + desired.freq = AV_SOUND_RATE; desired.format = AUDIO_S16SYS; desired.channels = 2; desired.samples = 4096; @@ -55,13 +55,14 @@ AV::AV() desired.userdata = this; if (SDL_OpenAudio(&desired, NULL) < 0) { - cerr << "Sound_OpenAudio() error." << endl; + cerr << "SDL_OpenAudio() error." << endl; exit(3); } } AV::~AV() { + SDL_CloseAudio(); Sound_Quit(); SDL_Quit(); } @@ -120,9 +121,19 @@ void AV::stop() } } -AV::Sound::Sound() +AV::Sound::Sound(AV & av) + : m_av(av) { m_rwops = NULL; + m_ss = NULL; +} + +AV::Sound::~Sound() +{ + if (m_ss != NULL) + { + Sound_FreeSample(m_ss); + } } bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path) @@ -131,7 +142,22 @@ bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path) if (buff.size > 0) { m_rwops = SDL_RWFromMem(buff.data, buff.size); - return true; + Sound_AudioInfo desired; + desired.channels = 2; + desired.format = AUDIO_S16SYS; + desired.rate = AV_SOUND_RATE; + m_ss = Sound_NewSample(m_rwops, NULL, &desired, 4192); + 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; } diff --git a/AV.h b/AV.h index e8b240f..a00ebe5 100644 --- a/AV.h +++ b/AV.h @@ -3,7 +3,11 @@ #define VIDEO_H #include "FileLoader/FileLoader.h" +#include "refptr/refptr.h" #include +#include + +#define AV_SOUND_RATE 44100 class AV { @@ -11,11 +15,14 @@ class AV class Sound { public: - Sound(); + Sound(AV & av); + ~Sound(); bool load(FileLoader & fileLoader, const FileLoader::Path & path); protected: + AV & m_av; SDL_RWops * m_rwops; + Sound_Sample * m_ss; }; AV(); @@ -46,6 +53,7 @@ class AV { return SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON; } + refptr createSound() { return new Sound(*this); } protected: int m_defaultWidth; diff --git a/Engine.cc b/Engine.cc index 917b6f3..f2d9451 100644 --- a/Engine.cc +++ b/Engine.cc @@ -89,7 +89,6 @@ Engine::Engine(const string & path, AV & av) : m_av(av) { m_next_object_index = 1; - m_next_sound_index = 1; m_next_joint_index = 1; m_eye[0] = 0; m_eye[1] = -1; @@ -298,11 +297,9 @@ int Engine::addObject(bool is_static, bool is_reference, return id; } -int Engine::addSound(AV::Sound * avs) +int Engine::addSound(refptr avs) { - int id = m_next_sound_index++; - m_sounds[id] = avs; - return id; + return m_sounds.add(avs); } int Engine::addAMotor(Object * o1, Object * o2) @@ -578,7 +575,7 @@ int Engine::loadSound(const string & name) { FileLoader::Path path("", name); - AV::Sound * avs = new AV::Sound(); + refptr avs = m_av.createSound(); if (avs->load(*m_fileLoader, path)) { diff --git a/Engine.h b/Engine.h index 0591db3..2b2ddc9 100644 --- a/Engine.h +++ b/Engine.h @@ -15,6 +15,7 @@ #include "refptr/refptr.h" #include "ftgl.h" #include "PhyObj/PhyObj.h" +#include "IDSet.h" class Engine { @@ -148,7 +149,7 @@ class Engine int addObject(bool is_static, bool is_reference, OdeWorld::GeomType geom_type, refptr< std::vector > args); - int addSound(AV::Sound * avs); + int addSound(refptr avs); int addAMotor(Object * o1, Object * o2); int addHinge(Object * o1, Object * o2, dReal anchor_x, dReal anchor_y, dReal anchor_z, @@ -264,10 +265,9 @@ class Engine std::string m_engine_path; OdeWorld m_world; std::map m_objects; - std::map m_sounds; + IDSet< refptr > m_sounds; std::map m_joints; int m_next_object_index; - int m_next_sound_index; int m_next_joint_index; GLdouble m_eye[3]; GLdouble m_center[3]; diff --git a/IDSet.h b/IDSet.h new file mode 100644 index 0000000..8cb2155 --- /dev/null +++ b/IDSet.h @@ -0,0 +1,35 @@ + +#ifndef IDSET_H +#define IDSET_H + +#include + +template +class IDSet +{ + public: + IDSet() { m_next_index = 1; } + int add(const T & o) + { + int id = m_next_index++; + m_data[id] = o; + return id; + } + void remove(int id) + { + m_data.erase(id); + } + T & operator[](int id) + { + return m_data[id]; + } + bool contains(int id) + { + return m_data.find(id) != m_data.end(); + } + protected: + std::map m_data; + int m_next_index; +}; + +#endif