xxxxxxxxxx
207
let loopBeat;
let bassSynth;
let cymbalSynth;
let amSynth;
let fmSynth;
let pluckSynth;
let snare;
let counter;
//teachable
// The video
let video;
// For displaying the label
let label = "waiting...";
// The classifier
let classifier;
let modelURL = 'https://storage.googleapis.com/tm-models/YadBJmj5/';
function preload() {
classifier = ml5.imageClassifier(modelURL + 'model.json');
}
function setup() {
createCanvas(640, 520);
// Create the video
video = createCapture(VIDEO);
video.hide();
// STEP 2: Start classifying
classifyVideo();
counter = 0;
bassSynth = new Tone.MembraneSynth().toMaster();
pluckSynth = new Tone.PluckSynth().toMaster();
amSynth = new Tone.AMSynth({
harmonicity: 1.4, //5/1, //3/1
detune: 0,
oscillator: {
type: "sine" //carrier signal
},
envelope: {
attack: 0.0001,
decay: 0.01,
sustain: 1,
release: 0.5
},
modulation: {
type: "square"
},
modulationEnvelope: {
attack: 0.005,
decay: 0,
sustain: 1,
release: 0.5
}
}).toMaster();
fmSynth = new Tone.FMSynth({
harmonicity: 1.02,
modulationIndex: 10,
detune: 0,
oscillator: {
type: "sine"
},
envelope: {
attack: 0.01,
decay: 0.01,
sustain: 1,
release: 0.5
},
modulation: {
type: "square"
},
modulationEnvelope: {
attack: 0.005,
decay: 0,
sustain: 1,
release: 0.5
}
}).toMaster();
cymbalSynth = new Tone.MetalSynth({
frequency: 250,
envelope: {
attack: 0.001,
decay: 0.1,
release: 0.01
},
harmonicity: 3.1,
modulationIndex: 16,
resonance: 8000,
octaves: 0.5
}
).toMaster();
snare = new Tone.NoiseSynth({
"volume": -5,
"envelope": {
"attack": 0.001,
"decay": 0.2,
"sustain": 0
},
"filterEnvelope": {
"attack": 0.001,
"decay": 0.1,
"sustain": 0
}
}).toMaster();
loopBeat = new Tone.Loop(song, '16n').start();;
}
function draw() {
Tone.Transport.start(0).bpm.value = 100;
background(0);
// Draw the video
image(video, 0, 0);
// STEP 4: Draw the label
textSize(32);
textAlign(CENTER, CENTER);
fill(255);
text(label, width / 2, height - 16);
// Pick an emoji, the "default" is train
let emoji = "🚂";
if (label == "Rainbow") {
emoji = "🌈";
} else if (label == "Unicorn") {
emoji = "🦄";
} else if (label == "Ukulele") {
emoji = "🎸";
}
// Draw the emoji
textSize(256);
text(emoji, width / 2, height / 2);
}
function song(time) {
if (counter % 4 == 0) {
bassSynth.triggerAttackRelease('F#1', '8n', time, 1);
}
if (counter % 4 !== 1) {
snare.triggerAttackRelease();
if (counter == 3 || counter == 12) {
cymbalSynth.envelope.decay = 0.5;
} else {
cymbalSynth.envelope.decay = 0.01;
}
cymbalSynth.triggerAttackRelease('32n', time, 0.3);
}
if (counter == 0) {
amSynth.triggerAttackRelease('A1', '16n', time, 1);
}
if (counter == 10) {
amSynth.triggerAttackRelease('Bb1', '16n', time, 1);
}
if (counter == 0) {
fmSynth.triggerAttackRelease('A2', '16n', time, 1);
}
if (counter == 10) {
fmSynth.triggerAttackRelease('Bb2', '16n', time, 1);
}
if (counter%2 == 0) {
pluckSynth.triggerAttackRelease('B6', '16n', time, 1);
}
if (counter == 10) {
pluckSynth.triggerAttackRelease('Bb2', '16n', time, 0.4);
}else{
pluckSynth.triggerAttackRelease('G#6', '16n', time, 0.4);
}
counter = (counter + 1) % 16;
}
function gotResults(error, results) {
// Something went wrong!
if (error) {
console.error(error);
return;
}
// Store the label and classify again!
label = results[0].label;
classifyVideo();
}