chiark / gitweb /
Remove some redundant includes.
[disorder] / lib / uaudio.c
CommitLineData
7a2c7068
RK
1/*
2 * This file is part of DisOrder.
5db8461a 3 * Copyright (C) 2009, 2013 Richard Kettlewell
7a2c7068
RK
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.
06385470 14 *
7a2c7068
RK
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"
76e72f65 27#include "log.h"
7a2c7068
RK
28
29/** @brief Options for chosen uaudio API */
30static hash *uaudio_options;
31
4fd38868
RK
32/** @brief Sample rate (Hz) */
33int uaudio_rate;
34
35/** @brief Bits per channel */
36int uaudio_bits;
37
38/** @brief Number of channels */
39int uaudio_channels;
40
41/** @brief Whether samples are signed or unsigned */
42int uaudio_signed;
43
5db8461a
RK
44/** @brief Frames of buffer to tolerate inside chosen API */
45int uaudio_buffer;
46
4fd38868
RK
47/** @brief Sample size in bytes
48 *
49 * NB one sample is a single point sample; up to @c uaudio_channels samples may
50 * play at the same time through different speakers. Thus this value is
51 * independent of @ref uaudio_channels.
52 */
53size_t uaudio_sample_size;
54
7a2c7068
RK
55/** @brief Set a uaudio option */
56void uaudio_set(const char *name, const char *value) {
ba70caca
RK
57 if(!value) {
58 if(uaudio_options)
59 hash_remove(uaudio_options, name);
60 return;
61 }
7a2c7068
RK
62 if(!uaudio_options)
63 uaudio_options = hash_new(sizeof(char *));
64 value = xstrdup(value);
65 hash_add(uaudio_options, name, &value, HASH_INSERT_OR_REPLACE);
66}
67
b50cfb8a
RK
68/** @brief Get a uaudio option */
69char *uaudio_get(const char *name, const char *default_value) {
ba70caca
RK
70 if(!uaudio_options)
71 return default_value ? xstrdup(default_value) : 0;
72 char **valuep = hash_find(uaudio_options, name);
73 if(!valuep)
74 return default_value ? xstrdup(default_value) : 0;
75 return xstrdup(*valuep);
7a2c7068
RK
76}
77
06385470 78/** @brief Set sample format
4fd38868
RK
79 * @param rate Sample rate in KHz
80 * @param channels Number of channels (i.e. 2 for stereo)
81 * @param bits Number of bits per channel (typically 8 or 16)
82 * @param signed_ True for signed samples, false for unsigned
83 *
84 * Sets @ref uaudio_rate, @ref uaudio_channels, @ref uaudio_bits, @ref
85 * uaudio_signed and @ref uaudio_sample_size.
86 *
87 * Currently there is no way to specify non-native endian formats even if the
88 * underlying API can conveniently handle them. Actually this would be quite
89 * convenient for playrtp, so it might be added at some point.
90 *
91 * Not all APIs can support all sample formats. Generally the @c start
92 * function will do some error checking but some may be deferred to the point
93 * the device is opened (which might be @c activate).
94 */
95void uaudio_set_format(int rate, int channels, int bits, int signed_) {
96 uaudio_rate = rate;
97 uaudio_channels = channels;
98 uaudio_bits = bits;
99 uaudio_signed = signed_;
100 uaudio_sample_size = bits / CHAR_BIT;
101}
102
06385470
RK
103/** @brief Choose the default audio API by context
104 * @param apis Table of APIs or a null pointer
105 * @param context @ref UAUDIO_API_SERVER or @ref UAUDIO_API_CLIENT
106 * @return Default API or a null pointer
107 */
108const struct uaudio *uaudio_default(const struct uaudio *const *apis,
109 unsigned context) {
110 if(apis) {
111 for(int n = 0; apis[n]; ++n)
112 if(apis[n]->flags & context)
113 return apis[n];
114 }
115 return 0;
116}
117
7a2c7068
RK
118/*
119Local Variables:
120c-basic-offset:2
121comment-column:40
122fill-column:79
123indent-tabs-mode:nil
124End:
125*/