AudioImporter

View source

ExtendsImporter

Imports browser-supported audio responses as decoded Audio assets.

The importer reports extensions from the current browser’s HTML audio capability checks, reads each response as an ArrayBuffer, and decodes it with an OfflineAudioContext. Each successful decode produces an Audio value containing the resulting AudioBuffer.

Examples

import { AssetServer } from '@wimaengine/asset'
import { Audio, AudioAssets } from '@wimaengine/audio'

function loadTheme(world) {
  const server = world.getResource(AssetServer)
  const handle = server.load(Audio, '/audio/theme.ogg')
  const sound = world.getResource(AudioAssets).get(handle)
  // `sound` is the decoded `Audio` asset when it is available.
  return sound
}

Constructor


constructor(options?: OfflineAudioContextOptions)

View source

Creates an importer with an OfflineAudioContext decoder.

The options are forwarded to the browser’s OfflineAudioContext constructor. If omitted, the importer uses a 44.1 kHz context with a 512-sample render length. This configuration affects decoding only.

Parameters

NameDescriptionDefault
options?

Offline decoding context options.

...

Methods


deserialize(response: Response, _typeRegistry: TypeRegistry): Promise<Audio>

View source

Converts a fetched response into the typed asset value.

Override this method to read the response body, parse its contents, and resolve a T. Use the TypeRegistry argument when the format stores reflected or nested values; a self-contained response can ignore it. A concrete importer should resolve an asset value that can be stored for the requested asset.

The base implementation always rejects with an Error because it has no response format to parse. The undefined return below is only an unreachable fallback after that error.

Parameters

NameDescriptionDefault
response

Fetched response containing encoded audio bytes.

—
_typeRegistry

Importer type registry, unused for browser audio decoding.

—

Throws

The base implementation always rejects with an error.

Returns

A concrete importer should resolve the imported asset. The base implementation always rejects before resolving.

getExtensions(): string[]

View source

Lists the path extensions handled by this importer.

AssetServer indexes each returned label with the importer’s asset constructor. Labels must omit the leading dot and match the extension extracted from a load path exactly, including case. Return one label for each supported response format.

The base implementation always throws an Error because it does not know which formats the concrete importer supports. The empty array below is only an unreachable fallback after that error.

Throws

The base implementation always throws an error.

Returns

Supported extension labels, such as ['json'].