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