Stephen Smith's Blog

Musings on Machine Learning…

Posts Tagged ‘sonic pi

Playing Music by Writing Code

leave a comment »

Introduction

The standard Raspbian Linux for the Raspberry Pi comes with an interesting program called Sonic Pi. This program lets you create music by writing code, the goal is that the coding is easy enough that anyone can learn it, and that you can code fast enough to play a live performance by writing the code as you go along, including jamming with other musicians. From my coding experience there is always a lot of pressure to write code faster, but here you need to write the next bit of code and stay on beat.

I was interested in this, primarily to use as a drum machine to accompany by very beginner guitar playing. Basically something a bit more interesting than a metronome. Sonic Pi will run on Windows, MacOS and Linux, but one of the goals of the program was to be as accessible as possible, so hopefully even low income school districts or third world countries can afford a Raspberry Pi or two.

Learning to Code

Like many programs on the Raspberry Pi, Sonic Pi is a backdoor way to try and get kids to learn how to code. Writing code to create music has been around since electronic synthesizers became digital, but how to do this was quite technical and required a lot of (then) expensive equipment. Sonic Pi comes with a lot sampled sounds so you can go quite far without a good sampler.

The programming language is based on Ruby and can do some quite powerful things. The emphasis in Sonic Pi tends to be on fairly straight forward functional programming and it recommended that you only use what is documented for Sonic Pi, but people have discovered hidden Ruby features that work if you try them.

The Sonic Pi language is described as a strongly timed language (a play on other strong typed languages) since music is all about timing and Sonic Pi is brilliant in how it keeps everything in time.

The language keeps things simply, so hopefully if a student can think musically then they can transfer that skill into programming via the Sonic Pi language. The hope being that this is a more interesting entry point to programming than say learning sorting algorithms.

The program is designed to be interactive, have lots of help and typical IDE type features like auto-complete. There is a vibrant online community and tons of examples. Additionally there is a free book available here.

Live Performances

People actually use their Raspberry Pi running Sonic Pi as a musical instrument. This isn’t just running a program they wrote previously for people to listen to. This is actually writing the code as people listen. It isn’t always a solo performance either, it could include jamming with other musicians either playing Sonic Pi or playing regular musical instruments.

The key part of the language to support this is the live loop, where you have a loop playing the music (or code) and it allows you to update the code and have the new code seamlessly take effect whenever you like without causing a break in the music.

This usage is often called live coding and there are actual clubs that have events where this happens called algoraves.

It’s hard to explain this, but if you check out some of the YouTube videos of such performances it’s easy to get the idea. A good introduction is Sam Aaron’s presentation at OSCON on how to start coding and playing Sonic Pi. Here is a Sonic Pi performance covering Daft Punk. Here is a jam session with a guitarist. And another example.

Some Code

At the most basic level you can get some noise out of Sonic Pi just by running the one line program:

play 100

This just plays a beep which decays to nothing. You can add another note with:

play 100
play 75

This actually plays the two notes at the same time, since the program doesn’t stop and wait for the first note to finish. To play the two notes one after the other we would do something like:

play 100
sleep 1
play 75

The notes are actual midi numbers from 0 to 254. The following diagram shows how they relate to the notes from a piano keyboard.

Beeps are all well and good, but you can get much more interesting notes by playing the built in samples. For instance:

sample :bd_haus
sleep 1
sample :ambi_choir

We aren’t very musical yet, but if we put it in a live_loop then it will play over and over.

live_loop :mySong do
    sample :bd_haus
    sample :ambi_choir
    sleep 1
end

Now we’re playing music and with the live_loop we can modify the code and when we hit run it seamlessly starts playing the new code allowing us to perform live by writing code.

Rather than including a lot of source code in this article, for more examples I’ll just provide a couple of links. Note that if you Google around, there are a lot of Sonic Pi samples that you can have a look at. A good little drum machine bit of code is this one. Since Sonic Pi is based on Ruby you can use Ruby string processing to convert ascii drum tabs into music as done here.

Summary

Sonic Pi is a pretty cool program. Basically you get a fairly sophisticated music synthesizer with your inexpensive Raspberry Pi. It’s fun to play with, and perhaps will motivate a few more kids to pick up coding. After all if you do learn how to program in Ruby with this, then you are well on your way to being a highly employable Ruby on Rails web developer. Combined with Scratch, the Raspberry Pi really offers some innovative ways to teach coding to students.

Advertisements

Written by smist08

November 25, 2017 at 9:25 pm

Playing with my Raspberry Pi

with 3 comments

Introduction

I do most of my work (like writing this blog posting) on my MacBook Air laptop. I used to have a good desktop computer for running various longer running processes or playing games. Last year the desktop packed it in (it was getting old anyway), so since then I’ve just been using my laptop. I wondered if I should get another desktop and run Ubuntu on it, since that is good for machine learning, but I wondered if it was worth price. Meanwhile I was intrigued with everything I see people doing with Raspberry Pi’s. So I figured why not just get a Raspberry Pi and see if I can do the same things with it as I did with my desktop. Plus I thought it would be fun to learn about the Pi and that it would be a good toy to play with.

Setup

Since I’m new to the Raspberry Pi, I figured the best way to get started was to order one of the starter kits. This way I’d be able to get up and running quicker and get everything I needed in one shot. I had a credit with Amazon, so I ordered one of the Canakits from there. It included the Raspberry Pi 3, a microSD card with Raspbian Linux, a case, a power supply, an electronics breadboard, some leds and resistors, heat sinks and an HDMI cable. Then I needed to supply a monitor, a USB keyboard and a USB mouse (which I had lying around).

Setting up was quite easy, though the quick setup instructions were missing a few steps like what to do with the heatsinks (which was obvious) or how to connect the breadboard. Setup was really just install the Raspberry Pi motherboard in the case, add the heat sinks, insert the microSD card and then connect the various cables.

As soon as I powered it on, it displayed an operating system selection and installation menu (with only one choice), so clicked install and 10 minutes later I was logged in and running Raspbian.

The quick setup guide then recommends you set your locale and change the default password, but they don’t tell you the existing password, which a quick Google reveals as “Raspberry”. Then I connected to our Wifi network and I was up and running. I could browse the Internet using Chromium, I could run Mathematica (a free Raspberry version comes pre-installed), run a Linux terminal session. All rather painless and fairly straight forward.

I was quite impressed how quickly it went and how powerful a computer I had up and running costing less than $100 (for everything) and how easy the installation and setup process was.

Software

I was extremely pleased with how much software the Raspberry Pi came with pre-installed. This was all on the provided 32Gig card, which with a few extra things installed, I still have 28Gig free. Amazingly compact. Some of the pre-installed software includes:

  • Mathematica. Great for Math students and to promote Mathematica. Runs from the Wolfram Language which is interesting in itself.
  • Python 2 and 3 (more on the pain of having Python 2 later).
  • LibreOffice. A full MS Office like suite of programs.
  • Lots of accessories like file manager, calculator, image viewer, etc.
  • Chromium web browser.
  • Two Java IDEs.
  • Sonic Pi music synthesizer.
  • Terminal command prompt.
  • Minecraft and some Python games.
  • Scratch programming environment.

Plus there is an add/remove software program where you can easily add many more open source Pi programs. You can also use the Linux apt-get command to get many other pre-compiled packages.

Generally I would say this is a very complete set of software for any student, hobbyist or even office worker.

Python

I use Python as my main goto programming language these days and generally I use a number of scientific and machine learning libraries. So I tried installing these. Usually I just use pip3 and away things go (at least on my Mac). However doing this caused pip3 to download the C++/Fortran source code and to try to compile it, which failed. I then Googled around on how to best install these packages.

Unfortunately most of the Google results were how to do this for Python 2, which I didn’t want. It will be so nice when Python 2 finally is discontinued and stops confusing everything. I wanted these for Python 3. Before you start you should update apt-get’s list of available software and upgrade all the packages on your machine. You can do this with:

sudo apt-get update        # Fetches the list of available updates
sudo apt-get upgrade       # Strictly upgrades the current packages

What I found is I could get most of what I wanted using apt-get. I got most of what I wanted with:

sudo apt-get install python3-numpy
sudo apt-get install python3-scipy
sudo apt-get install python3-matplotlib
sudo apt-get install python3-pandas

However I couldn’t find and apt-get module for SciKit Learn the machine learning library. So I tried pip3 and it did work even though it downloaded the source code and compiled it.

pip3 install sklearn –upgrade

Now I had all the scientific programming power of the standard Python libraries. Note that since the Raspberry Pi only has 1Gig RAM and the SD Card only has twenty something Gig free, you can’t really run large machine learning tasks. However if they do fit within the Pi then it is a very inexpensive way to do these computations. What a lot of people do is build clusters of Raspberry Pi’s that work together. I’ve seen articles on how University labs have built supercomputers out of hundreds or Pi’s all put together in a cluster. Further they run quite sophisticated software like Hadoop, Docker and Kubernetes to orchestrate the whole thing.

Summary

I now have the Raspberry Pi up and running and I’m enjoying playing with Mathematica and Sonic Pi. I’m doing a bit of Python programming and browsing the Internet. Quite an amazing little device. I’m also impressed with how much it can do for such a low cost. As other vendors like Apple, Microsoft, HP and Dell try to push people into more and more expensive desktops and laptops, it will be interesting to see how many people revolt and switch to the far more inexpensive DIY type solutions. Note that there are vendors that make things like Raspberry Pi complete desktop computers at quite a low cost as well.

Written by smist08

November 11, 2017 at 9:35 pm