playing music using neotrellis, python and arduino

9d23c21 adding multimedia

10 months ago

2938866 adding synth wiki

10 months ago

I've been playing around with neo trellis for a while. I had to pack it up in a box and froget about it for a few months due to life changes and priorities, and then I came back to it. I lost the sketchy code I made for it months back, so I decided to start over and re-read the documentation in Adafruit. I finally remembered everything and started working. First of all I already enjoy one of the examples from the seesaw NeoTrellis library: "interrupt".

Basically what it does is light a colorful rainbow on the keyboard and then when you press one of them it lights with the assigned color. After studying that code I decided a fast dirty way of hacking the keys was getting the code of them by printing them on serial monitor. The identification code of the keys was actually part of the funtion that creates and returns a color for each key. I basically picked that unique [code+color] data and programmed it to print it in the serial monitor whenever I pressed a key. That way I would open the serial monitor and press each key to get the code. Anyway since my NeoTrellis is a 4x4 matrix, I mapped the keys and made a conditional in a way that they would print an unique id letter in the monitor when they are pressed, instead of the weird code mentioned before. IN the end, the keys were set in this way:


The final result was that if I pressed the key in the left corner above it would print "A" in the serial (tty/ACM0 in my case) monitor, if I pressed the key in the right bottom corner it would print "P" and so on. Once this was done, I changed to python. I used pyserial to read the tty/ACM0 contents.

arduino = serial.Serial(device, 9600,timeout=1)

My original idea was playing simple "beep" sounds and similar. I searched for the notifications sounds in my system and used mplayer command to play the wav file of some sample sounds, such bells or guitars.

     valor = arduino.readline()
     valor = valor.decode('utf-8')
     if "A" in valor:
        print("playing sample music")
        subprocess.run(["mplayer", "/<path_to_my_music>/a.wav"])

Since this worked I decided to go a litle further. I opened Audacity, took my mic and my ukelele. I started recording some basic chords and saving them in a music folder in the project main folder. I also did some funny sounds with my mouth and recorded them. I changed the python code so it would read the path from that folder instead of the default system's.

     valor = arduino.readline()
     valor = valor.decode('utf-8')
     if "A" in valor:
        print("playing ukelele chord F")
        subprocess.run(["mplayer", "/<new_path_to_my_music>/ukelele1.wav"])

I wrapped up this in a while true loop that only scaped with one of the key signals (in my case I used "M" since I decided to use the last row of keys as option buttons).

And that's it! now I can play my own recorded things on my computer with the neotrellis. This is just an example, though. Once I have this map in the serial, I can actually program them to open folders, open firefox tabs or whatever I need. I'm thinking of creating a "git management" helper. It has a few seconds of delay, though!

Here's a short video showing the recording and the neotrellis.