2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "audio.h"
|
2008-09-07 19:19:55 +02:00
|
|
|
#include "audio_format.h"
|
2008-09-07 22:41:22 +02:00
|
|
|
#include "output_api.h"
|
2008-09-09 10:02:34 +02:00
|
|
|
#include "output_control.h"
|
2008-09-24 07:20:55 +02:00
|
|
|
#include "output_internal.h"
|
2009-02-10 18:51:49 +01:00
|
|
|
#include "output_all.h"
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "path.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2009-01-04 17:35:51 +01:00
|
|
|
#include "mixer_api.h"
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2008-10-28 20:33:56 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2009-01-03 13:36:20 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2008-10-12 01:00:00 +02:00
|
|
|
static struct audio_format configured_audio_format;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
void getOutputAudioFormat(const struct audio_format *inAudioFormat,
|
|
|
|
struct audio_format *outAudioFormat)
|
2004-05-10 04:20:15 +02:00
|
|
|
{
|
2008-10-12 01:00:00 +02:00
|
|
|
*outAudioFormat = audio_format_defined(&configured_audio_format)
|
|
|
|
? configured_audio_format
|
2008-09-09 10:04:42 +02:00
|
|
|
: *inAudioFormat;
|
2004-05-10 04:20:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initAudioConfig(void)
|
|
|
|
{
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param = config_get_param(CONF_AUDIO_OUTPUT_FORMAT);
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (NULL == param || NULL == param->value)
|
|
|
|
return;
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2008-10-12 01:00:00 +02:00
|
|
|
if (0 != parseAudioConfig(&configured_audio_format, param->value)) {
|
2008-11-25 17:47:46 +01:00
|
|
|
g_error("error parsing \"%s\" at line %i\n",
|
|
|
|
CONF_AUDIO_OUTPUT_FORMAT, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-10-23 03:04:58 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
int parseAudioConfig(struct audio_format *audioFormat, char *conf)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
char *test;
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
memset(audioFormat, 0, sizeof(*audioFormat));
|
2004-10-23 03:04:58 +02:00
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
audioFormat->sample_rate = strtol(conf, &test, 10);
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != ':') {
|
2008-11-25 17:47:46 +01:00
|
|
|
g_warning("error parsing audio output format: %s\n", conf);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
if (audioFormat->sample_rate <= 0) {
|
2008-11-25 17:47:46 +01:00
|
|
|
g_warning("sample rate %u is not >= 0\n",
|
|
|
|
audioFormat->sample_rate);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-10 14:03:33 +02:00
|
|
|
audioFormat->bits = (uint8_t)strtoul(test + 1, &test, 10);
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != ':') {
|
2008-11-25 17:47:46 +01:00
|
|
|
g_warning("error parsing audio output format: %s\n", conf);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-23 16:57:58 +02:00
|
|
|
if (audioFormat->bits != 16 && audioFormat->bits != 24 &&
|
|
|
|
audioFormat->bits != 8) {
|
2008-11-25 17:47:46 +01:00
|
|
|
g_warning("bits %u can not be used for audio output\n",
|
|
|
|
audioFormat->bits);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2008-10-10 14:03:33 +02:00
|
|
|
audioFormat->channels = (uint8_t)strtoul(test + 1, &test, 10);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (*test != '\0') {
|
2008-11-25 17:47:46 +01:00
|
|
|
g_warning("error parsing audio output format: %s\n", conf);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (audioFormat->channels) {
|
2004-10-23 03:04:58 +02:00
|
|
|
case 1:
|
2006-07-20 18:02:40 +02:00
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
2008-11-25 17:47:46 +01:00
|
|
|
g_warning("channels %u can not be used for audio output\n",
|
|
|
|
audioFormat->channels);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-10-23 03:04:58 +02:00
|
|
|
|
|
|
|
return 0;
|
2004-05-10 04:20:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishAudioConfig(void)
|
|
|
|
{
|
2008-10-12 01:00:00 +02:00
|
|
|
audio_format_clear(&configured_audio_format);
|
2004-05-10 04:20:15 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:51:50 +02:00
|
|
|
int enableAudioDevice(unsigned int device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-10 18:51:49 +01:00
|
|
|
struct audio_output *ao;
|
|
|
|
|
|
|
|
if (device >= audio_output_count())
|
2004-11-02 22:06:44 +01:00
|
|
|
return -1;
|
|
|
|
|
2009-02-10 18:51:49 +01:00
|
|
|
ao = audio_output_get(device);
|
|
|
|
|
|
|
|
ao->reopen_after = 0;
|
|
|
|
ao->enabled = true;
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_OUTPUT);
|
2004-11-02 22:06:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:51:50 +02:00
|
|
|
int disableAudioDevice(unsigned int device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-10 18:51:49 +01:00
|
|
|
struct audio_output *ao;
|
|
|
|
|
|
|
|
if (device >= audio_output_count())
|
2004-11-02 22:06:44 +01:00
|
|
|
return -1;
|
2008-09-07 13:51:50 +02:00
|
|
|
|
2009-02-10 18:51:49 +01:00
|
|
|
ao = audio_output_get(device);
|
|
|
|
|
|
|
|
ao->enabled = false;
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_OUTPUT);
|
2004-11-02 22:06:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-03 00:08:00 +01:00
|
|
|
|
2008-12-31 16:46:41 +01:00
|
|
|
bool mixer_control_setvol(unsigned int device, int volume, int rel)
|
|
|
|
{
|
|
|
|
struct audio_output *output;
|
2009-02-10 18:51:49 +01:00
|
|
|
|
|
|
|
if (device >= audio_output_count())
|
2008-12-31 16:46:41 +01:00
|
|
|
return false;
|
|
|
|
|
2009-02-10 18:51:49 +01:00
|
|
|
output = audio_output_get(device);
|
2008-12-31 16:46:41 +01:00
|
|
|
if (output->plugin && output->plugin->control) {
|
|
|
|
if (rel) {
|
|
|
|
int cur_volume;
|
|
|
|
if (!output->plugin->control(output->data, AC_MIXER_GETVOL, &cur_volume)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
volume = volume + cur_volume;
|
|
|
|
}
|
|
|
|
if (volume > 100)
|
|
|
|
volume = 100;
|
|
|
|
else if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
|
|
|
|
return output->plugin->control(output->data, AC_MIXER_SETVOL, &volume);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mixer_control_getvol(unsigned int device, int *volume)
|
|
|
|
{
|
|
|
|
struct audio_output *output;
|
2009-02-10 18:51:49 +01:00
|
|
|
|
|
|
|
if (device >= audio_output_count())
|
2008-12-31 16:46:41 +01:00
|
|
|
return false;
|
|
|
|
|
2009-02-10 18:51:49 +01:00
|
|
|
output = audio_output_get(device);
|
2008-12-31 16:46:41 +01:00
|
|
|
if (output->plugin && output->plugin->control) {
|
|
|
|
return output->plugin->control(output->data, AC_MIXER_GETVOL, volume);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|