From 7cd67c9fad56ea1b4ac40e0fc66debc00acff9ad Mon Sep 17 00:00:00 2001 From: martii Date: Wed, 5 Sep 2012 19:08:45 +0200 Subject: [PATCH] spark: add volume control for alsa mixer --- Makefile.am | 2 ++ include/audio_td.h | 3 ++ libspark/Makefile.am | 3 +- libspark/audio_mixer.cpp | 70 ++++++++++++++++++++++++++++++++++++++++ libspark/audio_mixer.h | 37 +++++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 libspark/audio_mixer.cpp create mode 100644 libspark/audio_mixer.h diff --git a/Makefile.am b/Makefile.am index b4e802d..d4e9875 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,9 +43,11 @@ libstb_hal_a_LIBADD += \ azbox/video.o endif if BOXTYPE_SPARK +libstb_hal_test_LDADD += -lasound SUBDIRS += libspark libeplayer3 libstb_hal_a_LIBADD += \ libspark/audio.o \ + libspark/audio_mixer.o \ libspark/dmx.o \ libspark/init.o \ libspark/irmp.o \ diff --git a/include/audio_td.h b/include/audio_td.h index 08610ea..b40b80d 100644 --- a/include/audio_td.h +++ b/include/audio_td.h @@ -3,6 +3,9 @@ #include "../libtriple/audio_td.h" #elif HAVE_SPARK_HARDWARE #include "../libspark/audio_lib.h" +#ifdef MARTII +#include "../libspark/audio_mixer.h" +#endif #elif HAVE_AZBOX_HARDWARE #include "../azbox/audio_lib.h" #else diff --git a/libspark/Makefile.am b/libspark/Makefile.am index dc74f60..e31d6f5 100644 --- a/libspark/Makefile.am +++ b/libspark/Makefile.am @@ -5,7 +5,7 @@ INCLUDES = \ noinst_LIBRARIES = libspark.a AM_CXXFLAGS = -fno-rtti -fno-exceptions -fno-strict-aliasing -AM_LDFLAGS = -lpthread +AM_LDFLAGS = -lpthread -lasound libspark_a_SOURCES = \ irmp.c \ @@ -13,6 +13,7 @@ libspark_a_SOURCES = \ dmx.cpp \ video.cpp \ audio.cpp \ + audio_mixer.cpp \ init.cpp \ playback_libeplayer3.cpp \ pwrmngr.cpp \ diff --git a/libspark/audio_mixer.cpp b/libspark/audio_mixer.cpp new file mode 100644 index 0000000..aabf11d --- /dev/null +++ b/libspark/audio_mixer.cpp @@ -0,0 +1,70 @@ +/* + * audio_mixer.c + * + * (C) 2012 martii + * + * 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, see . + */ + +#include + +mixerVolume::mixerVolume(const char *name, const char *card, long volume) { + sid = NULL; + elem = NULL; + handle = NULL; + min = 0; + max = 100; + char cardId[10]; + + if (!name || !card) + return; + + int cx = snd_card_get_index(card); + if (cx < 0 || cx > 31) + return; + snprintf(cardId, sizeof(cardId), "hw:%i", cx); + + if (0 > snd_mixer_open(&handle, 0)) + return; + if (0 > snd_mixer_attach(handle, cardId)) + return; + if (0 > snd_mixer_selem_register(handle, NULL, NULL)) + return; + if (0 > snd_mixer_load(handle)) + return; + snd_mixer_selem_id_alloca(&sid); + if (!sid) + return; + snd_mixer_selem_id_set_index(sid, 0); + snd_mixer_selem_id_set_name(sid, name); + elem = snd_mixer_find_selem(handle, sid); + if (elem) { + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + setVolume(volume); + } +} +mixerVolume::~mixerVolume() +{ + if (handle) + snd_mixer_close(handle); + if (sid) + snd_mixer_selem_id_free(sid); +} + +bool mixerVolume::setVolume(long volume) { + return elem + && (volume > -1) + && (volume < 101) + && !snd_mixer_selem_set_playback_volume_all(elem, min + volume * (max - min)/101); +} diff --git a/libspark/audio_mixer.h b/libspark/audio_mixer.h new file mode 100644 index 0000000..e716836 --- /dev/null +++ b/libspark/audio_mixer.h @@ -0,0 +1,37 @@ +/* + * audio_mixer.h + * + * (C) 2012 martii + * + * 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, see . + */ + +#ifndef __AUDIO_MIXER_H__ +#define __AUDIO_MIXER_H__ +#include + +class mixerVolume +{ + private: + long min, max; + snd_mixer_t *handle; + snd_mixer_elem_t* elem; + snd_mixer_selem_id_t *sid; + public: + mixerVolume(const char *selem_name, const char *Card, long volume = -1); + ~mixerVolume(void); + bool setVolume(long volume); +}; +#endif +