24 #define openal_audio_debug(x) \ 25 audio_debug("OpenALAudioSound \""<<get_name() \ 28 #define openal_audio_debug(x) ((void)0) 50 _positional(positional),
52 _max_dist(1000000000.0f),
53 _drop_off_factor(1.0f),
59 _basename(movie->get_filename().get_basename()),
60 _active(manager->get_active()),
72 if (!require_sound_data()) {
77 _length = _sd->_length;
79 if (_sd->_channels != 1) {
80 audio_warning(
"stereo sound " << movie->
get_filename() <<
" will not be spatialized");
83 release_sound_data(
false);
99 void OpenALAudioSound::
108 if (has_sound_data()) {
109 release_sound_data(
true);
111 _manager->release_sound(
this);
122 if (!is_valid())
return;
124 PN_stdfloat px,py,pz,vx,vy,vz;
133 if (!require_sound_data()) {
138 _manager->starting_sound(
this);
143 _manager->make_current();
149 alSourcei(_source,AL_SOURCE_RELATIVE,_positional?AL_FALSE:AL_TRUE);
150 al_audio_errcheck(
"alSourcei(_source,AL_SOURCE_RELATIVE)");
162 _playing_loops = _loop_count;
163 if (_playing_loops == 0) {
164 _playing_loops = 1000000000;
166 _loops_completed = 0;
168 double play_rate = _play_rate * _manager->get_play_rate();
169 audio_debug(
"playing. Rate=" << play_rate);
170 alSourcef(_source, AL_PITCH, play_rate);
171 _playing_rate = play_rate;
174 push_fresh_buffers();
175 alSourcef(_source, AL_SEC_OFFSET, _start_time);
176 _stream_queued[0]._time_offset = _start_time;
177 restart_stalled_audio();
179 audio_debug(
"Play: stream tell = " << _sd->_stream->tell() <<
" seeking " << _start_time);
180 if (_sd->_stream->tell() != _start_time) {
181 _sd->_stream->seek(_start_time);
183 push_fresh_buffers();
184 restart_stalled_audio();
187 set_calibrated_clock(rtc, _start_time, 1.0);
188 _current_time = _start_time;
199 if (!is_valid())
return;
202 _manager->make_current();
204 nassertv(has_sound_data());
207 alSourceStop(_source);
208 al_audio_errcheck(
"stopping a source");
209 alSourcei(_source, AL_BUFFER, 0);
210 al_audio_errcheck(
"clear source buffers");
211 for (
int i=0; i<((int)(_stream_queued.size())); i++) {
212 ALuint buffer = _stream_queued[i]._buffer;
213 if (buffer != _sd->_sample) {
214 _manager->delete_buffer(buffer);
217 _stream_queued.resize(0);
220 _manager->stopping_sound(
this);
221 release_sound_data(
false);
227 void OpenALAudioSound::
231 if (!is_valid())
return;
234 _current_time = _length;
235 if (!_finished_event.empty()) {
236 throw_event(_finished_event);
246 set_loop_count((loop)?0:1);
254 return (_loop_count == 0);
260 void OpenALAudioSound::
261 set_loop_count(
unsigned long loop_count) {
264 if (!is_valid())
return;
266 if (loop_count >= 1000000000) {
269 _loop_count=loop_count;
287 void OpenALAudioSound::
288 restart_stalled_audio() {
292 if (!is_valid())
return;
293 nassertv(is_playing());
295 if (_stream_queued.size() == 0) {
300 alGetSourcei(_source, AL_SOURCE_STATE, &
status);
301 if (
status != AL_PLAYING) {
302 alSourcePlay(_source);
309 void OpenALAudioSound::
310 queue_buffer(ALuint buffer,
int samples,
int loop_index,
double time_offset) {
313 nassertv(is_playing());
317 alSourceQueueBuffers(_source,1,&buffer);
318 ALenum err = alGetError();
319 if (err != AL_NO_ERROR) {
320 audio_error(
"could not load sample buffer into the queue");
325 buf._buffer = buffer;
326 buf._samples = samples;
327 buf._loop_index = loop_index;
328 buf._time_offset = time_offset;
329 _stream_queued.push_back(buf);
335 ALuint OpenALAudioSound::
336 make_buffer(
int samples,
int channels,
int rate,
unsigned char *data) {
339 nassertr(is_playing(), 0);
344 alGenBuffers(1, &buffer);
345 if (alGetError() != AL_NO_ERROR) {
346 audio_error(
"could not allocate an OpenAL buffer object");
353 (channels>1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16,
354 data, samples * channels * 2, rate);
355 int err = alGetError();
356 if (err != AL_NO_ERROR) {
357 audio_error(
"could not fill OpenAL buffer object with data");
369 int OpenALAudioSound::
370 read_stream_data(
int bytelen,
unsigned char *buffer) {
373 nassertr(has_sound_data(), 0);
376 double length = cursor->length();
377 int channels = cursor->audio_channels();
378 int rate = cursor->audio_rate();
379 int space = bytelen / (channels * 2);
382 while (space && (_loops_completed < _playing_loops)) {
383 double t = cursor->tell();
384 double remain =
length - t;
388 int samples = (int)(remain * rate);
390 _loops_completed += 1;
394 if (_sd->_stream->ready() == 0) {
395 if (_sd->_stream->aborted()) {
396 _loops_completed = _playing_loops;
400 if (samples > space) {
403 if (samples > _sd->_stream->ready()) {
404 samples = _sd->_stream->ready();
408 audio_debug(
"Streaming " << cursor->get_source()->get_name() <<
" at " << t <<
" hash " << hval);
411 buffer += (samples * channels * 2);
422 void OpenALAudioSound::
423 correct_calibrated_clock(
double rtc,
double t) {
426 nassertv(is_playing());
428 double cc = (rtc - _calibrated_clock_base) * _calibrated_clock_scale;
430 _calibrated_clock_decavg = (_calibrated_clock_decavg * 0.95) + (diff * 0.05);
432 set_calibrated_clock(rtc, t, 1.0);
433 _calibrated_clock_decavg = 0.0;
436 if ((_calibrated_clock_decavg > 0.01) && (diff > 0.01)) {
439 if ((_calibrated_clock_decavg < -0.01) && (diff < -0.01)) {
442 if ((_calibrated_clock_decavg < -0.05) && (diff < -0.05)) {
445 if ((_calibrated_clock_decavg < -0.15) && (diff < -0.15)) {
448 set_calibrated_clock(rtc, cc, scale);
450 cc = (rtc - _calibrated_clock_base) * _calibrated_clock_scale;
456 void OpenALAudioSound::
457 pull_used_buffers() {
460 if (!is_valid())
return;
461 nassertv(is_playing());
462 nassertv(has_sound_data());
464 while (_stream_queued.size()) {
466 ALint num_buffers = 0;
467 alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num_buffers);
468 if (num_buffers <= 0) {
471 alSourceUnqueueBuffers(_source, 1, &buffer);
472 int err = alGetError();
473 if (err == AL_NO_ERROR) {
474 if (_stream_queued[0]._buffer != buffer) {
479 bool found_culprit =
false;
480 for (
auto it = _stream_queued.begin(); it != _stream_queued.end(); ++it) {
481 if (it->_buffer == buffer) {
483 _stream_queued.erase(it);
484 found_culprit =
true;
488 if (!found_culprit) {
489 audio_error(
"corruption in stream queue");
494 _stream_queued.pop_front();
495 if (_stream_queued.size()) {
496 double al = _stream_queued[0]._time_offset + _stream_queued[0]._loop_index * _length;
498 correct_calibrated_clock(rtc, al);
500 if (buffer != _sd->_sample) {
501 _manager->delete_buffer(buffer);
514 void OpenALAudioSound::
515 push_fresh_buffers() {
517 static unsigned char data[65536];
519 if (!is_valid())
return;
520 nassertv(is_playing());
521 nassertv(has_sound_data());
524 while ((_loops_completed < _playing_loops) &&
525 (_stream_queued.size() < 100)) {
526 queue_buffer(_sd->_sample, 0,_loops_completed, 0.0);
527 _loops_completed += 1;
531 int channels = cursor->audio_channels();
532 int rate = cursor->audio_rate();
535 for (
size_t i = 0; i < _stream_queued.size(); i++) {
536 fill += _stream_queued[i]._samples;
539 while ((_loops_completed < _playing_loops) &&
540 (fill < (int)(audio_buffering_seconds * rate * channels))) {
541 int loop_index = _loops_completed;
542 double time_offset = cursor->tell();
543 int samples = read_stream_data(65536, data);
547 ALuint buffer = make_buffer(samples, channels, rate, data);
548 if (!is_valid() || !buffer)
return;
549 queue_buffer(buffer, samples, loop_index, time_offset);
550 if (!is_valid())
return;
575 return _current_time;
581 void OpenALAudioSound::
582 cache_time(
double rtc) {
585 nassertv(is_playing());
587 double t=get_calibrated_clock(rtc);
588 double max = _length * _playing_loops;
590 _current_time = _length;
592 _current_time = fmod(t, _length);
605 volume*=_manager->get_volume();
606 _manager->make_current();
608 alSourcef(_source,AL_GAIN,volume);
609 al_audio_errcheck(
"alSourcef(_source,AL_GAIN)");
626 audio_debug(
"OpenALAudioSound::set_balance() not implemented");
635 audio_debug(
"OpenALAudioSound::get_balance() not implemented");
647 _play_rate = play_rate;
649 alSourcef(_source, AL_PITCH, play_rate);
656 PN_stdfloat OpenALAudioSound::
657 get_play_rate()
const {
680 set_3d_attributes(PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz, PN_stdfloat vx, PN_stdfloat vy, PN_stdfloat vz) {
691 _manager->make_current();
694 alSourcefv(_source,AL_POSITION,_location);
695 al_audio_errcheck(
"alSourcefv(_source,AL_POSITION)");
696 alSourcefv(_source,AL_VELOCITY,_velocity);
697 al_audio_errcheck(
"alSourcefv(_source,AL_VELOCITY)");
706 get_3d_attributes(PN_stdfloat *px, PN_stdfloat *py, PN_stdfloat *pz, PN_stdfloat *vx, PN_stdfloat *vy, PN_stdfloat *vz) {
727 _manager->make_current();
730 alSourcef(_source,AL_REFERENCE_DISTANCE,_min_dist);
731 al_audio_errcheck(
"alSourcefv(_source,AL_REFERENCE_DISTANCE)");
752 _manager->make_current();
755 alSourcef(_source,AL_MAX_DISTANCE,_max_dist);
756 al_audio_errcheck(
"alSourcefv(_source,AL_MAX_DISTANCE)");
774 _drop_off_factor = factor;
777 _manager->make_current();
780 alSourcef(_source,AL_ROLLOFF_FACTOR,_drop_off_factor*_manager->audio_3d_get_drop_off_factor());
781 al_audio_errcheck(
"alSourcefv(_source,AL_ROLLOFF_FACTOR)");
790 return _drop_off_factor;
802 if (!is_valid())
return;
804 if (_active!=active) {
808 if (_paused && _loop_count==0) {
816 if (_loop_count==0) {
838 void OpenALAudioSound::
839 set_finished_event(
const std::string& event) {
840 _finished_event = event;
846 const std::string& OpenALAudioSound::
847 get_finished_event()
const {
848 return _finished_event;
868 return AudioSound::READY;
870 if ((_loops_completed >= _playing_loops)&&(_stream_queued.size()==0)) {
871 return AudioSound::READY;
873 return AudioSound::PLAYING;
get_filename
Returns the movie's filename.
void set_3d_max_distance(PN_stdfloat dist)
Set the distance that this sound stops falling off.
static TrueClock * get_global_ptr()
Returns a pointer to the one TrueClock object in the world.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
const std::string & get_name() const
Get name of sound file.
void set_time(PN_stdfloat time=0.0)
The next time you call play, the sound will start from the specified offset.
PN_stdfloat get_3d_max_distance() const
Get the distance that this sound stops falling off.
void set_3d_attributes(PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz, PN_stdfloat vx, PN_stdfloat vy, PN_stdfloat vz)
Set position and velocity of this sound.
void read_samples(int n, Datagram *dg)
Read audio samples from the stream into a Datagram.
PN_stdfloat get_time() const
Gets the play position within the sound.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static size_t add_hash(size_t start, const uint32_t *words, size_t num_words)
Adds a linear sequence of uint32 words to the hash.
void set_3d_min_distance(PN_stdfloat dist)
Set the distance that this sound begins to fall off.
PN_stdfloat get_3d_drop_off_factor() const
Control the effect distance has on audability.
bool get_loop() const
Returns whether looping is on or off.
void set_3d_drop_off_factor(PN_stdfloat factor)
Control the effect distance has on audability.
void set_active(bool active=true)
Sets whether the sound is marked "active".
PN_stdfloat get_balance() const
-1.0 to 1.0 scale -1 should be all the way left.
A MovieAudio is actually any source that provides a sequence of audio samples.
bool get_active() const
Returns whether the sound has been marked "active".
Similar to MutexHolder, but for a reentrant mutex.
void play()
Plays a sound.
void get_3d_attributes(PN_stdfloat *px, PN_stdfloat *py, PN_stdfloat *pz, PN_stdfloat *vx, PN_stdfloat *vy, PN_stdfloat *vz)
Get position and velocity of this sound Currently unimplemented.
PN_stdfloat get_3d_min_distance() const
Get the distance that this sound begins to fall off.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_volume(PN_stdfloat volume=1.0)
0.0 to 1.0 scale of volume converted to Fmod's internal 0.0 to 255.0 scale.
TypeHandle is the identifier used to differentiate C++ class types.
void set_loop(bool loop=true)
Turns looping on and off.
A MovieAudio is actually any source that provides a sequence of audio samples.
PN_stdfloat get_volume() const
Gets the current volume of a sound.
unsigned long get_loop_count() const
Return how many times a sound will loop.
PN_stdfloat length() const
Get length.
void set_balance(PN_stdfloat balance_right=0.0)
-1.0 to 1.0 scale
virtual void seek(double offset)
Skips to the specified offset within the file.
void set_play_rate(PN_stdfloat play_rate=1.0f)
Sets the speed at which a sound plays back.
AudioSound::SoundStatus status() const
Get status of the sound.