Revision f150aab5 server/src/AudioPlayerLibAO.cpp
server/src/AudioPlayerLibAO.cpp | ||
---|---|---|
1 | 1 |
/* |
2 |
* This file is part of hlrc_server |
|
3 |
* |
|
4 |
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de |
|
5 |
* http://opensource.cit-ec.de/projects/hlrc_server |
|
6 |
* |
|
7 |
* This file may be licensed under the terms of the |
|
8 |
* GNU General Public License Version 3 (the ``GPL''), |
|
9 |
* or (at your option) any later version. |
|
10 |
* |
|
11 |
* Software distributed under the License is distributed |
|
12 |
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either |
|
13 |
* express or implied. See the GPL for the specific language |
|
14 |
* governing rights and limitations. |
|
15 |
* |
|
16 |
* You should have received a copy of the GPL along with this |
|
17 |
* program. If not, go to http://www.gnu.org/licenses/gpl.html |
|
18 |
* or write to the Free Software Foundation, Inc., |
|
19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* The development of this software was supported by the |
|
22 |
* Excellence Cluster EXC 277 Cognitive Interaction Technology. |
|
23 |
* The Excellence Cluster EXC 277 is a grant of the Deutsche |
|
24 |
* Forschungsgemeinschaft (DFG) in the context of the German |
|
25 |
* Excellence Initiative. |
|
26 |
* |
|
27 |
*/ |
|
2 |
* This file is part of hlrc_server
|
|
3 |
*
|
|
4 |
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
|
5 |
* http://opensource.cit-ec.de/projects/hlrc_server
|
|
6 |
*
|
|
7 |
* This file may be licensed under the terms of the
|
|
8 |
* GNU General Public License Version 3 (the ``GPL''),
|
|
9 |
* or (at your option) any later version.
|
|
10 |
*
|
|
11 |
* Software distributed under the License is distributed
|
|
12 |
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
|
13 |
* express or implied. See the GPL for the specific language
|
|
14 |
* governing rights and limitations.
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GPL along with this
|
|
17 |
* program. If not, go to http://www.gnu.org/licenses/gpl.html
|
|
18 |
* or write to the Free Software Foundation, Inc.,
|
|
19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
20 |
*
|
|
21 |
* The development of this software was supported by the
|
|
22 |
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
|
23 |
* The Excellence Cluster EXC 277 is a grant of the Deutsche
|
|
24 |
* Forschungsgemeinschaft (DFG) in the context of the German
|
|
25 |
* Excellence Initiative.
|
|
26 |
*
|
|
27 |
*/
|
|
28 | 28 |
|
29 | 29 |
#include "AudioPlayerLibAO.h" |
30 | 30 |
|
31 | 31 |
using namespace std; |
32 | 32 |
using namespace boost; |
33 | 33 |
|
34 |
//new audio player with gievn device type (default is pulse) |
|
35 |
AudioPlayerLibAO::AudioPlayerLibAO(string driver): AudioPlayer(driver){
|
|
36 |
printf("> using libao for playback\n");
|
|
34 |
// new audio player with gievn device type (default is pulse)
|
|
35 |
AudioPlayerLibAO::AudioPlayerLibAO(string driver) : AudioPlayer(driver) {
|
|
36 |
printf("> using libao for playback\n");
|
|
37 | 37 |
|
38 |
//initialize
|
|
39 |
ao_initialize();
|
|
38 |
// initialize
|
|
39 |
ao_initialize();
|
|
40 | 40 |
|
41 |
//configure driver |
|
42 |
if (audio_driver.empty()){ |
|
43 |
//use default: |
|
44 |
driver = ao_default_driver_id(); |
|
45 |
}else{ |
|
46 |
driver = ao_driver_id(audio_driver.c_str()); |
|
47 |
} |
|
41 |
// configure driver |
|
42 |
if (audio_driver.empty()) { |
|
43 |
// use default: |
|
44 |
driver = ao_default_driver_id(); |
|
45 |
} |
|
46 |
else { |
|
47 |
driver = ao_driver_id(audio_driver.c_str()); |
|
48 |
} |
|
48 | 49 |
|
49 |
//start audio playback thread:
|
|
50 |
playback_thread_ptr = new boost::thread(boost::bind(&AudioPlayerLibAO::playback_thread, this));
|
|
50 |
// start audio playback thread:
|
|
51 |
playback_thread_ptr = new boost::thread(boost::bind(&AudioPlayerLibAO::playback_thread, this));
|
|
51 | 52 |
} |
52 | 53 |
|
54 |
// this will return once we start playing |
|
55 |
void AudioPlayerLibAO::play(boost::shared_ptr<AudioData> audio) { |
|
56 |
audio_data = audio; |
|
53 | 57 |
|
54 |
//this will return once we start playing |
|
55 |
void AudioPlayerLibAO::play(boost::shared_ptr<AudioData> audio){ |
|
56 |
audio_data = audio; |
|
58 |
printf("> AudioPlayerLibAO: play() %d samples requested\n", (int)audio_data->samples.size()); |
|
57 | 59 |
|
58 |
printf("> AudioPlayerLibAO: play() %d samples requested\n",(int)audio_data->samples.size()); |
|
60 |
// skip empty audio data |
|
61 |
if (audio_data->samples.size() == 0) { |
|
62 |
printf("> warning: empty audio data - skipped.\n"); |
|
63 |
return; |
|
64 |
} |
|
59 | 65 |
|
60 |
//skip empty audio data
|
|
61 |
if (audio_data->samples.size() == 0){
|
|
62 |
printf("> warning: empty audio data - skipped.\n");
|
|
63 |
return;
|
|
64 |
}
|
|
66 |
// check if we can play this file:
|
|
67 |
while (playback_state != IDLE) {
|
|
68 |
printf("> player not ready yet, waiting for 10ms\n");
|
|
69 |
usleep(10 * 1000);
|
|
70 |
}
|
|
65 | 71 |
|
66 |
//check if we can play this file: |
|
67 |
while(playback_state != IDLE){ |
|
68 |
printf("> player not ready yet, waiting for 10ms\n"); |
|
69 |
usleep(10*1000); |
|
70 |
} |
|
72 |
audio_format = extract_ao_format(audio_data); |
|
71 | 73 |
|
72 |
audio_format = extract_ao_format(audio_data);
|
|
74 |
playback_requested = true;
|
|
73 | 75 |
|
74 |
playback_requested = true; |
|
75 |
|
|
76 |
//wait for playback to start: |
|
77 |
printf("> waiting for playback start...\n"); |
|
78 |
while(playback_requested){ |
|
79 |
usleep(1*1000); |
|
80 |
} |
|
81 |
printf("> playback running.\n"); |
|
76 |
// wait for playback to start: |
|
77 |
printf("> waiting for playback start...\n"); |
|
78 |
while (playback_requested) { |
|
79 |
usleep(1 * 1000); |
|
80 |
} |
|
81 |
printf("> playback running.\n"); |
|
82 | 82 |
} |
83 | 83 |
|
84 |
|
|
85 |
ao_sample_format AudioPlayerLibAO::extract_ao_format(boost::shared_ptr<AudioData> audio){ |
|
86 |
ao_sample_format ao_format; |
|
87 |
|
|
88 |
//get bits per sample |
|
89 |
ao_format.bits = audio->sample_bit; |
|
90 |
//bitrate |
|
91 |
ao_format.rate = audio->sample_rate; |
|
92 |
//endianess |
|
93 |
ao_format.byte_format = audio->sample_big_endian?AO_FMT_BIG:AO_FMT_LITTLE; |
|
94 |
//number of channels |
|
95 |
ao_format.channels = audio->sample_channels; |
|
96 |
|
|
97 |
//format matrix: |
|
98 |
if (ao_format.channels == 1){ |
|
99 |
ao_format.matrix = (char *)"L"; |
|
100 |
}else if(ao_format.channels == 2){ |
|
101 |
ao_format.matrix = (char *)"L,R"; |
|
102 |
}else{ |
|
103 |
printf("> invalid AudioData channel count given. ignoring request\n"); |
|
104 |
throw runtime_error("AudioPlayerLibAO::extract_ao_format() unsupported number of audio channels given in AudioData"); |
|
105 |
} |
|
106 |
|
|
107 |
printf("> AudioPlayerLibAO: format=%d rate=%d endianess=%d channels=%d\n", ao_format.bits, ao_format.rate, ao_format.byte_format, ao_format.channels); |
|
108 |
|
|
109 |
return ao_format; |
|
84 |
ao_sample_format AudioPlayerLibAO::extract_ao_format(boost::shared_ptr<AudioData> audio) { |
|
85 |
ao_sample_format ao_format; |
|
86 |
|
|
87 |
// get bits per sample |
|
88 |
ao_format.bits = audio->sample_bit; |
|
89 |
// bitrate |
|
90 |
ao_format.rate = audio->sample_rate; |
|
91 |
// endianess |
|
92 |
ao_format.byte_format = audio->sample_big_endian ? AO_FMT_BIG : AO_FMT_LITTLE; |
|
93 |
// number of channels |
|
94 |
ao_format.channels = audio->sample_channels; |
|
95 |
|
|
96 |
// format matrix: |
|
97 |
if (ao_format.channels == 1) { |
|
98 |
ao_format.matrix = (char*)"L"; |
|
99 |
} |
|
100 |
else if (ao_format.channels == 2) { |
|
101 |
ao_format.matrix = (char*)"L,R"; |
|
102 |
} |
|
103 |
else { |
|
104 |
printf("> invalid AudioData channel count given. ignoring request\n"); |
|
105 |
throw runtime_error("AudioPlayerLibAO::extract_ao_format() unsupported number of audio channels given in AudioData"); |
|
106 |
} |
|
107 |
|
|
108 |
printf("> AudioPlayerLibAO: format=%d rate=%d endianess=%d channels=%d\n", ao_format.bits, ao_format.rate, |
|
109 |
ao_format.byte_format, ao_format.channels); |
|
110 |
|
|
111 |
return ao_format; |
|
110 | 112 |
} |
111 | 113 |
|
112 |
|
|
113 |
|
|
114 |
void AudioPlayerLibAO::playback_thread(){ |
|
115 |
printf("> playback thread started\n"); |
|
116 |
playback_state = IDLE; |
|
117 |
|
|
118 |
while(playback_state != EXITING){ |
|
119 |
switch (playback_state){ |
|
120 |
default: |
|
121 |
case(INITIALIZING): |
|
122 |
//waiting to be ready |
|
123 |
break; |
|
124 |
|
|
125 |
case(IDLE): |
|
126 |
//nothing to do, wait here until we are triggered to play! |
|
127 |
if (playback_requested){ |
|
128 |
printf("> AudioPlayer: loading\n"); |
|
129 |
playback_state = LOADING; |
|
130 |
} |
|
131 |
break; |
|
132 |
|
|
133 |
case(LOADING): |
|
134 |
//check if we can play that format: |
|
135 |
device = ao_open_live(driver, &audio_format, NULL /* no options */); |
|
136 |
|
|
137 |
if (device == NULL){ |
|
138 |
printf("> AudioPlayer: WARNING: can not open requested audio device. trying to fall back to default driver\n"); |
|
139 |
driver = ao_default_driver_id(); |
|
140 |
device = ao_open_live(driver, &audio_format, NULL /* no options */); |
|
141 |
} |
|
142 |
|
|
143 |
if (device == NULL){ |
|
144 |
//failed to init device for format: |
|
145 |
printf("> AudioPlayer: WARNING: can not open audio device, not playing audio file!\n"); |
|
146 |
playback_active = false; |
|
147 |
playback_requested = false; |
|
148 |
playback_state = IDLE; |
|
149 |
break; |
|
150 |
}else{ |
|
151 |
//ok we can now play the data |
|
152 |
printf("> AudioPlayer: playback started\n"); |
|
153 |
//mark request as beeing processed |
|
154 |
playback_requested = false; |
|
155 |
//enter actual playing state |
|
156 |
playback_state = PLAYING; |
|
157 |
playback_active = true; |
|
158 |
|
|
159 |
ao_play(device, audio_data->samples.data(), audio_data->samples.size()); |
|
160 |
printf("> AudioPlayer: finished playback\n"); |
|
161 |
} |
|
162 |
break; |
|
163 |
|
|
164 |
case(PLAYING): |
|
165 |
//when we reach this case, the ao_play() call finished |
|
166 |
//-> we can close this now: |
|
167 |
ao_close(device); |
|
168 |
playback_state = IDLE; |
|
169 |
playback_active = false; |
|
170 |
printf("> AudioPlayer: device closed\n"); |
|
171 |
break; |
|
172 |
|
|
173 |
case(ERROR): |
|
174 |
printf("> AudioPlayer ERROR! exiting\n"); |
|
175 |
exit(EXIT_FAILURE); |
|
176 |
break; |
|
177 |
} |
|
178 |
|
|
179 |
//1ms delay to safe cpu time |
|
180 |
usleep(1000); |
|
181 |
} |
|
182 |
|
|
183 |
//shutdown audio! |
|
184 |
ao_shutdown(); |
|
185 |
|
|
186 |
//done |
|
187 |
playback_state = CLOSED; |
|
188 |
|
|
114 |
void AudioPlayerLibAO::playback_thread() { |
|
115 |
printf("> playback thread started\n"); |
|
116 |
playback_state = IDLE; |
|
117 |
|
|
118 |
while (playback_state != EXITING) { |
|
119 |
switch (playback_state) { |
|
120 |
default: |
|
121 |
case (INITIALIZING): |
|
122 |
// waiting to be ready |
|
123 |
break; |
|
124 |
|
|
125 |
case (IDLE): |
|
126 |
// nothing to do, wait here until we are triggered to play! |
|
127 |
if (playback_requested) { |
|
128 |
printf("> AudioPlayer: loading\n"); |
|
129 |
playback_state = LOADING; |
|
130 |
} |
|
131 |
break; |
|
132 |
|
|
133 |
case (LOADING): |
|
134 |
// check if we can play that format: |
|
135 |
device = ao_open_live(driver, &audio_format, NULL /* no options */); |
|
136 |
|
|
137 |
if (device == NULL) { |
|
138 |
printf( |
|
139 |
"> AudioPlayer: WARNING: can not open requested audio device. trying to fall back to default driver\n"); |
|
140 |
driver = ao_default_driver_id(); |
|
141 |
device = ao_open_live(driver, &audio_format, NULL /* no options */); |
|
142 |
} |
|
143 |
|
|
144 |
if (device == NULL) { |
|
145 |
// failed to init device for format: |
|
146 |
printf("> AudioPlayer: WARNING: can not open audio device, not playing audio file!\n"); |
|
147 |
playback_active = false; |
|
148 |
playback_requested = false; |
|
149 |
playback_state = IDLE; |
|
150 |
break; |
|
151 |
} |
|
152 |
else { |
|
153 |
// ok we can now play the data |
|
154 |
printf("> AudioPlayer: playback started\n"); |
|
155 |
// mark request as beeing processed |
|
156 |
playback_requested = false; |
|
157 |
// enter actual playing state |
|
158 |
playback_state = PLAYING; |
|
159 |
playback_active = true; |
|
160 |
|
|
161 |
ao_play(device, audio_data->samples.data(), audio_data->samples.size()); |
|
162 |
printf("> AudioPlayer: finished playback\n"); |
|
163 |
} |
|
164 |
break; |
|
165 |
|
|
166 |
case (PLAYING): |
|
167 |
// when we reach this case, the ao_play() call finished |
|
168 |
//-> we can close this now: |
|
169 |
ao_close(device); |
|
170 |
playback_state = IDLE; |
|
171 |
playback_active = false; |
|
172 |
printf("> AudioPlayer: device closed\n"); |
|
173 |
break; |
|
174 |
|
|
175 |
case (ERROR): |
|
176 |
printf("> AudioPlayer ERROR! exiting\n"); |
|
177 |
exit(EXIT_FAILURE); |
|
178 |
break; |
|
179 |
} |
|
180 |
|
|
181 |
// 1ms delay to safe cpu time |
|
182 |
usleep(1000); |
|
183 |
} |
|
184 |
|
|
185 |
// shutdown audio! |
|
186 |
ao_shutdown(); |
|
187 |
|
|
188 |
// done |
|
189 |
playback_state = CLOSED; |
|
189 | 190 |
} |
190 | 191 |
|
191 |
AudioPlayerLibAO::~AudioPlayerLibAO(){ |
|
192 |
AudioPlayerLibAO::~AudioPlayerLibAO() {
|
|
192 | 193 |
} |
Also available in: Unified diff