From c8da3e6da36a7d7eea6b1aef5534ceb38f1f8d88 Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Tue, 3 Dec 2024 20:14:16 +0100 Subject: [PATCH] Added check on audio interval --- frontend/react/src/audio/filesource.ts | 29 ++++++++-------- .../react/src/audio/texttospeechsource.ts | 33 ++++++++++--------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/frontend/react/src/audio/filesource.ts b/frontend/react/src/audio/filesource.ts index 5e3b5859..f324b119 100644 --- a/frontend/react/src/audio/filesource.ts +++ b/frontend/react/src/audio/filesource.ts @@ -8,7 +8,7 @@ export class FileSource extends AudioSource { #source: AudioBufferSourceNode; #duration: number = 0; #currentPosition: number = 0; - #updateInterval: any; + #updateInterval: number | null; #lastUpdateTime: number = 0; #playing = false; #audioBuffer: AudioBuffer; @@ -56,19 +56,21 @@ export class FileSource extends AudioSource { AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); - this.#updateInterval = setInterval(() => { - /* Update the current position value every second */ - const now = Date.now() / 1000; - this.#currentPosition += now - this.#lastUpdateTime; - this.#lastUpdateTime = now; + if (this.#updateInterval === null) { + this.#updateInterval = window.setInterval(() => { + /* Update the current position value every second */ + const now = Date.now() / 1000; + this.#currentPosition += now - this.#lastUpdateTime; + this.#lastUpdateTime = now; - if (this.#currentPosition > this.#duration) { - this.#currentPosition = 0; - if (!this.#looping) this.pause(); - } + if (this.#currentPosition > this.#duration) { + this.#currentPosition = 0; + if (!this.#looping) this.pause(); + } - AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); - }, 1000); + AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); + }, 1000); + } } pause() { @@ -79,7 +81,8 @@ export class FileSource extends AudioSource { const now = Date.now() / 1000; this.#currentPosition += now - this.#lastUpdateTime; - clearInterval(this.#updateInterval); + if (this.#updateInterval) window.clearInterval(this.#updateInterval); + this.#updateInterval = null; AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); } diff --git a/frontend/react/src/audio/texttospeechsource.ts b/frontend/react/src/audio/texttospeechsource.ts index 29c61c03..0d69e8c8 100644 --- a/frontend/react/src/audio/texttospeechsource.ts +++ b/frontend/react/src/audio/texttospeechsource.ts @@ -6,7 +6,7 @@ export class TextToSpeechSource extends AudioSource { #source: AudioBufferSourceNode; #duration: number = 0; #currentPosition: number = 0; - #updateInterval: any; + #updateInterval: number | null; #lastUpdateTime: number = 0; #playing = false; #audioBuffer: AudioBuffer; @@ -69,22 +69,24 @@ export class TextToSpeechSource extends AudioSource { AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); - this.#updateInterval = setInterval(() => { - /* Update the current position value every second */ - const now = Date.now() / 1000; - this.#currentPosition += now - this.#lastUpdateTime; - this.#lastUpdateTime = now; + if (this.#updateInterval === null) { + this.#updateInterval = window.setInterval(() => { + /* Update the current position value every second */ + const now = Date.now() / 1000; + this.#currentPosition += now - this.#lastUpdateTime; + this.#lastUpdateTime = now; - if (this.#currentPosition > this.#duration) { - this.#currentPosition = 0; - if (!this.#looping) { - this.pause(); - this.onMessageCompleted(); + if (this.#currentPosition > this.#duration) { + this.#currentPosition = 0; + if (!this.#looping) { + this.pause(); + this.onMessageCompleted(); + } } - } - AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); - }, 1000); + AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); + }, 1000); + } } pause() { @@ -95,7 +97,8 @@ export class TextToSpeechSource extends AudioSource { const now = Date.now() / 1000; this.#currentPosition += now - this.#lastUpdateTime; - clearInterval(this.#updateInterval); + if (this.#updateInterval) window.clearInterval(this.#updateInterval); + this.#updateInterval = null; AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources()); }