1 /* libao player support for sox
2  * (c) Reuben Thomas <rrt@sc3d.org> 2007
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or (at
7  * your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include "sox_i.h"
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ao/ao.h>
25 
26 typedef struct {
27   int driver_id;
28   ao_device *device;
29   ao_sample_format format;
30   char *buf;
31   size_t buf_size;
32 } priv_t;
33 
startwrite(sox_format_t * ft)34 static int startwrite(sox_format_t * ft)
35 {
36   priv_t * ao = (priv_t *)ft->priv;
37 
38   ao->buf_size = sox_globals.bufsiz - (sox_globals.bufsiz % (ft->encoding.bits_per_sample >> 3));
39   ao->buf_size *= (ft->encoding.bits_per_sample >> 3);
40   ao->buf = lsx_malloc(ao->buf_size);
41 
42   if (!ao->buf)
43   {
44       lsx_fail_errno(ft, SOX_ENOMEM, "Can not allocate memory for ao driver");
45       return SOX_EOF;
46   }
47 
48 
49   ao_initialize();
50   if (strcmp(ft->filename,"default") == 0)
51   {
52       if ((ao->driver_id = ao_default_driver_id()) < 0) {
53           lsx_fail("Could not find a default ao driver");
54           return SOX_EOF;
55       }
56   }
57   else
58   {
59       if ((ao->driver_id = ao_driver_id(ft->filename)) < 0) {
60           lsx_fail("Could not find a ao driver %s", ft->filename);
61           return SOX_EOF;
62       }
63   }
64 
65   ao->format.bits = ft->encoding.bits_per_sample;
66   ao->format.rate = ft->signal.rate;
67   ao->format.channels = ft->signal.channels;
68   ao->format.byte_format = AO_FMT_NATIVE;
69   if ((ao->device = ao_open_live(ao->driver_id, &ao->format, NULL)) == NULL) {
70     lsx_fail("Could not open device: error %d", errno);
71     return SOX_EOF;
72   }
73 
74   return SOX_SUCCESS;
75 }
76 
sox_sw_write_buf(char * buf1,sox_sample_t const * buf2,size_t len,sox_bool swap,sox_uint64_t * clips)77 static void sox_sw_write_buf(char *buf1, sox_sample_t const * buf2, size_t len, sox_bool swap, sox_uint64_t * clips)
78 {
79     while (len--)
80     {
81         SOX_SAMPLE_LOCALS;
82         uint16_t datum = SOX_SAMPLE_TO_SIGNED_16BIT(*buf2++, *clips);
83         if (swap)
84             datum = lsx_swapw(datum);
85         *(uint16_t *)buf1 = datum;
86         buf1++; buf1++;
87     }
88 }
89 
write_samples(sox_format_t * ft,const sox_sample_t * buf,size_t len)90 static size_t write_samples(sox_format_t *ft, const sox_sample_t *buf, size_t len)
91 {
92   priv_t * ao = (priv_t *)ft->priv;
93   uint_32 aobuf_size;
94 
95   if (len > ao->buf_size / (ft->encoding.bits_per_sample >> 3))
96       len = ao->buf_size / (ft->encoding.bits_per_sample >> 3);
97 
98   aobuf_size = (ft->encoding.bits_per_sample >> 3) * len;
99 
100   sox_sw_write_buf(ao->buf, buf, len, ft->encoding.reverse_bytes,
101                    &(ft->clips));
102   if (ao_play(ao->device, (void *)ao->buf, aobuf_size) == 0)
103     return 0;
104 
105   return len;
106 }
107 
stopwrite(sox_format_t * ft)108 static int stopwrite(sox_format_t * ft)
109 {
110   priv_t * ao = (priv_t *)ft->priv;
111 
112   free(ao->buf);
113 
114   if (ao_close(ao->device) == 0) {
115     lsx_fail("Error closing libao output");
116     return SOX_EOF;
117   }
118   ao_shutdown();
119 
120   return SOX_SUCCESS;
121 }
122 
LSX_FORMAT_HANDLER(ao)123 LSX_FORMAT_HANDLER(ao)
124 {
125   static char const * const names[] = {"ao", NULL};
126   static unsigned const encodings[] = {SOX_ENCODING_SIGN2, 16, 0, 0};
127   static sox_format_handler_t const handler = {SOX_LIB_VERSION_CODE,
128     "Xiph's libao device driver", names, SOX_FILE_DEVICE | SOX_FILE_NOSTDIO,
129     NULL, NULL, NULL,
130     startwrite, write_samples, stopwrite,
131     NULL, encodings, NULL, sizeof(priv_t)
132   };
133   return &handler;
134 }
135