Kaluma library to play music with passive buzzer.
Raspberry Pi Pico | Passive buzzer |
---|---|
GND | - |
GP12 | + |
npm install https://github.com/niklauslee/buzzer-music
An example for playing a simple music (A part of Beethoven's "For Elise").
const {BuzzerMusic} = require('buzzer-music');
const pin = 12;
const rhythm = 8; // 8th note
const tempo = 120; // 120BPM
const music = new BuzzerMusic(pin, rhythm, tempo);
const score = '5eDeDe4b5dc4a---eab---eb5c---5eDeDe4b5dc4a---eab--e5c4ba---';
music.play(score);
You have to ensure that sufficient current and voltage are provided to the buzzer or speaker (Check the buzzer's datasheet). If it's no enough, note pitch may not be accurate.
You can make a score with a string consists of following characters:
1
: Octave 12
: Octave 23
: Octave 34
: Octave 4 (default)5
: Octave 56
: Octave 67
: Octave 7c
: Note CC
: Note C#d
: Note DD
: Note D#e
: Note Ef
: Note FF
: Note F#g
: Note GG
: Note G#a
: Note AA
: Note A#b
: Note B-
: Sustain.
: Rest
Here is the frequencies of musical note supported in this library.
Note \ Octave | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
C | 33 | 65 | 131 | 262 | 523 | 1047 | 2093 |
C# | 35 | 69 | 139 | 277 | 554 | 1109 | 2217 |
D | 37 | 73 | 147 | 294 | 587 | 1175 | 2349 |
D# | 39 | 78 | 156 | 311 | 622 | 1245 | 2489 |
E | 41 | 82 | 165 | 330 | 659 | 1319 | 2637 |
F | 44 | 87 | 175 | 349 | 698 | 1397 | 2794 |
F# | 46 | 93 | 185 | 370 | 740 | 1480 | 2960 |
G | 49 | 98 | 196 | 392 | 784 | 1568 | 3136 |
G# | 52 | 104 | 208 | 415 | 831 | 1661 | 3322 |
A | 55 | 110 | 220 | 440 | 880 | 1760 | 3520 |
A# | 58 | 117 | 233 | 466 | 932 | 1865 | 3729 |
B | 62 | 123 | 247 | 494 | 988 | 1976 | 3951 |
Create an instance of BuzzerMusic
.
pin
<number>
Pin number.rhythm
<number>
How fast a single note. Default is4
and it means quater note.tempo
<number>
Tempo. Default is120
BPM.toneInversion
<number>
If you want to use inversion pin fortone()
function, specify pin number to generate inverted signal. For more about, see the tone().
Play the score string.
score
<string>
loop
<boolean>
Repeat the given score untilstop()
is called. Default isfalse
.
Stop the music.