You are not logged in.

#1 2015-09-29 16:09:53

decadentgray
Member
Registered: 2014-10-16
Posts: 8

perl get audio volume via event, avoiding polling

Hello,

I'm tinkering on a small mixer using perl-gtk2, and I try to get volume changes in real time. So far I only can poll for them and process the results, wich is kinda resource wasting.
At the moment it looks like this:

	sub label_timeout
	{    
		$mixervolume = qx($mixer_read_command);
		$label->set_text($mixervolume);

		return TRUE;
	}
	label_timeout();

	$label->{timer} = Glib::Timeout->add(128, \&label_timeout, $label);

With $mixer_read_command being

amixer get Master | egrep -o "[0-9]+%" | sed 's/.$//' | tail -n 1

The amixer line is clumsy as hell (grep, awk and sed is'nt all to fast I'd assume), and running it 7 times a second looks like a bad idea to me.

Is there any way (pulseaudio or alsa) to get some kind of event into my perl script when the volume changes?

My best regards and thanks in advance,
dg

Offline

#2 2015-09-29 16:57:47

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: perl get audio volume via event, avoiding polling

Awk would be a lot faster than your current pipeline:

awk -F'[][]' '/[0-9]+%/ {print +$2}' <(amixer get Master)

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2015-10-03 15:34:19

decadentgray
Member
Registered: 2014-10-16
Posts: 8

Re: perl get audio volume via event, avoiding polling

Thanks, this helps a lot wink

Post Scriptum:

Here is the code of the mixer subroutine. It's still a bit rough (and I'm more hacking this thing together, I'm not too much of a programmer I fear), but it works wink

sub module_mixer
{
	my $label = Gtk2::Label->new();
	my $mixervolume="a";
	my $tmpvolume;
	my $user_control=0;


	sub scale_set_default_values
	{
		my $scale = shift;
		$scale->set_update_policy('continuous');
		$scale->set_digits(0);
		$scale->set_value_pos('right');
		$scale->set_draw_value(FALSE);
	}


	my $adj1 = Gtk2::Adjustment->new(qx($configfile{"mixerget"}), 0.0, 101.0, 1.0, 1.0, 1.0);
	my $hscale = Gtk2::HScale->new($adj1);
	$hscale->set_size_request(200, -1);
	scale_set_default_values($hscale);



	$hbox->pack_start($hscale, TRUE, TRUE, 0);

	sub volume_timeout
	{    
		if ( $user_control ne "1" ) {
			$tmpvolume = qx($configfile{"mixerget"});
			chomp $tmpvolume;
			if ( $tmpvolume ne $mixervolume ) {
				$mixervolume = $tmpvolume;
				$adj1->set_value ($mixervolume);
	
				}

		}
		return TRUE;
	}
	volume_timeout();


	sub volume_callback
	{
	        $user_control = 1;
		my ($adj) = @_;
	        my $mixervalue = $adj->value;
		$mixervalue = int($mixervalue + 0.5);
		my $mixercmd = $configfile{"cmd-mixer-set-volume-left"} . " $mixervalue" . $configfile{"cmd-mixer-set-volume-right"};
		qx($mixercmd);
		$user_control = 0;
	}
	

	$hscale->{timer} = Glib::Timeout->add(128, \&volume_timeout, $hscale);
	$adj1->signal_connect(value_changed => \&volume_callback);

	$hscale->show;


}

Last edited by decadentgray (2015-10-03 16:28:15)

Offline

Board footer

Powered by FluxBB