1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/* WMix 3.0 -- a mixer using the OSS mixer API.
* Copyright (C) 2000, 2001
* Daniel Richard G. <skunk@mit.edu>,
* timecop <timecop@japan.co.jp>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include "include/common.h"
#include "include/misc.h"
#include "include/mixer.h"
#define WMVOLUME_CHANNEL_NAMES \
"Master volume", \
"Bass", \
"Treble", \
"FM Synth volume", \
"PCM Wave volume", \
"PC Speaker", \
"Line In level", \
"Microphone level", \
"CD volume", \
"Recording monitor", \
"PCM Wave 2 volume", \
"Recording volume", \
"Input gain", \
"Output gain", \
"Line In 1", \
"Line In 2", \
"Line In 3", \
"Digital In 1", \
"Digital In 2", \
"Digital In 3", \
"Phone input", \
"Phone output", \
"Video volume", \
"Radio volume", \
"Monitor volume"
#ifdef OSS_CHANNEL_NAMES
#define CHANNEL_NAMES SOUND_DEVICE_LABELS
#else
#define CHANNEL_NAMES WMVOLUME_CHANNEL_NAMES
#endif
typedef struct {
const char *name; /* name of channel */
const char *sname; /* short name of the channel */
int dev; /* channel device number */
int prev_dev_lr_volume; /* last known left/right volume
* (in device format) */
float volume; /* volume, in [0, 1] */
float balance; /* balance, in [-1, 1] */
bool can_record; /* capable of recording? */
bool is_recording; /* is it recording? */
bool is_stereo; /* capable of stereo? */
bool is_muted; /* is it muted? */
} MixerChannel;
static const char *channel_names[] = { CHANNEL_NAMES };
static const char *short_names[] = SOUND_DEVICE_LABELS;
static int mixer_fd;
static MixerChannel mixer[SOUND_MIXER_NRDEVICES];
static int n_channels = 0;
static int cur_channel = 0;
static int prev_modify_counter = -1;
static bool get_mixer_state(void)
{
struct mixer_info mix_info;
int dev_lr_volume, dev_left_volume, dev_right_volume;
float left, right;
int srcmask;
int ch;
/* to really keep track of updates */
static MixerChannel oldmixer[SOUND_MIXER_NRDEVICES];
ioctl(mixer_fd, SOUND_MIXER_INFO, &mix_info);
if (mix_info.modify_counter == prev_modify_counter)
/*
* Mixer state has not changed
*/
return false;
/* Mixer state was changed by another program, so we need
* to update. As OSS cannot tell us specifically which
* channels changed, we read all of them in.
*
* prev_modify_counter was initialized to -1, so this part
* is guaranteed to run the first time this routine is
* called.
*/
if (ioctl(mixer_fd, SOUND_MIXER_READ_RECSRC, &srcmask) == -1) {
fprintf(stderr, "mixer read failed\n");
perror(NULL);
exit(EXIT_FAILURE);
}
for (ch = 0; ch < n_channels; ch++) {
if (ioctl(mixer_fd, MIXER_READ(mixer[ch].dev), &dev_lr_volume) ==
-1) {
fprintf(stderr, "mixer read failed\n");
exit(EXIT_FAILURE);
}
if (dev_lr_volume != mixer[ch].prev_dev_lr_volume) {
dev_left_volume = dev_lr_volume & 0xFF;
dev_right_volume = dev_lr_volume >> 8;
if ((dev_left_volume > 0) || (dev_right_volume > 0))
mixer[ch].is_muted = false;
left = (float) dev_left_volume / 100.0;
right = (float) dev_right_volume / 100.0;
if (!mixer[ch].is_muted) {
if (mixer[ch].is_stereo)
lr_to_vb(left,
right, &mixer[ch].volume, &mixer[ch].balance);
else {
mixer[ch].volume = left;
mixer[ch].balance = 0.0;
}
mixer[ch].prev_dev_lr_volume = dev_lr_volume;
}
}
mixer[ch].is_recording = ((1 << mixer[ch].dev) & srcmask) != 0;
}
prev_modify_counter = mix_info.modify_counter;
/* check if this was due to OSS stupidity or if we really changed */
if (!memcmp(&mixer, &oldmixer, sizeof(mixer))) {
memcpy(&oldmixer, &mixer, sizeof(mixer));
return false;
}
memcpy(&oldmixer, &mixer, sizeof(mixer));
return true;
}
static void set_mixer_state(void)
{
float left, right;
int dev_left_volume, dev_right_volume, dev_lr_volume;
if (mixer[cur_channel].is_muted) {
left = 0.0;
right = 0.0;
} else
vb_to_lr(mixer[cur_channel].volume,
mixer[cur_channel].balance, &left, &right);
dev_left_volume = (int) (100.0 * left);
dev_right_volume = (int) (100.0 * right);
dev_lr_volume = (dev_right_volume << 8) | dev_left_volume;
ioctl(mixer_fd, MIXER_WRITE(mixer[cur_channel].dev), &dev_lr_volume);
}
static void get_record_state(void)
{
int srcmask;
int ch;
if (ioctl(mixer_fd, SOUND_MIXER_READ_RECSRC, &srcmask) == -1) {
fprintf(stderr, "mixer read failed\n");
perror(NULL);
exit(EXIT_FAILURE);
}
for (ch = 0; ch < n_channels; ch++) {
mixer[ch].is_recording = ((1 << mixer[ch].dev) & srcmask) != 0;
}
}
static void set_record_state(void)
{
int srcmask;
if (ioctl(mixer_fd, SOUND_MIXER_READ_RECSRC, &srcmask) == -1) {
fputs("error: recording source mask ioctl failed\n", stderr);
exit(EXIT_FAILURE);
}
if (((1 << mixer[cur_channel].dev) & srcmask) == 0)
srcmask |= (1 << mixer[cur_channel].dev);
else
srcmask &= ~(1 << mixer[cur_channel].dev);
if (ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC, &srcmask) == -1) {
fputs("error: recording source mask ioctl failed\n", stderr);
exit(EXIT_FAILURE);
}
}
void mixer_init(const char *mixer_device, bool verbose)
{
int devmask, srcmask, recmask, stmask;
struct mixer_info mix_info;
int count;
int mask;
mixer_fd = open(mixer_device, O_RDWR);
if (mixer_fd == -1) {
fprintf(stderr, "error: cannot open mixer device %s\n",
mixer_device);
exit(EXIT_FAILURE);
}
if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
fputs("error: device mask ioctl failed\n", stderr);
exit(EXIT_FAILURE);
}
if (ioctl(mixer_fd, SOUND_MIXER_READ_RECSRC, &srcmask) == -1) {
fputs("error: recording source mask ioctl failed\n", stderr);
exit(EXIT_FAILURE);
}
if (ioctl(mixer_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1) {
fputs("error: recording mask ioctl failed\n", stderr);
exit(EXIT_FAILURE);
}
if (ioctl(mixer_fd, SOUND_MIXER_READ_STEREODEVS, &stmask) == -1) {
fputs("error: stereo mask ioctl failed\n", stderr);
exit(EXIT_FAILURE);
}
if (ioctl(mixer_fd, SOUND_MIXER_INFO, &mix_info) == -1) {
fputs("error: could not read mixer info\n", stderr);
exit(EXIT_FAILURE);
}
if (verbose) {
printf("%s (%s)\n", mix_info.name, mix_info.id);
puts("Supported channels:");
}
for (count = 0; count < SOUND_MIXER_NRDEVICES; count++) {
mask = 1 << count;
if (mask & devmask) {
mixer[n_channels].name = channel_names[count];
mixer[n_channels].sname = short_names[count];
mixer[n_channels].dev = count;
mixer[n_channels].prev_dev_lr_volume = -1;
mixer[n_channels].can_record = (mask & recmask) != 0;
mixer[n_channels].is_recording = (mask & srcmask) != 0;
mixer[n_channels].is_stereo = (mask & stmask) != 0;
mixer[n_channels].is_muted = false;
++n_channels;
if (verbose)
printf(" %d: %s\n", n_channels, channel_names[count]);
}
}
get_mixer_state();
}
bool mixer_is_changed(void)
{
return get_mixer_state();
}
int mixer_get_channel_count(void)
{
return n_channels;
}
int mixer_get_channel(void)
{
return cur_channel;
}
const char *mixer_get_channel_name(void)
{
return mixer[cur_channel].name;
}
const char *mixer_get_short_name(void)
{
return mixer[cur_channel].sname;
}
void mixer_set_channel(int channel)
{
assert((channel >= 0) && (channel < n_channels));
cur_channel = channel;
get_record_state();
}
void mixer_set_channel_rel(int delta_channel)
{
cur_channel = (cur_channel + delta_channel) % n_channels;
if (cur_channel < 0)
cur_channel += n_channels;
get_record_state();
}
float mixer_get_volume(void)
{
get_mixer_state();
return mixer[cur_channel].volume;
}
void mixer_set_volume(float volume)
{
assert((volume >= 0.0) && (volume <= 1.0));
mixer[cur_channel].volume = volume;
set_mixer_state();
}
void mixer_set_volume_rel(float delta_volume)
{
mixer[cur_channel].volume += delta_volume;
mixer[cur_channel].volume = CLAMP(mixer[cur_channel].volume, 0.0, 1.0);
set_mixer_state();
}
float mixer_get_balance(void)
{
get_mixer_state();
return mixer[cur_channel].balance;
}
void mixer_set_balance(float balance)
{
assert((balance >= -1.0) && (balance <= 1.0));
if (mixer[cur_channel].is_stereo) {
mixer[cur_channel].balance = balance;
set_mixer_state();
}
}
void mixer_set_balance_rel(float delta_balance)
{
if (mixer[cur_channel].is_stereo) {
mixer[cur_channel].balance += delta_balance;
mixer[cur_channel].balance =
CLAMP(mixer[cur_channel].balance, -1.0, 1.0);
set_mixer_state();
}
}
void mixer_toggle_mute(void)
{
mixer[cur_channel].is_muted = !mixer[cur_channel].is_muted;
set_mixer_state();
}
void mixer_toggle_rec(void)
{
if (mixer[cur_channel].can_record) {
mixer[cur_channel].is_recording = !mixer[cur_channel].is_recording;
set_record_state();
get_record_state();
}
}
bool mixer_is_muted(void)
{
return mixer[cur_channel].is_muted;
}
bool mixer_is_stereo(void)
{
return mixer[cur_channel].is_stereo;
}
bool mixer_is_rec(void)
{
return mixer[cur_channel].is_recording;
}
bool mixer_can_rec(void)
{
return mixer[cur_channel].can_record;
}
|