0N/A/*
3312N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#define USE_ERROR
0N/A//#define USE_TRACE
0N/A
0N/A#include "Ports.h"
0N/A#include "PLATFORM_API_LinuxOS_ALSA_CommonUtils.h"
0N/A#include <alsa/asoundlib.h>
0N/A
0N/A#if USE_PORTS == TRUE
0N/A
0N/A#define MAX_ELEMS (300)
0N/A#define MAX_CONTROLS (MAX_ELEMS * 4)
0N/A
0N/A#define CHANNELS_MONO (SND_MIXER_SCHN_LAST + 1)
0N/A#define CHANNELS_STEREO (SND_MIXER_SCHN_LAST + 2)
0N/A
0N/Atypedef struct {
0N/A snd_mixer_elem_t* elem;
0N/A INT32 portType; /* one of PORT_XXX_xx */
0N/A char* controlType; /* one of CONTROL_TYPE_xx */
0N/A /* Values: either SND_MIXER_SCHN_FRONT_xx, CHANNELS_MONO or CHANNELS_STEREO.
0N/A For SND_MIXER_SCHN_FRONT_xx, exactly this channel is set/retrieved directly.
0N/A For CHANNELS_MONO, ALSA channel SND_MIXER_SCHN_MONO is set/retrieved directly.
0N/A For CHANNELS_STEREO, ALSA channels SND_MIXER_SCHN_FRONT_LEFT and SND_MIXER_SCHN_FRONT_RIGHT
0N/A are set after a calculation that takes balance into account. Retrieved? Average of both
0N/A channels? (Using a cached value is not a good idea since the value in the HW may have been
0N/A altered.) */
0N/A INT32 channel;
0N/A} PortControl;
0N/A
0N/A
0N/Atypedef struct tag_PortMixer {
0N/A snd_mixer_t* mixer_handle;
0N/A /* Number of array elements used in elems and types. */
0N/A int numElems;
0N/A snd_mixer_elem_t** elems;
0N/A /* Array of port types (PORT_SRC_UNKNOWN etc.). Indices are the same as in elems. */
0N/A INT32* types;
0N/A /* Number of array elements used in controls. */
0N/A int numControls;
0N/A PortControl* controls;
0N/A} PortMixer;
0N/A
0N/A
0N/A///// implemented functions of Ports.h
0N/A
0N/AINT32 PORT_GetPortMixerCount() {
0N/A INT32 mixerCount;
0N/A int card;
0N/A char devname[16];
0N/A int err;
0N/A snd_ctl_t *handle;
0N/A snd_ctl_card_info_t* info;
0N/A
0N/A TRACE0("> PORT_GetPortMixerCount\n");
0N/A
0N/A initAlsaSupport();
0N/A
0N/A snd_ctl_card_info_malloc(&info);
0N/A card = -1;
0N/A mixerCount = 0;
0N/A if (snd_card_next(&card) >= 0) {
0N/A while (card >= 0) {
0N/A sprintf(devname, ALSA_HARDWARE_CARD, card);
0N/A TRACE1("PORT_GetPortMixerCount: Opening alsa device \"%s\"...\n", devname);
0N/A err = snd_ctl_open(&handle, devname, 0);
0N/A if (err < 0) {
0N/A ERROR2("ERROR: snd_ctl_open, card=%d: %s\n", card, snd_strerror(err));
0N/A } else {
0N/A mixerCount++;
0N/A snd_ctl_close(handle);
0N/A }
0N/A if (snd_card_next(&card) < 0) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A snd_ctl_card_info_free(info);
0N/A TRACE0("< PORT_GetPortMixerCount\n");
0N/A return mixerCount;
0N/A}
0N/A
0N/A
0N/AINT32 PORT_GetPortMixerDescription(INT32 mixerIndex, PortMixerDescription* description) {
0N/A snd_ctl_t* handle;
0N/A snd_ctl_card_info_t* card_info;
0N/A char devname[16];
0N/A int err;
0N/A char buffer[100];
0N/A
0N/A TRACE0("> PORT_GetPortMixerDescription\n");
0N/A snd_ctl_card_info_malloc(&card_info);
0N/A
0N/A sprintf(devname, ALSA_HARDWARE_CARD, (int) mixerIndex);
0N/A TRACE1("Opening alsa device \"%s\"...\n", devname);
0N/A err = snd_ctl_open(&handle, devname, 0);
0N/A if (err < 0) {
0N/A ERROR2("ERROR: snd_ctl_open, card=%d: %s\n", (int) mixerIndex, snd_strerror(err));
0N/A return FALSE;
0N/A }
0N/A err = snd_ctl_card_info(handle, card_info);
0N/A if (err < 0) {
0N/A ERROR2("ERROR: snd_ctl_card_info, card=%d: %s\n", (int) mixerIndex, snd_strerror(err));
0N/A }
0N/A strncpy(description->name, snd_ctl_card_info_get_id(card_info), PORT_STRING_LENGTH - 1);
0N/A sprintf(buffer, " [%s]", devname);
0N/A strncat(description->name, buffer, PORT_STRING_LENGTH - 1 - strlen(description->name));
0N/A strncpy(description->vendor, "ALSA (http://www.alsa-project.org)", PORT_STRING_LENGTH - 1);
0N/A strncpy(description->description, snd_ctl_card_info_get_name(card_info), PORT_STRING_LENGTH - 1);
0N/A strncat(description->description, ", ", PORT_STRING_LENGTH - 1 - strlen(description->description));
0N/A strncat(description->description, snd_ctl_card_info_get_mixername(card_info), PORT_STRING_LENGTH - 1 - strlen(description->description));
0N/A getALSAVersion(description->version, PORT_STRING_LENGTH - 1);
0N/A
0N/A snd_ctl_close(handle);
0N/A snd_ctl_card_info_free(card_info);
0N/A TRACE0("< PORT_GetPortMixerDescription\n");
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/Avoid* PORT_Open(INT32 mixerIndex) {
0N/A char devname[16];
0N/A snd_mixer_t* mixer_handle;
0N/A int err;
0N/A PortMixer* handle;
0N/A
0N/A TRACE0("> PORT_Open\n");
0N/A sprintf(devname, ALSA_HARDWARE_CARD, (int) mixerIndex);
0N/A if ((err = snd_mixer_open(&mixer_handle, 0)) < 0) {
0N/A ERROR2("Mixer %s open error: %s", devname, snd_strerror(err));
0N/A return NULL;
0N/A }
0N/A if ((err = snd_mixer_attach(mixer_handle, devname)) < 0) {
0N/A ERROR2("Mixer attach %s error: %s", devname, snd_strerror(err));
0N/A snd_mixer_close(mixer_handle);
0N/A return NULL;
0N/A }
0N/A if ((err = snd_mixer_selem_register(mixer_handle, NULL, NULL)) < 0) {
0N/A ERROR1("Mixer register error: %s", snd_strerror(err));
0N/A snd_mixer_close(mixer_handle);
0N/A return NULL;
0N/A }
0N/A err = snd_mixer_load(mixer_handle);
0N/A if (err < 0) {
0N/A ERROR2("Mixer %s load error: %s", devname, snd_strerror(err));
0N/A snd_mixer_close(mixer_handle);
0N/A return NULL;
0N/A }
0N/A handle = (PortMixer*) calloc(1, sizeof(PortMixer));
0N/A if (handle == NULL) {
0N/A ERROR0("malloc() failed.");
0N/A snd_mixer_close(mixer_handle);
0N/A return NULL;
0N/A }
0N/A handle->numElems = 0;
0N/A handle->elems = (snd_mixer_elem_t**) calloc(MAX_ELEMS, sizeof(snd_mixer_elem_t*));
0N/A if (handle->elems == NULL) {
0N/A ERROR0("malloc() failed.");
0N/A snd_mixer_close(mixer_handle);
0N/A free(handle);
0N/A return NULL;
0N/A }
0N/A handle->types = (INT32*) calloc(MAX_ELEMS, sizeof(INT32));
0N/A if (handle->types == NULL) {
0N/A ERROR0("malloc() failed.");
0N/A snd_mixer_close(mixer_handle);
0N/A free(handle->elems);
0N/A free(handle);
0N/A return NULL;
0N/A }
0N/A handle->controls = (PortControl*) calloc(MAX_CONTROLS, sizeof(PortControl));
0N/A if (handle->controls == NULL) {
0N/A ERROR0("malloc() failed.");
0N/A snd_mixer_close(mixer_handle);
0N/A free(handle->elems);
0N/A free(handle->types);
0N/A free(handle);
0N/A return NULL;
0N/A }
0N/A handle->mixer_handle = mixer_handle;
0N/A // necessary to initialize data structures
0N/A PORT_GetPortCount(handle);
0N/A TRACE0("< PORT_Open\n");
0N/A return handle;
0N/A}
0N/A
0N/A
0N/Avoid PORT_Close(void* id) {
0N/A TRACE0("> PORT_Close\n");
0N/A if (id != NULL) {
0N/A PortMixer* handle = (PortMixer*) id;
0N/A if (handle->mixer_handle != NULL) {
0N/A snd_mixer_close(handle->mixer_handle);
0N/A }
0N/A if (handle->elems != NULL) {
0N/A free(handle->elems);
0N/A }
0N/A if (handle->types != NULL) {
0N/A free(handle->types);
0N/A }
0N/A if (handle->controls != NULL) {
0N/A free(handle->controls);
0N/A }
0N/A free(handle);
0N/A }
0N/A TRACE0("< PORT_Close\n");
0N/A}
0N/A
0N/A
0N/A
0N/AINT32 PORT_GetPortCount(void* id) {
0N/A PortMixer* portMixer;
0N/A snd_mixer_elem_t *elem;
0N/A
0N/A TRACE0("> PORT_GetPortCount\n");
0N/A if (id == NULL) {
0N/A // $$mp: Should become a descriptive error code (invalid handle).
0N/A return -1;
0N/A }
0N/A portMixer = (PortMixer*) id;
0N/A if (portMixer->numElems == 0) {
0N/A for (elem = snd_mixer_first_elem(portMixer->mixer_handle); elem; elem = snd_mixer_elem_next(elem)) {
0N/A if (!snd_mixer_selem_is_active(elem))
0N/A continue;
0N/A TRACE2("Simple mixer control '%s',%i\n",
0N/A snd_mixer_selem_get_name(elem),
0N/A snd_mixer_selem_get_index(elem));
0N/A if (snd_mixer_selem_has_playback_volume(elem)) {
0N/A portMixer->elems[portMixer->numElems] = elem;
0N/A portMixer->types[portMixer->numElems] = PORT_DST_UNKNOWN;
0N/A portMixer->numElems++;
0N/A }
0N/A // to prevent buffer overflow
0N/A if (portMixer->numElems >= MAX_ELEMS) {
0N/A break;
0N/A }
0N/A /* If an element has both playback an capture volume, it is put into the arrays
0N/A twice. */
0N/A if (snd_mixer_selem_has_capture_volume(elem)) {
0N/A portMixer->elems[portMixer->numElems] = elem;
0N/A portMixer->types[portMixer->numElems] = PORT_SRC_UNKNOWN;
0N/A portMixer->numElems++;
0N/A }
0N/A // to prevent buffer overflow
0N/A if (portMixer->numElems >= MAX_ELEMS) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A TRACE0("< PORT_GetPortCount\n");
0N/A return portMixer->numElems;
0N/A}
0N/A
0N/A
0N/AINT32 PORT_GetPortType(void* id, INT32 portIndex) {
0N/A PortMixer* portMixer;
0N/A INT32 type;
0N/A TRACE0("> PORT_GetPortType\n");
0N/A if (id == NULL) {
0N/A // $$mp: Should become a descriptive error code (invalid handle).
0N/A return -1;
0N/A }
0N/A portMixer = (PortMixer*) id;
0N/A if (portIndex < 0 || portIndex >= portMixer->numElems) {
0N/A // $$mp: Should become a descriptive error code (index out of bounds).
0N/A return -1;
0N/A }
0N/A type = portMixer->types[portIndex];
0N/A TRACE0("< PORT_GetPortType\n");
0N/A return type;
0N/A}
0N/A
0N/A
0N/AINT32 PORT_GetPortName(void* id, INT32 portIndex, char* name, INT32 len) {
0N/A PortMixer* portMixer;
0N/A const char* nam;
0N/A
0N/A TRACE0("> PORT_GetPortName\n");
0N/A if (id == NULL) {
0N/A // $$mp: Should become a descriptive error code (invalid handle).
0N/A return -1;
0N/A }
0N/A portMixer = (PortMixer*) id;
0N/A if (portIndex < 0 || portIndex >= portMixer->numElems) {
0N/A // $$mp: Should become a descriptive error code (index out of bounds).
0N/A return -1;
0N/A }
0N/A nam = snd_mixer_selem_get_name(portMixer->elems[portIndex]);
0N/A strncpy(name, nam, len - 1);
0N/A name[len - 1] = 0;
0N/A TRACE0("< PORT_GetPortName\n");
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/Astatic int isPlaybackFunction(INT32 portType) {
0N/A return (portType & PORT_DST_MASK);
0N/A}
0N/A
0N/A
0N/A/* Sets portControl to a pointer to the next free array element in the PortControl (pointer)
0N/A array of the passed portMixer. Returns TRUE if successful. May return FALSE if there is no
0N/A free slot. In this case, portControl is not altered */
0N/Astatic int getControlSlot(PortMixer* portMixer, PortControl** portControl) {
0N/A if (portMixer->numControls >= MAX_CONTROLS) {
0N/A return FALSE;
0N/A } else {
0N/A *portControl = &(portMixer->controls[portMixer->numControls]);
0N/A portMixer->numControls++;
0N/A return TRUE;
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Protect against illegal min-max values, preventing divisions by zero.
0N/A */
0N/Ainline static long getRange(long min, long max) {
0N/A if (max > min) {
0N/A return max - min;
0N/A } else {
0N/A return 1;
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Idea: we may specify that if unit is an empty string, the values are linear and if unit is "dB",
0N/A the values are logarithmic.
0N/A*/
0N/Astatic void* createVolumeControl(PortControlCreator* creator,
0N/A PortControl* portControl,
0N/A snd_mixer_elem_t* elem, int isPlayback) {
0N/A void* control;
0N/A float precision;
0N/A long min, max;
0N/A
0N/A if (isPlayback) {
0N/A snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
0N/A } else {
0N/A snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
0N/A }
0N/A /* $$mp: The volume values retrieved with the ALSA API are strongly supposed to be logarithmic.
0N/A So the following calculation is wrong. However, there is no correct calculation, since
0N/A for equal-distant logarithmic steps, the precision expressed in linear varies over the
0N/A scale. */
0N/A precision = 1.0F / getRange(min, max);
0N/A control = (creator->newFloatControl)(creator, portControl, CONTROL_TYPE_VOLUME, 0.0F, +1.0F, precision, "");
0N/A return control;
0N/A}
0N/A
0N/A
0N/Avoid PORT_GetControls(void* id, INT32 portIndex, PortControlCreator* creator) {
0N/A PortMixer* portMixer;
0N/A snd_mixer_elem_t* elem;
0N/A void* control;
0N/A PortControl* portControl;
0N/A void* controls[10];
0N/A int numControls;
0N/A char* portName;
3312N/A int isPlayback = 0;
0N/A int isMono;
0N/A int isStereo;
0N/A char* type;
0N/A snd_mixer_selem_channel_id_t channel;
0N/A
0N/A TRACE0("> PORT_GetControls\n");
0N/A if (id == NULL) {
0N/A ERROR0("Invalid handle!");
0N/A // $$mp: an error code should be returned.
0N/A return;
0N/A }
0N/A portMixer = (PortMixer*) id;
0N/A if (portIndex < 0 || portIndex >= portMixer->numElems) {
0N/A ERROR0("Port index out of range!");
0N/A // $$mp: an error code should be returned.
0N/A return;
0N/A }
0N/A numControls = 0;
0N/A elem = portMixer->elems[portIndex];
0N/A if (snd_mixer_selem_has_playback_volume(elem) || snd_mixer_selem_has_capture_volume(elem)) {
0N/A /* Since we've splitted/duplicated elements with both playback and capture on the recovery
0N/A of elements, we now can assume that we handle only to deal with either playback or
0N/A capture. */
0N/A isPlayback = isPlaybackFunction(portMixer->types[portIndex]);
0N/A isMono = (isPlayback && snd_mixer_selem_is_playback_mono(elem)) ||
0N/A (!isPlayback && snd_mixer_selem_is_capture_mono(elem));
0N/A isStereo = (isPlayback &&
0N/A snd_mixer_selem_has_playback_channel(elem, SND_MIXER_SCHN_FRONT_LEFT) &&
0N/A snd_mixer_selem_has_playback_channel(elem, SND_MIXER_SCHN_FRONT_RIGHT)) ||
0N/A (!isPlayback &&
0N/A snd_mixer_selem_has_capture_channel(elem, SND_MIXER_SCHN_FRONT_LEFT) &&
0N/A snd_mixer_selem_has_capture_channel(elem, SND_MIXER_SCHN_FRONT_RIGHT));
0N/A // single volume control
0N/A if (isMono || isStereo) {
0N/A if (getControlSlot(portMixer, &portControl)) {
0N/A portControl->elem = elem;
0N/A portControl->portType = portMixer->types[portIndex];
0N/A portControl->controlType = CONTROL_TYPE_VOLUME;
0N/A if (isMono) {
0N/A portControl->channel = CHANNELS_MONO;
0N/A } else {
0N/A portControl->channel = CHANNELS_STEREO;
0N/A }
0N/A control = createVolumeControl(creator, portControl, elem, isPlayback);
0N/A if (control != NULL) {
0N/A controls[numControls++] = control;
0N/A }
0N/A }
0N/A } else { // more than two channels, each channels has its own control.
0N/A for (channel = SND_MIXER_SCHN_FRONT_LEFT; channel <= SND_MIXER_SCHN_LAST; channel++) {
0N/A if (isPlayback && snd_mixer_selem_has_playback_channel(elem, channel) ||
0N/A !isPlayback && snd_mixer_selem_has_capture_channel(elem, channel)) {
0N/A if (getControlSlot(portMixer, &portControl)) {
0N/A portControl->elem = elem;
0N/A portControl->portType = portMixer->types[portIndex];
0N/A portControl->controlType = CONTROL_TYPE_VOLUME;
0N/A portControl->channel = channel;
0N/A control = createVolumeControl(creator, portControl, elem, isPlayback);
0N/A // We wrap in a compound control to provide the channel name.
0N/A if (control != NULL) {
0N/A /* $$mp 2003-09-14: The following cast shouln't be necessary. Instead, the
0N/A declaration of PORT_NewCompoundControlPtr in Ports.h should be changed
0N/A to take a const char* parameter. */
0N/A control = (creator->newCompoundControl)(creator, (char*) snd_mixer_selem_channel_name(channel), &control, 1);
0N/A }
0N/A if (control != NULL) {
0N/A controls[numControls++] = control;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A // BALANCE control
0N/A if (isStereo) {
0N/A if (getControlSlot(portMixer, &portControl)) {
0N/A portControl->elem = elem;
0N/A portControl->portType = portMixer->types[portIndex];
0N/A portControl->controlType = CONTROL_TYPE_BALANCE;
0N/A portControl->channel = CHANNELS_STEREO;
0N/A /* $$mp: The value for precision is chosen more or less arbitrarily. */
0N/A control = (creator->newFloatControl)(creator, portControl, CONTROL_TYPE_BALANCE, -1.0F, 1.0F, 0.01F, "");
0N/A if (control != NULL) {
0N/A controls[numControls++] = control;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if (snd_mixer_selem_has_playback_switch(elem) || snd_mixer_selem_has_capture_switch(elem)) {
0N/A if (getControlSlot(portMixer, &portControl)) {
0N/A type = isPlayback ? CONTROL_TYPE_MUTE : CONTROL_TYPE_SELECT;
0N/A portControl->elem = elem;
0N/A portControl->portType = portMixer->types[portIndex];
0N/A portControl->controlType = type;
0N/A control = (creator->newBooleanControl)(creator, portControl, type);
0N/A if (control != NULL) {
0N/A controls[numControls++] = control;
0N/A }
0N/A }
0N/A }
0N/A /* $$mp 2003-09-14: The following cast shouln't be necessary. Instead, the
0N/A declaration of PORT_NewCompoundControlPtr in Ports.h should be changed
0N/A to take a const char* parameter. */
0N/A portName = (char*) snd_mixer_selem_get_name(elem);
0N/A control = (creator->newCompoundControl)(creator, portName, controls, numControls);
0N/A if (control != NULL) {
0N/A (creator->addControl)(creator, control);
0N/A }
0N/A TRACE0("< PORT_GetControls\n");
0N/A}
0N/A
0N/A
0N/AINT32 PORT_GetIntValue(void* controlIDV) {
0N/A PortControl* portControl = (PortControl*) controlIDV;
0N/A int value = 0;
0N/A snd_mixer_selem_channel_id_t channel;
0N/A
0N/A if (portControl != NULL) {
0N/A switch (portControl->channel) {
0N/A case CHANNELS_MONO:
0N/A channel = SND_MIXER_SCHN_MONO;
0N/A break;
0N/A
0N/A case CHANNELS_STEREO:
0N/A channel = SND_MIXER_SCHN_FRONT_LEFT;
0N/A break;
0N/A
0N/A default:
0N/A channel = portControl->channel;
0N/A }
0N/A if (portControl->controlType == CONTROL_TYPE_MUTE ||
0N/A portControl->controlType == CONTROL_TYPE_SELECT) {
0N/A if (isPlaybackFunction(portControl->portType)) {
0N/A snd_mixer_selem_get_playback_switch(portControl->elem, channel, &value);
0N/A } else {
0N/A snd_mixer_selem_get_capture_switch(portControl->elem, channel, &value);
0N/A }
0N/A if (portControl->controlType == CONTROL_TYPE_MUTE) {
0N/A value = ! value;
0N/A }
0N/A } else {
0N/A ERROR1("PORT_GetIntValue(): inappropriate control type: %s\n",
0N/A portControl->controlType);
0N/A }
0N/A }
0N/A return (INT32) value;
0N/A}
0N/A
0N/A
0N/Avoid PORT_SetIntValue(void* controlIDV, INT32 value) {
0N/A PortControl* portControl = (PortControl*) controlIDV;
0N/A snd_mixer_selem_channel_id_t channel;
0N/A
0N/A if (portControl != NULL) {
0N/A if (portControl->controlType == CONTROL_TYPE_MUTE) {
0N/A value = ! value;
0N/A }
0N/A if (portControl->controlType == CONTROL_TYPE_MUTE ||
0N/A portControl->controlType == CONTROL_TYPE_SELECT) {
0N/A if (isPlaybackFunction(portControl->portType)) {
0N/A snd_mixer_selem_set_playback_switch_all(portControl->elem, value);
0N/A } else {
0N/A snd_mixer_selem_set_capture_switch_all(portControl->elem, value);
0N/A }
0N/A } else {
0N/A ERROR1("PORT_SetIntValue(): inappropriate control type: %s\n",
0N/A portControl->controlType);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Astatic float scaleVolumeValueToNormalized(long value, long min, long max) {
0N/A return (float) (value - min) / getRange(min, max);
0N/A}
0N/A
0N/A
0N/Astatic long scaleVolumeValueToHardware(float value, long min, long max) {
0N/A return (long)(value * getRange(min, max) + min);
0N/A}
0N/A
0N/A
0N/Afloat getRealVolume(PortControl* portControl,
0N/A snd_mixer_selem_channel_id_t channel) {
0N/A float fValue;
0N/A long lValue = 0;
0N/A long min = 0;
0N/A long max = 0;
0N/A
0N/A if (isPlaybackFunction(portControl->portType)) {
0N/A snd_mixer_selem_get_playback_volume_range(portControl->elem,
0N/A &min, &max);
0N/A snd_mixer_selem_get_playback_volume(portControl->elem,
0N/A channel, &lValue);
0N/A } else {
0N/A snd_mixer_selem_get_capture_volume_range(portControl->elem,
0N/A &min, &max);
0N/A snd_mixer_selem_get_capture_volume(portControl->elem,
0N/A channel, &lValue);
0N/A }
0N/A fValue = scaleVolumeValueToNormalized(lValue, min, max);
0N/A return fValue;
0N/A}
0N/A
0N/A
0N/Avoid setRealVolume(PortControl* portControl,
0N/A snd_mixer_selem_channel_id_t channel, float value) {
0N/A long lValue = 0;
0N/A long min = 0;
0N/A long max = 0;
0N/A
0N/A if (isPlaybackFunction(portControl->portType)) {
0N/A snd_mixer_selem_get_playback_volume_range(portControl->elem,
0N/A &min, &max);
0N/A lValue = scaleVolumeValueToHardware(value, min, max);
0N/A snd_mixer_selem_set_playback_volume(portControl->elem,
0N/A channel, lValue);
0N/A } else {
0N/A snd_mixer_selem_get_capture_volume_range(portControl->elem,
0N/A &min, &max);
0N/A lValue = scaleVolumeValueToHardware(value, min, max);
0N/A snd_mixer_selem_set_capture_volume(portControl->elem,
0N/A channel, lValue);
0N/A }
0N/A}
0N/A
0N/A
0N/Astatic float getFakeBalance(PortControl* portControl) {
0N/A float volL, volR;
0N/A
0N/A // pan is the ratio of left and right
0N/A volL = getRealVolume(portControl, SND_MIXER_SCHN_FRONT_LEFT);
0N/A volR = getRealVolume(portControl, SND_MIXER_SCHN_FRONT_RIGHT);
0N/A if (volL > volR) {
0N/A return -1.0f + (volR / volL);
0N/A }
0N/A else if (volR > volL) {
0N/A return 1.0f - (volL / volR);
0N/A }
0N/A return 0.0f;
0N/A}
0N/A
0N/A
0N/Astatic float getFakeVolume(PortControl* portControl) {
0N/A float valueL;
0N/A float valueR;
0N/A float value;
0N/A
0N/A valueL = getRealVolume(portControl, SND_MIXER_SCHN_FRONT_LEFT);
0N/A valueR = getRealVolume(portControl, SND_MIXER_SCHN_FRONT_RIGHT);
0N/A // volume is the greater value of both
0N/A value = valueL > valueR ? valueL : valueR ;
0N/A return value;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * sets the unsigned values for left and right volume according to
0N/A * the given volume (0...1) and balance (-1..0..+1)
0N/A */
0N/Astatic void setFakeVolume(PortControl* portControl, float vol, float bal) {
0N/A float volumeLeft;
0N/A float volumeRight;
0N/A
0N/A if (bal < 0.0f) {
0N/A volumeLeft = vol;
0N/A volumeRight = vol * (bal + 1.0f);
0N/A } else {
0N/A volumeLeft = vol * (1.0f - bal);
0N/A volumeRight = vol;
0N/A }
0N/A setRealVolume(portControl, SND_MIXER_SCHN_FRONT_LEFT, volumeLeft);
0N/A setRealVolume(portControl, SND_MIXER_SCHN_FRONT_RIGHT, volumeRight);
0N/A}
0N/A
0N/A
0N/Afloat PORT_GetFloatValue(void* controlIDV) {
0N/A PortControl* portControl = (PortControl*) controlIDV;
0N/A float value = 0.0F;
0N/A
0N/A if (portControl != NULL) {
0N/A if (portControl->controlType == CONTROL_TYPE_VOLUME) {
0N/A switch (portControl->channel) {
0N/A case CHANNELS_MONO:
0N/A value = getRealVolume(portControl, SND_MIXER_SCHN_MONO);
0N/A break;
0N/A
0N/A case CHANNELS_STEREO:
0N/A value = getFakeVolume(portControl);
0N/A break;
0N/A
0N/A default:
0N/A value = getRealVolume(portControl, portControl->channel);
0N/A }
0N/A } else if (portControl->controlType == CONTROL_TYPE_BALANCE) {
0N/A if (portControl->channel == CHANNELS_STEREO) {
0N/A value = getFakeBalance(portControl);
0N/A } else {
0N/A ERROR0("PORT_GetFloatValue(): Balance only allowed for stereo channels!\n");
0N/A }
0N/A } else {
0N/A ERROR1("PORT_GetFloatValue(): inappropriate control type: %s!\n",
0N/A portControl->controlType);
0N/A }
0N/A }
0N/A return value;
0N/A}
0N/A
0N/A
0N/Avoid PORT_SetFloatValue(void* controlIDV, float value) {
0N/A PortControl* portControl = (PortControl*) controlIDV;
0N/A
0N/A if (portControl != NULL) {
0N/A if (portControl->controlType == CONTROL_TYPE_VOLUME) {
0N/A switch (portControl->channel) {
0N/A case CHANNELS_MONO:
0N/A setRealVolume(portControl, SND_MIXER_SCHN_MONO, value);
0N/A break;
0N/A
0N/A case CHANNELS_STEREO:
0N/A setFakeVolume(portControl, value, getFakeBalance(portControl));
0N/A break;
0N/A
0N/A default:
0N/A setRealVolume(portControl, portControl->channel, value);
0N/A }
0N/A } else if (portControl->controlType == CONTROL_TYPE_BALANCE) {
0N/A if (portControl->channel == CHANNELS_STEREO) {
0N/A setFakeVolume(portControl, getFakeVolume(portControl), value);
0N/A } else {
0N/A ERROR0("PORT_SetFloatValue(): Balance only allowed for stereo channels!\n");
0N/A }
0N/A } else {
0N/A ERROR1("PORT_SetFloatValue(): inappropriate control type: %s!\n",
0N/A portControl->controlType);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A#endif // USE_PORTS