-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_audio_pde.js
141 lines (122 loc) · 3.72 KB
/
record_audio_pde.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
/**
* @name Mic Input
* @description <p>Get audio input from your computer's microphone.
* Make noise to float the ellipse.</p>
* <p>Note: p5.AudioIn contains its own p5.Amplitude object,
* so you can call getLevel on p5.AudioIn without
* creating a p5.Amplitude.</p>
* <p><em><span class="small"> To run this example locally, you will need the
* <a href="http://p5js.org/reference/#/libraries/p5.sound">p5.sound library</a>
* and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">local server</a>.</span></em></p>
*/
var input, i;
var analyzer;
var SoundData = [];
var currentLocation;
var recording = null;
var amplitude;
var locationData,
locationDataIndex = 0,
mySound,
locationPosition = { x: 100, y: 200 },
locationRotation = [0, 0, 0]
function preload() {
soundFormats('mp3');
//mySound = loadSound('data/sample.mp3');
mySound = loadSound('data/subway.mp3');
locationData = loadJSON('data/data.json')
}
function setup() {
createCanvas(windowWidth, windowHeight);
//background(0)
rectMode(RADIUS)
// Create an Audio input
//mic = new p5.AudioIn();
amplitude = new p5.Amplitude();
// start the Audio Input.
mySound.setVolume(0.18);
mySound.play();
// By default, it does not .connect() (to the computer speakers)
//mic.start();
analyzer = new p5.FFT();
analyzer.setInput(mySound);
amplitude.setInput(mySound);
}
function draw() {
background(255, 0.09);
var level = amplitude.getLevel();
var size = map(level, 0, 1, 0, 200);
var spectrum = analyzer.analyze();
var waveform = analyzer.waveform();
// because we don't need the entire spectrum (1024) we will truncate it
spectrum.length = 512;
fill(127);
stroke(0);
translate(width/2, height/2);
for (i = 0; i < spectrum.length; i++) {
// evenly distribute specturm in a circle
rotate(TWO_PI/spectrum.length);
// Set color Value
colorMode(HSB);
noStroke();
var c = color(map(i, 0, spectrum.length, 0, 360), 100, 100);
fill(c);
// Circlular Lines 1
push();
stroke(c);
strokeWeight(1);
line( size + 50, 0, size + spectrum[i] + 50, 0);
//ellipse( 50, 0, map(spectrum[i], 0, 255, 0, 500) + 50, 0);
pop();
// Circles are plotted on line caps from center outward
push();
// Circle 2
ellipse(map(spectrum[i], 0, 255, 0, 255) + 100, 0, 5, 5);
pop();
}
for (i = 0; i< waveform.length; i++){
// evenly distribute waveform values in a circle
rotate(TWO_PI/waveform.length);
// Set color Ranges
noStroke();
fill(0);
push();
//blendMode(MULTIPLY);
ellipse(map(waveform[i], -1, 1, 0, 100) + 150, 0, 2, 2);
pop();
}
push()
//blendMode(SCREEN)
// invert color
//c.levels[0] = abs(255 - c.levels[0]);
//c.levels[1] = abs(255 - c.levels[1]);
//c.levels[2] = abs(255 - c.levels[2]);
// console.log(c);
fill(0, 0.75);
ellipse(locationPosition.x, locationPosition.y, 50, 50);
if (locationDataIndex < locationData.data.length) {
switch(locationData.data[locationDataIndex].sensor_name) {
case "LGE Linear Acceleration Sensor":
locationPosition.x -= locationData.data[locationDataIndex].value[0] * 20;
locationPosition.y -= locationData.data[locationDataIndex].value[1] * 20;
break;
case "LGE Magnetometer":
locationRotation = abs(locationRotation[0]) - locationData.data[locationDataIndex].value[0];
rotate(locationRotation[0]);
break;
case "LG Motion Accel":
break;
}
}
locationDataIndex++;
blendMode(BLEND)
pop();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function keyPressed() {
if (keyCode === 32) {
saveFrames("commute-visualization-", "jpg", 1, 25);
}
}