stats: improve bit depth reporting [bug #267]
[sox.git] / src / ao.c
blob4791d4ed7f3181c3a00625d228a318d7bb99f6b0
1 /* libao player support for sox
2 * (c) Reuben Thomas <rrt@sc3d.org> 2007
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.
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.
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
19 #include "sox_i.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ao/ao.h>
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;
34 static int startwrite(sox_format_t * ft)
36 priv_t * ao = (priv_t *)ft->priv;
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);
42 if (!ao->buf)
44 lsx_fail_errno(ft, SOX_ENOMEM, "Can not allocate memory for ao driver");
45 return SOX_EOF;
49 ao_initialize();
50 if (strcmp(ft->filename,"default") == 0)
52 if ((ao->driver_id = ao_default_driver_id()) < 0) {
53 lsx_fail("Could not find a default ao driver");
54 return SOX_EOF;
57 else
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;
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;
74 return SOX_SUCCESS;
77 static void sox_sw_write_buf(char *buf1, sox_sample_t const * buf2, size_t len, sox_bool swap, sox_uint64_t * clips)
79 while (len--)
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++;
90 static size_t write_samples(sox_format_t *ft, const sox_sample_t *buf, size_t len)
92 priv_t * ao = (priv_t *)ft->priv;
93 uint_32 aobuf_size;
95 if (len > ao->buf_size / (ft->encoding.bits_per_sample >> 3))
96 len = ao->buf_size / (ft->encoding.bits_per_sample >> 3);
98 aobuf_size = (ft->encoding.bits_per_sample >> 3) * len;
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;
105 return len;
108 static int stopwrite(sox_format_t * ft)
110 priv_t * ao = (priv_t *)ft->priv;
112 free(ao->buf);
114 if (ao_close(ao->device) == 0) {
115 lsx_fail("Error closing libao output");
116 return SOX_EOF;
118 ao_shutdown();
120 return SOX_SUCCESS;
123 LSX_FORMAT_HANDLER(ao)
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)
133 return &handler;