volume: moved code to mixer_all.c

This commit is contained in:
Max Kellermann 2009-03-14 11:35:40 +01:00
parent e7c3f469c3
commit 88af35c0ab
4 changed files with 113 additions and 30 deletions

View File

@ -79,6 +79,7 @@ mpd_headers = \
src/log.h \ src/log.h \
src/ls.h \ src/ls.h \
src/main.h \ src/main.h \
src/mixer_all.h \
src/mixer_api.h \ src/mixer_api.h \
src/mixer_control.h \ src/mixer_control.h \
src/event_pipe.h \ src/event_pipe.h \
@ -457,6 +458,7 @@ OUTPUT_SRC = \
MIXER_API_SRC = \ MIXER_API_SRC = \
src/mixer_control.c \ src/mixer_control.c \
src/mixer_all.c \
src/mixer_api.c src/mixer_api.c
MIXER_SRC = MIXER_SRC =

59
src/mixer_all.c Normal file
View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2003-2009 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "mixer_all.h"
#include "mixer_control.h"
#include "output_all.h"
#include <glib.h>
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "mixer"
int
mixer_all_get_volume(void)
{
unsigned count = audio_output_count(), ok = 0;
int volume, total = 0;
for (unsigned i = 0; i < count; i++) {
if (mixer_control_getvol(i, &volume)) {
g_debug("device %d: volume=%d", i, volume);
total += volume;
++ok;
}
}
if (ok == 0)
return -1;
return total / ok;
}
bool
mixer_all_set_volume(int volume, bool relative)
{
bool success = false;
unsigned count = audio_output_count();
for (unsigned i = 0; i < count; i++)
success = mixer_control_setvol(i, volume, relative) || success;
return success;
}

47
src/mixer_all.h Normal file
View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2003-2009 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/** \file
*
* Functions which affect the mixers of all audio outputs.
*/
#ifndef MPD_MIXER_ALL_H
#define MPD_MIXER_ALL_H
#include <stdbool.h>
/**
* Returns the average volume of all available mixers (range 0..100).
* Returns -1 if no mixer can be queried.
*/
int
mixer_all_get_volume(void);
/**
* Sets the volume on all available mixers.
*
* @param volume the volume (range 0..100 or -100..100 if #relative)
* @param relative if true, then the #volume is added to the current value
* @return true on success, false on failure
*/
bool
mixer_all_set_volume(int volume, bool relative);
#endif

View File

@ -23,9 +23,9 @@
#include "idle.h" #include "idle.h"
#include "pcm_volume.h" #include "pcm_volume.h"
#include "config.h" #include "config.h"
#include "mixer_control.h"
#include "output_all.h" #include "output_all.h"
#include "mixer_api.h" #include "mixer_api.h"
#include "mixer_all.h"
#include <glib.h> #include <glib.h>
@ -166,9 +166,6 @@ void volume_init(void)
static int hardware_volume_get(void) static int hardware_volume_get(void)
{ {
int device, count;
int volume, volume_total, volume_ok;
assert(hardware_volume_timer != NULL); assert(hardware_volume_timer != NULL);
if (last_hardware_volume >= 0 && if (last_hardware_volume >= 0 &&
@ -176,25 +173,9 @@ static int hardware_volume_get(void)
/* throttle access to hardware mixers */ /* throttle access to hardware mixers */
return last_hardware_volume; return last_hardware_volume;
volume_total = 0; last_hardware_volume = mixer_all_get_volume();
volume_ok = 0;
count = audio_output_count();
for (device=0; device<count ;device++) {
if (mixer_control_getvol(device, &volume)) {
g_debug("device %d: volume: %d\n", device, volume);
volume_total += volume;
volume_ok++;
}
}
if (volume_ok > 0) {
//return average
last_hardware_volume = volume_total / volume_ok;
g_timer_start(hardware_volume_timer); g_timer_start(hardware_volume_timer);
return last_hardware_volume; return last_hardware_volume;
} else {
return -1;
}
} }
static int software_volume_get(void) static int software_volume_get(void)
@ -245,16 +226,10 @@ static bool software_volume_change(int change, bool rel)
static bool hardware_volume_change(int change, bool rel) static bool hardware_volume_change(int change, bool rel)
{ {
int device, count;
/* reset the cache */ /* reset the cache */
last_hardware_volume = -1; last_hardware_volume = -1;
count = audio_output_count(); return mixer_all_set_volume(change, rel);
for (device=0; device<count ;device++) {
mixer_control_setvol(device, change, rel);
}
return true;
} }
bool volume_level_change(int change, bool rel) bool volume_level_change(int change, bool rel)