-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavEMS.pde
363 lines (263 loc) · 8.71 KB
/
wavEMS.pde
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
2018/9/7
Michinari Kono
U-Tokyo
wavEMS
audio output ver. for EMS
based on the Minim example
sin waves, saw waves, square waves, russian current
*/
import ddf.minim.*;
import ddf.minim.ugens.*;
import javax.sound.sampled.*;
//change the polar feat.
final int MONO = 0;
final int BI = 1;
//wave form
final int SINW = 0;
final int SAWW = 1;
final int SQUAREW = 2;
final int RUSSIAN = 3;
//number of mode variations
final int NMODE = 4;
//freq variations
float freqs[] = {20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 400};
float pulseWidths[] = {40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 400, 440, 480, 520, 560, 600};
//create waves
Minim minim;
AudioSample wave_square1, wave_square2, wave_saw1, wave_saw2, wave_sin1, wave_sin2, wave_russian1, wave_russian2;
AudioSample wave;
AudioSample waves1[] = new AudioSample[NMODE];
AudioSample waves2[] = new AudioSample[NMODE];
//initialize
int pole = MONO; // pole
int mode = SQUAREW; // mode of wave from
float waveFrequency = 160; // default freq.
int freqsi = 7; //8th parameter in freqs array
int sec = 1; //output second
float pulseWidth = 120; //default pulse
int pulsesi = 4; //5th parameter
float pulseWidthMapped;
float waveSampleRate = 44100f;
//1000000/44100=22.675
//set the gains, tends to be sq > sin > saw so it requires adjustement
float gainSin = 2;
float gainSaw = 6;
float gainSq = -2;
AudioFormat format = new AudioFormat( waveSampleRate, // sample rate
16, // sample size in bits
1, // channels
true, // signed
true // bigEndian
);
void setup()
{
size(1000, 300);
textSize(18);
minim = new Minim(this);
}
//draw the interface
void draw() {
background(0);
stroke(255);
if (wave != null) {
//text("VOL:" + wave.getVolume(), 0, 0);
// use the mix buffer to draw the waveforms.
for (int i = 0; i < wave.bufferSize() - 1; i++) {
line(i, 80 - wave.left.get(i)*50, i+1, 80 - wave.left.get(i+1)*50);
}
}
int posx = 60;
text("SIN", posx, 180);
text("SAW", posx, 180 + 30);
text("SQUARE", posx, 180 + 60);
text("RUSSIAN", posx, 180 + 90);
text("" + waveFrequency + " Hz (+/-)", posx + 130, 180);
text("" + pulseWidth + " PulseWidth (w/e)", posx + 130, 210);
if(pole == MONO)text("Mono Pole (Mono/Bi)", posx + 130, 240);
else if(pole == BI)text("Bi Pole (p)", posx + 130, 240);
text("LEFT/RIGHT:trigger UP/DOWN:wave type", posx + 130, 180+90);
noFill();
rect(posx - 10, 164 + mode * 30, 100, 25);
pulseWidthMapped = map(pulseWidth, 0, 1000000, 0, 44100);
}
void keyPressed() {
println("key=" + key + " keyCode:" + keyCode);
if (keyCode == LEFT) {
trigger();
wave = waves1[mode];
if(mode == SINW){
wave.setGain(gainSin);
}
else if(mode == SAWW){
wave.setGain(gainSaw);
}
else if(mode == SQUAREW){
wave.setGain(gainSq);
}
else if(mode == RUSSIAN){
wave.setGain(gainSin);
}
println(wave.getGain());
wave.trigger();
} else if (keyCode == RIGHT) {
trigger();
wave = waves2[mode];
if(mode == SINW){
wave.setGain(gainSin);
}
else if(mode == SAWW){
wave.setGain(gainSaw);
}
else if(mode == SQUAREW){
wave.setGain(gainSq);
}
else if(mode == RUSSIAN){
wave.setGain(gainSin);
}
println(wave.getGain());
wave.trigger();
} else if (keyCode == UP) {
mode = (mode - 1 + NMODE) % NMODE;
} else if (keyCode == DOWN) {
mode = (mode + 1) % NMODE;
} else if (keyCode == 61) { //+
freqsi = (freqsi + 1) % freqs.length;
waveFrequency = freqs[freqsi];
} else if (keyCode == 45) { //-
freqsi = (freqsi - 1 + freqs.length) % freqs.length;
waveFrequency = freqs[freqsi];
} else if (key == ' ') { // reset
freqsi = 7;
waveFrequency = freqs[freqsi];
mode = SQUAREW;
pole = MONO;
} else if (keyCode == 80) { //p
if(pole == MONO)pole = BI;
else if(pole == BI)pole = MONO;
} else if (keyCode == 69) { //e
pulsesi = (pulsesi + 1) % pulseWidths.length;
pulseWidth = pulseWidths[pulsesi];
} else if (keyCode == 87) { //w
pulsesi = (pulsesi - 1 + pulseWidths.length) % pulseWidths.length;
pulseWidth = pulseWidths[pulsesi];
}
}
void trigger(){
float[] samples1 = new float[(int)waveSampleRate*sec];
float[] samples2 = new float[(int)waveSampleRate*sec];
float dur = waveSampleRate / waveFrequency; //cycle
if(mode == SINW){
sinwaves(dur, samples1, samples2, pole);
//wave.setGain(gainSin);
}
else if(mode == SAWW){
sawwaves(dur, samples1, samples2, pole);
//wave.setGain(gainSaw);
}
else if(mode == SQUAREW){
squarewaves(dur, samples1, samples2, pole);
//wave.setGain(gainSq);
}
else if(mode == RUSSIAN){
russian(dur, samples1, samples2, pole);
//wave.setGain(gainSq);
}
}
void sinwaves(float dur, float[] samples1, float[] samples2, int pole){
for(int j = 0; j < samples1.length/dur; j++){
for(int i = 0; i < (int)pulseWidthMapped; i++){
samples1[i+(int)dur*j] = (float)Math.sin(i/pulseWidthMapped * 1 * Math.PI);
}
if(pole == BI){
for(int i = (int)pulseWidthMapped; i < pulseWidthMapped*2; i++){
samples1[i+(int)dur*j] = (float)Math.sin(i/pulseWidthMapped * 1 * Math.PI);
}
}
for(int ii = 0; ii < (dur - (pulseWidthMapped*(pole+1)) ); ii++){
samples1[(int)pulseWidthMapped*(pole+1) + ii +(int)dur*j] = 0;
}
}
//reverse the signals to sample2
for(int k = 0; k < samples1.length; k++)samples2[k] = -samples1[k];
// finally, create the AudioSample
wave_sin1 = minim.createSample( samples1, format);
wave_sin2 = minim.createSample( samples2, format);
waves1[0] = wave_sin1;
waves2[0] = wave_sin2;
}
void sawwaves(float dur, float[] samples1, float[] samples2, int pole){
for(int j = 0; j < samples1.length/dur; j++){
for(int i = 0; i < ((int)pulseWidthMapped); i++){
samples1[i+(int)dur*j] = i/(pulseWidthMapped-1);
}
if(pole == BI){
for(int i = (int)pulseWidthMapped; i < ((pulseWidthMapped*2) + 1); i++){
samples1[i+(int)dur*j] = -(i-pulseWidthMapped)/(pulseWidthMapped);
}
}
for(int ii = 0; ii < (dur - (pulseWidthMapped*(pole+1)) ); ii++){
samples1[(int)pulseWidthMapped*(pole+1) + ii +(int)dur*j] = 0;
}
}
//reverse the signals to sample2
for(int k = 0; k < samples1.length; k++)samples2[k] = -samples1[k];
// finally, create the AudioSample
wave_saw1 = minim.createSample( samples1, format);
wave_saw2 = minim.createSample( samples2, format);
waves1[1] = wave_saw1;
waves2[1] = wave_saw2;
}
void squarewaves(float dur, float[] samples1, float[] samples2, int pole){
//whole dur in one pulse and rest: dur
for(int j = 0; j < samples1.length/dur; j++){
for(int i = 0; i < (int)pulseWidthMapped; i++){
samples1[i+(int)dur*j] = 1;
}
if(pole == BI){
for(int i = (int)pulseWidthMapped; i < pulseWidthMapped*2; i++){
samples1[i+(int)dur*j] = -1;
}
}
for(int ii = 0; ii < (dur - (pulseWidthMapped*(pole+1)) ); ii++){
samples1[(int)pulseWidthMapped*(pole+1) + ii +(int)dur*j] = 0;
}
}
//reverse the signals to sample2
for(int k = 0; k < samples1.length; k++)samples2[k] = -samples1[k];
// finally, create the AudioSample
wave_square1 = minim.createSample( samples1, format);
wave_square2 = minim.createSample( samples2, format);
waves1[2] = wave_square1;
waves2[2] = wave_square2;
}
void russian(float dur, float[] samples1, float[] samples2, int pole){
//pulseWidth = 400/2
//frequency = 2.5 khz
//10ms ON and 10ms OFF
//waveFrequency = 2500;
//float cycle = waveSampleRate/2500;
pulseWidth = 200;
pulseWidthMapped = map(pulseWidth, 0, 1000000, 0, 44100);
for(int j = 0; j < samples1.length/882; j++){
for (int a = 0; a < 25; a ++){
for(int i = 0; i < (int)pulseWidthMapped; i++){
samples1[i+ j*882 +(int)pulseWidthMapped*2*a] = (float)Math.sin(i/pulseWidthMapped * 1 * Math.PI);
}
for(int i = (int)pulseWidthMapped; i < pulseWidthMapped*2; i++){
samples1[i+ j*882 +(int)pulseWidthMapped*2*a] = (float)Math.sin(i/pulseWidthMapped * 1 * Math.PI);
}
}
for (int b = 0; b < (int)waveSampleRate/100; b ++){
/////
samples1[(int)pulseWidthMapped*2*25 + b + j*882] = 0;
}
}
//reverse the signals to sample2
for(int k = 0; k < samples1.length; k++)samples2[k] = -samples1[k];
// finally, create the AudioSample
wave_russian1 = minim.createSample( samples1, format);
wave_russian2 = minim.createSample( samples2, format);
waves1[3] = wave_russian1;
waves2[3] = wave_russian2;
}