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; }
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; }
/** * 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 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); }
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); } }
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); }
/** * @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); }
static snd_mixer_elem_t *alsa_init_mixer_channel (const char *name, long *vol_min, long *vol_max) { snd_mixer_selem_id_t *sid; snd_mixer_elem_t *elem = NULL; snd_mixer_selem_id_malloc (&sid); snd_mixer_selem_id_set_index (sid, 0); snd_mixer_selem_id_set_name (sid, name); if (!(elem = snd_mixer_find_selem(mixer_handle, sid))) error ("Can't find mixer %s", name); else if (!snd_mixer_selem_has_playback_volume(elem)) { error ("Mixer device has no playback volume (%s).", name); elem = NULL; } else { snd_mixer_selem_get_playback_volume_range (elem, vol_min, vol_max); logit ("Opened mixer (%s), volume range: %ld-%ld", name, *vol_min, *vol_max); } snd_mixer_selem_id_free (sid); return elem; }
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); }
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); }
void asound_set_channel(const gchar * channel) { if(m_mixer == NULL || channel == NULL) { return; } if(g_strcmp0(channel, m_channel) == 0) return; // Clean up any previously set channels g_free(m_channel); m_channel = g_strdup(channel); if(m_elem) { snd_mixer_elem_set_callback(m_elem, NULL); m_elem = NULL; } // Setup m_elem using the provided channelname snd_mixer_selem_id_t * sid; snd_mixer_selem_id_malloc(&sid); snd_mixer_selem_id_set_name(sid, channel); m_elem = snd_mixer_find_selem(m_mixer, sid); if(m_elem != NULL) { snd_mixer_elem_set_callback(m_elem, asound_elem_event); snd_mixer_selem_id_free(sid); } }
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; }
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); }
snd_mixer_elem_t *getelem(const char *selem_name) { if (handle == NULL) init(); snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, 0); snd_mixer_selem_id_set_name(sid, selem_name); return snd_mixer_find_selem(handle, sid); }
bool AlsaMixer::GetVolumePercent( const char* channel, int* value ) { LOG( Logger::LOG_DEBUG, "AlsaMixer::GetVolume( %s )", channel); bool success = false; snd_mixer_elem_t *elem; 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, channel); elem = snd_mixer_find_selem(_handle, sid); if (!elem) { LOG( Logger::LOG_ERROR, "Unable to find simple control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); goto end; } if( snd_mixer_selem_has_playback_volume(elem) ) { long min, max; if( snd_mixer_selem_get_playback_volume_range(elem, &min, &max) < 0 ) { LOG( Logger::LOG_ERROR, "Unable to get playback volume range for control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); goto end; } long volume; for (int chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++) { if (snd_mixer_selem_get_playback_volume(elem, (snd_mixer_selem_channel_id_t)chn, &volume ) >= 0) { *value = (volume - min) * 100 / ( max - min ); success = true; break; } } }else if( snd_mixer_selem_has_capture_volume(elem) ) { long min, max; if( snd_mixer_selem_get_capture_volume_range(elem, &min, &max) < 0 ) { LOG( Logger::LOG_ERROR, "Unable to get capture volume range for control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); goto end; } long volume; for (int chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++) { if (snd_mixer_selem_get_capture_volume(elem, (snd_mixer_selem_channel_id_t)chn, &volume ) >= 0) { *value = (volume - min) * 100 / ( max - min ); success = true; break; } } } if( !success ) { LOG( Logger::LOG_ERROR, "Error getting control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); } end: return success; }
/* * 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; }
static snd_mixer_elem_t* alsactl_find_selem(void* handle, char* name) { 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, name); snd_mixer_elem_t* elem = snd_mixer_find_selem((snd_mixer_t*)handle, sid); free(name); // was malloced from go src by C.CString() return elem; }
int main(void) { Display *dpy; struct iwreq wreq; snd_mixer_t *handle; snd_mixer_elem_t *elem; snd_mixer_selem_id_t *vol_info; int sockfd, loops = 60; char *status, *mpd, *net, *vol, *bat, *clk; if(!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "dwmst: cannot open display.\n"); exit(EXIT_FAILURE); } memset(&wreq, 0, sizeof(struct iwreq)); snprintf(wreq.ifr_name, sizeof(WIRELESS_DEVICE), WIRELESS_DEVICE); sockfd = socket(AF_INET, SOCK_DGRAM, 0); 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_set_name(vol_info, VOL_CH); elem = snd_mixer_find_selem(handle, vol_info); if(elem == NULL) { fprintf(stderr, "dwmst: can not open device.\n"); cleanup(dpy, sockfd, handle, vol_info); exit(EXIT_FAILURE); } for(;;sleep(INTERVAL)) { if(++loops > 60) { loops = 0; mpd = get_mpd(); net = get_net(wreq, sockfd); bat = get_bat(); clk = get_time(); } vol = get_vol(handle, elem); status = smprintf("%s %s %s %s %s", mpd, net, vol, bat, clk); setstatus(dpy, status); free(vol); free(status); } free(mpd); free(net); free(bat); free(clk); cleanup(dpy, sockfd, handle, vol_info); exit(EXIT_SUCCESS); }
int eDVBVolumecontrol::openMixer() { #ifdef HAVE_ALSA if (!mainVolume) { int err; char *card = ALSA_CARD; eDebug("[eDVBVolumecontrol] Setup ALSA Mixer %s - %s", ALSA_CARD, ALSA_VOLUME_MIXER); /* Perform the necessary pre-amble to start up ALSA Mixer */ err = snd_mixer_open(&alsaMixerHandle, 0); if (err < 0) { eDebug("Mixer %s open error: %s", card, snd_strerror(err)); return err; } err = snd_mixer_attach(alsaMixerHandle, card); if (err < 0) { eDebug("Mixer attach %s error: %s", card, snd_strerror(err)); snd_mixer_close(alsaMixerHandle); alsaMixerHandle = NULL; return err; } err = snd_mixer_selem_register(alsaMixerHandle, NULL, NULL); if (err < 0) { eDebug("Mixer register error: %s", snd_strerror(err)); snd_mixer_close(alsaMixerHandle); alsaMixerHandle = NULL; return err; } err = snd_mixer_load(alsaMixerHandle); if (err < 0) { eDebug("Mixer %s load error: %s", card, snd_strerror(err)); snd_mixer_close(alsaMixerHandle); alsaMixerHandle = NULL; return err; } /* Set up Decoder 0 as the main volume control. */ 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, "Master"); mainVolume = snd_mixer_find_selem(alsaMixerHandle, sid); } return mainVolume ? 0 : -1; #else return open( AUDIO_DEV, O_RDWR ); #endif }
/* Set volume information */ int alsa_sound(char *stat) { int mute=0, realvol=0, len = 0; long vol=0, min=0, max=0; snd_mixer_t *handle; /* init alsa */ snd_mixer_selem_id_t *vol_info; /* init channel with volume info */ snd_mixer_selem_id_t *mute_info; /* init channel with 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_set_name(vol_info, VOL_CH); snd_mixer_elem_t* pcm_mixer = snd_mixer_find_selem(handle, vol_info); snd_mixer_selem_get_playback_volume_range(pcm_mixer, &min, &max); /* get volume */ snd_mixer_selem_get_playback_volume(pcm_mixer, SND_MIXER_SCHN_MONO, &vol); snd_mixer_selem_id_malloc(&mute_info); snd_mixer_selem_id_set_name(mute_info, VOL_CH); snd_mixer_elem_t* mas_mixer = snd_mixer_find_selem(handle, mute_info); snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); /* get mute state */ if(mute == 0) len = sprintf(stat, VOL_MUTE_STR, 0); else { realvol = (vol*100)/max; len = sprintf(stat, VOL_STR, realvol); } 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 len; }
int get_vol_mute(long* vol, int* mute) { snd_mixer_t* handle; int open_code = snd_mixer_open(&handle, 0); if (open_code != 0) return open_code; int attach_code = snd_mixer_attach(handle, "default"); if (attach_code != 0) { snd_mixer_close(handle); return attach_code; } int register_code = snd_mixer_selem_register(handle, NULL, NULL); if (register_code != 0) { cleanup_handle(handle); return register_code; } int load_code = snd_mixer_load(handle); if (load_code != 0) { cleanup_handle(handle); return load_code; } 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, "Master"); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); if (elem == 0) { cleanup_handle(handle); return 1; } int vol_code = snd_mixer_selem_get_playback_dB(elem, SND_MIXER_SCHN_UNKNOWN, vol); if (vol_code != 0) { cleanup_handle(handle); return vol_code; } int switch_code = snd_mixer_selem_get_playback_switch(elem, SND_MIXER_SCHN_UNKNOWN, mute); if (switch_code != 0) { cleanup_handle(handle); return switch_code; } /* snd_mixer_selem_id_free(sid); */ snd_mixer_detach(handle, "default"); snd_mixer_close(handle); return 0; }
static char * get_vol() { long vol = 0, max = 0, min = 0; int mute = 0, realvol = 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, SOUNDCARD); 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); if (!mute) realvol = 0; else realvol = (vol * 100) / 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 smprintf("%d%%", realvol); }
float getvolume(snd_mixer_t *handle, const char* vol_ch) { int mute = 0; long vol = 0, max = 0, min = 0; snd_mixer_elem_t *pcm_mixer, *max_mixer; snd_mixer_selem_id_t *vol_info, *mute_info; /*ToDo: maybe move all this to main?*/ snd_mixer_handle_events(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); max_mixer = snd_mixer_find_selem(handle, mute_info); snd_mixer_selem_get_playback_volume_range(pcm_mixer, &min, &max); snd_mixer_selem_get_playback_volume(pcm_mixer, 0, &vol); snd_mixer_selem_get_playback_switch(max_mixer, 0, &mute); snd_mixer_selem_id_free(vol_info); snd_mixer_selem_id_free(mute_info); return ((float)vol/(float)max)*100; }
snd_mixer_elem_t* AmeSystemSound::getMixerElem(snd_mixer_t *mixer, char *name, int index) { snd_mixer_selem_id_t* selem_id; snd_mixer_elem_t* elem; snd_mixer_selem_id_alloca(&selem_id); if (index != -1) snd_mixer_selem_id_set_index(selem_id, index); if (name != NULL) snd_mixer_selem_id_set_name(selem_id, name); elem = snd_mixer_find_selem(mixer, selem_id); return elem; }
/* Retrieve a mixer element by name * * \param mixer The mixer to search the element from * \param element_name The element name to search for */ snd_mixer_elem_t *mixer_get_element(snd_mixer_t *mixer, const char *element_name) { pa_assert(mixer); pa_assert(element_name); snd_mixer_selem_id_t *sid = NULL; snd_mixer_elem_t *mixer_element = NULL; snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_name(sid, element_name); snd_mixer_selem_id_set_index(sid, 0); mixer_element = snd_mixer_find_selem(mixer, sid); return mixer_element; }
static int supports_pcm_output_volume(px_mixer *Px) { PxInfo *info = (PxInfo *)Px->info; snd_mixer_selem_id_t *id; if (info->playback.handle) { snd_mixer_selem_id_alloca(&id); snd_mixer_selem_id_set_name(id, "PCM"); if (snd_mixer_find_selem(info->playback.handle, id)) { return TRUE; } } return FALSE; }
static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid) { int c, size; char buf[128]; char *ptr = buf; while (*str == ' ' || *str == '\t') str++; if (!(*str)) return -EINVAL; size = 1; /* for '\0' */ if (*str != '"' && *str != '\'') { while (*str && *str != ',') { if (size < (int)sizeof(buf)) { *ptr++ = *str; size++; } str++; } } else { c = *str++; while (*str && *str != c) { if (size < (int)sizeof(buf)) { *ptr++ = *str; size++; } str++; } if (*str == c) str++; } if (*str == '\0') { snd_mixer_selem_id_set_index(sid, 0); *ptr = 0; goto _set; } if (*str != ',') return -EINVAL; *ptr = 0; /* terminate the string */ str++; if (!isdigit(*str)) return -EINVAL; snd_mixer_selem_id_set_index(sid, atoi(str)); _set: snd_mixer_selem_id_set_name(sid, buf); return 0; }
long AudioALSA::getVolume() const { snd_mixer_t* handle; snd_mixer_elem_t* elem; snd_mixer_selem_id_t* sid; // Open sound card snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, this->mixIndex); snd_mixer_selem_id_set_name(sid, this->mixName.c_str()); if (snd_mixer_open(&handle, 0) < 0) { throw std::runtime_error("Error while mixer channel \"" + mixName + "\" opening"); } if (snd_mixer_attach(handle, this->card.c_str())) { snd_mixer_close(handle); throw std::runtime_error("Error while attaching soundcard \"" + card + "\" to mixer"); } if (snd_mixer_selem_register(handle, NULL, NULL) < 0) { snd_mixer_close(handle); throw std::runtime_error("Error while mixer channel registering"); } if (snd_mixer_load(handle) < 0) { snd_mixer_close(handle); throw std::runtime_error("Error while loading mixer elements"); } elem = snd_mixer_find_selem(handle, sid); if (!elem) { snd_mixer_close(handle); throw std::runtime_error("Error while finding mixer element"); } // Get sound volume long volume, vmin, vmax; snd_mixer_selem_get_playback_volume_range(elem, &vmin, &vmax); if (snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &volume) < 0) { snd_mixer_close(handle); throw std::runtime_error("Error while getting playback volume"); } volume -= vmin; vmax -= vmin; volume = 100 * volume / vmax; snd_mixer_close(handle); return volume; }
bool AlsaMixer::GetChannelSwitch( const char* channel, bool* on ) { bool success = false; snd_mixer_elem_t *elem; 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, channel); elem = snd_mixer_find_selem(_handle, sid); if (!elem) { LOG( Logger::LOG_ERROR, "Unable to find simple control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); goto end; } if( snd_mixer_selem_has_playback_switch(elem) ) { for (int chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++) { int value; if (snd_mixer_selem_get_playback_switch(elem, (snd_mixer_selem_channel_id_t)chn, &value) >= 0) { *on = value != 0; success = true; break; } } }else if( snd_mixer_selem_has_capture_switch(elem) ) { for (int chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++) { int value; if (snd_mixer_selem_get_capture_switch(elem, (snd_mixer_selem_channel_id_t)chn, &value ) >= 0) { *on = value != 0; success = true; break; } } } if( !success ) { LOG( Logger::LOG_ERROR, "Error getting control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); } end: return success; }