forked from geraintluff/jsfx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
frequency-graph.jsfx-inc
133 lines (120 loc) · 3.16 KB
/
frequency-graph.jsfx-inc
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
@init
function frequency_graph_init(low_freq, high_freq, low_db, high_db) (
this.low_freq = low_freq;
this.high_freq = high_freq;
this.low_db = low_db;
this.high_db = high_db;
);
function frequency_graph_x_hz(freq) local(xratio) (
xratio = log(freq/this.low_freq)/log(this.high_freq/this.low_freq);
//xratio = xratio*0.9 + 0.1*(freq - this.low_freq)/(this.high_freq - this.low_freq);
ui_left() + xratio*ui_width();
);
function frequency_graph_y_db(db) local(yratio) (
yratio = (db - this.high_db)/(this.low_db - this.high_db);
ui_top() + yratio*ui_height();
);
function frequency_graph_graph(buffer, count, high_freq, logfreq, dbAmpOrPower) local(i, hz, amp, db, step_counter, freq_range, x, prev_x, y) (
freq_range = log(this.high_freq/this.low_freq);
ui_push();
ui_retina(1);
i = 0;
prev_x = ui_left() - 1;
while (i < count) (
hz = logfreq == 0 ? i/count*high_freq : this.low_freq*exp(freq_range*i/count);
amp = buffer[i];
step_counter = 1;
while (i < count && this.frequency_graph_x_hz(hz) < prev_x + 0.5) (
i += 1;
hz = logfreq == 0 ? i/count*high_freq : this.low_freq*exp(freq_range*i/count);
amp += buffer[i];
step_counter += 1;
);
amp /= step_counter;
x = this.frequency_graph_x_hz(hz);
prev_x = x;
db = dbAmpOrPower == 0 ? amp : (
buffer[i] !== 0 ? (
dbAmpOrPower == 1 ? 20*log10(amp) : 10*log10(amp);
) : (
-360;
)
);
x = this.frequency_graph_x_hz(hz);
y = this.frequency_graph_y_db(db);
x = max(ui_left(), min(ui_right(), x));
y = max(ui_top(), min(ui_bottom(), y));
i == 0 ? (
gfx_x = x;
gfx_y = y;
) : (
gfx_lineto(x, y);
);
i += 1;
);
ui_pop();
);
function frequency_graph_hz_db(buffer, count, high_freq) (
this.frequency_graph_graph(buffer, count, high_freq, 0, 0);
);
function frequency_graph_grid() local(fi, fmag, freq, xratio, db) (
ui_push();
ui_retina(1);
fi = 1;
fmag = 10;
while (fi*fmag < this.high_freq) (
freq = fi*fmag;
freq > this.low_freq ? (
gfx_x = this.frequency_graph_x_hz(freq);
gfx_y = ui_top();
ui_color(255, 255, 255, 0.3);
gfx_lineto(gfx_x, ui_bottom());
fi == 1 ? (
ui_fontsize(10*ui_retina());
ui_color(255, 255, 255, 0.5);
gfx_x += ui_texth()*0.5;
gfx_y = ui_bottom() - ui_texth()*1.5;
fmag >= 1000 ? (
gfx_printf("%ikHz", freq/1000);
) : (
gfx_printf("%iHz", freq);
);
);
);
fi += 1;
fi >= 10 ? (
fi /= 10;
fmag *= 10;
);
);
db = 0;
while (abs(db) < max(abs(this.high_db), abs(this.low_db))) (
debug.plot += 1;
db > this.low_db && db < this.high_db ? (
gfx_y = this.frequency_graph_y_db(db);
debug.y = gfx_y;
gfx_x = ui_left();
db == 0 ? (
ui_color(255, 255, 255, 0.5);
) : (
ui_color(255, 255, 255, 0.15);
);
gfx_lineto(ui_right(), gfx_y);
ui_fontsize(10*ui_retina());
abs(gfx_y - this.frequency_graph_y_db(db*0.25)) > ui_texth() ? (
ui_color(255, 255, 255, 0.5);
gfx_x = ui_left() + ui_texth()*0.5;
gfx_y -= ui_texth()*1;
gfx_printf("%idB", dB);
);
);
db == 0 ? (
db = 3;
) : db > 0 ? (
db = -db;
) : (
db = -2*db;
);
);
ui_pop();
);