-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
66 lines (52 loc) · 1.95 KB
/
main.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
import meyda from 'meyda'
import { visualize } from './visualize.js';
const audioContext = new AudioContext();
let rmsLevels = 0;
// const source = audioContext.createMediaElementSource(audio);
// const audioElement = document.querySelector('audio');
recordButton.addEventListener('click', () => {
if (audioContext.state === 'suspended') {
audioContext.resume();
navigator.mediaDevices.getUserMedia({ audio: true })
.then((stream) => {
//Use the stream here
const micStream = audioContext.createMediaStreamSource(stream);
meydaAnalyzers(micStream, 'start');
console.log('Microphone activated');
//micStream.connect(audioContext.destination);
recordButton.classList.add('micButtonOn');
recordButton.classList.remove('micButtonOff');
}).catch((err) => {
console.log('Microphone error');
}); //end of navigator function
} //End of if statement
//Suspend mic use after user button press
else if (audioContext.state !== 'suspended') {
audioContext.suspend();
console.log('Microphone suspended');
meydaAnalyzers('', 'stop');
recordButton.classList.remove('micButtonOn');
recordButton.classList.add('micButtonOff');
}
});
//source.connect(audioContext.destination);
function meydaAnalyzers(sourceStream, command) {
if (typeof meyda === "undefined") {
console.log("Meyda could not be found! Have you included it?");
} else {
const analyzer = meyda.createMeydaAnalyzer({
audioContext: audioContext,
source: sourceStream,
bufferSize: 512,
featureExtractors: ["rms", "amplitudeSpectrum", "spectralCentroid"],
callback: (features) => {
visualize(features);
//console.log(features.amplitudeSpectrum); //float32 array
audioLevels.innerHTML = features.rms * 1000;
spectralCentroid.textContent = features.spectralCentroid;
},
});
analyzer.start();
console.log('Starting Analyzer');
}
}