From d8138f61107622fef9ebba6bceea93c2239eae43 Mon Sep 17 00:00:00 2001 From: khaled Date: Thu, 30 Nov 2023 20:54:37 +0100 Subject: [PATCH] Channel Streamer Erorr Enhancement of error handling --- nuxt.config.ts | 4 +- pages/ChannelStreamer.vue | 88 +++++++++++++++++++++++---------------- pages/octave.js | 1 + 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/nuxt.config.ts b/nuxt.config.ts index f7a6991..2ae15c6 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -8,7 +8,9 @@ import fs from 'fs' export default defineNuxtConfig({ - + alias: { + '@': path.resolve(__dirname, ''), + }, devServer: { diff --git a/pages/ChannelStreamer.vue b/pages/ChannelStreamer.vue index e533d2e..295dc22 100644 --- a/pages/ChannelStreamer.vue +++ b/pages/ChannelStreamer.vue @@ -18,53 +18,67 @@ export default { }, methods: { async startMicrophone() { - - this.audioContext = new window.AudioContext (); - - // Request access to the microphone - this.microphoneStream = await navigator.mediaDevices.getUserMedia({ audio: true }); - - // Check if the microphone stream is available - if (!this.microphoneStream) { - console.error("Microphone stream is not available."); - this.errorMessage = "Microphone stream is not available."; - return; - } - - // Ensure that the microphoneStream is a MediaStream object - if (!(this.microphoneStream instanceof MediaStream)) { - console.error("Invalid microphone stream."); - this.errorMessage = "Invalid microphone stream."; - return; - } - - // Create a MediaStreamAudioSourceNode from the microphone stream - const microphoneSource = this.audioContext.createMediaStreamSource(this.microphoneStream); - await this.audioContext.audioWorklet.addModule('@/plugins/octave.js'); - this.audioProcessorNode = new AudioWorkletNode(this.audioContext, 'octave'); - microphoneSource.connect(this.audioProcessorNode); - this.audioProcessorNode.connect(this.audioContext.destination); - // Try to add the Audio Worklet module - + try { + this.initializeAudioContext(); + await this.requestMicrophoneAccess(); + await this.setupAudioProcessing(); + } catch (error) { + console.error('Error starting microphone:', error.message); + this.errorMessage = error.message; + } }, stopMicrophone() { + this.cleanup(); + }, + initializeAudioContext() { + this.audioContext = new window.AudioContext(); + }, + async requestMicrophoneAccess() { + try { + this.microphoneStream = await navigator.mediaDevices.getUserMedia({ audio: true }); + } catch (error) { + console.error('Error accessing microphone:', error.message); + this.errorMessage = 'Microphone access denied. Please grant permission in your browser settings.'; + throw new Error('Microphone access denied.'); + } + + if (!this.microphoneStream || !(this.microphoneStream instanceof MediaStream)) { + throw new Error('Microphone stream is not available.'); + } + }, + async setupAudioProcessing() { + try { + const microphoneSource = this.audioContext.createMediaStreamSource(this.microphoneStream); + await this.audioContext.audioWorklet.addModule('@/plugins/octave.js'); + + //this.audioProcessorNode = new AudioWorkletNode(this.audioContext, 'octave'); + //microphoneSource.connect(this.audioProcessorNode); + //this.audioProcessorNode.connect(this.audioContext.destination); + } catch (error) { + console.error('Error setting up audio processing:', error.message); + this.errorMessage = 'Error setting up audio processing. Please check your microphone and try again.'; + throw new Error('Audio processing setup failed.'); + } + }, + cleanup() { if (this.audioContext) { - // Disconnect and close the AudioWorkletNode if (this.audioProcessorNode) { this.audioProcessorNode.disconnect(); this.audioProcessorNode.port.postMessage({ command: 'stop' }); } - - // Close the AudioContext this.audioContext.close(); - - // Reset variables - this.audioContext = null; - this.microphoneStream = null; - this.audioProcessorNode = null; - this.errorMessage = null; + this.resetVariables(); } }, + resetVariables() { + this.audioContext = null; + this.microphoneStream = null; + this.audioProcessorNode = null; + this.errorMessage = null; + }, + }, + beforeDestroy() { + this.cleanup(); }, }; diff --git a/pages/octave.js b/pages/octave.js index 1442b5b..4b3d1de 100644 --- a/pages/octave.js +++ b/pages/octave.js @@ -1,3 +1,4 @@ +console.log('Octave module loaded successfully!'); new AudioWorkletProcessor () class OctaveBandProcessor extends AudioWorkletProcessor {