Category Archives: Music

Using JACK, for everything.

If you have ever used JACK before, you will know it’s a low-latency sound server daemon. It uses ALSA (or OSS, etc) as a backend, and allows you to do sound in realtime. It also handles communication between ‘jackified’ applications differently. You pretty much set them up in a similar way you would in a studio, plugging outputs into inputs, etc.

Anyway, JACK is used for sound production in Linux because ALSA, Pulseaudio, etc are far too slow. This is excellent, but there is a problem. Not all applications support JACK. Adobe Flash for example, does not.

For a long time this was a limiting factor for me, and the reason I only started JACK when I needed it. Not anymore! Here are a few simple steps to get JACK running all the time and not notice! (under Gentoo!)

You must add ‘jack’ to your USE flags, to get JACK support in all applications you have installed. Go ahead and update that.

emerge --ask --deep --newuse world

Things that will work without a problem are mpd, mplayer.

Next configure wine, by running winecfg and ticking JACK under sound. Untick ALSA.

Finally, the next step is to trick ALSA enabled applications into using JACK. Add the following lines to /etc/asound.conf

pcm.!default {
	type plug
	slave { pcm "jack" }
}
 
pcm.jack {
	type jack
 
	playback_ports {
 
		0 alsa_pcm:playback_1
		1 alsa_pcm:playback_2
 
	}
 
	capture_ports {
 
		0 alsa_pcm:capture_1
		1 alsa_pcm:capture_2
 
	}
}
 
ctl.mixer0 {
	type hw
	card 0
}

That’s it! Now Adobe Flash will work in Firefox! Go ahead and test it out! (you must have jackd running of course!)

It is useful to note that mpd will pick up JACK and ALSA when it starts, so if you only configure JACK in ~/.mpdconf it will work fine. Otherwise you will hear everything twice.

audio_output {
	type "jack"
	name "MPD JACK output"
}

This is great, but you will notice you must start jackd manually. This is tedious. Here’s how to set up an /etc/init.d script.

/etc/init.d/jackd

#!/sbin/runscript
# This programm will be used by init in order to launch jackd with the privileges
# and id of the user defined into /etc/conf.d/jackd
 
depend() {
	need alsasound
}
 
 start() {
	if ! test -f "${JACKDHOME}/.jackdrc"; then
		eerror "You must start and configure jackd before launch it. Sorry."
		eerror "You can use qjackctl for that."
		return 1
	else JACKDOPTS=$(cat "${JACKDHOME}/.jackdrc"|sed -e 's\/usr/bin/jackd \\')
	fi
 
	if [ -e /var/run/jackd.pid ]; then
		rm /var/run/jackd.pid
 	fi
 
	ebegin "Starting JACK Daemon"
	env HOME="${JACKDHOME}" start-stop-daemon --start \
			--quiet --background \
			--make-pidfile --pidfile /var/run/jackd.pid \
			-c ${JACKDUSER} \
			-x /usr/bin/jackd -- ${JACKDOPTS} >${LOG}
 
	sleep 2
 
	if ! pgrep -u ${JACKDUSER} jackd > /dev/null; then
		eerror "JACK daemon can't be started! Check logfile: ${LOG}"
 	fi
 
	eend $?
}
 
stop() {
	ebegin "Stopping JACK daemon -- please wait"
 	start-stop-daemon --stop --pidfile /var/run/jackd.pid &>/dev/null
 	eend $?
}	
 
restart() {
	svc_stop
	while `pgrep -u ${JACKDUSER} jackd >/dev/null`; do
		sleep 1
 	done
 	svc_start
}

/etc/conf.d/jackd

# owner of jackd process (Must be an existing user.)
JACKDUSER="<user>"
 
# .jackdrc location for that user (Must be existing, JACKDUSER can use
# qjackctl in order to create it.) 
JACKDHOME="/home/${JACKDUSER}" 
 
# logfile (/dev/null for nowhere)
LOG=/var/log/jackd.log

~/.jackdrc

/usr/bin/jackd -R -d alsa

This is just the arguments you would usually use to start jackd. You don’t need to install qjackctl if you know what you’re doing!

Finally add /etc/init.d/jackd to startup!

# rc-update add jackd default

And you’re done!

Gentoo and MIDI keyboard

I recently purchased a M-Audio Keystation Pro-88 MIDI keyboard, so I could continue playing the piano while I am at uni. I got the keyboard working well under Windows using Reason and Reason Pianos – but being a Linux geek I couldn’t be booting into Windows every day just to play. Wine isn’t an option, sound work is intensive and I didn’t want to rely on Windows emulation. There must be a way to do this without Windows emulation and still get decent results.

After only an hour or so Googling and setting things up, my keyboard was up and running in Linux, and here’s how I did it.

Install and setup JACK
You’re going to need a decent sound server for audio work. When it comes to Linux, JACK is the only option. I used these instructions to get a JACK server up and running in no time at all. http://en.gentoo-wiki.com/wiki/JACK

Install snd_usb_audio kernel module
You will need the snd_usb_audio kernel module. This can be found:


Device Drivers --->
Sound card support --->
Advanced Linux Sound Architecture --->
[*] USB sound devices --->
USB Audio/MIDI driver

Install qjackctl and qsynth
qjackctl is the tool you will be using to start/stop JACK and setup the correct connections.
qsynth is a soundfont reader which will turn your MIDI input into a piano.

You can use JACK via command line and avoid using qjackctl. This is preferred but is more complicated and beyond the scope of this article. Maybe another time.

emerge -a qjackctl qsynth

Get the Acoustic Grand Piano soundfont
This can be found here: http://zenvoid.org/audio.html

wget http://zenvoid.org/audio/acoustic_grand_piano_ydp_20080910.sf2

Start qjackctl and qsynth
Do this however you want.

Start JACK sound server
ALSA will need to be free for this, so close Firefox and killall mpd. (mpd can use JACK, so can mplayer. Firefox can’t!)

Finally
Load the soundfont into qsynth, and using the qjackctl connections dialog connect the MIDI keyboard to qsynth, under the “ALSA” tab.

DONE

Let me know if you find any more decent free Piano soundfonts.

A long awaited blog post…

It’s been a while since I made a post, but here I am. Back again. What’s new? Not a lot. Finished my A level exams (just got STEP to go), had prom the other day which was fun. But now, down to business.

I wanted to write about an awesome little script setup I’ve been working on over the last few days. Intended solely to make my life easier, I am very happy with how they turned out!

This system uses mpd, dmenu, xmonad.hs and a handy perl script. I have linked them all together in what I feel is an epic music setup.

I started by writing a script called plmpc. It’s written in Perl.

#!/usr/bin/perl
 
use strict;
use warnings;
 
use Switch;
 
## CONFIG
my $fadetime = 1;
## END CONFIG
 
my $dmp = `mpc`;
$dmp =~ /volume:([0-9]*)%/;
my $vol = $1;
 
my $playing;
if ($dmp =~ /playing/) {
	$playing = 1;
} else {
	$playing = 0;
}
 
 
if ($#ARGV+1 > 0) {
	my $arg = $ARGV[0];
 
	switch($arg) {
		case 'stop'	{ stop(); 	}
		case 'pause'	{ pause(); 	}
		case 'play'	{ play(); 	}
 
		case 'splay'	{
			my $term = '';
			foreach my $argnum (1 .. $#ARGV) {
				$term = $term." $ARGV[$argnum]";
			}
 
			$term =~ s/^ //;
 
			print "Searching: $term\n";
 
			my $tmp = `mpc playlist | wc -l`;
			$tmp =~ /([0-9]*)/;
			my $toplay = $1;
			$toplay++;
 
			print `mpc search any "$term" | mpc add`;
			`mpc play $toplay`;
		}
 
		case 'toggle'	{ ($playing eq 1) ? pause() : play(); }
 
		else  {
			my $cmds = '';
			my $c = 0;
			while($c < $#ARGV+1) {
				$cmds = $cmds.' '.$ARGV[$c];				
				$c += 1;
			}
 
			system("mpc$cmds");
		}
	}
} else {
	system("mpc");	
}
 
 
#####################
 
sub play {
	my $tmp = 0;
 
	`mpc volume 0`;
	system('mpc play');
	while($tmp < $vol) {
		$tmp += 2;
		`mpc volume $tmp`;
		sleep($fadetime/50);
	}
}
 
sub stop {
	my $tmp = $vol;
 
	while ($tmp > 0) {
		$tmp -= 2;
		`mpc volume $tmp`;
		sleep($fadetime/50);
	}
 
	system('mpc stop');
	`mpc volume $vol`;
}
 
 
sub pause {
	my $tmp = $vol;
 
	while ($tmp > 0) {
		$tmp -= 2;
		`mpc volume $tmp`;
		sleep($fadetime/50);
	}
 
	system('mpc pause');
	`mpc volume $vol`;
}

This script does exactly the same as mpc, but if you issue a stop, pause, play or toggle command it will fade the sound. Handy. It also has another trick up it’s sleeve. “splay” will search for items, add them to the playlist and start playing them.

Next I built some commands into xmonad:

, ((modMask .|. controlMask, xK_period), spawn "mpc next")
, ((modMask .|. controlMask, xK_comma ), spawn "mpc prev")
, ((modMask .|. controlMask, xK_m ), spawn "mpc toggle")
, ((modMask .|. controlMask, xK_n ), spawn "mpc stop")
, ((modMask .|. controlMask, xK_z ), spawn "mpc volume -2")
, ((modMask .|. controlMask, xK_x ), spawn "mpc volume +2")
, ((modMask .|. controlMask, xK_c ), spawn "mpd; mpc clear; mpc ls | mpc add; mpc random on; mpc play")
, ((modMask .|. controlMask, xK_v ), spawn "mpc random")
, ((modMask .|. controlMask, xK_b ), spawn "plmpc splay `echo | dmenu -nb '#222222' -nf '#999999' -sb '#5B7496' -fn '-*-montecarlo-medium-*-*-*-*-*-*-*-*-*-*-*'`")

I should note that the plmpc script should be in ~/bin/ and ~/bin/ should be in $PATH.

All of these (I should hope) are self explanatory – but the last line is the coolest of them all. I used dmenu as a simple input device for my “plmpc splay” script.

Sorry for the terrible formatting; WordPress’ fault not mine. ;)