Random Calming(?) iPad Music

TL;DR: I wrote a small Pythonista script on the iPad to generate calming music instead of freaking out in the middle of the night.

Raise your hand if you have night terrors. I do! I have had them forever and have learned to cope by catching them early and focusing on *anything* to think my way away from the sense of dread. In the past, Minecraft was a great coping mechanism! But these days, I have been on a programming kick. The result of my latest panic-induced creative: a random calming music generator!

I love the idea of randomness as a creative tool. So the other day (meaning the other night), I fired up Pythonista and started noodling around.

Let’s talk about Pythonista

Screen capture of the Pythonista IDE website displaying an iPhone and an iPad on a green and white background.
The Pythonista website at the time of writing

Pythonista is a Python editor for the iPad. It comes with a few quirks: only certain libraries or types of libraries are available, and its graphical interface abilities are different from traditional GUI tools like tkinter.

Using Pythonista on a touch device like the iPad and typing in it as tricky as you would expect. But Pythonista does a great job of suggesting autocomplete options to make your life easier and limit typos, thanks to a bunch of syntax highlighting and autocompletion features. If you can hook your iPad to an external keyboard, great! Otherwise, just know you’ll be fine anyway, just a little less comfortable than at your workstation. But the portability is well worth it!

Pythonista can also leverage unique iPad features, including using system sounds in scripts! Once I found out about that, and because it was already 1 in the morning, I decided to throw together something quick using the system sounds and some default Python libraries.

How the music happens

Armed with that, all I needed to do from there was summon our time and random libraries, one for pauses between notes, the other for picking up notes that fit…Or not! Here’s how it works.

First, we set the foundations by importing the random and time libraries (you probably can guess what these are for – managing randomness and managing time). Then, we import the Pythonista-specific sound library. And just like that we can play with system sounds on the iPad!

Next, we create empty lists and variables to store data! Specifically:

  • a random_list list to store a bunch of random generated numbers,
  • a piano_list list that stores the name of all the piano notes available in the iPad sound system library,
  • a tune_length variable tied to a user input to define how long the tune will be,
  • a countdown variable that starts off equal to tune_length.

Next, the script picks up the value in tune_length (set by the user as we fire up the script, up to 99) and generates the same amount of random numbers between 1 and 21. This is important, because there are only 21 piano sounds on the iPad. That way, each number is tied to a note and the script won’t try to hit note #22 (which doesn’t exist). This set of numbers is stored in random_list. And then…

…The music happens! The script looks at the list of numbers from random_list, finds and play the corresponding note in piano_list, then takes a short break. And so on, until it reaches the end of the list! You can use the time.sleep(0.25) instructions to make the script pause a certain amount of time between notes.

Listen to the sounds

By 2 in the morning I was done writing the code. I didn’t want to wake up my wife or our dog, so I only ran it silently to see if it could go through the whole program. Success! I only tried it with sound on the next morning and you know what? It actually sounded pretty good!

It turned out that the notes available on the iPad make for tunes that are a little more creepy than relaxing! Shorter tunes between 5 and 10 notes between A3 and C4# make them a little better/less creepy. You can tweak which notes are used by changing the value in random.randrange(1, 21). Go forth and experiment!

The result? I kinda like it! You can find the script on Github. And if you are reading this on an iPad, here’s the kicker: you can just click this link to open the whole script into your own Pythonista instance on your device!

I might throw together a quick non-iPad version when I have the time too, and maybe a GUI to continue practicing with tkinter.

Anyway, enjoy!