Skip to content

Commit

Permalink
Added TypeScript typings to Audio class
Browse files Browse the repository at this point in the history
  • Loading branch information
profexorgeek committed Dec 28, 2021
1 parent 8661450 commit 5eb02ee
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Audio/Audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import Data from '../Data/Data';

export default class Audio {
private context: AudioContext;
private _cache = {};
private _cache: any = {};

constructor() {
this.context = new AudioContext();
}

enable() {
enable(): void {
if(this.context.state === 'suspended') {
this.context.resume();
}
}

playSound(src) {
playSound(src: string): AudioBufferSourceNode {
// try to resume context if suspended
if(this.context.state == 'suspended') {
this.enable();
}


// EXCEPTION: sounds must be fully loaded before they can be played
if(src in this._cache == false || this._cache[src] == null) {
const msg = `Tried to play sound that hasn't been loaded: ${src}`;
FrostFlake.Log.error(msg);
Expand All @@ -31,7 +31,7 @@ export default class Audio {
// EARLY OUT: can't play audio because the context isn't running
if(this.context.state !== 'running') {
FrostFlake.Log.warn('Cannot play sounds: audio context is not running');
return;
return null;
}

let sound = this.context.createBufferSource();
Expand All @@ -41,7 +41,7 @@ export default class Audio {
return sound;
}

async loadSound(src) {
async loadSound(src: string): Promise<AudioBuffer> {
// EARLY OUT: sound already loaded
if(src in this._cache) {
return this._cache[src];
Expand All @@ -60,7 +60,7 @@ export default class Audio {
throw Error(msg);
}

const promise = new Promise((resolve, reject) => {
const promise = new Promise<AudioBuffer>((resolve, reject) => {
this.context.decodeAudioData(buffer, (decoded) => {
this._cache[src] = decoded;
resolve(decoded);
Expand Down

0 comments on commit 5eb02ee

Please sign in to comment.