-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (102 loc) · 3.03 KB
/
script.js
File metadata and controls
107 lines (102 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//Define variables
//Sound Classification ML Model URL
let modelURL = "https://teachablemachine.withgoogle.com/models/ux_wTI7c4/";
//Label that changes based on classification
let label = 'Waiting For Sound';
//Classifier that imports the sound classification model using ml5
let classifier;
//Importing ML Model
function preload() {
classifier = ml5.soundClassifier(modelURL + 'model.json');
}
//Setup function
function setup() {
//Adjusts canvas size
var cnv = createCanvas(640, 520);
var x = (windowWidth) / 2 - 400;
var y = (windowHeight) / 2 + 350;
cnv.position(x, y);
//Calls the classify audio function
classifyAudio();
//Calls the change image function
changeImage();
console.log("setup complete")
}
//Classifies audio heard
function classifyAudio() {
//Calls the got results function
classifier.classify(gotResults);
console.log("classifies");
}
//Draw function
function draw() {
//Draws background and adjusts text size and label size
background(211);
textSize(50);
//Text changes position based on what is in the label
if (label === 'Background Noise') {
text(label, 205, 50);
}
if(label === 'A Note') {
text(label, 285, 50);
}
if(label === 'B Note') {
text(label, 285, 50);
}
if(label === 'C Note') {
text(label, 285, 50);
}
if(label === 'D Note') {
text(label, 285, 50);
}
if(label === 'E Note') {
text(label, 285, 50);
}
if(label === 'F Note') {
text(label, 285, 50);
}
if(label === 'G Note') {
text(label, 285, 50);
}
}
//Gets classification based on the audio it hears
function gotResults(error, results) {
//If it is unable to classify something, it will return an error
if (error) {
console.error(error);
return;
}
//If it able to classify something, change the label to the name of the object it classifies
label = results[0].label;
//Repeatedly change image and classify audio
changeImage();
classifyAudio();
}
//Changes image based on the text inside the label
function changeImage() {
if(label === 'A Note') {
document.getElementById("img").src = "PianoNotes/ANote.png";
console.log("a reached");
} else if (label === 'B Note') {
document.getElementById("img").src = "PianoNotes/BNote.png";
console.log("b reached");
} else if (label === 'C Note') {
document.getElementById("img").src = "PianoNotes/CNote.png";
console.log("c reached");
} else if (label === 'D Note') {
document.getElementById("img").src = "PianoNotes/DNote.png";
console.log("d reached");
} else if (label === 'E Note') {
document.getElementById("img").src = "PianoNotes/ENote.png";
console.log("e reached");
} else if (label === 'F Note') {
document.getElementById("img").src = "PianoNotes/FNote.png";
console.log("f reached");
} else if (label === 'G Note') {
document.getElementById("img").src = "PianoNotes/GNote.png";
console.log("g reached");
} else {
document.getElementById("img").src = "PianoNotes/blankPiano.png";
console.log("blank piano reached");
}
}