-
Notifications
You must be signed in to change notification settings - Fork 0
/
p.js
98 lines (87 loc) · 1.56 KB
/
p.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
var minim = null;
var in = null;
var fft = null;
var note = null;
var n = 0;
var c = 0x00000000;
var sampleRate = 44100;
var max = $p.createJavaArray('float', [sampleRate/2]); var maximum = 0; var frequency = 0;
function setup() {
$p.size(800, 400);
minim = new Minim(this);
minim.debugOn();
in = minim.getLineIn(Minim.MONO, 4096*8, sampleRate);
fft = new FFT(in.left.size(), sampleRate);
}
$p.setup = setup;
function draw() {
$p.background(0);
findNote();
$p.textSize(50);
$p.text (frequency+" hz", 50, 80); $p.pushStyle();
$p.fill(c);
$p.text (""+note, 50, 150); $p.popStyle();
}
$p.draw = draw;
function findNote() {
fft.forward(in.left);
for (var f = 0; f<sampleRate/2; f++) {
max[f]=fft.getFreq($p.parseFloat(f));
}
maximum=max(max);
for (var i = 0; i<max.length; i++) {
if (max[i] == maximum) {
frequency= i;
}
}
frequency =
n= $p.parseInt(frequency);
if (n==248)
{
note = "E";
c = $p.color (255, 0, 0);
}
if (n==221) {
note = "A";
c = $p.color (255, 150, 0);
}
if (n==263) {
note = "C";
c = $p.color (255, 255, 0);
}
if (n==131)
{
note = "C";
c = $p.color (255, 255, 0);
}
if (n==98)
{
note = "G";
c = $p.color (100, 150, 190);
}
if (n==197)
{
note = "G";
c = $p.color (100, 150, 190);
}
if (n==587) {
note = "G";
c = $p.color (100, 150, 190);
}
if (n==147)
{
note = "G"; c = $p.color (100, 150, 190);
}
if (n==740)
{
note = "D";
c = $p.color (0, 150, 250);
}
}
$p.findNote = findNote;
function stop() {
in.close();
minim.stop();
$super.stop();
}
$p.stop = stop;