define AudioSound::set_time() to have effect only upon the next call to play()

This commit is contained in:
David Rose 2007-08-24 17:17:09 +00:00
parent ad2d5e3ba7
commit 93bfc2ea3f
9 changed files with 133 additions and 75 deletions

View File

@ -56,9 +56,9 @@ PUBLISHED:
// a file. // a file.
// time in seconds: 0 = beginning; length() = end. // time in seconds: 0 = beginning; length() = end.
// inits to 0.0. // inits to 0.0.
// - Unlike the other get_* and set_* calls for a sound, the // - The current time position will not change while the sound is
// current time position will change while the sound is playing. // playing; you must call play() again to effect the change. To
// To play the same sound from a time offset a second time, // play the same sound from a time offset a second time,
// explicitly set the time position again. When looping, the // explicitly set the time position again. When looping, the
// second and later loops will start from the beginning of the // second and later loops will start from the beginning of the
// sound. // sound.

View File

@ -102,9 +102,17 @@ play() {
set_volume(_volume); set_volume(_volume);
set_play_rate(_play_rate); set_play_rate(_play_rate);
AIL_set_sample_loop_count(_sample, _loop_count); AIL_set_sample_loop_count(_sample, _loop_count);
if (_got_start_time) {
do_set_time(_start_time);
AIL_resume_sample(_sample);
} else {
AIL_start_sample(_sample); AIL_start_sample(_sample);
} }
} }
_got_start_time = false;
}
} else { } else {
// In case _loop_count gets set to forever (zero): // In case _loop_count gets set to forever (zero):
audio_debug(" paused "<<_file_name ); audio_debug(" paused "<<_file_name );
@ -139,30 +147,6 @@ stop() {
} }
} }
////////////////////////////////////////////////////////////////////
// Function: MilesAudioSample::set_time
// Access: Public, Virtual
// Description:
////////////////////////////////////////////////////////////////////
void MilesAudioSample::
set_time(float time) {
miles_audio_debug("set_time(time="<<time<<")");
if (_sample != 0) {
// Ensure we don't inadvertently run off the end of the sound.
float max_time = length();
if (time > max_time) {
milesAudio_cat.warning()
<< "set_time(" << time << ") requested for sound of length "
<< max_time << "\n";
time = max_time;
}
S32 time_ms = (S32)(1000.0f * time);
AIL_set_sample_ms_position(_sample, time_ms);
}
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: MilesAudioSample::get_time // Function: MilesAudioSample::get_time
// Access: Public, Virtual // Access: Public, Virtual
@ -171,6 +155,9 @@ set_time(float time) {
float MilesAudioSample:: float MilesAudioSample::
get_time() const { get_time() const {
if (_sample == 0) { if (_sample == 0) {
if (_got_start_time) {
return _start_time;
}
return 0.0f; return 0.0f;
} }
@ -337,4 +324,27 @@ finish_callback(HSAMPLE sample) {
self->_manager->_sounds_finished = true; self->_manager->_sounds_finished = true;
} }
////////////////////////////////////////////////////////////////////
// Function: MilesAudioSample::do_set_time
// Access: Private
// Description: Sets the start time of an already allocated sample.
////////////////////////////////////////////////////////////////////
void MilesAudioSample::
do_set_time(float time) {
miles_audio_debug("do_set_time(time="<<time<<")");
nassertv(_sample != 0);
// Ensure we don't inadvertently run off the end of the sound.
float max_time = length();
if (time > max_time) {
milesAudio_cat.warning()
<< "set_time(" << time << ") requested for sound of length "
<< max_time << "\n";
time = max_time;
}
S32 time_ms = (S32)(1000.0f * time);
AIL_set_sample_ms_position(_sample, time_ms);
}
#endif //] #endif //]

View File

@ -44,7 +44,6 @@ public:
virtual void play(); virtual void play();
virtual void stop(); virtual void stop();
virtual void set_time(float time=0.0f);
virtual float get_time() const; virtual float get_time() const;
virtual void set_volume(float volume=1.0f); virtual void set_volume(float volume=1.0f);
@ -61,6 +60,7 @@ public:
private: private:
void internal_stop(); void internal_stop();
static void AILCALLBACK finish_callback(HSAMPLE sample); static void AILCALLBACK finish_callback(HSAMPLE sample);
void do_set_time(float time);
PT(MilesAudioManager::SoundData) _sd; PT(MilesAudioManager::SoundData) _sd;
HSAMPLE _sample; HSAMPLE _sample;

View File

@ -98,9 +98,17 @@ play() {
set_volume(_volume); set_volume(_volume);
set_play_rate(_play_rate); set_play_rate(_play_rate);
AIL_set_sequence_loop_count(_sequence, _loop_count); AIL_set_sequence_loop_count(_sequence, _loop_count);
if (_got_start_time) {
do_set_time(_start_time);
AIL_resume_sequence(_sequence);
} else {
AIL_start_sequence(_sequence); AIL_start_sequence(_sequence);
} }
} }
_got_start_time = false;
}
} else { } else {
// In case _loop_count gets set to forever (zero): // In case _loop_count gets set to forever (zero):
audio_debug(" paused "<<_file_name ); audio_debug(" paused "<<_file_name );
@ -135,27 +143,6 @@ stop() {
} }
} }
////////////////////////////////////////////////////////////////////
// Function: MilesAudioSequence::set_time
// Access: Public, Virtual
// Description:
////////////////////////////////////////////////////////////////////
void MilesAudioSequence::
set_time(float time) {
miles_audio_debug("set_time(time="<<time<<")");
if (_sequence != 0) {
S32 time_ms = (S32)(1000.0f * time);
// Ensure we don't inadvertently run off the end of the sound.
S32 length_ms;
AIL_sequence_ms_position(_sequence, &length_ms, NULL);
time_ms = min(time_ms, length_ms);
AIL_set_sequence_ms_position(_sequence, time_ms);
}
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: MilesAudioSequence::get_time // Function: MilesAudioSequence::get_time
// Access: Public, Virtual // Access: Public, Virtual
@ -164,6 +151,9 @@ set_time(float time) {
float MilesAudioSequence:: float MilesAudioSequence::
get_time() const { get_time() const {
if (_sequence == 0) { if (_sequence == 0) {
if (_got_start_time) {
return _start_time;
}
return 0.0f; return 0.0f;
} }
@ -314,4 +304,25 @@ finish_callback(HSEQUENCE sequence) {
self->_manager->_sounds_finished = true; self->_manager->_sounds_finished = true;
} }
////////////////////////////////////////////////////////////////////
// Function: MilesAudioSequence::do_set_time
// Access: Private
// Description: Sets the start time of an already allocated stream.
////////////////////////////////////////////////////////////////////
void MilesAudioSequence::
do_set_time(float time) {
miles_audio_debug("do_set_time(time="<<time<<")");
nassertv(_sequence != 0);
S32 time_ms = (S32)(1000.0f * time);
// Ensure we don't inadvertently run off the end of the sound.
S32 length_ms;
AIL_sequence_ms_position(_sequence, &length_ms, NULL);
time_ms = min(time_ms, length_ms);
AIL_set_sequence_ms_position(_sequence, time_ms);
}
#endif //] #endif //]

View File

@ -43,7 +43,6 @@ public:
virtual void play(); virtual void play();
virtual void stop(); virtual void stop();
virtual void set_time(float time=0.0f);
virtual float get_time() const; virtual float get_time() const;
virtual void set_volume(float volume=1.0f); virtual void set_volume(float volume=1.0f);
@ -59,6 +58,7 @@ public:
private: private:
void internal_stop(); void internal_stop();
static void AILCALLBACK finish_callback(HSEQUENCE sequence); static void AILCALLBACK finish_callback(HSEQUENCE sequence);
void do_set_time(float time);
PT(MilesAudioManager::SoundData) _sd; PT(MilesAudioManager::SoundData) _sd;
HSEQUENCE _sequence; HSEQUENCE _sequence;

View File

@ -44,7 +44,10 @@ MilesAudioSound(MilesAudioManager *manager,
_file_name(file_name), _file_name(file_name),
_volume(1.0f), _balance(0), _play_rate(1.0f), _volume(1.0f), _balance(0), _play_rate(1.0f),
_loop_count(1), _loop_count(1),
_active(true), _paused(false) _active(true),
_paused(false),
_start_time(0.0f),
_got_start_time(false)
{ {
nassertv(!file_name.empty()); nassertv(!file_name.empty());
} }
@ -132,6 +135,20 @@ get_play_rate() const {
return _play_rate; return _play_rate;
} }
////////////////////////////////////////////////////////////////////
// Function: MilesAudioSound::set_time
// Access: Public, Virtual
// Description:
////////////////////////////////////////////////////////////////////
void MilesAudioSound::
set_time(float time) {
miles_audio_debug("set_time(time="<<time<<")");
// Mark this position for the next play().
_start_time = time;
_got_start_time = true;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: MilesAudioSound::set_active // Function: MilesAudioSound::set_active
// Access: Public, Virtual // Access: Public, Virtual

View File

@ -46,6 +46,8 @@ public:
virtual float get_balance() const; virtual float get_balance() const;
virtual float get_play_rate() const; virtual float get_play_rate() const;
virtual void set_time(float start_time=0.0);
virtual void set_active(bool active=true); virtual void set_active(bool active=true);
virtual bool get_active() const; virtual bool get_active() const;
@ -81,6 +83,11 @@ protected:
// with stop(). Note: no longer implemented. // with stop(). Note: no longer implemented.
string _finished_event; string _finished_event;
// This is set whenever we call set_time(). Calling play() will
// respect this if it is set, and then reset it.
float _start_time;
bool _got_start_time;
public: public:
static TypeHandle get_class_type() { static TypeHandle get_class_type() {
return _type_handle; return _type_handle;

View File

@ -103,13 +103,21 @@ play() {
AIL_set_stream_loop_count(_stream, _loop_count); AIL_set_stream_loop_count(_stream, _loop_count);
AIL_start_stream(_stream);
if (_got_start_time) {
// There's no AIL_resume_stream(), so we start in the middle by
// starting normally, then immediately skipping to the middle.
do_set_time(_start_time);
}
if (miles_audio_panda_threads) { if (miles_audio_panda_threads) {
AIL_auto_service_stream(_stream, 0); AIL_auto_service_stream(_stream, 0);
_manager->start_service_stream(_stream); _manager->start_service_stream(_stream);
} else { } else {
AIL_auto_service_stream(_stream, 1); AIL_auto_service_stream(_stream, 1);
} }
AIL_start_stream(_stream);
_got_start_time = false;
} else { } else {
// In case _loop_count gets set to forever (zero): // In case _loop_count gets set to forever (zero):
@ -143,25 +151,6 @@ stop() {
} }
} }
////////////////////////////////////////////////////////////////////
// Function: MilesAudioStream::
// Access: Public, Virtual
// Description:
////////////////////////////////////////////////////////////////////
void MilesAudioStream::
set_time(float time) {
if (_stream != 0) {
S32 time_ms = (S32)(1000.0f * time);
// Ensure we don't inadvertently run off the end of the sound.
S32 length_ms;
AIL_stream_ms_position(_stream, &length_ms, NULL);
time_ms = min(time_ms, length_ms);
AIL_set_stream_ms_position(_stream, time_ms);
}
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: MilesAudioStream:: // Function: MilesAudioStream::
// Access: Public, Virtual // Access: Public, Virtual
@ -169,7 +158,12 @@ set_time(float time) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
float MilesAudioStream:: float MilesAudioStream::
get_time() const { get_time() const {
nassertr(_stream, 0.0f); if (_stream == 0) {
if (_got_start_time) {
return _start_time;
}
return 0.0f;
}
S32 current_ms; S32 current_ms;
AIL_stream_ms_position(_stream, NULL, &current_ms); AIL_stream_ms_position(_stream, NULL, &current_ms);
@ -314,5 +308,24 @@ finish_callback(HSTREAM stream) {
self->_manager->_sounds_finished = true; self->_manager->_sounds_finished = true;
} }
////////////////////////////////////////////////////////////////////
// Function: MilesAudioStream::do_set_time
// Access: Private
// Description: Sets the start time of an already allocated stream.
////////////////////////////////////////////////////////////////////
void MilesAudioStream::
do_set_time(float time) {
nassertv(_stream != 0);
S32 time_ms = (S32)(1000.0f * time);
// Ensure we don't inadvertently run off the end of the sound.
S32 length_ms;
AIL_stream_ms_position(_stream, &length_ms, NULL);
time_ms = min(time_ms, length_ms);
AIL_set_stream_ms_position(_stream, time_ms);
}
#endif //] #endif //]

View File

@ -44,7 +44,6 @@ public:
virtual void play(); virtual void play();
virtual void stop(); virtual void stop();
virtual void set_time(float time=0.0f);
virtual float get_time() const; virtual float get_time() const;
virtual void set_volume(float volume=1.0f); virtual void set_volume(float volume=1.0f);
@ -59,6 +58,7 @@ public:
private: private:
static void AILCALLBACK finish_callback(HSTREAM stream); static void AILCALLBACK finish_callback(HSTREAM stream);
void do_set_time(float time);
Filename _path; Filename _path;
HSTREAM _stream; HSTREAM _stream;