-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preload sounds before they are played
- Loading branch information
Showing
8 changed files
with
114 additions
and
66 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from "./library.ts"; | ||
export * from "./loader.ts"; | ||
export * from "./types.ts"; |
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 |
---|---|---|
@@ -1,31 +1,97 @@ | ||
import { loadPlayer, nullPlayer } from "./loader.ts"; | ||
import { type Player, type SoundAssets, type SoundName } from "./types.ts"; | ||
import { expectType, request } from "@keybr/request"; | ||
import { getAudioContext } from "./audiocontext.ts"; | ||
import { pickPlayableUrl } from "./mediatypes.ts"; | ||
import { nullPlayer, WebAudioPlayer } from "./player.ts"; | ||
import { | ||
type Player, | ||
type PlayerConfig, | ||
type SoundAssets, | ||
type SoundName, | ||
} from "./types.ts"; | ||
|
||
const library = { | ||
players: new Map<SoundName, Player>(), | ||
}; | ||
class PlayerLoader { | ||
buffer: ArrayBuffer | null = null; | ||
player: Player | null = null; | ||
|
||
export function loadSounds(assets: SoundAssets): void { | ||
constructor(readonly config: PlayerConfig) {} | ||
|
||
/** | ||
* Stage one: we load sound data, but we don't create players yet | ||
* because there was no user gesture and AudioContext is not available. | ||
*/ | ||
async load() { | ||
try { | ||
const url = pickPlayableUrl(this.config.urls); | ||
if (url != null) { | ||
const response = await request | ||
.use(expectType("audio/*")) | ||
.GET(url) | ||
.send(); | ||
this.buffer = await response.arrayBuffer(); | ||
} else { | ||
this.player = nullPlayer; | ||
} | ||
} catch (err) { | ||
this.player = nullPlayer; | ||
throw err; | ||
} | ||
} | ||
|
||
/** | ||
* Stage two: we convert the loaded sound data into players. | ||
* We assume that at this point there was a user gesture | ||
* and AudioContext is already available. | ||
*/ | ||
async init() { | ||
if (this.buffer != null && this.player == null) { | ||
try { | ||
const context = getAudioContext(); | ||
if (context != null) { | ||
const buffer = await context.decodeAudioData(this.buffer); | ||
const player = new WebAudioPlayer(context, buffer); | ||
this.buffer = null; | ||
this.player = player; | ||
} else { | ||
this.buffer = null; | ||
this.player = nullPlayer; | ||
} | ||
} catch (err) { | ||
this.buffer = null; | ||
this.player = nullPlayer; | ||
throw err; | ||
} | ||
} | ||
return this.player ?? nullPlayer; | ||
} | ||
} | ||
|
||
const loaders = new Map<SoundName, PlayerLoader>(); | ||
|
||
export function loadSounds(assets: SoundAssets) { | ||
for (const [name, config] of Object.entries(assets)) { | ||
if (!library.players.has(name)) { | ||
library.players.set(name, nullPlayer()); | ||
loadPlayer(config).then( | ||
(player) => { | ||
library.players.set(name, player); | ||
}, | ||
(error) => { | ||
console.log(error); | ||
}, | ||
); | ||
let loader = loaders.get(name); | ||
if (loader == null || loader.config !== config) { | ||
loader = new PlayerLoader(config); | ||
loaders.set(name, loader); | ||
loader.load().catch(catchError); | ||
} | ||
} | ||
} | ||
|
||
export function playSound(name: SoundName, volume: number = 1): void { | ||
const player = library.players.get(name); | ||
if (player == null) { | ||
export function playSound(name: SoundName, volume: number = 1) { | ||
const loader = loaders.get(name); | ||
if (loader == null) { | ||
throw new Error(String(name)); | ||
} | ||
player.volume(volume); | ||
player.play(); | ||
loader | ||
.init() | ||
.then((player) => { | ||
player.volume(volume); | ||
player.play(); | ||
}) | ||
.catch(catchError); | ||
} | ||
|
||
function catchError(err: any) { | ||
console.error(err); | ||
} |
This file was deleted.
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
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