patch from Nox_fire(galaxy)

This commit is contained in:
rdb 2011-05-11 14:12:37 +00:00
parent 5b74526c15
commit 4a5db4ca35
4 changed files with 55 additions and 9 deletions

View File

@ -24,12 +24,13 @@ TypeHandle UserDataAudio::_type_handle;
// a means to supply raw audio samples manually.
////////////////////////////////////////////////////////////////////
UserDataAudio::
UserDataAudio(int rate, int channels) :
UserDataAudio(int rate, int channels, bool remove_after_read) :
MovieAudio("User Data Audio"),
_desired_rate(rate),
_desired_channels(channels),
_cursor(NULL),
_aborted(false)
_aborted(false),
_remove_after_read(remove_after_read)
{
}

View File

@ -25,12 +25,17 @@ class UserDataAudioCursor;
////////////////////////////////////////////////////////////////////
// Class : UserDataAudio
// Description : A UserDataAudio is a way for the user to manually
// supply raw audio samples.
// supply raw audio samples. remove_after_read means the
// data will be removed if read once. Else data will
// be stored (enable looping and seeking).
// Expects data as 16 bit signed (word); Example for stereo:
// 1.word = 1.channel,2.word = 2.channel,
// 3.word = 1.channel,4.word = 2.channel, etc.
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA_MOVIES UserDataAudio : public MovieAudio {
PUBLISHED:
UserDataAudio(int rate, int channels);
UserDataAudio(int rate, int channels, bool remove_after_read=true);
virtual ~UserDataAudio();
virtual PT(MovieAudioCursor) open();
@ -47,6 +52,7 @@ class EXPCL_PANDA_MOVIES UserDataAudio : public MovieAudio {
UserDataAudioCursor *_cursor;
pdeque<PN_int16> _data;
bool _aborted;
bool _remove_after_read;
friend class UserDataAudioCursor;
public:

View File

@ -27,9 +27,13 @@ UserDataAudioCursor(UserDataAudio *src) :
{
_audio_rate = src->_desired_rate;
_audio_channels = src->_desired_channels;
_can_seek = false;
_can_seek_fast = false;
_can_seek = !src->_remove_after_read;
_can_seek_fast = !src->_remove_after_read;
_aborted = false;
if(!src->_remove_after_read) {
assert(src->_aborted && "UserData was not closed before by a done() call");
_length = static_cast<double>(src->_data.size() / _audio_channels) / _audio_rate;
}
}
////////////////////////////////////////////////////////////////////
@ -54,10 +58,43 @@ UserDataAudioCursor::
void UserDataAudioCursor::
read_samples(int n, PN_int16 *data) {
UserDataAudio *source = (UserDataAudio*)(MovieAudio*)_source;
source->read_samples(n, data);
if(source->_remove_after_read) {
source->read_samples(n, data);
}
else {
int offset = _samples_read * _audio_channels;
int avail = source->_data.size() - offset;
int desired = n * _audio_channels;
if (avail > desired) avail = desired;
for (int i=0; i<avail; i++) {
data[i] = source->_data[i+offset];
}
for (int i=avail; i<desired; i++) {
data[i] = 0;
}
}
_samples_read += n;
}
////////////////////////////////////////////////////////////////////
// Function: UserDataAudioCursor::ready
// Access: Published
// Description: Set the offset if possible.
////////////////////////////////////////////////////////////////////
void UserDataAudioCursor::
seek(double t) {
if(_can_seek && 0 <= t && _length <= t) {
_samples_read = static_cast<int>(t * _audio_rate * _audio_channels + 0.5f);
}
else {
_samples_read = 0;
}
_last_seek = t;
}
////////////////////////////////////////////////////////////////////
// Function: UserDataAudioCursor::ready
// Access: Private
@ -68,5 +105,7 @@ int UserDataAudioCursor::
ready() const {
UserDataAudio *source = (UserDataAudio*)(MovieAudio*)_source;
((UserDataAudioCursor*)this)->_aborted = source->_aborted;
return (source->_data.size()) / _audio_channels;
if(source->_remove_after_read) return source->_data.size() / _audio_channels;
else return source->_data.size() / _audio_channels - _samples_read;
}

View File

@ -35,7 +35,7 @@ PUBLISHED:
public:
virtual void read_samples(int n, PN_int16 *data);
virtual int ready() const;
friend class UserDataAudio;
virtual void seek(double offset);
public:
static TypeHandle get_class_type() {