toldcodger
Posts: 22
Joined: Sat Feb 25, 2017 7:15 pm

Musical notes in Python

Mon Sep 24, 2018 7:08 pm

Hi,
I was recently playing around with micro python on a BBC microbit. Using some basic instructions I got it to show rows of LED’s and using the music library I got it to play the notes of the Close Encounters riff very simply by stating the note, octave and duration, i.e G4:4, A4:4 etc. I think that is similar to Sonic Pi

The coding may not be the prettiest thing you’ve ever seen, but it worked :)

I was going to try to do something similar using Python, a Blinkt stick or a Unicorn hat but I can’t find a way to create the notes using just Python on the Pi.

I’m not a programmer so I am expecting to be corrected on that last statement. But bearing in mind I am not a programmer, what’s the easiest way to do this. ?

Please try to keep it simple, I’m not as young as I used to be :)

Regards

Dave
You should always keep the prime objective in mind.
However, when you’re up to your ass in alligators, it’s hard to remember that you came to drain the swamp. :D

User avatar
MrYsLab
Posts: 142
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Musical notes in Python

Mon Sep 24, 2018 11:11 pm

I used the pigoio library "waves" (PWM) in conjunction with a piezo buzzer . Here is a snippet of code using pigpio waves to play a single tone. It was adapted from the s2-pi project https://mryslab.github.io/s2-pi/ s2-pi.py, https://github.com/MrYsLab/s2-pi/blob/m ... i/s2_pi.py lines 64-80

Code: Select all


import pigpio

pin = 18 # pin that buzzer is connected to

# frequency is in hz.
play_tone = 1000

# instantiate pigpio
pi = pigpio.pi()

# set pin to output mode
pi.set_mode(pin, pigpio.OUTPUT)

frequency = int((1000 / play_tone) * 1000)
tone = [pigpio.pulse(1 << pin, 0, frequency),
    pigpio.pulse(0, 1 << pin, frequency)]

pi.wave_add_generic(tone)
wid = pi.wave_create()

if wid >= 0:
    self.pi.wave_send_repeat(wid)
time.sleep(1)
pi.wave_tx_stop()
pi.wave_delete(wid)


 

User avatar
scruss
Posts: 1863
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Musical notes in Python

Tue Sep 25, 2018 12:58 am

Something like asdwsda/rtttl will do the conversion from note & octave to frequency.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

toldcodger
Posts: 22
Joined: Sat Feb 25, 2017 7:15 pm

Re: Musical notes in Python

Tue Sep 25, 2018 6:42 pm

Thanks guys,

On the micro bit I lit up a row of leds then played a note, then another row of leds and another note etc.

I think the rttl method may be the best fit.For this method.

If I get it working on the Pi i’ll Post it on here.

Thanks again

Dave
You should always keep the prime objective in mind.
However, when you’re up to your ass in alligators, it’s hard to remember that you came to drain the swamp. :D

toldcodger
Posts: 22
Joined: Sat Feb 25, 2017 7:15 pm

Re: Musical notes in Python

Fri Sep 28, 2018 7:12 pm

I got it to work with the UnicornHat and SonicPi.

Unfortunately the movie won’t upload. It looks like I need to convert it to an avi file.

I was looking at SonicPi and saw the OSC option. You have to have SonicPi open and running an OSC listening script but it works.
You should always keep the prime objective in mind.
However, when you’re up to your ass in alligators, it’s hard to remember that you came to drain the swamp. :D

Return to “Python”