When we are kids, our mothers tell us bedtime stories and sing us lullabies before we go to sleep. But what when we are grown up and living alone? I love music a lot and I listen to music almost all the time: at work, at home, when I am traveling and even when I am about to fall asleep. When I go to bed, I play music on my computer. I use Debian GNU/Linux at home and I play music using Rhythmbox with 'Repeat' enabled for the album or artist I am listening to on the day.

When I am falling asleep with the music playing on my computer, gradually the music becomes disturbing and prevents me from going into deep sleep. Decreasing the volume helps but it helps only for the next ten minutes or so and then it feels too loud again. So, after sometime I need to stop playing the music and go to sleep peacefully without any music to disturb me. Decreasing the volume every five minutes or so and finally taking care to stop the player before sleeping is not a nice way to fall asleep.

I thought of writing a shell script to automate this so that I do not have to worry about the volume and the player. It occurred to me that I can make the computer tell me bedtime stories too. I needed two things to do it: a way to pause Rhythmbox from the shell and a way to decrease the volume as the night progresses. I found rhythmbox-client command to control Rhythmbox from the shell and aumix command to adjust the volume of the audio mixer. I used espeak command to read text files aloud.

So far, so good. How about asking the computer to wake me up in the morning? If the script can start the music player in the morning and increase the volume gradually, I'll have a morning alarm too.

Here is the script:


# Bedtime story and lullaby cum morning alarm script.
# Susam Pal

# Falling asleep (0.5 hour)
espeak -a 100 -s 150 -f story.txt &
for volume in {80..51} 
    aumix -v $volume
    echo Reduced volume to $volume. 
    sleep 1m 

# Deep sleep (5 hours)
pkill espeak
rhythmbox-client --pause
echo Sleeping at `date`
sleep 5h

# Morning alarm (1 hour)
echo Waking up at `date`
rhythmbox-client --play
for volume in {41..100}
    aumix -v $volume
    echo Increased volume to $volume. 
    sleep 1m 

If you find espeak too electronic and monotonous, you can go for the festival command which can be configured to sound more human. To do this, you can just replace the line containing the espeak command with

festival --tts story.txt &

Of course, you need to replace pkill espeak with pkill festival as well.

I add the following code to my /etc/festival.scm file to set a good voice and a higher volume.

(set! voice_default 'voice_en1_mbrola)
(set! default_after_synth_hooks
    (list (lambda (utt) (utt.wave.rescale utt 1.0 t))))

The first line of code sets the voice to one that I like. The next two lines sets the volume I want. To see the list of voices available, run festival in interactive mode and run the (voice.list) command.


Joe said:

Good yaar, everytime you are trying something which is different. Keep it up!

JB said:

Wow, Initially I wondered why Susam started writing about lullabies. I like this.

Lakshman Prasad said:

Since long I have been using the system as my Alarm.

sleep 6h && rhythmbox Bromas.mp3

Now I need to upgrade to this shell script. Pretty cool :)

Satyajit said:

Awesome. I always wanted to do the same thing but was never confident of writing a script.

Post a comment