Tscope5
audio_internal.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file audio_internal.c
11 /// Definitions of internal audio functions.
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 
15 #include "../include/tscope5/audio_internal.h"
16 #include "../include/tscope5/timer_internal.h"
17 #include "../include/tscope5/system_internal.h"
18 
19 #include <allegro5/allegro_acodec.h>
20 
21 /// Is the audio subsystem installed?
23 
24 /// Is the audio recorder subsystem installed?
26 
27 /// Is the audio recorder recording?
29 
30 /// Is the voicekey subsystem installed?
32 
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Do some checks at the start of each audio function.
36 ///
37 /// \param calling_function Name the function that calls for
38 /// this check or installation.
39 ///
40 /// Checks whether the audio subsystem is installed.
41 /// If not, the audio subsystem is installed.
42 ////////////////////////////////////////////////////////////////////////////////
43 void ts5_check_audio(char *calling_function)
44 {
45  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_audio\n", calling_function);
47  ts5_install_audio(calling_function);
48  }
49 }
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Install the audio subsystem.
54 ///
55 /// \param calling_function Name the function that calls for
56 /// this check or installation.
57 ///
58 /// This function is called automatically if necessary.
59 ////////////////////////////////////////////////////////////////////////////////
60 void ts5_install_audio(char *calling_function)
61 {
63  ts5_install_tscope5(calling_function);
64  }
65 
67 
69  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 audio\n",
70  calling_function);
71 
72  if (!al_install_audio()) {
73  ts5_fatal("%s: could not install Tscope5 audio\n",
74  calling_function);
75  }
76 
77  if (!al_init_acodec_addon()) {
78  ts5_fatal("%s: could not install Tscope5 audio codec addon\n",
79  calling_function);
80  }
81 
82  ALLEGRO_CHANNEL_CONF channels = 0;
83 
84  if (_ts5_status.audio.channels==TS5_MONO) {
85  channels=ALLEGRO_CHANNEL_CONF_1;
86  }
87  else if (_ts5_status.audio.channels==TS5_STEREO) {
88  channels=ALLEGRO_CHANNEL_CONF_2;
89  }
90  else {
91  ts5_fatal("%s: unknown channel configuration requested\n",
92  calling_function);
93  }
94 
95  /*
96  ALLEGRO_AUDIO_DEPTH depth;
97  if (_ts5_status.audio.depth==TS5_INTEGER) {
98  depth=ALLEGRO_AUDIO_DEPTH_INT16;
99  }
100  else if (_ts5_status.audio.depth==TS5_FLOAT) {
101  depth=ALLEGRO_AUDIO_DEPTH_FLOAT32;
102  }
103  else {
104  ts5_fatal("ts5_install_audio: unknown sample depth requested\n");
105  }
106  */
107 
108  _ts5_data.audio.voice = al_create_voice(_ts5_status.audio.samplerate,
109  ALLEGRO_AUDIO_DEPTH_INT16, channels);
110 
111  if (!_ts5_data.audio.voice) {
112  ts5_fatal("%s: could not create voice\n", calling_function);
113  }
114 
115  _ts5_data.audio.mixer = al_create_mixer(_ts5_status.audio.samplerate,
116  ALLEGRO_AUDIO_DEPTH_FLOAT32, channels);
117 
118  if (!_ts5_data.audio.mixer) {
119  ts5_fatal("%s: could not create mixer\n", calling_function);
120  }
121 
122  if (!al_attach_mixer_to_voice(_ts5_data.audio.mixer,
123  _ts5_data.audio.voice)) {
124  ts5_fatal("%s: could not attach mixer to voice\n",
125  calling_function);
126  }
127 
128  atexit(ts5_uninstall_audio);
129  }
130 }
131 
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Uninstall the audio subsystem.
135 ///
136 /// This function is called automatically at the end of the program.
137 ////////////////////////////////////////////////////////////////////////////////
139 {
141 
142  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 audio\n");
143 
144  //al_detach_mixer(_ts5_data.audio.mixer);
145  //al_detach_voice(_ts5_data.audio.voice);
146 
147  //al_destroy_mixer(_ts5_data.audio.mixer);
148  //al_destroy_voice(_ts5_data.audio.voice);
149 
150  _ts5_data.audio.mixer = NULL;
151  _ts5_data.audio.voice = NULL;
152 
153  //al_uninstall_audio();
154 
156  }
157 }
158 
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// Do some checks at the start of each audio recorder function.
162 ///
163 /// \param calling_function Name the function that calls for
164 /// this check or installation.
165 ///
166 /// Checks whether the audio recorder subsystem is installed.
167 /// If not, the function audio recorder subsystem is installed.
168 ////////////////////////////////////////////////////////////////////////////////
169 void ts5_check_audio_recorder(char *calling_function)
170 {
171  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_audio_recorder\n", calling_function);
173  ts5_install_audio_recorder(calling_function);
174  }
175 }
176 
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Do some checks at the start of each audio recorder function.
180 ///
181 /// \param calling_function Name the function that calls for
182 /// this check or installation.
183 ///
184 /// Checks whether the audio recorder subsystem is recording.
185 ///
186 /// Aborts if necessary.
187 ////////////////////////////////////////////////////////////////////////////////
188 void ts5_check_audio_recorder2(char *calling_function)
189 {
190  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_audio_recorder2\n", calling_function);
192  ts5_fatal("%s: %s %s\n", calling_function,
193  "audio recorder not running,",
194  "add a call to ts5_start_audio_recorder()");
195  }
196 }
197 
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Do some checks at the start of each audio recorder function.
201 ///
202 /// \param calling_function Name the function that calls for
203 /// this check or installation.
204 ///
205 /// \param sample The sample that has to be checked.
206 ///
207 /// Checks whether the audio sample is a recording buffer allocated by
208 /// ts5_alloc_sample.
209 ///
210 /// Aborts if necessary.
211 ////////////////////////////////////////////////////////////////////////////////
212 void ts5_check_audio_recorder3(char *calling_function, TS5_SAMPLE *sample)
213 {
214  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_audio_recorder3(%p)\n",
215  calling_function, sample);
216 
217  if (!sample) {
218  ts5_fatal("%s: sample pointer is null\n", calling_function);
219  }
220 
221  if (!sample->isbuffer) {
222  ts5_fatal("%s: %s %p %s\n", calling_function,
223  "audio sample", sample,
224  "is not a recording buffer");
225  }
226 }
227 
228 
229 ////////////////////////////////////////////////////////////////////////////////
230 /// Install the audio recorder subsystem.
231 ///
232 /// \param calling_function Name the function that calls for
233 /// this check or installation.
234 ///
235 /// This function is called automatically if necessary.
236 ////////////////////////////////////////////////////////////////////////////////
237 void ts5_install_audio_recorder(char *calling_function)
238 {
240  ts5_install_tscope5(calling_function);
241  }
242 
244  ts5_install_audio(calling_function);
245  }
246 
248 
250 
251  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 audio recorder\n",
252  calling_function);
253 
254  // create a recorder with a buffer of 10.0 seconds
255  unsigned int samplerate = _ts5_status.audio.samplerate;
256  unsigned int samples = 4094;
257  size_t fragment_count = 10.0 * samplerate / samples;
258 
259  _ts5_data.audio.recorder =
260  al_create_audio_recorder(fragment_count,
261  samples,
262  samplerate,
263  ALLEGRO_AUDIO_DEPTH_INT16,
264  _ts5_status.audio.channels);
265 
266  if(!_ts5_data.audio.recorder) {
267  ts5_fatal("%s: could create audio recorder\n",
268  calling_function);
269  }
270 
271  // event queue for the buffer fragments
272  _ts5_data.audio.recorder_queue = al_create_event_queue();
273  if(!_ts5_data.audio.recorder_queue) {
274  ts5_fatal("%s: could not create audio recorder event cue\n",
275  calling_function);
276  }
277  al_register_event_source(_ts5_data.audio.recorder_queue,
278  al_get_audio_recorder_event_source(_ts5_data.audio.recorder));
279 
281  }
282 }
283 
284 
285 ////////////////////////////////////////////////////////////////////////////////
286 /// Uninstall the audio recoder subsystem.
287 ///
288 /// This function is called automatically at the end of the program.
289 ////////////////////////////////////////////////////////////////////////////////
291 {
293 
294  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 audio recorder\n");
295 
296  al_unregister_event_source(_ts5_data.audio.recorder_queue,
297  al_get_audio_recorder_event_source(_ts5_data.audio.recorder));
298 
299  al_destroy_event_queue(_ts5_data.audio.recorder_queue);
300 
301  al_destroy_audio_recorder(_ts5_data.audio.recorder);
302 
303  // al_fclose(_ts5_data.audio.fp);
304 
306  }
307 }
308 
309 
310 ////////////////////////////////////////////////////////////////////////////////
311 /// Do some checks at the start of each voicekey function.
312 ///
313 /// \param calling_function Name the function that calls for
314 /// this check or installation.
315 ///
316 /// Checks whether the voicekey subsystem is installed.
317 /// If not, the voicekey subsystem is installed.
318 ////////////////////////////////////////////////////////////////////////////////
319 void ts5_check_voicekey(char *calling_function)
320 {
321  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_voicekey\n", calling_function);
323  ts5_install_voicekey(calling_function);
324  }
325 }
326 
327 
328 ////////////////////////////////////////////////////////////////////////////////
329 /// Install the voicekey subsystem.
330 ///
331 /// \param calling_function Name the function that calls for
332 /// this check or installation.
333 ///
334 /// This function is called automatically if necessary.
335 ////////////////////////////////////////////////////////////////////////////////
336 void ts5_install_voicekey(char *calling_function)
337 {
339  ts5_install_tscope5(calling_function);
340  }
341 
343  ts5_install_timer(calling_function);
344  }
345 
347  ts5_install_audio(calling_function);
348  }
349 
351  ts5_install_audio_recorder(calling_function);
352  }
353 
355 
357 
358  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 voice key\n",
359  calling_function);
360 
361  // initialize event source
362  al_init_user_event_source(
363  &_ts5_data.timer.voicekey_response_event_source);
364 
365  // register event source
366  al_register_event_source(_ts5_data.timer.response_queue,
367  &_ts5_data.timer.voicekey_response_event_source);
368 
369  _ts5_status.timer.voicekey.num_buttons = 1;
370  _ts5_status.timer.voicekey.num_defined_buttons = 0;
371  _ts5_status.timer.voicekey.num_active_buttons = 0;
372 
373  _ts5_status.timer.voicekey.treshold = 0.5;
374  _ts5_status.timer.voicekey.rise_delay = 0.2;
375  _ts5_status.timer.voicekey.drop_delay = 0.2;
376 
377  _ts5_status.timer.voicekey.button_press_defined =
378  (int *)al_malloc(sizeof(int)
379  * _ts5_status.timer.voicekey.num_buttons);
380 
381  _ts5_status.timer.voicekey.button_press_active =
382  (int *)al_malloc(sizeof(int)
383  * _ts5_status.timer.voicekey.num_buttons);
384 
385  _ts5_status.timer.voicekey.button_release_defined =
386  (int *)al_malloc(sizeof(int)
387  * _ts5_status.timer.voicekey.num_buttons);
388 
389  _ts5_status.timer.voicekey.button_release_active =
390  (int *)al_malloc(sizeof(int)
391  * _ts5_status.timer.voicekey.num_buttons);
392 
393  int i;
394  for (i=0; i<_ts5_status.timer.voicekey.num_buttons; i++) {
395  _ts5_status.timer.voicekey.button_press_defined[i] = 0;
396  _ts5_status.timer.voicekey.button_press_active[i] = 0;
397  _ts5_status.timer.voicekey.button_release_defined[i] = 0;
398  _ts5_status.timer.voicekey.button_release_active[i] = 0;
399  }
400 
401  atexit(ts5_uninstall_voicekey);
402  }
403 }
404 
405 
406 ////////////////////////////////////////////////////////////////////////////////
407 /// Uninstall the voicekey subsystem.
408 ///
409 /// This function is called automatically at the end of the program.
410 ////////////////////////////////////////////////////////////////////////////////
412 {
414 
415  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 voice key\n");
416 
417  _ts5_status.timer.voicekey.num_buttons = 0;
418  _ts5_status.timer.voicekey.num_defined_buttons = 0;
419  _ts5_status.timer.voicekey.num_active_buttons = 0;
420 
421  al_free(_ts5_status.timer.voicekey.button_press_defined);
422  _ts5_status.timer.voicekey.button_press_defined = NULL;
423 
424  al_free(_ts5_status.timer.voicekey.button_press_active);
425  _ts5_status.timer.voicekey.button_press_active = NULL;
426 
427  al_free(_ts5_status.timer.voicekey.button_release_defined);
428  _ts5_status.timer.voicekey.button_release_defined = NULL;
429 
430  al_free(_ts5_status.timer.voicekey.button_release_active);
431  _ts5_status.timer.voicekey.button_release_active = NULL;
432 
433  _ts5_status.timer.voicekey_is_response_device = 0;
434 
435  // unregister event source
436  al_unregister_event_source(_ts5_data.timer.response_queue,
437  &_ts5_data.timer.voicekey_response_event_source);
438 
439  // remove event source
440  al_destroy_user_event_source(
441  &_ts5_data.timer.voicekey_response_event_source);
442 
444  }
445 }
446 
447 
448 
449 
450 
int _ts5_is_tscope5_installed
Is Tscope5 installed?
int _ts5_is_timer_installed
Is the timer subsystem installed?
int _ts5_is_audio_installed
Is the audio subsystem installed?
void ts5_uninstall_audio_recorder()
Uninstall the audio recoder subsystem.
int _ts5_is_audio_recorder_recording
Is the audio recorder recording?
void ts5_check_audio_recorder(char *calling_function)
Do some checks at the start of each audio recorder function.
int _ts5_is_audio_recorder_installed
Is the audio recorder subsystem installed?
void ts5_install_voicekey(char *calling_function)
Install the voicekey subsystem.
void ts5_install_timer(char *calling_function)
Install the timer subsystem.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
void ts5_install_tscope5(char *calling_function)
Install Tscope5.
void ts5_install_audio_recorder(char *calling_function)
Install the audio recorder subsystem.
int _ts5_is_voicekey_installed
Is the voicekey subsystem installed?
void ts5_check_voicekey(char *calling_function)
Do some checks at the start of each voicekey function.
void ts5_check_audio_recorder2(char *calling_function)
Do some checks at the start of each audio recorder function.
void ts5_install_audio(char *calling_function)
Install the audio subsystem.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533
void ts5_check_audio_recorder3(char *calling_function, TS5_SAMPLE *sample)
Do some checks at the start of each audio recorder function.
void ts5_uninstall_audio()
Uninstall the audio subsystem.
void ts5_uninstall_voicekey()
Uninstall the voicekey subsystem.
void ts5_check_audio(char *calling_function)
Do some checks at the start of each audio function.