Monday, December 5, 2011

Experimenting with Rosegarden and MIDI

Some time ago I managed to get notes for the classic klezmer nigun "Dos Zekele Mit Koyln" - דאָס זעקעלע מיט קוילן (heartful thanks go to Bob Freedman and his great archive). Recently I decided to try to put them into digital MIDI format using some good music editor for Linux. Since the experience was new to me, I decided to describe the steps in case someone will find them useful (all steps were taken on the current Debian testing, so for other distributions you'll need to adjust them accordingly).

First of all, one is faced with the choice which editor to use. There is a number of them available, but since I needed something that could allow me to type in music notation, I picked Rosegarden, which has a nice and rich UI for notes editing. However using it can be rather confusing in the beginning. First of all, installing Rosegarden alone is not sufficient, I soon was learning that it also requires a synthesizer to function. I didn't bother with finding a hardware synthesizer, so I picked one of the commonly available software options - Fluidsynth (which also has a nice GUI front end Qsynth). Software synthesizers however rely on sound fonts. So one needs to install some sound fonts as well (luckily some are available for Fluidsynth).

Installing it all on Debian is actually rather easy:

sudo apt-get install rosegarden qsynth fluidsynth fluid-soundfont-gm

With next step you need to configure Fluidsynth. Run Qsynth (qsynth) and go to Setup > Sound Fonts there. Press Open and put in the path to your Fluidsynth sound fonts. In my case it was:
/usr/share/sounds/sf2/FluidR3_GM.sf2
(GM stands for General MIDI here). Also, choose appropriate MIDI driver in Setup > Midi section. I needed something simple, so I picked alsa_seq there (if you are planning on using jackd, or oss4 - make the choice accordingly).

After that, Qsynth restarts. Now, run Rosegarden, while Qsynth is running. It's the key sequence, if you run Rosegarden without Qsynth running first - you'll get no sound! I didn't really need to configure Rosegarden MIDI devices, since it picked General MIDI device which connected to correct synth port, which was provided by Fluidsynth. But probably in some cases you'll need to configure them to work together, so you can do it from Studio > Manage MIDI devices.

This should be basically enough to start editing music.

You'll see 32 tracks (channels) in Rosegarden UI by default, and for something simple you probably don't need that much. So for my test I deleted all tracks except for the first 2 (solo and bass). Just click on some track (in the column with the list of them on the left) and press Ctrl+D (or Tracks > Delete Track).









Now, switch to Draw mode (F3, or pencil button in the toolbar), and select some region in your tracks. For example a few measures for each track, and you can always stretch them to more measures switching to Select and Edit mode (F2).

 








You can now select both of your tracks in Select mode (just make a rectangle on them holding the left mouse button), and then double click on the selection. It'll open the music notation sheet at last! Edit the music as you need (it has lot's of options), and then you can export it as MIDI. 





Changing the instrument however was a bit confusing, because one part of the UI was hidden by default. In order to enable it press P or do: View > Show Special Parameters. It will open a panel in UI for instruments management. Now choose the desired track (as before) and switch your instruments in "Instruments Parameters" section.

Here is my first result - Koyln edited in Rosegarden :)

Here you can hear Koyln performed by Mishka Zinganoff in 1919:



(You can play MIDI files in VLC by the way, if you enable and configure the MIDI plugin. On Debian I also had to install vlc-plugin-fluidsynth package for that, and to set up sound fonts in VLC configuration).

Monday, October 3, 2011

Protocol prefix trimming in Frefox 7

Firefox 7 introduced a controversial idea of omitting http:// protocol prefix from the URL bar (other prefixes such as https:// or ftp:// aren't omitted). This is very irritating for me personally and I see it as an inconsistent approach which falls into a general modern trend of oversimplification.

Fortunately, it's possible to disable this behavior by setting the following key in about:config to false:

browser.urlbar.trimURLs


Enjoy the full URL again.

Tuesday, June 14, 2011

Some fun with audio forwarding through ssh

It's pretty simple to forward audio to remote machine using ssh and mplayer. Here is an example how to forward local microphone stream (using ALSA recorder on Linux on local machine with ogg encoder for compression, and mplayer on remote machine):

arecord -f cd -t raw | oggenc - -r | ssh <user>@<remotehost> 'cat - | mplayer -'

If needed change ALSA recorder to any other mic recorder, and just redirect its output to the encoder.

And here is how to forward playing of local audio file to remote machine (cache setting enables mplayer to accumulate the stream, otherwise it fails when too much data comes at once):

cat <audiofile> | ssh <user>@<remotehost> 'cat - | mplayer -cache 8192 -'

Have fun with your audio.

Monday, November 8, 2010

Thunderbird new mail notification for freedesktop compliant systems

For a while it bothered me, that Thunderbird doesn't not provide an integrated new mail notification for freedesktop compliant systems - it doesn't display new mail icon in system tray. While there is moztraybiff add-on - it's not up to date for a while already, and it requires recompilation for each particular OS.

Recently I came across Mailbox Alert add-on which allows executing a custom program when new mail appears in a given folder (it can be conveniently configured for each folder separately). This gave me an idea how to make a simple new mail notification alert. So I wrote a small script for that (it uses wmctrl utility which appeared not to be installed by default on my OpenIndiana and Debian Linux where I tested this script, but it's available for installation from standard repositories).

Create let's say ~/bin/tb_new_mail_alert.sh as follows:
#!/bin/bash

# Author: Shmerl
# Date : November, 2010

. ~/bin/helpers/locking.sh

TB_WINDOW_SUBTITLE='Mozilla Thunderbird'
NEW_MAIL_ALERT_ICON='/usr/share/icons/gnome/48x48/status/mail-unread.png'

exclusive_lock_require `basename $0` 1 2>&1 > /dev/null
zenity --notification --window-icon="$NEW_MAIL_ALERT_ICON" --text='New mail has arrived!'

# Currently it just raises some matching window.
# This can be modified to address specific cases like
# two Thunderbirds running in parallel using different profiles etc.
# In that case the script can take profile name as a parameter and
# use that to alter the lock name and window subtitle pick.

if [ `wmctrl -l | grep -c "${TB_WINDOW_SUBTITLE}"` -ne 0 ]
then
wmctrl -a "$TB_WINDOW_SUBTITLE" # bring to front
fi

Note: On Debian I'm using it with actual Thunderbird, but if you want to use it with Icedove you should modify TB_WINDOW_SUBTITLE value above accordingly.

Here is locking.sh (included as ~/bin/helpers/locking.sh) which is needed to ensure that only one instance of the notification script will be able to run (thanks to Jason Weathered from Stack Overflow for this example).

#!/bin/bash

#
# The code is taken from Stack Overflow
# Thanks to Jason Weathered
#

function exclusive_lock_try() # [lockname]
{
local LOCK_NAME="${1:-`basename $0`}"

LOCK_DIR="/tmp/.${LOCK_NAME}.lock"
local LOCK_PID_FILE="${LOCK_DIR}/${LOCK_NAME}.pid"

if [ -e "$LOCK_DIR" ]
then
local LOCK_PID="`cat "$LOCK_PID_FILE" 2> /dev/null`"
if [ ! -z "$LOCK_PID" ] && ps -p "$LOCK_PID" &> /dev/null
then
# locked by non-dead process
echo "\"$LOCK_NAME\" lock currently held by PID $LOCK_PID"
return 1
else
# orphaned lock, take it over
( echo $$ > "$LOCK_PID_FILE" ) 2> /dev/null &&
local LOCK_PID="$$"
fi
fi
if [ "`trap -p EXIT`" != "" ]
then
# already have an EXIT trap
echo "Cannot get lock, already have an EXIT trap"
return 1
fi
if [ "$LOCK_PID" != "$$" ] &&
! ( umask 077 && mkdir "$LOCK_DIR" && umask 177 &&
echo $$ > "$LOCK_PID_FILE" ) 2> /dev/null
then
local LOCK_PID="`cat "$LOCK_PID_FILE" 2> /dev/null`"
# unable to acquire lock, new process got in first
echo "\"$LOCK_NAME\" lock currently held by PID $LOCK_PID"
return 1
fi
trap "/bin/rm -rf \"$LOCK_DIR\"; exit;" EXIT

return 0 # got lock
}

function exclusive_lock_retry() # [lockname] [retries] [delay]
{
local LOCK_NAME="$1"
local MAX_TRIES="${2:-5}"
local DELAY="${3:-2}"

local TRIES=0
local LOCK_RETVAL

while [ "$TRIES" -lt "$MAX_TRIES" ]
do
if [ "$TRIES" -gt 0 ]
then
sleep "$DELAY"
fi
local TRIES=$(( $TRIES + 1 ))

if [ "$TRIES" -lt "$MAX_TRIES" ]
then
exclusive_lock_try "$LOCK_NAME" > /dev/null
else
exclusive_lock_try "$LOCK_NAME"
fi
LOCK_RETVAL="${PIPESTATUS[0]}"

if [ "$LOCK_RETVAL" -eq 0 ]
then
return 0
fi
done

return "$LOCK_RETVAL"
}

function exclusive_lock_require() # [lockname] [retries] [delay]
{
if ! exclusive_lock_retry "$@"
then
exit 1
fi
}


Make sure you point zenity to the existing icon (I used Gnome's mail-unread.png in the example above. After you set up these scripts and install Mailbox Alert add-on, right click on the mail folder in Thunderbird for which you want to receive notifications, and select "Mailbox Alert". Enable "Execute Command" field there and point it to the tb_new_mail_alert.sh and you should be set.

A useful thing to add would be an indication of current number of new messages since Mailbox Alert allows to pass it to the script. The downside so far - the notification doesn't go away, until the icon is actually clicked. If add-on would provide some action hook for "no more new mail" kind of event - this could be improved.