Panda3D
userDataAudio.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 userDataAudio.cxx
10  * @author jyelon
11  * @date 2007-07-02
12  */
13 
14 #include "userDataAudio.h"
15 #include "userDataAudioCursor.h"
16 
17 TypeHandle UserDataAudio::_type_handle;
18 
19 /**
20  * This constructor returns a UserDataAudio --- a means to supply raw audio
21  * samples manually.
22  */
24 UserDataAudio(int rate, int channels, bool remove_after_read) :
25  MovieAudio("User Data Audio"),
26  _desired_rate(rate),
27  _desired_channels(channels),
28  _cursor(nullptr),
29  _aborted(false),
30  _remove_after_read(remove_after_read)
31 {
32 }
33 
34 /**
35  *
36  */
37 UserDataAudio::
38 ~UserDataAudio() {
39 }
40 
41 /**
42  * Open this audio, returning a UserDataAudioCursor. A UserDataAudio can only
43  * be opened by one consumer at a time.
44  */
45 PT(MovieAudioCursor) UserDataAudio::
46 open() {
47  if (_cursor) {
48  nassert_raise("A UserDataAudio can only be opened by one consumer at a time.");
49  return nullptr;
50  }
51  _cursor = new UserDataAudioCursor(this);
52  return _cursor;
53 }
54 
55 /**
56  * Read audio samples from the stream. N is the number of samples you wish to
57  * read. Your buffer must be equal in size to N * channels. Multiple-channel
58  * audio will be interleaved.
59  */
60 void UserDataAudio::
61 read_samples(int n, int16_t *data) {
62  int ready = (_data.size() / _desired_channels);
63  int desired = n * _desired_channels;
64  int avail = ready * _desired_channels;
65  if (avail > desired) avail = desired;
66  for (int i=0; i<avail; i++) {
67  data[i] = _data[i];
68  }
69  for (int i=avail; i<desired; i++) {
70  data[i] = 0;
71  }
72  for (int i=0; i<avail; i++) {
73  _data.pop_front();
74  }
75 }
76 
77 /**
78  * Appends audio samples to the buffer.
79  */
80 void UserDataAudio::
81 append(int16_t *data, int n) {
82  nassertv(!_aborted);
83  int words = n * _desired_channels;
84  for (int i=0; i<words; i++) {
85  _data.push_back(data[i]);
86  }
87 }
88 
89 /**
90  * Appends audio samples to the buffer from a datagram. This is intended to
91  * make it easy to send streaming raw audio over a network.
92  */
93 void UserDataAudio::
94 append(DatagramIterator *src, int n) {
95  nassertv(!_aborted);
96  int maxlen = src->get_remaining_size() / (2 * _desired_channels);
97  if (n > maxlen) n = maxlen;
98  int words = n * _desired_channels;
99  for (int i=0; i<words; i++) {
100  _data.push_back(src->get_int16());
101  }
102 }
103 
104 /**
105  * Appends audio samples to the buffer from a string. The samples must be
106  * stored little-endian in the string. This is not particularly efficient,
107  * but it may be convenient to deal with samples in python.
108  */
109 void UserDataAudio::
110 append(const std::string &str) {
111  nassertv(!_aborted);
112  int samples = str.size() / (2 * _desired_channels);
113  int words = samples * _desired_channels;
114  for (int i=0; i<words; i++) {
115  int c1 = ((unsigned char)str[i*2+0]);
116  int c2 = ((unsigned char)str[i*2+1]);
117  int16_t n = (c1 | (c2 << 8));
118  _data.push_back(n);
119  }
120 }
121 
122 /**
123  * Promises not to append any more samples, ie, this marks the end of the
124  * audio stream.
125  */
126 void UserDataAudio::
127 done() {
128  _aborted = true;
129 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void done()
Promises not to append any more samples, ie, this marks the end of the audio stream.
void append(int16_t *data, int n)
Appends audio samples to the buffer.
PT(MovieAudioCursor) UserDataAudio
Open this audio, returning a UserDataAudioCursor.
size_t get_remaining_size() const
Return the bytes left in the datagram.
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.
int16_t get_int16()
Extracts a signed 16-bit integer.
UserDataAudio(int rate, int channels, bool remove_after_read=true)
This constructor returns a UserDataAudio — a means to supply raw audio samples manually.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
A MovieAudio is actually any source that provides a sequence of audio samples.
Definition: movieAudio.h:44
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.