Play a sound before or after GRUB loads
How
In the file /etc/default/grub
, set the variable GRUB_INIT_TUNE
to the tune you want.
For instance: GRUB_INIT_TUNE="2000 400 4 0 1 500 4 0 1 600 4 0 1 800 6"
Once done, apply your changes with sudo update-grub2
.
Existing tunes
(The ones I could find)
-
Mario Bros. Mushroom Powerup:
1750 523 1 392 1 523 1 659 1 784 1 1047 1 784 1 415 1 523 1 622 1 831 1 622 1 831 1 1046 1 1244 1 1661 1 1244 1 466 1 587 1 698 1 932 1 1195 1 1397 1 1865 1 1397 1
-
Star Wars' Imperial Death March:
480 440 4 440 4 440 4 349 3 523 1 440 4 349 3 523 1 440 8 659 4 659 4 659 4 698 3 523 1 415 4 349 3 523 1 440 8
-
xiè-jìléi's tune (ascending):
2000 400 4 0 1 500 4 0 1 600 4 0 1 800 6
-
Adams's tune (rington-like to me):
480 220 1 277 1 330 1 440 1 185 1 220 1 277 1 370 1 294 1 370 1 440 1 587 1 330 1 415 1 494 1 659 1
More of them here.
Understand a tune
The syntax is: GRUB_INIT_TUNE="
tempo [pitch1 duration1] [pitch2 duration2] ...
"
The tempo is the base for all note durations. 60 gives a 1-second base, 120 gives a half-second base, etc. Pitches are Hz. Set pitch to 0 to produce a rest.
Source: the grub documentation, the grub play command documentation (same page).
Preview / test a tune
If you want to test the tune: Install sox
and use the script from that ubuntuforum.org thread (slightly modified by me to remove warnings):
grub-playtune
#!/bin/dash
if [ $# -lt 3 ]; then
echo "Usage: $0 tempo freq dur [freq dur freq dur...]" >&2
exit 1
fi
tempo=$1; shift
tmpdir=$(mktemp -d)
while [ -n "$*" ]; do
freq=$1; shift
dur=$1; shift
dur=$(echo "$dur*(60/$tempo)"|bc -l)
sox -e mu-law -r 8000 -n -t raw - synth $dur sine $freq >>$tmpdir/grubtune.ul 2> /dev/null
done
play -q -c1 -r 8000 $tmpdir/grubtune.ul
rm -r $tmpdir
To make it executable chmod +x grub-playtune
Use example:
grub-playtune 2000 400 4 0 1 500 4 0 1 600 4 0 1 800 6
Frequency of a note
A python3-based script to compute the frequency of a note:
pitch
#!/bin/bash
python3 -c "print(*(int(0.5 + 440 * 2 ** (note/12)) for note in (${*/%/,})))"
Use examples:
$ pitch 0 # gives you A_4
440
$ pitch 2 # gives you B_4
494
$ pitch -2 # gives you G_3
392
$ pitch -12 0 12 # gives you A_3, A_4 and A_5
220 440 880
Frequency table
One is available here. It can also be generated, using the above script like so:
$ pitch {-12..0} # A_3 to A_4
220 233 247 262 277 294 311 330 349 370 392 415 440
$ pitch {0..12} # A_4 to A_5
440 466 494 523 554 587 622 659 698 740 784 831 880
$ pitch {12..24} # A_5 to A_6
880 932 988 1047 1109 1175 1245 1319 1397 1480 1568 1661 1760
According to the GRUB manual, the first note is a 'tempo', and each following pair of numbers are duration and pitch.
The pitch is in Hz, so in order to play nice-sounding notes (which are in tune), you'll need the frequencies of notes in the western equal temperament scale:
http://pastebin.com/rJY30FmM
Incidentally, here's what I came up with after writing and using the Java code I pasted:
GRUB_INIT_TUNE="480 220 1 277 1 330 1 440 1 185 1 220 1 277 1 370 1 294 1 370 1 440 1 587 1 330 1 415 1 494 1 659 1"
This plays an 4-note arpeggio of A, F#m, D, E.
-
Edit file
/etc/default/grub
to include following line (Here is my init tune):GRUB_INIT_TUNE="2000 400 4 0 1 500 4 0 1 600 4 0 1 800 6"
Run
sudo update-grub2
to apply the change.
You ask, Can it be done? The simple answer? Yes, apparently. How can it be done? You need to put more money in the slot for that answer. Just my little joke. The best that I can do is this:
The GRUB manual
Go to section 5.1 Simple Configuration Handling and look for the line, GRUB_INIT_TUNE. You will need to follow the links to Play and File name syntax. You will have manually edit a Grub file.
I created a python script that converts midi files to these init tunes:
https://gitlab.com/lukasfink1/midi2grub
It needs the mido library to run. Also note that the grub play command is only able to play monophonic melodies, so your midis might sound strange if they aren’t.