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
29 ao_sample_format format
;
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
);
44 lsx_fail_errno(ft
, SOX_ENOMEM
, "Can not allocate memory for ao driver");
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");
59 if ((ao
->driver_id
= ao_driver_id(ft
->filename
)) < 0) {
60 lsx_fail("Could not find a ao driver %s", ft
->filename
);
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
);
77 static void sox_sw_write_buf(char *buf1
, sox_sample_t
const * buf2
, size_t len
, sox_bool swap
, sox_uint64_t
* clips
)
82 uint16_t datum
= SOX_SAMPLE_TO_SIGNED_16BIT(*buf2
++, *clips
);
84 datum
= lsx_swapw(datum
);
85 *(uint16_t *)buf1
= datum
;
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
;
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
,
102 if (ao_play(ao
->device
, (void *)ao
->buf
, aobuf_size
) == 0)
108 static int stopwrite(sox_format_t
* ft
)
110 priv_t
* ao
= (priv_t
*)ft
->priv
;
114 if (ao_close(ao
->device
) == 0) {
115 lsx_fail("Error closing libao output");
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
,
130 startwrite
, write_samples
, stopwrite
,
131 NULL
, encodings
, NULL
, sizeof(priv_t
)