2 * This file is part of DisOrder.
3 * Copyright (C) 2009 Richard Kettlewell
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.
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.
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/>.
19 /** @file lib/uaudio.c
20 * @brief Uniform audio interface
29 /** @brief Options for chosen uaudio API */
30 static hash *uaudio_options;
32 /** @brief Sample rate (Hz) */
35 /** @brief Bits per channel */
38 /** @brief Number of channels */
41 /** @brief Whether samples are signed or unsigned */
44 /** @brief Sample size in bytes
46 * NB one sample is a single point sample; up to @c uaudio_channels samples may
47 * play at the same time through different speakers. Thus this value is
48 * independent of @ref uaudio_channels.
50 size_t uaudio_sample_size;
52 /** @brief Set a uaudio option */
53 void uaudio_set(const char *name, const char *value) {
56 hash_remove(uaudio_options, name);
60 uaudio_options = hash_new(sizeof(char *));
61 value = xstrdup(value);
62 hash_add(uaudio_options, name, &value, HASH_INSERT_OR_REPLACE);
65 /** @brief Get a uaudio option */
66 char *uaudio_get(const char *name, const char *default_value) {
68 return default_value ? xstrdup(default_value) : 0;
69 char **valuep = hash_find(uaudio_options, name);
71 return default_value ? xstrdup(default_value) : 0;
72 return xstrdup(*valuep);
75 /** @brief Set sample format
76 * @param rate Sample rate in KHz
77 * @param channels Number of channels (i.e. 2 for stereo)
78 * @param bits Number of bits per channel (typically 8 or 16)
79 * @param signed_ True for signed samples, false for unsigned
81 * Sets @ref uaudio_rate, @ref uaudio_channels, @ref uaudio_bits, @ref
82 * uaudio_signed and @ref uaudio_sample_size.
84 * Currently there is no way to specify non-native endian formats even if the
85 * underlying API can conveniently handle them. Actually this would be quite
86 * convenient for playrtp, so it might be added at some point.
88 * Not all APIs can support all sample formats. Generally the @c start
89 * function will do some error checking but some may be deferred to the point
90 * the device is opened (which might be @c activate).
92 void uaudio_set_format(int rate, int channels, int bits, int signed_) {
94 uaudio_channels = channels;
96 uaudio_signed = signed_;
97 uaudio_sample_size = bits / CHAR_BIT;