# @ferret/media: API (continued)

### SoundDef

*interface* · `packages/media/src/audio.ts:35`

```ts
interface SoundDef
```

- `url?: string` — a recording; or `synth`, a recipe played live (see synth.ts)
- `synth?: SynthRecipe`
- `bus?: Bus`
- `volume?: number` — 0..1, default 1
- `loop?: boolean`
- `loopPoints?: 'file' | { start: number; end: number }` — where a looping recording repeats: `file` takes the first loop the WAV file carries (its `smpl` chunk, as a sound editor writes it), or `{ start, end }` in seconds; default the whole sound
- `spatial?: Spatial` — spatial: louder near the source; `min` is the distance of full volume, `max` where it is silent (metres)

### SoundHandle

*interface* · `packages/media/src/audio.ts:70`

```ts
interface SoundHandle
```

- `readonly playing: boolean`
- `setVolume(v: number, fade?: number): void`
- `stop(fade?: number): void`
- `set(param: string, value: number): void` — set a live parameter of a synthesised sound (an engine's throttle); nothing for a recording

### SoundInfo

*interface* · `packages/media/src/sound-io.ts:9`

```ts
interface SoundInfo
```

- `format: SoundFormat`
- `rate: number`
- `channels: number`
- `frames: number` — frames per channel
- `duration: number`
- `bitrate: number` — kbit/s as stored (MP3: the average) or what the WAV sample format makes: rate * channels * bits
- `bits?: number` — WAV: bits per sample and encoding; MP3: variable bitrate and the encoder's name
- `encoding?: string`
- `vbr?: boolean`
- `encoder?: string`
- `loops: LoopPoints[]` — WAV `smpl` loops; an MP3 has none (it is cut gapless instead)
- `cues: CuePoint[]`

### SoundWriteOptions

*interface* · `packages/media/src/sound-io.ts:52`

```ts
interface SoundWriteOptions
```

- `wav?: WavWriteOptions`
- `mp3?: Mp3EncodeOptions`

### SourceResult

*interface* · `packages/media/src/source.ts:18`

```ts
interface SourceResult
```

- `mode: 'native' | 'hls.js' | 'none'`
- `destroy(): void`

### Spatial

*interface* · `packages/media/src/audio.ts:12`

```ts
interface Spatial
```

How loudness falls with distance (Web Audio's models): `linear` from full at `min` to silent at `max`; `inverse` min / (min + rolloff * (d - min)),
never silent (the far guns of a battle); `exponential` (d / min) ^ -rolloff.

- `min?: number`
- `max?: number`
- `model?: 'linear' | 'inverse' | 'exponential'`
- `rolloff?: number`
- `air?: boolean` — the air takes the highs of a far sound (ISO 9613-1 absorption, 20 C, 70 % humidity): a lowpass that follows the distance to the listener, so a far source is duller as well as quieter

### SynthContext

*interface* · `packages/media/src/synth.ts:51`

```ts
interface SynthContext
```

The part of an AudioContext a recipe needs (tests pass a fake).

- `currentTime: number`
- `sampleRate: number`
- `createOscillator(): OscillatorNode`
- `createBufferSource(): AudioBufferSourceNode`
- `createBiquadFilter(): BiquadFilterNode`
- `createGain(): GainNode`
- `createBuffer(channels: number, length: number, rate: number): AudioBuffer`

### SynthLayer

*interface* · `packages/media/src/synth.ts:24`

```ts
interface SynthLayer
```

- `osc?: OscType` — an oscillator (`osc` with `freq` in Hz) or white noise (`noise: true`)
- `freq?: Value`
- `noise?: boolean`
- `filter?: FilterSpec`
- `gain?: Value` — the layer's gain (default 1)
- `start?: number` — seconds after the sound's start the layer begins (default 0) and how long it lasts (default the recipe's `dur`)
- `dur?: number`

### SynthRecipe

*interface* · `packages/media/src/synth.ts:37`

```ts
interface SynthRecipe
```

- `layers: SynthLayer[]`
- `dur?: number` — seconds; a recipe without it plays until it is stopped (a loop: an engine, a wind)
- `filter?: FilterSpec` — after the layers are mixed: one filter and one envelope for all of them
- `gain?: Value`
- `params?: Record<string, number>` — the parameters' values before the game sets them
- `then?: Array<{ at: number; sound: string; volume?: number }>` — more sounds started from this one, `at` seconds after it on the audio clock (a boom after a roar)

### SynthVoice

*interface* · `packages/media/src/synth.ts:61`

```ts
interface SynthVoice
```

- `set(param: string, value: number): void` — set a live parameter
- `stop(when: number): void` — stop every source at that audio time
- `readonly end: number` — the audio time it ends by itself (Infinity for a loop)
- `onended: (() => void) | null` — called once when every source has ended

### VideoScreenParams

*interface* · `packages/media/src/video.ts:4`

```ts
interface VideoScreenParams extends MediaPlayerParams
```

- `video?: HTMLVideoElement` — or a video element you made (a MediaStream, a canvas capture)
- `width?: number` — size of the screen in metres (default 4 x 2.25)
- `height?: number`

### WavInfo

*interface* · `packages/media/src/sound-file.ts:53`

```ts
interface WavInfo
```

- `encoding: WavEncoding`
- `bits: number` — bits per sample as stored
- `channels: number`
- `rate: number`
- `frames: number`
- `duration: number`
- `loops: LoopPoints[]` — the loops of the `smpl` chunk (sampler loops: what a sound editor writes for a sound that repeats)
- `cues: CuePoint[]` — the cue marks (`cue ` chunk) with the labels of an `adtl` list
- `note?: number` — the MIDI unity note of the `smpl` chunk, when there is one
- `dataOffset: number` — where the sample data is in the file
- `dataBytes: number`

### WavWriteOptions

*interface* · `packages/media/src/sound-file.ts:199`

```ts
interface WavWriteOptions
```

- `bits?: 8 | 16 | 24 | 32 | 'float32'` — `float32` is IEEE float; default 16
- `loops?: LoopPoints[]` — written as `smpl` loops (frames, `end` the first frame after the loop)
- `cues?: CuePoint[]` — written as `cue ` marks (with `adtl` labels)
- `dither?: boolean` — add triangular noise of one step before rounding to 8/16/24 bits (hides the rounding of quiet sounds); deterministic

### WebmTrack

*interface* · `packages/media/src/capture.ts:43`

```ts
interface WebmTrack
```

- `codec: 'V_VP9' | 'V_VP8' | 'V_AV1'` — Matroska codec id: V_VP9, V_VP8 or V_AV1
- `width: number`
- `height: number`
- `fps: number`
- `durationMs?: number` — the length of the video, ms (default: the last frame's time plus one frame)

### WorldTextParams

*interface* · `packages/media/src/text.ts:4`

```ts
interface WorldTextParams
```

- `text: string` — the text; `\n` starts a new line
- `size?: number` — height of a letter's em box in metres (Unreal's TextRender `WorldSize` / 100)
- `xScale?: number` — extra scale across and along the lines (Unreal's `XScale`, `YScale`)
- `yScale?: number`
- `color?: number[]` — colour as [r, g, b] or [r, g, b, a], 0..1
- `align?: 'left' | 'center' | 'right'` — how the text sits on the entity's origin, as a reader sees it
- `valign?: 'top' | 'center' | 'bottom'`
- `font?: string`
- `bold?: boolean`

## Types

### Bus

*type* · `packages/media/src/audio.ts:6`

```ts
type Bus = 'music' | 'sfx' | 'ambient' | 'voice'
```

### ChannelMode

*type* · `packages/media/src/mp3.ts:13`

```ts
type ChannelMode = 'stereo' | 'joint' | 'dual' | 'mono'
```

### CurvePoint

*type* · `packages/media/src/synth.ts:11`

```ts
type CurvePoint = [t: number, v: number, ramp?: Ramp]
```

a point of a curve: seconds from the sound's start, the value, how the value gets there from the point before (default 'lin')

### HlsLike

*type* · `packages/media/src/source.ts:2`

```ts
type HlsLike = { isSupported(): boolean; new (cfg?: unknown): { loadSource(u: string): void; attachMedia(v: HTMLMediaElement): void; destroy(): void; on(ev: string, fn: (...a: any[]) => void): void } }
```

### HtmlFetch

*type* · `packages/media/src/html.ts:5`

```ts
type HtmlFetch = (url: string) => Promise<{ ok: boolean; status: number; text(): Promise<string>; blob(): Promise<Blob> }>
```

What `HtmlScreen` needs from `fetch` (a Platform file API or a proxy fits): the page and its pieces come through it.

### MpegVersion

*type* · `packages/media/src/mp3.ts:12`

```ts
type MpegVersion = 1 | 2 | 2.5
```

### OscType

*type* · `packages/media/src/synth.ts:16`

```ts
type OscType = 'sine' | 'square' | 'sawtooth' | 'triangle'
```

### PlayerState

*type* · `packages/media/src/player.ts:6`

```ts
type PlayerState = 'idle' | 'loading' | 'playing' | 'paused' | 'ended' | 'error'
```

### Ramp

*type* · `packages/media/src/synth.ts:9`

```ts
type Ramp = 'lin' | 'exp' | 'set'
```

Synthesised sounds: a recipe of plain data instead of a recording. A recipe is a few layers (an oscillator or noise, an optional filter,
a gain envelope) mixed into an optional shared filter and envelope; every number of it is a constant, a curve over time (points with a
ramp to each) or a live parameter the game sets while the sound plays (an engine's throttle). The same recipes run in any game and can
be stored in a level, edited and written by an agent, since they are only data. `AudioService.define(name, { synth })` registers one;
it then plays like any other sound: buses, the world position, the unlock, the mute.

### RecorderState

*type* · `packages/media/src/recorder.ts:5`

```ts
type RecorderState = 'idle' | 'recording' | 'paused'
```

### Repeat

*type* · `packages/media/src/player.ts:7`

```ts
type Repeat = 'none' | 'one' | 'all'
```

### SoundFormat

*type* · `packages/media/src/sound-file.ts:17`

```ts
type SoundFormat = 'wav' | 'mp3'
```

### Value

*type* · `packages/media/src/synth.ts:14`

```ts
type Value = number | CurvePoint[] | ParamRef
```

### WavEncoding

*type* · `packages/media/src/sound-file.ts:51`

```ts
type WavEncoding = 'pcm' | 'float' | 'alaw' | 'mulaw'
```

## Constants

### FX_SYNTH_PRESETS

*const* · `packages/media/src/synth-presets-fx.ts:15`

```ts
const FX_SYNTH_PRESETS: Record<string, Preset>
```

### MP3_BITRATES_V1

*const* · `packages/media/src/mp3.ts:45`

```ts
const MP3_BITRATES_V1: number[]
```

the bitrates of MPEG-1 Layer III, kbit/s

### MP3_BITRATES_V2

*const* · `packages/media/src/mp3.ts:47`

```ts
const MP3_BITRATES_V2: number[]
```

the bitrates of MPEG-2 and 2.5 Layer III

### MP3_ENCODER_DELAY

*const* · `packages/media/src/mp3.ts:825`

```ts
const MP3_ENCODER_DELAY: 528
```

samples between the sound and its first output sample through the encoder's filter bank and transform (written to the header so a decoder can cut them)

### SOUND_MIME

*const* · `packages/media/src/sound-file.ts:42`

```ts
const SOUND_MIME: Record<SoundFormat, string>
```

### SYNTH_PRESETS

*const* · `packages/media/src/synth-presets.ts:13`

```ts
const SYNTH_PRESETS: Record<string, { recipe: SynthRecipe; volume?: number | undefined; }>
```

