chiark / gitweb /
Add xcalloc_noptr(), which allows uaudio-thread.c to ask for
[disorder] / lib / mixer.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
bd8895a8 3 * Copyright (C) 2007 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
b25aac59 18/** @file lib/mixer.c
19 * @brief Mixer support
20 */
460b9539 21
05b75f8d 22#include "common.h"
460b9539 23
460b9539 24#include "configuration.h"
25#include "mixer.h"
26#include "log.h"
b25aac59 27#include "mem.h"
460b9539 28
b25aac59 29/** @brief Whether lack of volume support has been reported yet */
30static int none_reported;
31
32/** @brief Get/set volume stub if volume control is not supported */
33static int none_get_set(int attribute((unused)) *left,
34 int attribute((unused)) *right) {
35 if(!none_reported) {
36 error(0, "don't know how to get/set volume with this api");
37 none_reported = 1;
38 }
39 return -1;
40}
41
42/** @brief Stub mixer control */
43static const struct mixer mixer_none = {
44 -1,
45 none_get_set,
46 none_get_set,
47 "",
48 ""
49};
50
51/** @brief Table of mixer definitions */
bd8895a8 52static const struct mixer *mixers[] = {
460b9539 53#if HAVE_SYS_SOUNDCARD_H
b25aac59 54 &mixer_oss,
55#endif
56#if HAVE_ALSA_ASOUNDLIB_H
57 &mixer_alsa,
460b9539 58#endif
b25aac59 59 &mixer_none /* make sure array is never empty */
bd8895a8 60};
460b9539 61
b25aac59 62/** @brief Number of mixer definitions */
bd8895a8 63#define NMIXERS (sizeof mixers / sizeof *mixers)
64
b25aac59 65/** @brief Find the mixer definition */
bd8895a8 66static const struct mixer *find_mixer(int api) {
67 size_t n;
68
3c499fe7
RK
69 if(api == -1)
70 api = config->api;
bd8895a8 71 for(n = 0; n < NMIXERS; ++n)
72 if(mixers[n]->api == api)
73 return mixers[n];
b25aac59 74 return &mixer_none;
460b9539 75}
76
3c499fe7
RK
77/** @brief Return true if we know how to drive the mixer
78 * @param api Sound api or -1 for default
79 * @return true if suppored, false otherwise
80 */
81int mixer_supported(int api) {
82 const struct mixer *const m = find_mixer(api);
83 return m != &mixer_none;
84}
85
b25aac59 86/** @brief Get/set volume
3c499fe7 87 * @param api Sound api or -1 for default
b25aac59 88 * @param left Left channel level, 0-100
89 * @param right Right channel level, 0-100
90 * @param set Set volume if non-0
91 * @return 0 on success, non-0 on error
92 *
93 * If getting the volume then @p left and @p right are filled in.
94 *
95 * If setting the volume then the target levels are read from @p left and
96 * @p right, and the actual level set is stored in them.
97 */
3c499fe7
RK
98int mixer_control(int api, int *left, int *right, int set) {
99 const struct mixer *const m = find_mixer(api);
bd8895a8 100
b25aac59 101 /* We impose defaults bizarrely late, but this has the advantage of
102 * not making everything depend on sound libraries */
103 if(!config->mixer)
104 config->mixer = xstrdup(m->device);
105 if(!config->channel)
106 config->channel = xstrdup(m->channel);
107 if(set)
108 return m->set(left, right);
109 else
110 return m->get(left, right);
460b9539 111}
460b9539 112
113/*
114Local Variables:
115c-basic-offset:2
116comment-column:40
117End:
118*/