Data As Music

Data As Music

Featuring the Traffic Song

Erik Anderson's photo
Erik Anderson
·May 15, 2022·

3 min read

The code I'm about to describe uses a binary interpretation of data to create a rhythm. Is it a musical rhythm? Decide for your self by listening on YouTube here.

fake_traffic_data =
  (ring 25, 53, 150, 180,580,
   100, 400,1000, 3500, 4000, 3001,
   809, 700, 300, 312, 350, 374)

I like code, so why not start this blog post with code. The code above is the (fake) traffic data I used to develop a piece of music. And because I like code, I'll jump straight into the next section of the code.

i = 0
live_loop :binary do
  n = fake_traffic_data[i]
  12.times do
    if n%2==1 then
      sample :elec_blip, amp: 1
    end
    n = n >> 1
    sleep 0.25
  end
  i += 1
end

For those of you who don't like big blocks of code, I'll break down some of the important pieces.

live_loop is a construct in Sonic Pi that allows you to-- Wait, maybe I should define Sonic Pi. Sonic Pi is a program, available at sonic-pi.net, that allows you to use code to create music. --anyway, as I was saying, the live loop allows you to define some sounds, and have them continue in sync with the beat that the Sonic Pi keeps track of. I gave my live loop the name :binary for reasons that will become clear soon.

n is a selected data point from the traffic data set. Note that fake_traffic_data from above is both fake data, and also a ring structure, which means it acts like an endless looping list. With the value of n in hand, 12.times do instructs the program to do something twelve times. Notice that that something includes sleep 0.25. If you do the math, you'll find that those sleep calls add up to 3 units of time.

If you've been getting ahead of me, you may have done more math and noticed that the data set fits within 0 to around 4000, and that 2^12=4096. This is convenient. It means that we can encode the data as a binary series of beeps and silences. That's what the statment if n%2==1 thenand the statementn = n >>1do. The if statement determines if the current least significant binary digit is nonzero. Which is another way if saying it determines if n is odd. If so, we hear an electric beep, courtesy ofsample :elec_blip. Meanwhile the>>` operator performs a bit shift operation, which is another way of saying that it divides the number by two.

If you listened to the YouTube video (I hope you have), you will have heard the pattern of beeps and silences I was describing.

The rest of the code is less mathematically interesting, but it does lay out the beat the gives the rhythmic beets context.

live_loop :beat do
  3.times do
    sample :bd_808
    sleep 1
  end
end

live_loop :snare do
  sleep 0.5
  sample :sn_dub, amp: 0.5
  sleep 0.5
  sleep 0.5
  sample :sn_dub, amp: 0.5
  sleep 0.5
  sleep 0.5
  sample :sn_dub, amp: 0.5
  sleep 0.25
  sample :sn_dub, amp: 0.5
  sleep 0.25
end

In this code, sample :bd_808 represents a bass drum sound, and sample :sn_dub represents a snare drum sound. Both sounds are built into Sonic Pi.

 
Share this