Created
January 1, 2021 20:22
-
-
Save max-degterev/81a2facfc3c3cb793a8938c0dade8eb3 to your computer and use it in GitHub Desktop.
Sort of plays sound
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Gpio } = require('pigpio'); | |
const NOTE_B0 = 31; | |
const NOTE_C1 = 33; | |
const NOTE_CS1 = 35; | |
const NOTE_D1 = 37; | |
const NOTE_DS1 = 39; | |
const NOTE_E1 = 41; | |
const NOTE_F1 = 44; | |
const NOTE_FS1 = 46; | |
const NOTE_G1 = 49; | |
const NOTE_GS1 = 52; | |
const NOTE_A1 = 55; | |
const NOTE_AS1 = 58; | |
const NOTE_B1 = 62; | |
const NOTE_C2 = 65; | |
const NOTE_CS2 = 69; | |
const NOTE_D2 = 73; | |
const NOTE_DS2 = 78; | |
const NOTE_E2 = 82; | |
const NOTE_F2 = 87; | |
const NOTE_FS2 = 93; | |
const NOTE_G2 = 98; | |
const NOTE_GS2 = 104; | |
const NOTE_A2 = 110; | |
const NOTE_AS2 = 117; | |
const NOTE_B2 = 123; | |
const NOTE_C3 = 131; | |
const NOTE_CS3 = 139; | |
const NOTE_D3 = 147; | |
const NOTE_DS3 = 156; | |
const NOTE_E3 = 165; | |
const NOTE_F3 = 175; | |
const NOTE_FS3 = 185; | |
const NOTE_G3 = 196; | |
const NOTE_GS3 = 208; | |
const NOTE_A3 = 220; | |
const NOTE_AS3 = 233; | |
const NOTE_B3 = 247; | |
const NOTE_C4 = 262; | |
const NOTE_CS4 = 277; | |
const NOTE_D4 = 294; | |
const NOTE_DS4 = 311; | |
const NOTE_E4 = 330; | |
const NOTE_F4 = 349; | |
const NOTE_FS4 = 370; | |
const NOTE_G4 = 392; | |
const NOTE_GS4 = 415; | |
const NOTE_A4 = 440; | |
const NOTE_AS4 = 466; | |
const NOTE_B4 = 494; | |
const NOTE_C5 = 523; | |
const NOTE_CS5 = 554; | |
const NOTE_D5 = 587; | |
const NOTE_DS5 = 622; | |
const NOTE_E5 = 659; | |
const NOTE_F5 = 698; | |
const NOTE_FS5 = 740; | |
const NOTE_G5 = 784; | |
const NOTE_GS5 = 831; | |
const NOTE_A5 = 880; | |
const NOTE_AS5 = 932; | |
const NOTE_B5 = 988; | |
const NOTE_C6 = 1047; | |
const NOTE_CS6 = 1109; | |
const NOTE_D6 = 1175; | |
const NOTE_DS6 = 1245; | |
const NOTE_E6 = 1319; | |
const NOTE_F6 = 1397; | |
const NOTE_FS6 = 1480; | |
const NOTE_G6 = 1568; | |
const NOTE_GS6 = 1661; | |
const NOTE_A6 = 1760; | |
const NOTE_AS6 = 1865; | |
const NOTE_B6 = 1976; | |
const NOTE_C7 = 2093; | |
const NOTE_CS7 = 2217; | |
const NOTE_D7 = 2349; | |
const NOTE_DS7 = 2489; | |
const NOTE_E7 = 2637; | |
const NOTE_F7 = 2794; | |
const NOTE_FS7 = 2960; | |
const NOTE_G7 = 3136; | |
const NOTE_GS7 = 3322; | |
const NOTE_A7 = 3520; | |
const NOTE_AS7 = 3729; | |
const NOTE_B7 = 3951; | |
const NOTE_C8 = 4186; | |
const NOTE_CS8 = 4435; | |
const NOTE_D8 = 4699; | |
const NOTE_DS8 = 4978; | |
const REST = 0; | |
const GPIO_BUZZ = 12; | |
const TEMPO_MULT = 1; | |
const FREQ_MULT = 1; | |
const tempo = 144; | |
const melody = [ | |
NOTE_E5, 4, NOTE_B4,8, NOTE_C5,8, NOTE_D5,4, NOTE_C5,8, NOTE_B4,8, | |
NOTE_A4, 4, NOTE_A4,8, NOTE_C5,8, NOTE_E5,4, NOTE_D5,8, NOTE_C5,8, | |
NOTE_B4, -4, NOTE_C5,8, NOTE_D5,4, NOTE_E5,4, | |
NOTE_C5, 4, NOTE_A4,4, NOTE_A4,8, NOTE_A4,4, NOTE_B4,8, NOTE_C5,8, | |
NOTE_D5, -4, NOTE_F5,8, NOTE_A5,4, NOTE_G5,8, NOTE_F5,8, | |
NOTE_E5, -4, NOTE_C5,8, NOTE_E5,4, NOTE_D5,8, NOTE_C5,8, | |
NOTE_B4, 4, NOTE_B4,8, NOTE_C5,8, NOTE_D5,4, NOTE_E5,4, | |
NOTE_C5, 4, NOTE_A4,4, NOTE_A4,4, REST, 4, | |
NOTE_E5, 4, NOTE_B4,8, NOTE_C5,8, NOTE_D5,4, NOTE_C5,8, NOTE_B4,8, | |
NOTE_A4, 4, NOTE_A4,8, NOTE_C5,8, NOTE_E5,4, NOTE_D5,8, NOTE_C5,8, | |
NOTE_B4, -4, NOTE_C5,8, NOTE_D5,4, NOTE_E5,4, | |
NOTE_C5, 4, NOTE_A4,4, NOTE_A4,8, NOTE_A4,4, NOTE_B4,8, NOTE_C5,8, | |
NOTE_D5, -4, NOTE_F5,8, NOTE_A5,4, NOTE_G5,8, NOTE_F5,8, | |
NOTE_E5, -4, NOTE_C5,8, NOTE_E5,4, NOTE_D5,8, NOTE_C5,8, | |
NOTE_B4, 4, NOTE_B4,8, NOTE_C5,8, NOTE_D5,4, NOTE_E5,4, | |
NOTE_C5, 4, NOTE_A4,4, NOTE_A4,4, REST, 4, | |
NOTE_E5,2, NOTE_C5,2, | |
NOTE_D5,2, NOTE_B4,2, | |
NOTE_C5,2, NOTE_A4,2, | |
NOTE_GS4,2, NOTE_B4,4, REST,8, | |
NOTE_E5,2, NOTE_C5,2, | |
NOTE_D5,2, NOTE_B4,2, | |
NOTE_C5,4, NOTE_E5,4, NOTE_A5,2, | |
NOTE_GS5,2, | |
]; | |
const gpio = new Gpio(GPIO_BUZZ, { mode: Gpio.OUTPUT }); | |
const maxVolume = gpio.getPwmRealRange(); | |
const sleep = (time = 100) => new Promise((resolve) => setTimeout(resolve, time)); | |
const beep = async(time = 100, tone = 100) => { | |
gpio.pwmWrite(tone); | |
await sleep(time); | |
gpio.pwmWrite(0); | |
}; | |
// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits) | |
// there are two values per note (pitch and duration), so for each note there are four bytes | |
const notes = melody.length / 2; | |
// this calculates the duration of a whole note in ms | |
const wholenote = (1000 * 60 * 4) / (tempo * TEMPO_MULT); | |
const run = async() => { | |
// iterate over the notes of the melody. | |
// Remember, the array is twice the number of notes (notes + durations) | |
for (let thisNote = 0; thisNote < notes * 2; thisNote += 2) { | |
// calculates the duration of each note | |
const divider = melody[thisNote + 1]; | |
let noteDuration = 0; | |
if (divider > 0) { | |
// regular note, just proceed | |
noteDuration = wholenote / divider; | |
} else if (divider < 0) { | |
// dotted notes are represented with negative durations!! | |
noteDuration = wholenote / Math.abs(divider); | |
noteDuration *= 1.5; // increases the duration in half for dotted notes | |
} | |
gpio.pwmFrequency(Math.floor(melody[thisNote] * FREQ_MULT)); | |
gpio.pwmWrite(Math.floor(maxVolume / 2)); | |
await sleep(noteDuration*0.9) | |
gpio.pwmWrite(0); | |
await sleep(noteDuration*0.1) | |
} | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment