-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
149 lines (101 loc) · 3.75 KB
/
sound.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
142
143
144
145
146
147
148
149
// const canvas = document.querySelector('#stream');
// const canvasCtx = canvas.getContext('2d');
// const WIDTH = window.innerWidth;
// const HEIGHT = window.innerHeight;
// canvas.width = WIDTH;
// canvas.height = HEIGHT;
// Globals
var aCtx;
var analyser;
var microphone;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true}, function(stream) {
aCtx = new webkitAudioContext();
analyser = aCtx.createAnalyser();
microphone = aCtx.createMediaStreamSource(stream);
microphone.connect(analyser);
analyser.connect(aCtx.destination);
process();
});
};
function process(){
setInterval(function(){
FFTData = new Float32Array(analyser.frequencyBinCount);
analyser.getFloatFrequencyData(FFTData);
console.log(FFTData[0]);
},10);
}
// var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// var analyser = audioCtx.createAnalyser();
// function gotStream(stream) {
// // Create an AudioNode from the stream
// var mediaStreamSource = audioCtx.createMediaStreamSource(stream);
// // Connect it to destination to hear yourself
// // or any other node for processing!
// mediaStreamSource.connect(audioCtx.destination);
// draw();
// }
// navigator.mediaDevices.getUserMedia({audio:true})
// .then(gotStream)
// .catch(function(err) { console.log(err); });
// analyser.fftSize = 256;
// var bufferLength = analyser.frequencyBinCount;
// console.log(bufferLength);
// var dataArray = new Float32Array(bufferLength);
// canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
// function draw() {
// drawVisual = requestAnimationFrame(draw);
// analyser.getFloatFrequencyData(dataArray);
// canvasCtx.fillStyle = 'rgb(0, 0, 0)';
// canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
// var barWidth = (WIDTH / bufferLength) * 2.5;
// var barHeight;
// var x = 0;
// for(var i = 0; i < bufferLength; i++) {
// barHeight = (dataArray[i] + 140)*2;
// canvasCtx.fillStyle = 'rgb(' + Math.floor(barHeight+100) + ',50,50)';
// canvasCtx.fillRect(x,HEIGHT-barHeight/2,barWidth,barHeight/2);
// x += barWidth + 1;
// }
// };
// window.AudioContext = window.AudioContext || window.webkitAudioContext;
// var audioContext = new AudioContext();
// var analyser = audioContext.createAnalyser();
// function gotStream(stream) {
// // Create an AudioNode from the stream
// var mediaStreamSource = audioContext.createMediaStreamSource(stream);
// // Connect it to destination to hear yourself
// // or any other node for processing!
// mediaStreamSource.connect(audioContext.destination);
// visualize();
// }
// navigator.mediaDevices.getUserMedia({audio:true})
// .then(gotStream)
// .catch(function(err) { console.log(err); });
// function visualize() {
// analyser.fftSize = 256;
// var bufferLength = analyser.frequencyBinCount;
// var dataArray = new Float32Array(bufferLength);
// // var dataArray = new Uint8Array(bufferLength);
// var barWidth = (WIDTH / bufferLength) * 2.5;
// var barHeight;
// var x = 0;
// canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
// function draw() {
// // console.log('drawing');
// drawVisual = requestAnimationFrame(draw);
// // analyser.getByteFrequencyData(dataArray);
// // analyser.getByteFrequencyData(dataArray);
// analyser.getFloatFrequencyData(dataArray);
// // Make background black
// canvasCtx.fillStyle = 'rgb(0,0,0)';
// canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
// for(var i = 0; i < bufferLength; i++) {
// barHeight = dataArray[i]/2;
// canvasCtx.fillStyle = 'rgb(' + (barHeight+100) + ',50,50)';
// canvasCtx.fillRect(x,HEIGHT-barHeight/2,barWidth,barHeight);
// x += barWidth + 1;
// }
// }
// draw();
// }