Linux Format

USB Dance mat.....................

Les Pounder busts out his best moves on the dance floor and explains how to create music (or something like it) using a dance mat to control Sonic Pi.

-

Les Pounder busts out his best moves on the dance floor and shows us how to create music with a dance mat.

For our tutorial this month we delve into our loft and dust off the USB dance mat that we bought in the 2000s. The goal of this project is to use the dance mat as a method of input for SonicPi. Now, we can’t just plug in and go; we first need to write some Python code that will talk to SonicPi’s server and react to our dance moves.

To start our project, connect your keyboard, mouse, HDMI and SD card, and finally the USB dance mat to a spare USB port. Now power up your Raspberry Pi and boot to the Raspbian desktop. Once at the desktop, we need to open LXTerminal, the icon for which is in the top-left of the screen and resembles a monitor. In the terminal we’ll first install the library that will enable our dance mat to be used with Python. Inputs is a Python library created by Zeth. The goal of Inputs is to simplify the use of game controller­s in Python. We’ll install Inputs using the Python package manager. In the terminal type the following. $ sudo pip3 install inputs

Once Inputs is installed the terminal will be returned back for use, and the next thing to do is install the second Python library. Python Sonic is a Python library that enables a Python project to talk to the server that runs SonicPi. But in order to connect Python Sonic to SonicPi we need to install pythonosc. In the terminal type the following, pressing Enter at the end of each line. $ sudo pip3 install python-osc $ sudo pip3 install python-sonic

That concludes the installati­on of the software, and the terminal can now be closed. Our next step is to open SonicPi. This is crucial because Python Sonic requires the applicatio­n be open for use. SonicPi can be found in the Programmin­g section of the main menu. Minimise SonicPi and return to the Programmin­g menu. This time select Python 3.

Once Python 3 opens, click File > New and a new blank document will appear. Immediatel­y save this by clicking File > Save and name the new file dancemat.py. Future saves will be quicker.

In the blank document we start our code by importing the libraries that will provide the functional­ity required. From the Inputs library we import the get_gamepad class; this is used to communicat­e with our dance mat. Next we import the entire Python Sonic library using * – this removes the need

to preface every function call with the name of the library. Our last import sees us importing the Thread class from

threading (more on this later).

from inputs import get_gamepad from psonic import * from threading import Thread

Our next section of code is the creation of two functions, objects that we can call by their name and this triggers their code to be executed. Our first function is called beat() and it plays a heavy kick drum beat every half second using Python Sonic. The kick drum is a pre-recorded sample contained within SonicPi. PythonSoni­c contains a function sleep() which mirrors the sleep command used in SonicPi.

def beat(): while True: sample(DRUM_HEAVY_KICK) sleep(0.5)

Our second function plays another sample, the classic Amen loop. This time we set the amplitude of the sample to 0.5 to ensure that the volume is consistent with other samples. We add a sleep for 1.753 seconds, the exact duration of the Amen sample.

def Amen(): while True: sample(LOOP_AMEN, amp=0.5) sleep(1.753)

Moving to the beat

We now move on to the main body of our code. Here we use a try…except test that will attempt to run the contained code, which starts with an infinite loop, while True, to run the contained code. try: while True:

Inside of our loop we’ll create an object called events that will be used to link to our attached dance mat using the Inputs library. We then use a for loop to extract the event code, the button that has been pressed on the dance mat, and the state of the button, on or off, which is shown as 1 or 0. We then store the referenced button press to a variable called button and then print the informatio­n to the Python shell for debug purposes. Our last line in this section changes the default instrument for SonicPi to a Tron inspired digital noise.

events = get_gamepad() for event in events:

print(event.code, event.state) button = event.code print("The button pressed was ",button) use_synth(PROPHET)

We now enter into a conditiona­l test that will check for each button press and react accordingl­y. This test uses a series of if..elif (short for else if) tests that will check the state of the dance mat. Our first test checks to see if the Select button, which Inputs refers to as BTN_BASE3 has been pressed, identified by its state changing to 1. If both of those tests are correct then the indented code is executed. In this case it will print the button for debug. The select button has a special function in that it can provide a background beat for us to dance to. Earlier we created the Amen function, and this function will be called and contained in a separate thread of Python code. In essence we are running two sequences at once to give us a layered sound.

if button == "BTN_BASE3" and event.state == 1: print("SELECT") Amen_thread = Thread(target=Amen) Amen_thread.start() elif

The next condition is a similar test to check if Start has been pressed. If so another thread is launched to play a drum beat. We’ll skip this elif condition, but it is present in the full code download for this tutorial.

Our next elif condition tests to see if the X button has been pressed. If so then a cymbal sample is played as well as printing the button to the shell for debug.

elif button == "BTN_BASE" and event.state == 1: print("X") sample(DRUM_CYMBAL_CLOSED, amp=0.5)

For each button present on the dance mat there is a correspond­ing elif condition. Once a condition is proven to be true, the code within is executed, and it’s this that gives us the different sounds.

We now come out of the conditiona­l tests and out of the infinite loop and come to the except section of code. If the code contained in try were to fail, or if the user wished to exit, then pressing Ctrl+X will force close the project and print EXIT on to the screen. except KeyboardIn­terrupt: print("EXIT") With the code completed we can now save our work and click Run > Run Module from the menu. Now get ready to dance to the beat of your own music!

 ??  ?? Yes, we are kind of assuming you’ve got a dance mat up in your loft, if not on your floor right now. Hasn’t everyone?
Yes, we are kind of assuming you’ve got a dance mat up in your loft, if not on your floor right now. Hasn’t everyone?
 ??  ??

Newspapers in English

Newspapers from Australia