Translate

Senin, 09 September 2019

Playing with Grub2 tunes

In Grub2 we know that it has a feature to make sound when booting. This feature use play command with syntax is

play file | tempo [pitch1 duration1] [pitch2 duration2]...

From syntax above we can create boot tunes via two ways. The easy way is create tune via GRUB_INIT_TUNE parameter in file: /etc/default/grub. In this file, we can fill this parameter with tempo, frequency, and duration of the tunes like this one:

GRUB_INIT_TUNE="480 900 2 1000 2 800 2 400 2 600 3"


We can find more tune example from internet like in this forum
After that, save the file and update grub2 configuration with command sudo update-grub via terminal. Reboot to hearing test this tunes.

The hard way is create binary file that contain value of parameter GRUB_INIT_TUNE. According to Grub Manual, the file format is first the tempo as an unsigned 32bit little-endian number, then pairs of unsigned 16bit little-endian numbers for pitch and duration pairs.
So, to make this binary file, we must convert parameter values into hexadecimal numbers with format following to the grub manual. I had made a script to convert those parameter numbers into 32bit little-endian format, and save the result into binary file.
#!/bin/sh

 if [ $# -lt 4 ]; then
     echo "Usage: $0 savefile tempo freq dur [freq dur freq dur...]" >&2
     exit 1
 fi
 file=$1; shift
 tempo=$1; shift
 a=`printf "%0.8x" $tempo)`
 echo -en "\x${a:6:2}\x${a:4:2}\x${a:2:2}\x${a:0:2}" > $file
 while [ -n "$*" ]; do
     freq=$1; shift
     [ $freq -eq 0 ] && freq=1
     dur=$1 ; shift
     a=`printf "%0.4x" $freq)`
     echo -en "\x${a:2:2}\x${a:0:2}" >> $file
     a=`printf "%0.4x" $dur)`
     echo -en "\x${a:2:2}\x${a:0:2}" >> $file
 done
hexdump -Cv $file
Example operation using script above (name of script ex.: tunehex) is:
./tunehex tune.bin 480 900 2 1000 2 800 2 400 2 600 3
Copy this file to boot folder:
sudo cp tune.bin /boot
Edit file /etc/default/grub and make sure parameter GRUB_INIT_TUNE is commented.
And also edit file /etc/grub.d/00_header and add line in section "Play an initial tune" like this one:
# Play an initial tune
if [ "x${GRUB_INIT_TUNE}" != "x" ] ; then
  echo "play ${GRUB_INIT_TUNE}"
else
  echo "play /boot/tune.bin"
fi
Don't forget to update grub with update-grub command. And final step is make a cup of coffee and reboot computer to listening this tunes.

NOTE: This tunes can only be heared on old PC or laptop that has 4-pin internal speaker (usually has a tune at BIOS boot time).



Must-۩o 2019
I've got C in english.