Skip to content

Commit

Permalink
feat: add supported audio playback format detection
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Oct 7, 2023
1 parent a3d9cf5 commit 83a5c75
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/bootstrap/features.ts
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
85 changes: 85 additions & 0 deletions src/utilities/audio.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 83a5c75

Please sign in to comment.