Sunday, 29 January 2023

3Playing music with Raspberry Pi Pico

from picozero import Speaker from time import sleep #from random import randint speaker = Speaker(5) print("Play the frequency 523, middle C.") sound = [ [523, 1], [None, 0.1], [523, 4] ] speaker.play(sound) speaker.play(sound, wait=False) # don't delay the main code sleep(0.5) print("Gradually increases in frequency to create a positive sound") for i in range(2000, 5000, 100): speaker.play(i, 0.05) # short duration sleep(0.5) print("Decreases the pitch to create a bird chirping sound.") for i in range(5000, 2000, -100): speaker.play(i, 0.05) # very short duration sleep(0.5) print("Create a list of notes and durations to make a tune. You can then play the tune.") BEAT = 0.6 # The length of a note in a single beat liten_mus = [ ['d5', BEAT / 2], ['d#5', BEAT / 2], ['f5', BEAT], ['d6', BEAT], ['a#5', BEAT], ['d5', BEAT], ['f5', BEAT], ['d#5', BEAT], ['d#5', BEAT], ['c5', BEAT / 2],['d5', BEAT / 2], ['d#5', BEAT], ['c6', BEAT], ['a5', BEAT], ['d5', BEAT], ['g5', BEAT], ['f5', BEAT], ['f5', BEAT], ['d5', BEAT / 2], ['d#5', BEAT / 2], ['f5', BEAT], ['g5', BEAT], ['a5', BEAT], ['a#5', BEAT], ['a5', BEAT], ['g5', BEAT], ['g5', BEAT], ['', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT / 2], ['d6', BEAT / 2], ['c6', BEAT / 2], ['a#5', BEAT / 2], ['a5', BEAT / 2], ['g5', BEAT / 2], ['a5', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT], ['f5', BEAT], ['f5', BEAT], ['f5', BEAT / 2], ['d#5', BEAT / 2], ['d5', BEAT], ['f5', BEAT], ['d6', BEAT], ['d6', BEAT / 2], ['c6', BEAT / 2], ['b5', BEAT], ['g5', BEAT], ['g5', BEAT], ['c6', BEAT / 2], ['a#5', BEAT / 2], ['a5', BEAT], ['f5', BEAT], ['d6', BEAT], ['a5', BEAT], ['a#5', BEAT * 1.5] ] speaker.play(liten_mus) sleep(0.5) print("Also can use a loop to play the tune, one note at a time.") for note in liten_mus: speaker.play(note) #https://projects.raspberrypi.org/en/projects/i

No comments: