-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
156 lines (154 loc) · 5.37 KB
/
audio.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
150
151
152
153
154
155
156
function WebAudio() {
this.type = "";
this.audioCtx = new (window.AudioContext ||
window.webkitAudioContext)();
this.stream = {};
this.audioSourceNode = {};
this.analyserNode = {};
}
wa = new WebAudio();
document.getElementById("buff_size").value = 512;
document.getElementById("source_audio")
.addEventListener("change",async function (e) {
try {
wa.audioSourceNode.disconnect();
wa.analyserNode.disconnect()
}
catch {
console.log("could not disconnect audio node");
}
if (e.target.value == "audio_in") {
document.getElementById("l_osc_type").classList.add("hidden")
document.getElementById("l_osc_freq").classList.add("hidden")
document.getElementById("osc_type").classList.add("hidden")
document.getElementById("osc_freq").classList.add("hidden")
document.getElementById("l_file_input").classList.add("hidden")
document.getElementById("file_input").classList.add("hidden")
wa.type = e.target.value;
wa.stream = await get_mic();
wa.audioSourceNode = wa.audioCtx
.createMediaStreamSource(wa.stream);
} else if (e.target.value == "file") {
document.getElementById("l_osc_type").classList.add("hidden")
document.getElementById("l_osc_freq").classList.add("hidden")
document.getElementById("osc_type").classList.add("hidden")
document.getElementById("osc_freq").classList.add("hidden")
document.getElementById("l_file_input").classList.remove("hidden")
document.getElementById("file_input").classList.remove("hidden")
wa.type = e.target.value;
wa.stream = new Audio();
wa.stream.src = await get_input();
wa.stream.autoplay = true;
wa.stream.preload = 'auto';
wa.audioSourceNode = wa.audioCtx
.createMediaElementSource(wa.stream);
} else {
wa.type = "osc";
wa.audioSourceNode = wa.audioCtx.createOscillator();
wa.audioSourceNode.type = document.getElementById("osc_type")
.value;
wa.audioSourceNode.frequency.setValueAtTime(
document.getElementById("osc_freq").value,
wa.audioCtx.currentTime);
wa.audioSourceNode.start();
document.getElementById("l_osc_type").classList.remove("hidden")
document.getElementById("l_osc_freq").classList.remove("hidden")
document.getElementById("osc_type").classList.remove("hidden")
document.getElementById("osc_freq").classList.remove("hidden")
document.getElementById("l_file_input").classList.add("hidden")
document.getElementById("file_input").classList.add("hidden")
}
wa.analyserNode = wa.audioCtx.createAnalyser();
wa.analyserNode.fftSize = document.getElementById("buff_size").value;
svg.init_graph(wa.analyserNode.fftSize)
wa.bufferLength = wa.analyserNode.fftSize;
wa.dataArray = new Uint8Array(wa.bufferLength);
wa.audioSourceNode.connect(wa.analyserNode);
if (document.getElementById("out_monitor").checked
&& wa.type != "audio_in") {
wa.analyserNode.connect(wa.audioCtx.destination);
} else {
document.getElementById("out_monitor").checked = false;
}
if (!svg.drawing("status")) {
draw();
async function draw() {
wa.analyserNode.getByteTimeDomainData(wa.dataArray);
svg.fourier(wa.dataArray)
await incr_wait(0,
(wa.analyserNode.fftSize/wa.audioCtx.sampleRate)*1000)
draw();
}
}
})
function get_mic() {
return new Promise(function(resolve,reject){
navigator.mediaDevices.getUserMedia ({audio: true, video: false})
.then(async function(stream) {resolve(stream)})
})
}
document.getElementById("osc_freq").addEventListener("change",
function (e) {
wa.audioSourceNode.frequency
.setValueAtTime(e.target.value, wa.audioCtx.currentTime);
})
document.getElementById("osc_type").addEventListener("change",
function (e) {
wa.audioSourceNode.type = e.target.value;
})
document.getElementById("pt_size").addEventListener("change",
function (e) {
svg.init_graph(wa.analyserNode.fftSize)
})
document.getElementById("buff_size").addEventListener("change",
function (e) {
bufsize = (wa.analyserNode.fftSize || 512);
if (e.target.value != bufsize) {
bufsize = pow_two(bufsize);
bufsize = (wa.analyserNode.fftSize > e.target.value) ?
Math.pow(2,bufsize-1):Math.pow(2,bufsize+1);
try {
svg.init_graph(bufsize)
e.target.value = bufsize;
wa.analyserNode.fftSize = bufsize;
wa.bufferLength = wa.analyserNode.fftSize;
wa.dataArray = new Uint8Array(wa.bufferLength);
}
catch {
console.log("buffersize failure");
}
}
})
function get_input() {
return new Promise(function(resolve,reject){
document.getElementById("file_input").addEventListener("change",
async function (e) {
src = await read(e.target.files[0]);
resolve(src)
})
})
}
document.getElementById("out_monitor").addEventListener("change",
async function (e) {
if (e.target.checked && wa.type != "audio_in") {
wa.analyserNode.connect(wa.audioCtx.destination);
} else {
wa.analyserNode.disconnect();
}
})
function read(what) {
return new Promise(function(resolve,reject){
let reader = new FileReader();
reader.addEventListener('load', function() {
resolve(this.result)
});
reader.readAsDataURL(what);
})
}
function pow_two(number) {
let i = 1;
while (Math.pow(2,i)<number) {
i++;
}
return i;
}