Skip to content

Using the library to play RTTTL on GPIO #1

@rjdza

Description

@rjdza

I'm not sure if this library is intended to parse only or also to play, but if you are looking for a way to use the library to play RTTTL on a Raspberry Pi's GPIOs with a passive buzzer try this:

import RPi.GPIO as GPIO
import time
from time import sleep
from rtttl import parse_rtttl

buzzer_pin = 12

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(buzzer_pin, GPIO.OUT)
buzz = GPIO.PWM(buzzer_pin, 90)
buzz.start(90)
   
song_ateam="A-Team:d=8,o=5,b=125:4d#6,a#,2d#6,16p,g#,4a#,4d#.,p,16g,16a#,d#6,a#,f6,2d#6,16p,c#.6,16c6,16a#,g#.,2a#"
song_barbie="Barbie girl:d=4,o=5,b=125:8g#,8e,8g#,8c#6,a,p,8f#,8d#,8f#,8b,g#,8f#,8e,p,8e,8c#,f#,c#,p,8f#,8e,g#,f#"

def play_note(duration, frequency):
    if frequency > 0:
        buzz.ChangeFrequency(frequency)
    time.sleep ( duration / 1000 )

notes = parse_rtttl(song_barbie)

for note in notes["notes"]:
    print (note)
    play_note(note["duration"], note["frequency"] + 0)

GPIO.cleanup()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions