#include <stream_sound_source.hpp>
Inherits OpenALSoundSource.
Public Types | |
enum | FadeState { NoFading, FadingOn, FadingOff } |
Public Member Functions | |
StreamSoundSource () | |
virtual | ~StreamSoundSource () |
void | set_sound_file (SoundFile *file) |
void | set_fading (FadeState state, float fadetime) |
FadeState | get_fade_state () const |
void | update () |
void | set_looping (bool looping) |
bool | get_looping () const |
Private Member Functions | |
bool | fillBufferAndQueue (ALuint buffer) |
StreamSoundSource (const StreamSoundSource &) | |
StreamSoundSource & | operator= (const StreamSoundSource &) |
Private Attributes | |
SoundFile * | file |
ALuint | buffers [STREAMFRAGMENTS] |
FadeState | fade_state |
float | fade_start_time |
float | fade_time |
bool | looping |
Static Private Attributes | |
static const size_t | STREAMBUFFERSIZE = 1024 * 500 |
static const size_t | STREAMFRAGMENTS = 5 |
static const size_t | STREAMFRAGMENTSIZE = STREAMBUFFERSIZE / STREAMFRAGMENTS |
Definition at line 24 of file stream_sound_source.hpp.
StreamSoundSource::StreamSoundSource | ( | ) |
Definition at line 23 of file stream_sound_source.cpp.
References buffers, SoundManager::check_al_error(), SoundManager::register_for_update(), sound_manager, and STREAMFRAGMENTS.
00023 : 00024 file(0), 00025 fade_state(NoFading), 00026 fade_start_time(), 00027 fade_time(), 00028 looping(false) 00029 { 00030 alGenBuffers(STREAMFRAGMENTS, buffers); 00031 SoundManager::check_al_error("Couldn't allocate audio buffers: "); 00032 //add me to update list 00033 sound_manager->register_for_update( this ); 00034 }
StreamSoundSource::~StreamSoundSource | ( | ) | [virtual] |
Definition at line 36 of file stream_sound_source.cpp.
References buffers, SoundManager::check_al_error(), file, SoundManager::remove_from_update(), sound_manager, OpenALSoundSource::stop(), and STREAMFRAGMENTS.
00037 { 00038 //don't update me any longer 00039 sound_manager->remove_from_update( this ); 00040 delete file; 00041 stop(); 00042 alDeleteBuffers(STREAMFRAGMENTS, buffers); 00043 SoundManager::check_al_error("Couldn't delete audio buffers: "); 00044 }
StreamSoundSource::StreamSoundSource | ( | const StreamSoundSource & | ) | [private] |
void StreamSoundSource::set_sound_file | ( | SoundFile * | file | ) |
Definition at line 47 of file stream_sound_source.cpp.
References buffers, file, fillBufferAndQueue(), OpenALSoundSource::source, and STREAMFRAGMENTS.
Referenced by SoundManager::intern_create_sound_source().
00048 { 00049 delete file; 00050 file = newfile; 00051 00052 ALint queued; 00053 alGetSourcei(source, AL_BUFFERS_QUEUED, &queued); 00054 for(size_t i = 0; i < STREAMFRAGMENTS - queued; ++i) { 00055 if(fillBufferAndQueue(buffers[i]) == false) 00056 break; 00057 } 00058 }
void StreamSoundSource::set_fading | ( | FadeState | state, | |
float | fadetime | |||
) |
Definition at line 103 of file stream_sound_source.cpp.
References fade_start_time, fade_state, and real_time.
Referenced by SoundManager::stop_music().
00104 { 00105 this->fade_state = state; 00106 this->fade_time = fade_time; 00107 this->fade_start_time = real_time; 00108 }
FadeState StreamSoundSource::get_fade_state | ( | ) | const [inline] |
Definition at line 35 of file stream_sound_source.hpp.
References fade_state.
Referenced by SoundManager::stop_music().
00036 { 00037 return fade_state; 00038 }
void StreamSoundSource::update | ( | ) | [virtual] |
Reimplemented from OpenALSoundSource.
Definition at line 61 of file stream_sound_source.cpp.
References SoundManager::check_al_error(), fade_start_time, fade_state, fade_time, FadingOff, FadingOn, fillBufferAndQueue(), log_info, looping, NoFading, OpenALSoundSource::play(), OpenALSoundSource::playing(), real_time, OpenALSoundSource::set_gain(), OpenALSoundSource::source, and OpenALSoundSource::stop().
Referenced by SoundManager::update().
00062 { 00063 ALint processed = 0; 00064 alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed); 00065 for(ALint i = 0; i < processed; ++i) { 00066 ALuint buffer; 00067 alSourceUnqueueBuffers(source, 1, &buffer); 00068 SoundManager::check_al_error("Couldn't unqueue audio buffer: "); 00069 00070 if(fillBufferAndQueue(buffer) == false) 00071 break; 00072 } 00073 00074 if(!playing()) { 00075 if(processed == 0 || !looping) 00076 return; 00077 00078 // we might have to restart the source if we had a buffer underrun 00079 log_info << "Restarting audio source because of buffer underrun" << std::endl; 00080 play(); 00081 } 00082 00083 if(fade_state == FadingOn) { 00084 float time = real_time - fade_start_time; 00085 if(time >= fade_time) { 00086 set_gain(1.0); 00087 fade_state = NoFading; 00088 } else { 00089 set_gain(time / fade_time); 00090 } 00091 } else if(fade_state == FadingOff) { 00092 float time = real_time - fade_start_time; 00093 if(time >= fade_time) { 00094 stop(); 00095 fade_state = NoFading; 00096 } else { 00097 set_gain( (fade_time-time) / fade_time); 00098 } 00099 } 00100 }
void StreamSoundSource::set_looping | ( | bool | looping | ) | [inline, virtual] |
bool StreamSoundSource::get_looping | ( | ) | const [inline] |
Definition at line 45 of file stream_sound_source.hpp.
References looping.
00046 { 00047 return looping; 00048 }
bool StreamSoundSource::fillBufferAndQueue | ( | ALuint | buffer | ) | [private] |
Definition at line 111 of file stream_sound_source.cpp.
References SoundManager::check_al_error(), file, SoundManager::get_sample_format(), looping, SoundFile::rate, SoundFile::read(), SoundFile::reset(), OpenALSoundSource::source, and STREAMFRAGMENTSIZE.
Referenced by set_sound_file(), and update().
00112 { 00113 // fill buffer 00114 char* bufferdata = new char[STREAMFRAGMENTSIZE]; 00115 size_t bytesread = 0; 00116 do { 00117 bytesread += file->read(bufferdata + bytesread, 00118 STREAMFRAGMENTSIZE - bytesread); 00119 // end of sound file 00120 if(bytesread < STREAMFRAGMENTSIZE) { 00121 if(looping) 00122 file->reset(); 00123 else 00124 break; 00125 } 00126 } while(bytesread < STREAMFRAGMENTSIZE); 00127 00128 if(bytesread > 0) { 00129 ALenum format = SoundManager::get_sample_format(file); 00130 alBufferData(buffer, format, bufferdata, bytesread, file->rate); 00131 SoundManager::check_al_error("Couldn't refill audio buffer: "); 00132 00133 alSourceQueueBuffers(source, 1, &buffer); 00134 SoundManager::check_al_error("Couldn't queue audio buffer: "); 00135 } 00136 delete[] bufferdata; 00137 00138 // return false if there aren't more buffers to fill 00139 return bytesread >= STREAMFRAGMENTSIZE; 00140 }
StreamSoundSource& StreamSoundSource::operator= | ( | const StreamSoundSource & | ) | [private] |
const size_t StreamSoundSource::STREAMBUFFERSIZE = 1024 * 500 [static, private] |
Definition at line 51 of file stream_sound_source.hpp.
const size_t StreamSoundSource::STREAMFRAGMENTS = 5 [static, private] |
Definition at line 52 of file stream_sound_source.hpp.
Referenced by set_sound_file(), StreamSoundSource(), and ~StreamSoundSource().
const size_t StreamSoundSource::STREAMFRAGMENTSIZE = STREAMBUFFERSIZE / STREAMFRAGMENTS [static, private] |
SoundFile* StreamSoundSource::file [private] |
Definition at line 57 of file stream_sound_source.hpp.
Referenced by fillBufferAndQueue(), set_sound_file(), and ~StreamSoundSource().
ALuint StreamSoundSource::buffers[STREAMFRAGMENTS] [private] |
Definition at line 58 of file stream_sound_source.hpp.
Referenced by set_sound_file(), StreamSoundSource(), and ~StreamSoundSource().
FadeState StreamSoundSource::fade_state [private] |
Definition at line 60 of file stream_sound_source.hpp.
Referenced by get_fade_state(), set_fading(), and update().
float StreamSoundSource::fade_start_time [private] |
float StreamSoundSource::fade_time [private] |
bool StreamSoundSource::looping [private] |
Definition at line 63 of file stream_sound_source.hpp.
Referenced by fillBufferAndQueue(), get_looping(), and update().