aiy.toneplayer

A simple melodic music player for the piezo buzzer.

This API is designed for the Vision Kit, but has no dependency on the Vision Bonnet, so may be used without it. It only requires a piezo buzzer connected to aiy.pins.BUZZER_GPIO_PIN.

class aiy.toneplayer.Note(name, octave=4, bpm=120, period=4)

Bases: aiy.toneplayer.Rest

Simple internal class to represent a musical note.

Used in part with the TonePlayer class, this object represents a musical note, including its name, octave, and how long it is played. End users shouldn’t have to care about this too much and instead focus on the music language described in the TonePlayer class.

BASE_OCTAVE = 4
to_frequency(tuning=440.0)

Converts from a name and octave to a frequency in Hz.

Uses the specified tuning.

Parameters:tuning – the frequency of the natural A note, in Hz.
class aiy.toneplayer.Rest(bpm=120, period=4)

Bases: object

Simple internal class to represent a musical rest note.

Used in part with the TonePlayer class, this object represents a period of time in a song where no sound is made. End users shouldn’t have to care about this too much and instead focus on the music language described in the TonePlayer class.

EIGHTH = 8
HALF = 2
QUARTER = 4
SIXTEENTH = 16
WHOLE = 1
to_length_secs()

Converts from musical notation to a period of time in seconds.

class aiy.toneplayer.TonePlayer(gpio, bpm=120, debug=False)

Bases: object

Class to play a simplified music notation via a PWMController.

This class makes use of a very simple music notation to play simple musical tones out of a PWM controlled piezo buzzer.

The language consists of notes and rests listed in an array. Rests are moments in the song when no sound is produced, and are written in this way:

r<length>

The <length> may be one of the following five characters, or omitted:

w: whole note h: half note q: quarter note (the default – if you don’t specify the length, we

assume quarter)

e: eighth note s: sixteenth note

So a half note rest would be written as “rh”. A quarter note rest could be written as “r” or “rq”.

Notes are similar to rests, but take the following form:

<note_name><octave><length>

<note_names> are written using the upper and lower case letters A-G and a-g. Uppercase letters are the natural notes, whereas lowercase letters are shifted up one semitone (sharp). Represented on a piano keyboard, the lowercase letters are the black keys. Thus, ‘C’ is the natural note C, whereas ‘c’ is actually C#, the first black key to the right of the C key.

The octave is optional, but is the numbers 1-8. If omitted, the TonePlayer assumes octave 4. Like the rests, the <length> may also be omitted and uses the same notation as the rest <length> parameter. If omitted, TonePlayer assumes a length of one quarter.

With this in mind, a middle C whole note could be written “C3w”. Likewise, a C# quarter note in the 4th octave could be written as “c” or “c4q” or “cq”.

NOTE_RE = re.compile('(?P<name>[A-Ga-g])(?P<octave>[1-8])?(?P<length>[whqes])?')
PERIOD_MAP = {'e': 8, 'h': 2, 'q': 4, 's': 16, 'w': 1}
REST_RE = re.compile('r(?P<length>[whqes])?')
play(*args)

Plays a sequence of notes out the piezo buzzer.