1 /*
2  * libao output module for wavbreaker
3  * Copyright (C) 2015 Thomas Perl
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include <ao/ao.h>
24 
25 #include "wavbreaker.h"
26 #include "aoaudio.h"
27 
28 
29 static ao_device *device;
30 
ao_audio_close_device()31 void ao_audio_close_device()
32 {
33     if (device) {
34         ao_close(device);
35         device = NULL;
36 
37         ao_shutdown();
38     }
39 }
40 
ao_audio_write(unsigned char * devbuf,int size)41 int ao_audio_write(unsigned char *devbuf, int size)
42 {
43     if (device) {
44         if (ao_play(device, (char *)devbuf, size) == 0) {
45             fprintf(stderr, "Error in ao_play()\n");
46             return -1;
47         }
48         return 0;
49     }
50 
51     return -1;
52 }
53 
ao_audio_open_device(SampleInfo * sampleInfo)54 int ao_audio_open_device(SampleInfo *sampleInfo)
55 {
56     int default_driver;
57     ao_sample_format format;
58 
59     ao_initialize();
60 
61     default_driver = ao_default_driver_id();
62     memset(&format, 0, sizeof(format));
63     format.bits = sampleInfo->bitsPerSample;
64     format.channels = sampleInfo->channels;
65     format.rate = sampleInfo->samplesPerSec;
66     format.byte_format = AO_FMT_LITTLE;
67 
68     sampleInfo->bufferSize = DEFAULT_BUF_SIZE;
69 
70     device = ao_open_live(default_driver, &format, NULL);
71 
72     if (device == NULL) {
73         fprintf(stderr, "Cannot open default libao device\n");
74         return -1;
75     }
76 
77     return 0;
78 }
79