-
Notifications
You must be signed in to change notification settings - Fork 10
/
sketch.js
executable file
·127 lines (103 loc) · 2.45 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
// p5.func examples - gen2_harmonics
// I<3DM rld
var p = 0.;
var gen = new p5.Gen();
var speed = 0.005;
var numharms = 16;
var tabsize = 2048;
var harmonics = [1., 0.3, 0.2, 0.,
0., 0., 0., 0.,
0., 0., 0., 0.,
0., 0., 0., 0.];
var buf = new Tone.Buffer();
var bufplayer;
var si; // sampling increment
var testArr;
var t = new Tone.Frequency();
var within = false;
var tb; // textbox
function setup()
{
createCanvas(800, 600);
doclear = 1;
initBufferSource();
tb = createDiv('');
tb.style("font-family", "Courier");
tb.style("font-size", "12px");
tb.position(20, 20);
tb.size(500, 500);
}
function draw()
{
// background(0);
background(255);
noStroke();
fill(192, 0, 0, 128);
rect(width*0.25, height*0.15, width*0.5, height*0.5);
fill(0);
var q = gen.harmonics(p, harmonics);
var hs = '';
hs+= 'p5.Gen()<br>';
hs+= 'draw harmonics in sliders below.<br>';
tb.html(hs);
var x1 = map(p, 0., 1., width*0.25, width*0.7);
var y1 = map(q, -1., 1., height*0.7, height*0.15);
ellipse(x1, y1, 10, 10);
var base = floor(map(q, -1., 1., 40, 90));
bufplayer.playbackRate.value = si*t.midiToFrequency(base);
noFill();
stroke(255, 0, 0);
rect(width*0.25, height*0.7, width*0.5, height*0.25-10);
var colstep = width*0.5/numharms;
for(var i = 0;i<numharms;i++)
{
i%2 ? fill(192, 192, 0, 128) : fill(255, 0, 0, 128);
var x = i*colstep+width*0.25;
var y = map(harmonics[i], 0., 1., height-45, height*0.7);
rect(x, y, colstep, 5);
}
p=(p+speed)%1.;
}
function drawBar(_x, _y)
{
var i = imap(_x, width*0.25, width*0.75, 0, numharms);
var j = constrain(map(_y, height*0.7, height-45, 1., 0.), 0., 1.);
harmonics[i] = j;
}
function initBufferSource()
{
testArr = gen.fillFloat32Array("harmonics", tabsize, harmonics);
buf.fromArray(testArr);
si = buf.length/Tone.context.sampleRate; // sampling increment
bufplayer = new Tone.BufferSource(buf).toMaster();
bufplayer.loop = true;
bufplayer.playbackRate.value = si*440;
bufplayer.start();
}
function mousePressed()
{
if(mouseX>width*.25&&mouseX<width*.75&&mouseY>height*0.7&&mouseY<height-15)
{
within = true;
drawBar(mouseX, mouseY);
}
else
{
within = false;
}
}
function mouseDragged()
{
if(within)
{
drawBar(mouseX, mouseY);
}
}
function mouseReleased()
{
if(within)
{
bufplayer.dispose(); // out with the old...
initBufferSource(); // ...in with the new.
}
}