From 83a5c754a76620b860b14c3d0892a8e55dd0a5c1 Mon Sep 17 00:00:00 2001 From: Valerian Saliou Date: Sat, 7 Oct 2023 14:13:49 +0200 Subject: [PATCH] feat: add supported audio playback format detection --- src/bootstrap/features.ts | 18 +++++++++ src/main.ts | 1 + src/utilities/audio.ts | 85 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/bootstrap/features.ts create mode 100644 src/utilities/audio.ts diff --git a/src/bootstrap/features.ts b/src/bootstrap/features.ts new file mode 100644 index 00000000..9405a2b8 --- /dev/null +++ b/src/bootstrap/features.ts @@ -0,0 +1,18 @@ +/* + * This file is part of prose-app-web + * + * Copyright 2023, Prose Foundation + */ + +/************************************************************************** + * IMPORTS + * ************************************************************************* */ + +// PROJECT: UTILITIES +import UtilitiesAudio from "@/utilities/audio"; + +/************************************************************************** + * FEATURES + * ************************************************************************* */ + +UtilitiesAudio.detect(); diff --git a/src/main.ts b/src/main.ts index e0221748..415a129c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,6 +21,7 @@ import Store from "@/store"; // PROJECT: BOOTSTRAP import "@/bootstrap/icons"; +import "@/bootstrap/features"; import BootstrapConfig from "@/bootstrap/config"; import BootstrapFilters from "@/bootstrap/filters"; diff --git a/src/utilities/audio.ts b/src/utilities/audio.ts new file mode 100644 index 00000000..bb551256 --- /dev/null +++ b/src/utilities/audio.ts @@ -0,0 +1,85 @@ +/* + * This file is part of prose-app-web + * + * Copyright 2023, Prose Foundation + */ + +/************************************************************************** + * IMPORTS + * ************************************************************************* */ + +// PROJECT: UTILITIES +import logger from "@/utilities/logger"; + +/************************************************************************** + * ENUMERATIONS + * ************************************************************************* */ + +enum AudioFormat { + // OGA format (here: Vorbis codec). + OGA = "oga", + // M4A format (here: AAC codec). + M4A = "m4a" +} + +/************************************************************************** + * CONSTANTS + * ************************************************************************* */ + +const AUDIO_TYPES_PIPELINE = [ + { + format: AudioFormat.OGA, + mime: 'audio/ogg; codecs="vorbis"' + }, + + { + format: AudioFormat.M4A, + mime: 'audio/mp4; codecs="mp4a.40.2"' + } +]; + +/************************************************************************** + * AUDIO + * ************************************************************************* */ + +class UtilitiesAudio { + private __format: AudioFormat | null; + + constructor() { + // Initialize supported audio format + this.__format = null; + } + + detect(): void { + if (this.__format === null) { + // Create transient audio element + const audioElement = document.createElement("audio"); + + if (typeof audioElement.canPlayType === "function") { + for (let i = 0; i < AUDIO_TYPES_PIPELINE.length; i++) { + const audioType = AUDIO_TYPES_PIPELINE[i]; + + if (audioElement.canPlayType(audioType.mime) === "probably") { + this.__format = audioType.format; + + break; + } + } + } + + if (this.__format === null) { + logger.warn("No audio format supported, sounds will not play"); + } else { + logger.info(`Detected supported audio format: ${this.__format}`); + } + } else { + throw new Error("Audio format already detected"); + } + } +} + +/************************************************************************** + * EXPORTS + * ************************************************************************* */ + +export default new UtilitiesAudio();