-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
143 lines (120 loc) · 2.88 KB
/
sketch.js
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
var volumeLevel;
var freq;
var wave;
var button;
var playing;
var time;
var count = "";
var counter;
var freqList = "";
var sel;
var fft;
var amp;
var nowPlaying = "";
function setup() {
createCanvas(300, 380);
volumeLevel = createSlider(0.05, 2, 0, 0.01);
volumeLevel.position(80, 30);
freq = createInput();
freq.position(60, 110);
time = createInput();
time.position(60, 170);
sel = createSelect();
sel.position(110, 220);
sel.option('square');
sel.option('sine');
sel.option('triangle');
sel.option('sawtooth');
sel.changed(setWave);
button = createButton('play / stop');
button.position(110, 260);
button.mousePressed(toggle);
wave = new p5.Oscillator();
wave.setType('square');
fft = new p5.FFT(0.8,128);
fft.setInput(wave);
}
function draw() {
background(50);
fill(255);
noStroke();
text('Volume', volumeLevel.x + volumeLevel.width / 2 - 10, 30);
text('Freqencies (Hz)', freq.x + freq.width / 2 - 38, 85);
if (freqList.length === 1 || time.value() === "") {
text('Current: ' + freqList[0], freq.x + freq.width / 2 - 40, 105);
} else {
text('Current: ' + nowPlaying + ' Hz / time: ' + timeLeft + ' s', freq.x + freq.width / 2 - 60, 105);
}
text('Wave', sel.x + sel.width, 215);
text('Time ' + count + ' (seconds)', time.x + time.width / 2 - 40, 160);
noFill();
stroke(255);
let spectrum = fft.analyze();
rect(85, height - 80, 128, 70);
beginShape();
for (x = 0; x < spectrum.length; x++) {
let y = map(spectrum[x], 0, 255, height-10, 300);
vertex(x+85, y);
}
endShape();
wave.amp(volumeLevel.value());
}
var timer;
var freqChange;
var currentFreq;
var timeLeft;
function toggle() {
if (!playing) {
wave.start();
freqList = freq.value().split(",");
currentFreq = 0;
if(freqList.length > 1 && time.value() === "") {
time.value(60)
}
wave.freq(float(freqList[currentFreq]));
nowPlaying = float(freqList[currentFreq]);
playing = true;
freqChange = setInterval(() => {
currentFreq++;
wave.freq(float(freqList[currentFreq]));
nowPlaying = float(freqList[currentFreq]);
if (currentFreq === time.value()) {
clearInterval(freqChange);
currentFreq = 0;
}
}, time.value() * 1000);
if (float(time.value()) > 0) {
counter = 0;
timeLeft = time.value();
count = time.value() * freqList.length;
timer = setInterval(() => {
count = float(time.value() * freqList.length - counter - 1);
counter++;
timeLeft--;
if (timeLeft === 0) {
timeLeft = time.value();
}
if (counter === float(time.value()) * freqList.length) {
wave.stop();
playing = false;
clearInterval(timer);
clearInterval(freqChange);
currentFreq = 0;
timeLeft = 0;
nowPlaying = "";
}
}, 1000);
}
} else {
wave.stop();
playing = false;
clearInterval(timer);
clearInterval(freqChange);
count = "";
timeLeft = 0;
currentFreq = 0;
}
}
function setWave() {
wave.setType(sel.value());
}