static int initMixer (snd_mixer_t **mixer, const char *name) { int err; if ((err = snd_mixer_open(mixer, 0)) < 0) { LOGE("Unable to open mixer: %s", snd_strerror(err)); return err; } #if 0 if ((err = snd_mixer_attach(*mixer, name)) < 0) { LOGW("Unable to attach mixer to device %s: %s", name, snd_strerror(err)); if ((err = snd_mixer_attach(*mixer, "hw:00")) < 0) { LOGE("Unable to attach mixer to device default: %s", snd_strerror(err)); snd_mixer_close (*mixer); *mixer = NULL; return err; } } #else if ((err = snd_mixer_attach(*mixer, "hw:00")) < 0) { LOGE("Unable to attach mixer to device default: %s", snd_strerror(err)); snd_mixer_close (*mixer); *mixer = NULL; return err; } #endif if ((err = snd_mixer_selem_register(*mixer, NULL, NULL)) < 0) { LOGE("Unable to register mixer elements: %s", snd_strerror(err)); snd_mixer_close (*mixer); *mixer = NULL; return err; } // Get the mixer controls from the kernel if ((err = snd_mixer_load(*mixer)) < 0) { LOGE("Unable to load mixer elements: %s", snd_strerror(err)); snd_mixer_close (*mixer); *mixer = NULL; return err; } return 0; }
bool AlsaMixer::Init( const char* card ) { LOG( Logger::LOG_DEBUG, "AlsaMixer::Init( %s )", card); int err = 0; bool success = false; if ( (err = snd_mixer_open(&_handle, 0)) < 0 ) { LOG( Logger::LOG_ERROR, "Mixer %s open error: %s", card, snd_strerror(err)); _handle = NULL; goto end; } if( (err = snd_mixer_attach(_handle, card)) < 0) { LOG( Logger::LOG_ERROR, "Mixer %s attach error: %s", card, snd_strerror(err)); goto end; } if ((err = snd_mixer_selem_register(_handle, NULL, NULL)) < 0) { LOG( Logger::LOG_ERROR, "Mixer register error: %s", snd_strerror(err)); goto end; } if ((err = snd_mixer_load(_handle)) < 0) { LOG( Logger::LOG_ERROR, "Mixer %s load error: %s", card, snd_strerror(err)); goto end; } success = true; end: if( !success ) { if( _handle ) { snd_mixer_close(_handle); _handle = NULL; } } return success; }
/** * init_alsa * must be called before other alsa functions * only needs to be called once * initializes alsa handle */ void init_alsa() { snd_mixer_open(&alsa, 0); snd_mixer_attach(alsa, SOUNDCARD); snd_mixer_selem_register(alsa, NULL, NULL); snd_mixer_load(alsa); if (alsa == NULL) { fprintf(stderr, "error opening sound card " SOUNDCARD); return; } snd_mixer_selem_id_t *sid; snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, 0); snd_mixer_selem_id_set_name(sid, ALSAMIXER); alsamixer = snd_mixer_find_selem(alsa, sid); if (alsamixer == NULL) { fprintf(stderr, "error opening alsa mixer " ALSAMIXER); return; } }
void set_volume(int vol) { long min, max; snd_mixer_t *handlev; snd_mixer_selem_id_t *sid; const char *card = "default"; const char *selem_name = "Master"; if (vol < 1 || vol > 100) { printf("ERROR: Volume out of range [0,100] %%\n"); printf("\tSetting volume to 50%%...\n"); vol = 50; } snd_mixer_open(&handlev, 0); snd_mixer_attach(handlev, card); snd_mixer_selem_register(handlev, NULL, NULL); snd_mixer_load(handlev); snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, 0); snd_mixer_selem_id_set_name(sid, selem_name); snd_mixer_elem_t *elem = snd_mixer_find_selem(handlev, sid); snd_mixer_selem_get_playback_volume_range(elem, &min, &max); snd_mixer_selem_set_playback_volume_all(elem, vol * max / 100 + min); snd_mixer_close(handlev); }
void mixerGuiAlsaMix::cardChanged( int card ) { #ifdef HAVE_ALSA channelSelect->clear(); // get the cards channels snd_mixer_t* alsaDev; snd_mixer_open( &alsaDev, 0); char device[16]; sprintf(device, "hw:%i", card); snd_mixer_attach(alsaDev, device ); snd_mixer_selem_register(alsaDev, NULL, NULL); snd_mixer_load(alsaDev); //load up the mixer snd_mixer_elem_t* mixerElem = snd_mixer_first_elem(alsaDev); while( mixerElem != snd_mixer_last_elem(alsaDev) ) { if(snd_mixer_selem_has_playback_volume(mixerElem) || snd_mixer_selem_has_capture_volume(mixerElem)) channelSelect->addItem( QString( snd_mixer_selem_get_name(mixerElem)), QVariant( snd_mixer_selem_get_name(mixerElem)) ); mixerElem=snd_mixer_elem_next( mixerElem ); } for(int i=0; i<= channelSelect->count(); i++ ) if( channelSelect->itemData(i) == settings["alsa_channel"].toString() ) channelSelect->setCurrentIndex( i ); #endif }
mixerVolume::mixerVolume(const char *name, const char *card, long volume) { snd_mixer_selem_id_t *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); } }
/** * @brief Get the Volume for the master channel. * * This function uses the Alsa API to get the volume * for the master channel. * * @param [out] ptr pointer to long, output will be between 0 and 99. * @return Void. */ void get_volume(long *ptr){ long min, max; snd_mixer_t *handle; snd_mixer_selem_id_t *sid; const char *card = "default"; //const char *selem_name = "Master"; const char *selem_name = "DAC2 Digital Course"; 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, selem_name); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); snd_mixer_selem_get_playback_volume_range(elem, &min, &max); printd("Volume range <%lu,%lu>\n", min, max); snd_mixer_selem_get_playback_volume(elem,0,ptr); printd("volume val = %lu\n",*ptr); *ptr /= (max / 100); snd_mixer_close(handle); }
int initMixer() { int result; if ((result = snd_mixer_open( &mixerFd, 0)) < 0) { printf("sucks1!\n"); mixerFd = NULL; return result; } if ((result = snd_mixer_attach( mixerFd, "default")) < 0) { printf("snd_mixer_attach error"); snd_mixer_close(mixerFd); mixerFd = NULL; return result; } if ((result = snd_mixer_selem_register( mixerFd, NULL, NULL)) < 0) { printf("snd_mixer_selem_register error"); snd_mixer_close(mixerFd); mixerFd = NULL; return result; } if ((result = snd_mixer_load( mixerFd)) < 0) { printf("snd_mixer_load error"); snd_mixer_close(mixerFd); mixerFd = NULL; return result; } return result; }
int NeoControl::openAlsaMixer() { system("alsactl -f /opt/qtmoko/etc/alsa-scenarios/gsmhandset.state restore"); int ret = 0; QString text(tr("Call volume settings\n\n")); if ((ret = snd_mixer_open(&mixerFd, 0)) < 0) { text += QString("snd_mixer_open error %1").arg(ret); goto err; } if ((ret = snd_mixer_attach(mixerFd, "default")) < 0) { text += QString("snd_mixer_attach error %1").arg(ret); goto err; } if ((ret = snd_mixer_selem_register(mixerFd, NULL, NULL)) < 0) { text += QString("snd_mixer_selem_register error %1").arg(ret); goto err; } if ((ret = snd_mixer_load(mixerFd)) < 0) { text += QString("snd_mixer_load error %1").arg(ret); goto err; } goto ok; err: if (mixerFd) snd_mixer_close(mixerFd); mixerFd = NULL; ok: label->setText(text); return ret; }
void SetAlsaSwitchMute(const char* card, const char* selem_name) { // long min, max; snd_mixer_t *handle; snd_mixer_selem_id_t *sid; 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, selem_name); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); int* muted; snd_mixer_selem_channel_id_t channel; snd_mixer_selem_get_playback_switch(elem,channel,muted); printf("Muted: %d\n",*muted); if (snd_mixer_selem_has_playback_switch(elem)) { snd_mixer_selem_set_playback_switch_all(elem, !*muted); } snd_mixer_close(handle); }
static int mixer_init(char *name, char *iname) { int n, i; struct pollfd *fds; if (snd_mixer_open(&mixer, 0) < 0) { fprintf(stderr, "can't open mixer\n"); exit(1); } snd_mixer_attach(mixer, "default"); snd_mixer_selem_register(mixer, NULL, NULL); snd_mixer_load(mixer); get_element(name, 1, &ch[0]); get_element(iname, 0, &ch[1]); n = snd_mixer_poll_descriptors_count(mixer); fds = calloc(n, sizeof(struct pollfd)); snd_mixer_poll_descriptors(mixer, fds, n); for (i = 0; i < n; i++) { GIOChannel* channel = g_io_channel_unix_new( fds[i].fd ); g_io_add_watch(channel, G_IO_IN|G_IO_HUP, on_mixer_event, NULL); g_io_channel_unref(channel); } return 0; }
char *getvolume() { char *buf; int active; long volume, min, max; snd_mixer_t *handle; snd_mixer_selem_id_t *sid; snd_mixer_elem_t* elem; 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, selem_name); elem = snd_mixer_find_selem(handle, sid); snd_mixer_selem_get_playback_volume_range(elem, &min, &max); snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &volume); snd_mixer_selem_get_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, &active); buf = xmalloc(10); if (active) snprintf(buf, 10, "%0.f%%", (double) volume / (double) max * 100); else snprintf(buf, 10, "off"); snd_mixer_close(handle); return buf; }
int volume_set(int volume) { long min, max; snd_mixer_t *handle; snd_mixer_selem_id_t *sid; const char *card = "default"; //const char *selem_name = "Master"; const char *selem_name = "Digital"; 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, selem_name); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); snd_mixer_selem_get_playback_volume_range(elem, &min, &max); int v = volume*max /100; snd_mixer_selem_set_playback_volume_all(elem, v); snd_mixer_close(handle); return v; }
char* getvolume() { int sound_on; double normalized_volume; int err; snd_mixer_t *handle; snd_mixer_selem_id_t *sid; snd_mixer_open(&handle, 0); snd_mixer_attach(handle, soundcard); 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, soundelement); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); err = snd_mixer_selem_get_playback_switch(elem, channel, &sound_on); if (err < 0 || !sound_on) { snd_mixer_close(handle); /* return smprintf("\x9D "); */ return smprintf("Muted \x19 "); } normalized_volume = getnormvolume(elem); snd_mixer_close(handle); /* return smprintf("%.0f\x90 ", normalized_volume * 100); */ return smprintf("Vol %.0f%% \x19 ", normalized_volume * 100); }
int AmeSystemSound::getMixer(snd_mixer_t **mixer, QString card) { char *dev; int err; dev = strdup(card.toAscii().data()); if ((err = snd_mixer_open(mixer, 0)) < 0) { qWarning("SoundOutput: Failed to open empty mixer: %s", snd_strerror(-err)); mixer = NULL; return -1; } if ((err = snd_mixer_attach(*mixer, dev)) < 0) { qWarning("SoundOutput: Attaching to mixer %s failed: %s", dev, snd_strerror(-err)); return -1; } if ((err = snd_mixer_selem_register(*mixer, NULL, NULL)) < 0) { qWarning("SoundOutput: Failed to register mixer: %s", snd_strerror(-err)); return -1; } if ((err = snd_mixer_load(*mixer)) < 0) { qWarning("SoundOutput: Failed to load mixer: %s", snd_strerror(-err)); return -1; } free(dev); return (*mixer != NULL); }
static long get_master_volume() { snd_mixer_t *handle; snd_mixer_open(&handle, 0); snd_mixer_attach(handle, Y50_CTL_NAME); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); snd_mixer_selem_id_t *master_sid; const char *master_selem_name = "Master"; snd_mixer_selem_id_alloca(&master_sid); snd_mixer_selem_id_set_index(master_sid, 0); snd_mixer_selem_id_set_name(master_sid, master_selem_name); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, master_sid); long min, max; snd_mixer_selem_get_playback_volume_range(elem, &min, &max); long volume; snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &volume); snd_mixer_close(handle); return volume * 100 / max; }
void set_volume(GtkAdjustment *adjustment, gpointer user_data) { int value = (int) 100 - gtk_adjustment_get_value(adjustment); g_debug("Setting master volume to: %d", value); if(value == 0) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(s_mute), TRUE); else gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(s_mute), FALSE); long min, max; snd_mixer_t *mix; snd_mixer_selem_id_t *sid; const char *card = "default"; const char *selem_name = "Master"; snd_mixer_open(&mix, 0); snd_mixer_attach(mix, card); snd_mixer_selem_register(mix, NULL, NULL); snd_mixer_load(mix); snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, 0); snd_mixer_selem_id_set_name(sid, selem_name); snd_mixer_elem_t* elem = snd_mixer_find_selem(mix, sid); snd_mixer_selem_get_playback_volume_range(elem, &min, &max); snd_mixer_selem_set_playback_volume_all(elem, value * max / 100); snd_mixer_close(mix); }
static snd_mixer_t *alsa_mixer_open(const char *mixdev){ snd_mixer_t *mixer=NULL; int err; err=snd_mixer_open(&mixer,0); if (err<0){ ms_warning("Could not open alsa mixer: %s",snd_strerror(err)); return NULL; } if ((err = snd_mixer_attach (mixer, mixdev)) < 0){ ms_warning("Could not attach mixer to card: %s",snd_strerror(err)); snd_mixer_close(mixer); return NULL; } if ((err = snd_mixer_selem_register (mixer, NULL, NULL)) < 0){ ms_warning("snd_mixer_selem_register: %s",snd_strerror(err)); snd_mixer_close(mixer); return NULL; } if ((err = snd_mixer_load (mixer)) < 0){ ms_warning("snd_mixer_load: %s",snd_strerror(err)); snd_mixer_close(mixer); return NULL; } return mixer; }
/** * Opens the mixer, attaches the alsa card to it, * registers the mixer simple element class and * loads the mixer elements. * * @param card HCTL name * @param opts Options container * @param level mixer level * @return the mixer handle, or NULL on failure */ static snd_mixer_t * open_mixer(const char *card, struct snd_mixer_selem_regopt *opts, int level) { int err; snd_mixer_t *mixer = NULL; DEBUG_PRINT("Card %s: opening mixer", card); if ((err = snd_mixer_open(&mixer, 0)) < 0) { DEBUG_PRINT("Card %s: mixer open error: %s", card, snd_strerror(err)); return NULL; } if (level == 0 && (err = snd_mixer_attach(mixer, card)) < 0) { DEBUG_PRINT("Card %s: mixer attach error: %s", card, snd_strerror(err)); snd_mixer_close(mixer); return NULL; } if ((err = snd_mixer_selem_register( mixer, level > 0 ? opts : NULL, NULL)) < 0) { DEBUG_PRINT("Card %s: mixer register error: %s", card, snd_strerror(err)); snd_mixer_close(mixer); return NULL; } if ((err = snd_mixer_load(mixer)) < 0) { DEBUG_PRINT("Card %s: mixer load error: %s", card, snd_strerror(err)); snd_mixer_close(mixer); return NULL; } return mixer; }
int volume_up(int vol_min, int vol_max) { 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, selem_name); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); snd_mixer_selem_get_playback_volume_range(elem, &min, &max); for (int i = vol_min; i<vol_max; i++) { int v = i*max /100; snd_mixer_selem_set_playback_volume_all(elem, v); //printf("min = %d, max= %d, volume = %d\n", min, max, volume); usleep(50000); } snd_mixer_close(handle); }
bool select_card_by_name(const char *device_name) { int err; bool opened; char *msg; close_hctl(); unplugged = FALSE; opened = FALSE; if (device_name) { err = snd_mixer_attach(mixer, device_name); if (err >= 0) opened = TRUE; else { msg = casprintf(_("Cannot open mixer device '%s'."), device_name); show_alsa_error(msg, err); free(msg); } } if (opened) { mixer_device_name = cstrdup(device_name); err = snd_mixer_load(mixer); if (err < 0) fatal_alsa_error(_("cannot load mixer controls"), err); } display_card_info(); set_view_mode(view_mode); return opened; }
char *get_vol(char *buf) { long max = 0, min = 0, vol = 0; int mute = 0; snd_mixer_t *handle; snd_mixer_elem_t *pcm_mixer, *mas_mixer; snd_mixer_selem_id_t *vol_info, *mute_info; snd_mixer_open(&handle, 0); snd_mixer_attach(handle, "default"); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); snd_mixer_selem_id_set_name(vol_info, VOL_CH); snd_mixer_selem_id_set_name(mute_info, VOL_CH); pcm_mixer = snd_mixer_find_selem(handle, vol_info); mas_mixer = snd_mixer_find_selem(handle, mute_info); snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); sprintf(buf, !(mute) ? VOL_MUTE_S : VOL_S, (int)vol * 100 / (int)max); if(vol_info) snd_mixer_selem_id_free(vol_info); if(mute_info) snd_mixer_selem_id_free(mute_info); if(handle) snd_mixer_close(handle); return buf; }
static int alsa_mixer_open(int *volume_max) { snd_mixer_selem_id_t *sid; snd_mixer_elem_t *elem; int count; int rc; snd_mixer_selem_id_alloca(&sid); rc = snd_mixer_open(&alsa_mixer_handle, 0); if (rc < 0) goto error; rc = snd_mixer_attach(alsa_mixer_handle, alsa_mixer_device); if (rc < 0) goto error; rc = snd_mixer_selem_register(alsa_mixer_handle, NULL, NULL); if (rc < 0) goto error; rc = snd_mixer_load(alsa_mixer_handle); if (rc < 0) goto error; count = snd_mixer_get_count(alsa_mixer_handle); if (count == 0) { d_print("error: mixer does not have elements\n"); return -2; } elem = snd_mixer_first_elem(alsa_mixer_handle); while (elem) { const char *name; int has_vol, has_switch; snd_mixer_selem_get_id(elem, sid); name = snd_mixer_selem_id_get_name(sid); d_print("name = %s\n", name); d_print("has playback volume = %d\n", snd_mixer_selem_has_playback_volume(elem)); d_print("has playback switch = %d\n", snd_mixer_selem_has_playback_switch(elem)); if (strcasecmp(name, alsa_mixer_element)) { elem = snd_mixer_elem_next(elem); continue; } has_vol = snd_mixer_selem_has_playback_volume(elem); if (!has_vol) { d_print("mixer element `%s' does not have playback volume\n", name); return -2; } snd_mixer_selem_get_playback_volume_range(elem, &mixer_vol_min, &mixer_vol_max); has_switch = snd_mixer_selem_has_playback_switch(elem); /* FIXME: get number of channels */ mixer_elem = elem; *volume_max = mixer_vol_max - mixer_vol_min; return 0; } d_print("error: mixer element `%s' not found\n", alsa_mixer_element); return -2; error: d_print("error: %s\n", snd_strerror(rc)); return -1; }
void* alsactl_open() { snd_mixer_t* handle; snd_mixer_open(&handle, 0); snd_mixer_attach(handle, card); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); return handle; }
/* * originally from * stackoverflow.com/questions/7657624/get-master-sound-volume-in-c-in-linux */ static int get_audio_volume(MixSelect select, long* outvol) { int ret = 0; snd_mixer_t* handle; snd_mixer_elem_t* elem; snd_mixer_selem_id_t* sid; long minv, maxv; snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, select.mix_index); snd_mixer_selem_id_set_name(sid, select.mix_name); if ((snd_mixer_open(&handle, 0)) < 0) { return -1; } if ((snd_mixer_attach(handle, select.card)) < 0) { snd_mixer_close(handle); return -2; } if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) { snd_mixer_close(handle); return -3; } ret = snd_mixer_load(handle); if (ret < 0) { snd_mixer_close(handle); return -4; } elem = snd_mixer_find_selem(handle, sid); if (!elem) { snd_mixer_close(handle); return -5; } snd_mixer_selem_get_playback_volume_range(elem, &minv, &maxv); if(snd_mixer_selem_get_playback_volume(elem, 0, outvol) < 0) { snd_mixer_close(handle); return -6; } // make volume out of 100 *outvol -= minv; maxv -= minv; minv = 0; *outvol = 100 * (*outvol) / maxv; snd_mixer_close(handle); return 0; }
void moko_alsa_volume_control_set_element_from_name (MokoAlsaVolumeControl *control, const gchar *name) { MokoAlsaVolumeControlPrivate *priv = ALSA_VOLUME_CONTROL_PRIVATE (control); if (!priv->device) return; detach_mixer (control); if (!name) { moko_alsa_volume_control_set_element (control, NULL); return; } open_mixer (control); if ((snd_mixer_attach (priv->mixer_handle, priv->device) == 0) && (snd_mixer_selem_register (priv->mixer_handle, NULL, NULL) == 0) && (snd_mixer_load (priv->mixer_handle) == 0)) { snd_mixer_elem_t *elem; elem = snd_mixer_first_elem (priv->mixer_handle); while (elem) { const char *elem_name = snd_mixer_selem_get_name (elem); if (strcmp (elem_name, name) == 0) break; elem = snd_mixer_elem_next (elem); } if (!elem) { snd_mixer_detach (priv->mixer_handle, priv->device); close_mixer (control); g_warning ("Mixer element '%s' not found", name); attach_mixer (control); } else { snd_mixer_selem_id_t *id; if (snd_mixer_selem_id_malloc (&id) != 0) { g_warning ("Unable to allocate element id"); snd_mixer_detach ( priv->mixer_handle, priv->device); close_mixer (control); } else { snd_mixer_selem_get_id (elem, id); snd_mixer_detach ( priv->mixer_handle, priv->device); close_mixer (control); g_debug ("Setting element ID"); moko_alsa_volume_control_set_element ( control, id); snd_mixer_selem_id_free (id); } } } else g_warning ("Unable to open mixer on card '%s'", priv->device); }
int AlsaMixer::load_handle() { int err; if (((err = snd_mixer_open(&this->handle, 0)) < 0) || ((err = snd_mixer_attach(this->handle, this->cardname.c_str())) < 0) || ((err = snd_mixer_selem_register(this->handle, NULL, NULL)) < 0) || ((err = snd_mixer_load(this->handle)) < 0)) return err; return 0; }
void asound_setup(const gchar * card, const gchar * channel, void (*volume_changed)(int,gboolean)) { // Make sure (for now) that the setup function only gets called once static int asound_setup_called = 0; assert(asound_setup_called == 0); asound_setup_called++; // Save card, volume_changed strcpy(m_card, card); m_volume_changed = volume_changed; // Load the mixer for the provided cardname snd_mixer_open(&m_mixer, 0); snd_mixer_attach(m_mixer, m_card); snd_mixer_selem_register(m_mixer, NULL, NULL); snd_mixer_load(m_mixer); // Setup g_io_watch for the mixer int count = snd_mixer_poll_descriptors_count(m_mixer); if(count >= 1) { struct pollfd pfd; count = snd_mixer_poll_descriptors(m_mixer, &pfd, 1); if(count == 1) { GIOChannel * giochannel = g_io_channel_unix_new(pfd.fd); g_io_add_watch_full(giochannel, G_PRIORITY_DEFAULT, G_IO_IN, asound_poll_cb, NULL, NULL); } } // Iterate over the elements in the mixer and store them in m_channel_names int elemcount = snd_mixer_get_count(m_mixer); snd_mixer_elem_t * elem = snd_mixer_first_elem(m_mixer); int loop; for(loop = 0; loop < elemcount; loop++) { const char * elemname = snd_mixer_selem_get_name(elem); if(snd_mixer_selem_has_playback_volume(elem)) { m_channel_names = g_list_append(m_channel_names, (gpointer)g_strdup(elemname)); } elem = snd_mixer_elem_next(elem); } // Setup m_elem using the provided channelname if(channel != NULL && asound_channel_exists(channel)) asound_set_channel(channel); else if(m_channel_names != NULL) asound_set_channel((const gchar*)m_channel_names->data); }
static void set_volume(AudioCapture *capture, int level) { snd_mixer_t *mixer=NULL; int err; err=snd_mixer_open(&mixer,0); if(err < 0) { fprintf(stderr, "Could not open alsa mixer: %s\r\n",snd_strerror(err)); exit(1); } char hw_id[100]; snprintf(hw_id, sizeof(hw_id), "hw:%d", capture->pcm_id); if ((err = snd_mixer_attach (mixer, hw_id)) < 0){ fprintf(stderr, "Could not attach mixer to card(%s): %s\r\n", hw_id, snd_strerror(err)); snd_mixer_close(mixer); exit(1); } if ((err = snd_mixer_selem_register (mixer, NULL, NULL)) < 0){ fprintf(stderr, "snd_mixer_selem_register: %s\r\n",snd_strerror(err)); snd_mixer_close(mixer); exit(1); } if ((err = snd_mixer_load (mixer)) < 0){ fprintf(stderr, "snd_mixer_load: %s\r\n",snd_strerror(err)); snd_mixer_close(mixer); exit(1); } snd_mixer_elem_t *elem; elem = snd_mixer_first_elem(mixer); while(elem) { const char *elemname = snd_mixer_selem_get_name(elem); if(strcmp(elemname, "Capture") && strcmp(elemname, "Mic")) { fprintf(stderr, "Skip setting volume for %s\r\n", elemname); elem=snd_mixer_elem_next(elem); continue; } if (snd_mixer_selem_has_capture_volume(elem)){ fprintf(stderr, "Set volume for %s\r\n", elemname); long sndMixerPMin; long sndMixerPMax; long newvol; snd_mixer_selem_get_playback_volume_range(elem, &sndMixerPMin, &sndMixerPMax); newvol=(((sndMixerPMax-sndMixerPMin)*level)/100)+sndMixerPMin; snd_mixer_selem_set_capture_volume_all(elem,newvol); elem=snd_mixer_elem_next(elem); } else { fprintf(stderr, "Can't set capture volume\r\n"); exit(1); } } }
static int initialize_mixer_device(struct audio_info_struct *ai, char *error) { int sts; snd_mixer_t *mixer; char *mixerdev = "default"; const char *elemnam; snd_mixer_elem_t *mixerelem; sts = snd_mixer_open(&mixer, 0); if (sts) { if (error) { sprintf(error, "snd_mixer_open failed; %d\n", sts); } return 1; } if (mixer) { sts = snd_mixer_attach(mixer, mixerdev); if (sts) { if (error) { sprintf(error, "snd_mixer_attach: failed; %d\n", sts); } return 1; } sts = snd_mixer_selem_register(mixer, NULL, NULL); if (sts) { if (error) { sprintf(error, "snd_mixer_selem_register: failed; %d\n", sts); } return 1; } sts = snd_mixer_load(mixer); if (sts) { if (error) { sprintf(error, "snd_mixer_selem_register: failed; %d\n", sts); } return 1; } mixerelem = snd_mixer_first_elem(mixer); snd_mixer_set_callback(mixer, mixer_callback_func); while (mixerelem) { elemnam = snd_mixer_selem_get_name(mixerelem); if (strcasecmp(elemnam, "Master") == 0) { ai->vh = mixerelem; return 0; } mixerelem = snd_mixer_elem_next(mixerelem); } } return 1; }