Übung 47 (P5.JS Sound)
Code für meine Melodie:
da für "polySynth.Play" die zeit benötigt wird wann die Note gespielt wird, rechnen wir für jede Note die gespielt wurde ihre Dauer auf die gesamte zeit drauf.
Beispiel:
als erstes spielen wir eine G2 für 1s, wir rechnen also auf die Startzeit 0 5*0,2s drauf. Dafür schreiben wir in das Array ['', 5, 'G4',... ]. Wenn diese Note gespielt wurde ist die "aktuelle" zeit also 1. Wenn wir danach 2 mal (also 0,4s) A5 spielen wollen, erweitern wir das array auf ['', 5, 'G4',2, 'A5' ]
Der "Kniff" besteht also darin, das wir während wir die Sounds abspielen anhand von ihrer länge ausrechnen wann der nächste Sound gestartet werden soll.
Sketch:
let polySynth;
let melody = ['', '3', 'G4', '2', 'A5', '2', 'A4', '1', 'A3', '2', 'G1', '2', 'G4', '2', 'G4', '1', 'G4', '1', 'F4', '2', 'A4', '3', 'C4', '1', 'D4', '3', 'E3', '2', 'E2', '1', 'A2', '4', 'D4', '1', 'C4', '1', 'C4', '1', 'C4', '3', 'G4', '2', 'A4', '1', 'A3', '2', 'G1'];
function setup() {
let cnv = createCanvas(100, 100);
cnv.mousePressed(playSynth);
background(220);
text('click to play', 20, 20);
polySynth = new p5.PolySynth();
}
function playSynth() {
userStartAudio();
// note duration (in seconds)
let dur = 0.1;
// time from now (in seconds)
let time = 0;
// velocity (volume, from 0 to 1)
let vel = 0.5;
for (let i = 2; i < melody.length; i = i + 2) {
let note = melody[i];
let on = 0.2 * parseFloat(melody[i - 1]);
polySynth.play(note, vel, time += on, dur);
}}
