Panda3D
 All Classes Functions Variables Enumerations
userDataAudioCursor.cxx
1 // Filename: userDataAudioCursor.cxx
2 // Created by: jyelon (02Jul07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "userDataAudioCursor.h"
16 
17 TypeHandle UserDataAudioCursor::_type_handle;
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: UserDataAudioCursor::Constructor
21 // Access:
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 UserDataAudioCursor::
25 UserDataAudioCursor(UserDataAudio *src) :
26  MovieAudioCursor(src)
27 {
28  _audio_rate = src->_desired_rate;
29  _audio_channels = src->_desired_channels;
30  _can_seek = !src->_remove_after_read;
31  _can_seek_fast = !src->_remove_after_read;
32  _aborted = false;
33  if(!src->_remove_after_read) {
34  assert(src->_aborted && "UserData was not closed before by a done() call");
35  _length = static_cast<double>(src->_data.size() / _audio_channels) / _audio_rate;
36  }
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: UserDataAudioCursor::Destructor
41 // Access: Public, Virtual
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 UserDataAudioCursor::
45 ~UserDataAudioCursor() {
46  UserDataAudio *source = (UserDataAudio*)(MovieAudio*)_source;
47  source->_cursor = NULL;
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: UserDataAudioCursor::read_samples
52 // Access: Private
53 // Description: Read audio samples from the stream. N is the
54 // number of samples you wish to read. Your buffer
55 // must be equal in size to N * channels.
56 // Multiple-channel audio will be interleaved.
57 ////////////////////////////////////////////////////////////////////
59 read_samples(int n, PN_int16 *data) {
60  UserDataAudio *source = (UserDataAudio*)(MovieAudio*)_source;
61 
62  if(source->_remove_after_read) {
63  source->read_samples(n, data);
64  }
65  else {
66  int offset = _samples_read * _audio_channels;
67  int avail = source->_data.size() - offset;
68  int desired = n * _audio_channels;
69  if (avail > desired) avail = desired;
70 
71  for (int i=0; i<avail; i++) {
72  data[i] = source->_data[i+offset];
73  }
74  for (int i=avail; i<desired; i++) {
75  data[i] = 0;
76  }
77  }
78 
79  _samples_read += n;
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: UserDataAudioCursor::ready
84 // Access: Published
85 // Description: Set the offset if possible.
86 ////////////////////////////////////////////////////////////////////
88 seek(double t) {
89  if(_can_seek && 0 <= t && _length <= t) {
90  _samples_read = static_cast<int>(t * _audio_rate * _audio_channels + 0.5f);
91  }
92  else {
93  _samples_read = 0;
94  }
95  _last_seek = t;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: UserDataAudioCursor::ready
100 // Access: Private
101 // Description: Returns the number of audio samples ready to be
102 // read.
103 ////////////////////////////////////////////////////////////////////
105 ready() const {
106  UserDataAudio *source = (UserDataAudio*)(MovieAudio*)_source;
107  ((UserDataAudioCursor*)this)->_aborted = source->_aborted;
108 
109  if(source->_remove_after_read) return source->_data.size() / _audio_channels;
110  else return source->_data.size() / _audio_channels - _samples_read;
111 }
virtual void seek(double offset)
Set the offset if possible.
virtual void read_samples(int n, PN_int16 *data)
Read audio samples from the stream.
A UserDataAudioCursor is a means to manually supply a sequence of raw audio samples.
A MovieAudio is actually any source that provides a sequence of audio samples.
A UserDataAudio is a way for the user to manually supply raw audio samples.
Definition: userDataAudio.h:35
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A MovieAudio is actually any source that provides a sequence of audio samples.
Definition: movieAudio.h:48
virtual int ready() const
Returns the number of audio samples ready to be read.