Introduction: Ubuntu BASH MIDI to WAV

Picture of Ubuntu BASH MIDI to WAV

So there I was, looking for a MIDI to WAV converter that made use of soundfonts. All I got was shareware and payware. Not what I needed, it was. There had to be something somewhere that was free. I knew Timidity could do it, but I didn't know the command line. After some extensive looks through the man pages and online, I found a solution. I wrote a BASH script to handle the job. But it was fully CLI. I wanted GUI. So I went looking at Zenity. I finally got the CLI working. But what if I wanted to save in a read-execute directory. I needed SUDO permissions. But I didn't want SUDO if the directory had write permissions.

Step 1: Requirements

The requirements here aren't too many. You need Zenity and Timidity. Timidity does the conversion, Zenity provides a graphic front end. You will also need, and already have, a text editor and BASH. If you have the programming skills, this can be converted to Windows' BATCH scripts. This tutorial assumes you have a working knowledge of how to configure Timidity to use soundfonts.

Step 2: The Script

Coding is like a puzzle. You have all the pieces - you just need to put them together. It help to draw a diagram listing how things should happen. Then it's just coding the blocks. Lucky for you, the blocks have been filled in and it's just a matter of downloading or copy/pasting (don't forget to give it read-execute permissions! It will remove them). But here are my blocks:
1. Ask for a MIDI file to convert.
2. Ask for where to save the converted file.
3. Test if the directory is writable.
4A. If yes, convert the file with normal permissions.
4B. If no, ask for user password then convert with SUDO permissions.

There are a lot of if statements in the code; three of which tests if "Cancel" was pressed. The rest is testing if SUDO permissions are needed.

#!/bin/bash

# Get the MIDI path/file name
mid=$(zenity --file-selection)
if [ $? = 1 ];
then exit
fi

# Get save path/file name
wav=$(zenity --file-selection --save --confirm-overwrite)
if [ $? = 1 ];
then exit
fi

# see if current user has write permissions by creating an empty file
> $wav
# if so, do the conversion and show progress bar
if [ $?  -eq 0 ]; then
timidity "$mid" -Ow -o "$wav" | zenity --progress --pulsate --auto-close --text "Converting..."

# Tell us the conversion is done
zenity --info --text "Conversion complete!"

# if not, get root password, run command as root
else
# Get the users password
passwd=$(zenity --password)

# Do the conversion and show a progress bar
echo $passwd|sudo -S timidity "$mid" -Ow -o "$wav" | zenity --progress --pulsate --auto-close --text "Converting..."
if [ $? = 1 ];
then exit
fi

# Tell us the conversion is done
zenity --info --text "Conversion complete!"
fi

Step 3: A Breakdown of the Code

The very first line tells the terminal what program (/bin/bash) to execute the script. The rest of it is the script. First, we make a box asking for a MIDI file. Then we open a box to ask where to save the file. We then try to create an empty file in the save directory, giving the file the same name as what the converted file will be. We will overwrite this soon. If the file has been successfully created, tell Timidity to convert the file. If the file was not created (because it needs to run at root level), ask for the SUDO password and run the conversion at root level.

Step 4: Screenshots

Picture of Screenshots

And here are a few screenshots (in order of how they appear). I forgot to capture the final box and the password box.

Comments

About This Instructable

1,865views

3favorites

Bio: I once wrote an about me that said: "I can make your dreams come true.... if your dreams involve a stranger entering your home and ... More »
Add instructable to: