-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
361 additions
and
10 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { assertIsDefined } from "../utils/asserts"; | ||
|
||
|
||
export enum AudioResourceState { | ||
LOADING, | ||
READY, | ||
ERROR // Just in case I may decide to try again due to network issues... | ||
}; | ||
|
||
export type AudioResourceListener = (resource: AudioResource) => void; | ||
|
||
export class AudioResource { | ||
private state: AudioResourceState = AudioResourceState.LOADING; | ||
private buffer: AudioBuffer | undefined; | ||
|
||
constructor(public readonly url: string, context: AudioContext, listener: AudioResourceListener) { | ||
const request = new XMLHttpRequest(); | ||
request.responseType = 'arraybuffer'; | ||
request.open('GET', url, true); | ||
request.onload = () => { | ||
context.decodeAudioData(request.response, | ||
buffer => { | ||
this.buffer = buffer; | ||
this.state = AudioResourceState.READY; | ||
listener(this); | ||
}, | ||
e => { | ||
this.state = AudioResourceState.ERROR; | ||
console.error(`Error decoding audio data from resource "${url}".`, e); | ||
}); | ||
}; | ||
request.onerror = () => { | ||
this.state = AudioResourceState.ERROR; | ||
console.error(`Error loading audio resource "${url}"`); | ||
}; | ||
request.send(); | ||
} | ||
|
||
getBuffer(): AudioBuffer { | ||
assertIsDefined(this.buffer, `Trying to use an invalid AudioResource: ${this.url}`); | ||
return this.buffer; | ||
} | ||
|
||
get isReady(): boolean { return this.state === AudioResourceState.READY; } | ||
} | ||
|
||
interface AudioResourceWrapper { | ||
resource: AudioResource; | ||
listeners: AudioResourceListener[]; | ||
} | ||
|
||
export class AudioResourceManager { | ||
private resources: Map<string, AudioResourceWrapper> = new Map(); | ||
|
||
constructor(private context: AudioContext) { } | ||
|
||
load(url: string, listener: AudioResourceListener): AudioResource { | ||
let wrapper = this.resources.get(url); | ||
|
||
if (!wrapper) { | ||
const resource = new AudioResource(url, this.context, resource => this.onResourceLoaded(resource)); | ||
wrapper = { resource, listeners: [listener] }; | ||
this.resources.set(url, wrapper); | ||
} else if (!wrapper.resource.isReady) { | ||
wrapper.listeners.push(listener); | ||
} | ||
|
||
return wrapper.resource; | ||
} | ||
|
||
private onResourceLoaded(resource: AudioResource) { | ||
const wrapper = this.resources.get(resource.url); | ||
assertIsDefined(wrapper); | ||
wrapper.listeners.forEach(l => l(resource)); | ||
wrapper.listeners.length = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { AudioResourceManager } from "./audioResources"; | ||
|
||
export class AudioClip { | ||
private audioSource: AudioBufferSourceNode | undefined; | ||
private gainNode: GainNode | undefined; | ||
private _rate = 1.0; | ||
private _gain = 1.0; | ||
private started = false; | ||
|
||
constructor(url: string, private context: AudioContext, resources: AudioResourceManager, private loop: boolean) { | ||
resources.load(url, resource => { | ||
this.audioSource = new AudioBufferSourceNode(context, { buffer: resource.getBuffer(), loop, playbackRate: this._rate }); | ||
this.gainNode = new GainNode(context, { gain: this._gain }); | ||
this.audioSource.connect(this.gainNode).connect(context.destination); | ||
}); | ||
} | ||
|
||
set rate(value: number) { | ||
this._rate = value; | ||
if (this.audioSource) { | ||
this.audioSource.playbackRate.value = value; | ||
} | ||
} | ||
|
||
set gain(value: number) { | ||
this._gain = value; | ||
if (this.gainNode) { | ||
this.gainNode.gain.value = value; | ||
} | ||
} | ||
|
||
play() { | ||
if (!this.started) { | ||
this.started = true; | ||
this.audioSource?.start(); | ||
} else { | ||
this.gainNode?.connect(this.context.destination); | ||
} | ||
} | ||
|
||
stop() { | ||
this.gainNode?.disconnect(this.context.destination); | ||
} | ||
} | ||
|
||
export class AudioSystem { | ||
private context = new AudioContext(); | ||
private resources = new AudioResourceManager(this.context); | ||
private globals: Map<string, AudioClip> = new Map(); | ||
|
||
getGlobal(url: string, loop: boolean): AudioClip { | ||
let clip = this.globals.get(url); | ||
|
||
if (!clip) { | ||
clip = new AudioClip(url, this.context, this.resources, loop); | ||
this.globals.set(url, clip); | ||
} | ||
|
||
return clip; | ||
} | ||
|
||
getInstance(url: string, loop: boolean): AudioClip { | ||
return new AudioClip(url, this.context, this.resources, loop); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters