Sonic Pi and TouchOSC Sequencer

Software updated to allow operation on latest Sonic Pi > version 3.2. Write up is unaltered, but all code of form /osc/……. is updated to /osc*/… allowing correct operation. In downloaded software use version 2, not the original.

This project uses a TouchOSC template which controls the operation of a Sonic Pi program and produces a 16 step sequencer with 16 channels, where each channel can be switched on or off for any given sequence step. The resultant sequence can be varied in playback tempo, either by using the green slider at the bottom of the template, or by using the 5 preset tempo buttons surrounding this slider, and the patterns  played can be altered while the sequence is playing. You can also select a sub-section of the sequence to play, using the S button to change the start position and the E button to change the ending position again making alterations while the sequence is playing without any interruption. The A button lets you quickly select the entire 16 steps. The current start and end positions are indicated at the top of the template. You can pause the sequence at any time, and also use a single step mode. Each horizontal row represents a channel, with the top 8 rows devoted to different percussion samples, and the bottom eight rows to the notes of a C major scale. Thus simple tunes can be accompanied by a percussion pattern. The central “green” row indicates the current step  of the sequence which is being played. The volume of the 8 percussion row section and the eight note sections can be altered independently, using two sliders at the bottom of the template. Buttons at the left hand end of each row can clear all the entries selected for that row. You can save up to four separate patterns to a json format text file, and these can be reloaded while the program is running without interrupting the sequence. An enable button gives some protection against these operations being accidentally selected. The same protection prevents the clear patterns button from accidentally clearing the entered pattern.

All of the intelligence is in the program running on Sonic Pi 3. This receives OSC messages from the various buttons and sliders on the TouchOSC screen, and it also sends feedback to TouchOSC initialiising all of the buttons at the start for example, and controlling various buttons and leds at different stages of operation. The program will work with Sonic Pi 3.0.1 on a Raspberry Pi 3, but response and operation are smoother when using a Mac, and it should also be with the Windows version of Sonic PI although I have not tested this.

I do not intend to give a blow by blow account of the program operation, but it is heavily commented, so you should be able to follow what is happening. In the main it consists of multiple live-loops each responding to a particular OSC message. To reduce the number of live loops I use wild cards in the received OSC message addresses, and then parse the actual address received to decide on the action to be taken using a function parse_sync_address The TouchOSC/Sonic Pi combination is incredibly flexible which makes it easy to develop an idea or program. In this case I started by trying to control four percussion samples with a fixed tempo and a repeating 16 position pattern. To set the pattern for each sample I used a 1×16 way multi-toggle switch in TouchOSC set for non exclusive mode, so that more than one switch position could be selected at a time. I used a live loop which I called :metro  which advanced a tick counter every beat, I stored the current value in a time-state location :n I used a separate live_loop called :pl (short for play) which looked at the current state of each multitoggle switch array (which I mirrored in a 16 position list stored in Sonic Pi, and used this to control when the sample was played. I used a separate 1×16 multitoggle switch to indicate the current position in the sequence, but this one was set to exclusive mode, so that when one switch was turned on it turned all the other ones off. This was also controlled in the metro loop.

Having got this simple system working I then extended it first to 8 channels with percussion samples, and then I added a further eight channels, but these were each used to control one note in a C major scale, and so you could introduce simple note sequences of up to 16 notes using these different pitches. From then on it was simply a matter of formulating questions like “Could we control the volumes of the notes and samples separately to get a better balance?” or “What about varying the tempo?” or “What about saving a sequence and being able to restore it later?” or “Could we use just part of the sequence and repeat that?” or “Could we pause and/or single step the sequence?” or “Could we clear just one row of the pattern, or the whole pattern easily?” or “What about using different synths for the notes?” All of these questions proved to be possible to accommodate, and in the end, there wasn’t room on the TouchOSC screen to accommodate further features, although this could be done at the expense of adding a further screen page. However I think the response time of the program might begin to suffer so I left it at that.

So we ended up with

  • a 16 step sequencer
  • 16 separate channels, 8 with percussion samples, 8 with notes
  • choice of 4 synths for the notes
  • 5 preset tempos, with variable tempo also available
  • variable independent sample and note volumes
  • each channel sequence set using 16 buttons, one for each step of the sequence
  • quick zero row buttons
  • clear the whole pattern button with safety eanble
  • ability to save up to 4 complete patterns (with safety enable, and indicator of last slot used.
  • ability to load from each of the 4 saved files (with safety enable and indicator of last read slot)
  • ability to pause sequence and to single step it whilst in the paused state
  • ability to set a sub sequence with start and end positions independently chosen, and indicated at the top of the display
  • single button to revert to full 16 step sequence.

You can download the Sonic Pi program and the TouchOSC template from here

There is a video showing the project in action here