Wednesday, December 01, 2021

Tasker To Power A New Habit

I'm working on creating a habit that requires one minute of my attention a few times a day. I've tried using my watch's countdown timer to mark off the time, but I've found this both (slightly) inconvenient and (slightly) ineffective.

What would be ideal is an icon I could press on my phone's home screen that would play a soothing sound for a minute. I'm sure there are apps out there that do this, but building this in Tasker is too easy, so let's do it!

Step #1: Get Some Sounds

A quick Google Search turned up FreeSoundsLibrary.com/ambient-sound as a source of sounds that fit my needs. I downloaded a sample, being careful not to click on any of the button-looking ads, unziped it and played it using mpg123. Success!

In a few minutes I downloaded 23 different sounds.

Step #2: Unzip and Rename the MP3 files

Using a bit of linux-fu, I unzipped each of the sound files and renamed them to <count>.mp3. The goal was to end up with a directory full of files named: 0.mp3, 1.mp3, etc. up to 22.mp3.

# unzip 'em all
$ for f in *.zip ; do unzip -o $f ; done

# see if I can use 'nl' to come up with with a 'count' value
# for each mp3 file. I can!
$ ls -1 *.mp3  | nl -v 0
     0  432-hz-music-for-relaxation.mp3
     1  ambient-synth-music.mp3
     2  bird-chirping-in-the-garden-sound-effect.mp3
     3  birds-song-in-forest.mp3
     4  calming-sea-sounds.mp3
     5  city-storm-sound-effect.mp3
     6  deep-relaxation-ambient-music.mp3
     7  dreamy-ambient-background-music-free.mp3
     8  farm-ambience-sfx.mp3
     9  night-ambient-sounds-cricket.mp3
    10  outer-space-atmosphere-sound-effect.mp3
    11  peaceful-village-ambience-sound-effect.mp3
    12  relaxing-nature-music.mp3
    13  sad-emotional-background-melody.mp3
    14  sea-waves-sound.mp3
    15  small-sea-waves-crashing-sound-effect.mp3
    16  soothing-nighttime-ambience.mp3
    17  spring-forest-birds-sounds.mp3
    18  stormy-wave-sounds.mp3
    19  stream-and-bird-sounds-for-relaxation.mp3
    20  summer-night-ambience.mp3
    21  village-sounds-ambience.mp3
    22  wind-sound-effect-free.mp3

# Here's a one liner that sets $n to the count, $f to the file name
# and does a 'mv $f $n.mp3'
$ ls *.mp3 | nl -v 0 | while read line ; do n=$(echo $line | cut -d' ' -f1) ; f=$(echo $line | cut -d' ' -f2); mv -v $f $n.mp3; done
renamed '432-hz-music-for-relaxation.mp3' -> '0.mp3'
renamed 'ambient-synth-music.mp3' -> '1.mp3'
renamed 'bird-chirping-in-the-garden-sound-effect.mp3' -> '2.mp3'
renamed 'birds-song-in-forest.mp3' -> '3.mp3'
renamed 'calming-sea-sounds.mp3' -> '4.mp3'
renamed 'city-storm-sound-effect.mp3' -> '5.mp3'
renamed 'deep-relaxation-ambient-music.mp3' -> '6.mp3'
renamed 'dreamy-ambient-background-music-free.mp3' -> '7.mp3'
renamed 'farm-ambience-sfx.mp3' -> '8.mp3'
renamed 'night-ambient-sounds-cricket.mp3' -> '9.mp3'
renamed 'outer-space-atmosphere-sound-effect.mp3' -> '10.mp3'
renamed 'peaceful-village-ambience-sound-effect.mp3' -> '11.mp3'
renamed 'relaxing-nature-music.mp3' -> '12.mp3'
renamed 'sad-emotional-background-melody.mp3' -> '13.mp3'
renamed 'sea-waves-sound.mp3' -> '14.mp3'
renamed 'small-sea-waves-crashing-sound-effect.mp3' -> '15.mp3'
renamed 'soothing-nighttime-ambience.mp3' -> '16.mp3'
renamed 'spring-forest-birds-sounds.mp3' -> '17.mp3'
renamed 'stormy-wave-sounds.mp3' -> '18.mp3'
renamed 'stream-and-bird-sounds-for-relaxation.mp3' -> '19.mp3'
renamed 'summer-night-ambience.mp3' -> '20.mp3'
renamed 'village-sounds-ambience.mp3' -> '21.mp3'
renamed 'wind-sound-effect-free.mp3' -> '22.mp3'

# Bonus: test out my random sound strategy from the command line
# Pick a random number, force it to be between 0 and 22 and then
# use mpg123 to play the audio
$ mpg123 $(($RANDOM % 23)).mp3
...
Playing MPEG stream 1 of 1: 8.mp3 ...
...

# Success!

Step #3: Put the Files Where Tasker Can See Them

I plugged my phone into my computer via USB C cable and used Windows Explorer to copy the mp3 files to: <Internal Storage>\Tasker\sounds. Tasker should have easy access to these files.

Step #4: Write The Tasker Code

I created a new Task called 1 Minute of Action with four steps:

  1. Set the variable %index to a random number between 0 and 22.
  2. Asks the phone to play a sound that uses %index in its name. This is the magic that causes Tasker to play a random sound, because %index is randomized in the previous step. I've set the sound to loop so that if any of the sounds I picked are less than one minute in length they'll repeat.
  3. Do nothing for precisely one minute.
  4. Stop playing the sound.

Step #5: Add An Icon To The Home Screen

The final touch was to add a Tasker Widget to my home screen that kicks off the '1 Minute of Action' Task. Now I'm a single button press away from a random, ambient sound playing for one minute.

Step #6: Celebrate!

And just like that, you're an app developer! Congrats. You can import this Task by visiting this TaskerNet URL.

There are many ways you could enhance this little app. You could have the Task prompt you, with a slider, for how long to play the music. You could add a task for canceling the currently playing audio. You could log to a Google Spreadsheet, e-mail a friend or send out a Tweet every time you complete the Task, thereby noting the completion of your practice. And of course, you can add sounds, including recording your own. Such is the joy of being a programmer, you're the boss and the computer is just waiting to be put to work.

No comments:

Post a Comment