This code is for alsa mixer volume
Code:
static const char alsa_core_devnames[] = "default";
static char *card, *channel;
static int muted = 0;
static int mutecount = 0;
static snd_mixer_t *handle = NULL;
static snd_mixer_elem_t *elem = NULL;
static long alsa_min, alsa_max, alsa_vol;
/* Volume saved to file */
static int alsa_get_unmute_volume( void )
{
long val;
assert (elem);
if (snd_mixer_selem_is_playback_mono(elem)) {
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &val);
return val;
} else {
int c, n = 0;
long sum = 0;
for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) {
if (snd_mixer_selem_has_playback_channel(elem, c)) {
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &val);
sum += val;
n++;
}
}
if (! n) {
return 0;
}
val = sum / n;
sum = (long)((double)(alsa_vol * (alsa_max - alsa_min)) / 100. + 0.5);
if (sum != val) {
alsa_vol = (long)(((val * 100.) / (alsa_max - alsa_min)) + 0.5);
}
return alsa_vol;
}
}
static int alsa_get_volume( void )
{
if (muted)
return 0;
else
return alsa_get_unmute_volume();
}
static int alsa_set_volume( int percentdiff )
{
long volume;
alsa_get_volume();
alsa_vol += percentdiff;
if( alsa_vol > 100 ) alsa_vol = 100;
if( alsa_vol < 0 ) alsa_vol = 0;
volume = (long)((alsa_vol * (alsa_max - alsa_min) / 100.) + 0.5);
snd_mixer_selem_set_playback_volume_all(elem, volume + alsa_min);
snd_mixer_selem_set_playback_switch_all(elem, 1);
muted = 0;
mutecount = 0;
return alsa_vol;
}
static void alsa_mute( int mute )
{
if( !mute && mutecount ) mutecount--;
if( mutecount ) return;
if( mute ) {
mutecount++;
muted = 1;
if (snd_mixer_selem_has_playback_switch(elem))
snd_mixer_selem_set_playback_switch_all(elem, 0);
else
fprintf(stderr, "mixer: mute not implemented\n");
} else {
muted = 0;
if (snd_mixer_selem_has_playback_switch(elem))
snd_mixer_selem_set_playback_switch_all(elem, 1);
else
fprintf(stderr, "mixer: mute not implemented\n");
}
}
static int alsa_ismute( void )
{
return muted;
}
Please help me to write a GtkVolumeButton to work with the code above with the following functions:
1. volume value changed
2. toggle mute volume
Tried this but when value from gtk button is changed up or down, alsa mixer always jumps to 100 %:
Code:
void start_mixer(GtkWidget *app)
{
gint vol;
char *mixer_device = "hw:0/Line";
mixer_set_device(mixer_device);
vol = alsa_get_volume();
if (vol >= 0) {
gtk_volume_button_set_value(button, vol);
}
}
int gtk_volume_button_get_value (GtkWidget *button)
{
return (int) (gtk_scale_button_get_value(GTK_SCALE_BUTTON(button)) * 100);
}
void gtk_volume_button_set_value (GtkWidget *button, int value)
{
gtk_scale_button_set_value(GTK_SCALE_BUTTON(button), (gdouble) value / 100);
}
void volume_value_changed_cb(GtkVolumeButton *button, gpointer user_data)
{
int vol = (int)(gtk_volume_button_get_value(button) + 0.5);
alsa_set_volume(vol);
}
void toggle_volume(void)
{
static int old_vol;
int vol = alsa_get_volume();
if (vol) {
old_vol = vol;
vol = 0;
} else {
vol = old_vol;
}
alsa_set_volume(vol);
gtk_volume_button_set_value(button, vol);
}