With an ionic angular app, I am trying to get the audio signal from navigator.getMediaDevices({ audio : true} ) to then use an audio analyser to get the float time domain data.
The code executes as follows:
somewhere in the service constructor:
this.audioContext = new window.AudioContext();
this.analyser = this.audioContext.createAnalyser();
this.analyser.fftSize = this.settings.values.fftSize; // Valued 8192
this.audioBuffer = new Float32Array(this.analyser.fftSize);
Then, somewhere else I toggle the stream with getUserMedia
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then((stream => {
//console.log('Mic stream OK! ', stream);
this.stream = stream;
this.isCapturing = true;
// Create an AudioNode from the stream
this.mediaStreamSource = this.audioContext.createMediaStreamSource(this.stream);
this.mediaStreamSource.connect(this.analyser);
//start the analysis cycle. This is non related but it starts the cycle that uses the getfloatTimeDomainData(buffer)
this.animationFrame = requestAnimationFrame(this.streamAnalysis.bind(this));
}).bind(this))
.catch(err => console.log('[PMATIC] Error accessing mic: ', err));
this prompts my users to enable the app to listen from the microphone
And then, in the analysis cycle (The cycle is constantly recording until the user prompts it to stop)
private tuneAnalysis():void { this.analyser.getFloatTimeDomainData(this.audioBuffer);
However, whenever I access this.audioBuffer, the array elements are always equal to 0.
Sometimes it works, but 99% of the times it doesn't. This used to work and works fine when I deploy in a mobile android app using cordova, however, here, in google chrome it simply doesn't ,or at least not reliably. And I can't seem to find a pattern of why it works or it doesn't work. I've checked my microphone and in use with other apps it works correctly. No exception is raised, i can record from my mic perfectly with other apps, and I also ensure other apps that may be using the microphone are closed when I try to record. I'm using typescript and i've tried declaring the variables both public and private but it doesn't really work.