Hearing the weather-sonification in Sonic Pi

:: Pi, Sonic-Pi, Mathematica

I’ve been thinking more about sound ‘visualisation’ with Sonic Pi (there’s a name for it too, I discover—_sonification_). So, what about loading weather data, and listening to that. How to get some data? Why not take advantage of the fact that Raspbian now comes complete with a copy of Mathematica?

A quick way of exploring data which might be available in Mathematica, is to run a few queries in Wolfram Alpha. For example, the query temperature Aberystwyth (substitute your place of choice) results in this…

…so if we could get the temperature history datapoints which are plotted on the graph into Sonic Pi then we could, with some scaling, play notes corresponding to them. Mathematica has access to many datasets, including the weather ones, so the next step is to load it.

Current versions of Raspbian have an icon for Mathematica on the desktop; opening it brings up the interactive notebook. Some rummaging in the Mathematica online documentation suggests

WeatherData["Aberystwyth", "Temperature", {2014, 6, 6}]

Entering this into the notebook (we’ve selected data for 6th June 2014), and evaluating it (SHIFT-ENTER) results in a long pause (just on the first execution—while Mathematica loads the data) finally giving

{{{2014, 6, 6, 0, 0, 0}, 5.3}, {{2014, 6, 6, 1, 0, 0}, 4.1},  ... }}

which is a list of lists of hourly time and temperature (Celsius) for the day. Plot this to see what we’ve got

DateListPlot[WeatherData["Aberystwyth", "Temperature", {2014, 6, 6}], 
 Joined -> True]

Now extracting just the temperature values can be done with Mathematica’s list functions— [[All, 2]] extracts the second ‘column’ from all ‘rows’.

WeatherData["Aberystwyth", "Temperature", {2014, 6, 6}][[All, 2]]

{5.3, 4.1, 4.9, 6.7, 8.6, 11.1, 12.8, 14, 14.2, 15.1, 15.5,  ...}

And finally writing it into a file (perhaps the easiest way of getting the data into Sonic Pi)

	Export["temperature.txt",  WeatherData["Aberystwyth", "Temperature", {2014, 6, 6}][[All, 2]]

Now to Sonic Pi (you will have to quit Mathematica first!). Create a workspace with

# Welcome to Sonic Pi v2.0

use_synth :tri
File.open("/home/pi/temperature.txt") do |file|
  file.each_line {
    puts ("Temp = #{line}")
    play (line.to_i * 4) # very simple scaling of temp -> note
    sleep 0.2

Sonic Pi can make use of features of the Ruby language, and so we can read each line of the temperature file, print its value to the output panel as a confirmation, and then play a corresponding note—we just convert the temperature string value to an integer, and multiply by 4 as a simple scaling factor, which works with Celsius and British weather. The result is this. What fun!