chiark / gitweb /
Merge from uniform audio branch. disorder-playrtp now uses the uaudio
[disorder] / lib / uaudio.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2009 Richard Kettlewell
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 /** @file lib/uaudio.c
20  * @brief Uniform audio interface
21  */
22
23 #include "common.h"
24 #include "uaudio.h"
25 #include "hash.h"
26 #include "mem.h"
27
28 /** @brief Options for chosen uaudio API */
29 static hash *uaudio_options;
30
31 /** @brief Sample rate (Hz) */
32 int uaudio_rate;
33
34 /** @brief Bits per channel */
35 int uaudio_bits;
36
37 /** @brief Number of channels */
38 int uaudio_channels;
39
40 /** @brief Whether samples are signed or unsigned */
41 int uaudio_signed;
42
43 /** @brief Sample size in bytes
44  *
45  * NB one sample is a single point sample; up to @c uaudio_channels samples may
46  * play at the same time through different speakers.  Thus this value is
47  * independent of @ref uaudio_channels.
48  */
49 size_t uaudio_sample_size;
50
51 /** @brief Set a uaudio option */
52 void uaudio_set(const char *name, const char *value) {
53   if(!uaudio_options)
54     uaudio_options = hash_new(sizeof(char *));
55   value = xstrdup(value);
56   hash_add(uaudio_options, name, &value, HASH_INSERT_OR_REPLACE);
57 }
58
59 /** @brief Set a uaudio option */
60 const char *uaudio_get(const char *name) {
61   const char *value = (uaudio_options ?
62                        *(char **)hash_find(uaudio_options, name)
63                        : NULL);
64   return value ? xstrdup(value) : NULL;
65 }
66
67 /** @brief Set sample format 
68  * @param rate Sample rate in KHz
69  * @param channels Number of channels (i.e. 2 for stereo)
70  * @param bits Number of bits per channel (typically 8 or 16)
71  * @param signed_ True for signed samples, false for unsigned
72  *
73  * Sets @ref uaudio_rate, @ref uaudio_channels, @ref uaudio_bits, @ref
74  * uaudio_signed and @ref uaudio_sample_size.
75  *
76  * Currently there is no way to specify non-native endian formats even if the
77  * underlying API can conveniently handle them.  Actually this would be quite
78  * convenient for playrtp, so it might be added at some point.
79  *
80  * Not all APIs can support all sample formats.  Generally the @c start
81  * function will do some error checking but some may be deferred to the point
82  * the device is opened (which might be @c activate).
83  */
84 void uaudio_set_format(int rate, int channels, int bits, int signed_) {
85   uaudio_rate = rate;
86   uaudio_channels = channels;
87   uaudio_bits = bits;
88   uaudio_signed = signed_;
89   uaudio_sample_size = bits / CHAR_BIT;
90 }
91
92 /** @brief List of known APIs
93  *
94  * Terminated by a null pointer.
95  *
96  * The first one will be used as a default, so putting ALSA before OSS
97  * constitutes a policy decision.
98  */
99 const struct uaudio *uaudio_apis[] = {
100 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
101   &uaudio_coreaudio,
102 #endif  
103 #if HAVE_ALSA_ASOUNDLIB_H
104   &uaudio_alsa,
105 #endif
106 #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST
107   &uaudio_oss,
108 #endif
109   &uaudio_rtp,
110   NULL,
111 };
112
113 /*
114 Local Variables:
115 c-basic-offset:2
116 comment-column:40
117 fill-column:79
118 indent-tabs-mode:nil
119 End:
120 */