-
-
Save dchabot/0eaf07df4296e181b8c5937ae275cc8d to your computer and use it in GitHub Desktop.
a simple alsa volume control program, see http://stackoverflow.com/questions/6787318/set-alsa-master-volume-from-c-code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# build helloworld executable when user executes "make" | |
volumecontrol: volumecontrol.o | |
$(CC) $(LDFLAGS) -lasound volumecontrol.o -o volumecontrol | |
volumecontrol.o: volumecontrol.c | |
$(CC) $(CFLAGS) -c volumecontrol.c | |
# remove object files and executable when user executes "make clean" | |
clean: | |
rm *.o volumecontrol | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################## | |
# OpenWrt Makefile for helloworld program | |
# | |
# | |
# Most of the variables used here are defined in | |
# the include directives below. We just need to | |
# specify a basic description of the package, | |
# where to build our program, where to find | |
# the source files, and where to install the | |
# compiled program on the router. | |
# | |
# Be very careful of spacing in this file. | |
# Indents should be tabs, not spaces, and | |
# there should be no trailing whitespace in | |
# lines that are not commented. | |
# | |
############################################## | |
include $(TOPDIR)/rules.mk | |
# Name and release number of this package | |
PKG_NAME:=volumecontrol | |
PKG_RELEASE:=0.1 | |
# This specifies the directory where we're going to build the program. | |
# The root build directory, $(BUILD_DIR), is by default the build_mipsel | |
# directory in your OpenWrt SDK directory | |
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) | |
include $(INCLUDE_DIR)/package.mk | |
# Specify package information for this program. | |
# The variables defined here should be self explanatory. | |
define Package/volumecontrol | |
SECTION:=utils | |
CATEGORY:=Utilities | |
TITLE:=volumecontrol -- a simple volume control program | |
DEPENDS:=+alsa-lib | |
# DESCRIPTION:=\ | |
# If you can't figure out what this program does, \\\ | |
# you're probably brain-dead and need immediate \\\ | |
# medical attention. | |
endef | |
# Specify what needs to be done to prepare for building the package. | |
# In our case, we need to copy the source files to the build directory. | |
# This is NOT the default. The default uses the PKG_SOURCE_URL and the | |
# PKG_SOURCE which is not defined here to download the source from the web. | |
# In order to just build a simple program that we have just written, it is | |
# much easier to do it this way. | |
define Build/Prepare | |
mkdir -p $(PKG_BUILD_DIR) | |
$(CP) ./src/* $(PKG_BUILD_DIR)/ | |
endef | |
# We do not need to define Build/Configure or Build/Compile directives | |
# The defaults are appropriate for compiling a simple program such as this one | |
# Specify where and how to install the program. Since we only have one file, | |
# the helloworld executable, install it by copying it to the /bin directory on | |
# the router. The $(1) variable represents the root directory on the router running | |
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install | |
# directory if it does not already exist. Likewise $(INSTALL_BIN) contains the | |
# command to copy the binary file from its current location (in our case the build | |
# directory) to the install directory. | |
define Package/volumecontrol/install | |
$(INSTALL_DIR) $(1)/bin | |
$(INSTALL_BIN) $(PKG_BUILD_DIR)/volumecontrol $(1)/bin/ | |
endef | |
# This line executes the necessary commands to compile our program. | |
# The above define directives specify all the information needed, but this | |
# line calls BuildPackage which in turn actually uses this information to | |
# build a package. | |
$(eval $(call BuildPackage,volumecontrol)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <alsa/asoundlib.h> | |
#include <alsa/mixer.h> | |
int main(int argc, char *argv[]) | |
{ | |
long volume; | |
if (argc != 3) { | |
printf("Usage: %s snd_card_name vol\n",argv[0]); | |
return 1; | |
} | |
volume = atol(argv[2]); | |
long min, max; | |
snd_mixer_t *handle; | |
snd_mixer_selem_id_t *sid; | |
const char *card = "default"; | |
//const char *selem_name = "Master"; | |
snd_mixer_open(&handle, 0); | |
snd_mixer_attach(handle, card); | |
snd_mixer_selem_register(handle, NULL, NULL); | |
snd_mixer_load(handle); | |
snd_mixer_selem_id_alloca(&sid); | |
snd_mixer_selem_id_set_index(sid, 0); | |
snd_mixer_selem_id_set_name(sid, argv[1]); | |
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); | |
snd_mixer_selem_get_playback_volume_range(elem, &min, &max); | |
snd_mixer_selem_set_playback_volume_all(elem, volume * max / 100); | |
snd_mixer_close(handle); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment