-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
165 lines (157 loc) · 3.81 KB
/
sketch.js
File metadata and controls
165 lines (157 loc) · 3.81 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Basic Pitch Detection
=== */
let audioContext;
let mic;
let pitch;
const width = document.documentElement.clientWidth;
//const height = document.documentElement.clientHeight;
const height = 500;
let point_list = [];
const point_num = 50;
const point_size = 3;
const freqs = new Array(12);
let targetNote;
let closest;
let base_c = 130.813;
let canvas;
let ctx;
let freq = -1;
let shifter;
let splitter;
let merger;
const notes = [
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B",
"C",
];
function setup() {
initializeCanvas();
targetNote = document.getElementById("target").value;
freqs[0] = [32.703, 65.406, 130.813, 261.626, 523.251, 1046.502];
for (let i = 1; i < freqs.length; i++) {
freqs[i] = freqs[0].map((v) => v * Math.pow(2, i / 12));
}
mic = new p5.AudioIn();
mic2 = mic;
mic.start();
}
const initializeCanvas = () => {
canvas = document.getElementById("myChart");
canvas.width = width;
canvas.height = height + 20;
ctx = canvas.getContext("2d");
ctx.lineWidth = 10;
ctx.strokeStyle = "#FF0000";
ctx.lineCap = "round";
};
function touchStarted() {
userStartAudio();
getAudioContext().suspend();
if (getAudioContext().state !== "running") {
//audioContext = getAudioContext();
audioContext = getAudioContext();
audioContext.resume();
Tone.context = audioContext;
// ここから本格スタート
startPitch();
shifter = new Tone.PitchShift({
pitch: 0,
});
merger = audioContext.createChannelMerger(2);
mic.connect(merger, 0, 0);
mic.connect(shifter);
shifter.connect(merger, 0, 1);
merger.connect(audioContext.destination);
}
}
const startPitch = () => {
pitch = ml5.pitchDetection("./model/", audioContext, mic.stream, modelLoaded);
};
const modelLoaded = () => {
select("#status").html("Model Loaded");
getPitch();
};
const getPitch = () => {
pitch.getPitch((err, frequency) => {
if (frequency) {
select("#result").html(frequency);
freq = frequency;
} else {
select("#result").html("No pitch detected");
freq = -1;
}
getPitch();
});
};
const onChangeTarget = () => {
targetNote = document.getElementById("target").value;
};
function drawnotes() {
notes.map((note, i) => {
ctx.fillText(note, 0, 15 + height * (2 - Math.pow(2, i / 12)));
ctx.beginPath();
ctx.moveTo(15, 10 + height * (2 - Math.pow(2, i / 12)));
ctx.lineTo(width, 10 + height * (2 - Math.pow(2, i / 12)));
if (i == targetNote || (targetNote == 0 && (i == 0 || i == 12))) {
ctx.strokeStyle = "#00FF00";
} else if ([1, 3, 6, 8, 10].includes(i)) {
ctx.strokeStyle = "#C0C0C0";
} else {
ctx.strokeStyle = "#000000";
}
ctx.lineWidth = 5;
ctx.stroke();
});
}
function drawPoint(x, y) {
ctx.beginPath();
ctx.arc(x, y, point_size, (0 * Math.PI) / 180, (360 * Math.PI) / 180, false);
ctx.fillStyle = "rgb(255,0,0)";
ctx.fill();
ctx.restore();
}
const drawAllPoints = () => {
point_list.map((p, i) => {
drawPoint(
width / 2 - (width / 2 / point_num) * (point_list.length - i),
7.5 + p
);
});
};
function draw() {
ctx.clearRect(0, 0, width, height + 20);
drawnotes();
if (point_list.length > point_num) {
point_list.shift();
}
if (freq < 0) {
drawAllPoints();
return;
}
base_c = freqs[0].filter((e) => e <= freq).slice(-1)[0];
point_list.push(height * (2 - freq / base_c));
closest = freqs[targetNote].reduce((prev, curr) => {
return Math.abs(curr - freq) < Math.abs(prev - freq) ? curr : prev;
});
shifter.pitch = Math.round(
(freq < closest ? -1 : 1) *
(12 * Math.log2(Math.min(freq, closest) / Math.max(freq, closest)))
);
drawAllPoints();
}