@mica-js/widget 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/audio-guest.d.ts +168 -0
- package/dist/audio-guest.js +586 -0
- package/dist/audio-guest.js.map +1 -0
- package/dist/audio-host.d.ts +163 -0
- package/dist/audio-host.js +1 -0
- package/dist/engine-6tEkErMg.js +331 -0
- package/dist/engine-6tEkErMg.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -347
- package/dist/index.js.map +1 -1
- package/dist/protocol-BuE4pcSf.js +23 -0
- package/dist/protocol-BuE4pcSf.js.map +1 -0
- package/package.json +11 -3
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { RpcPort } from '@mica-js/core/rpc';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates the guest half of the audio path; one per sandbox runtime.
|
|
5
|
+
*
|
|
6
|
+
* The guest owns the public handles and their synchronous validation. Everything that makes sound
|
|
7
|
+
* happens in the worklet, reached over the port the host transfers in: this module only mirrors the
|
|
8
|
+
* worklet's answers, credits and terminal events.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createWidgetAudioRuntime(options: WidgetAudioRuntimeOptions): WidgetAudioRuntime;
|
|
11
|
+
|
|
12
|
+
/** Instance-level audio API behind `api.audio`. */
|
|
13
|
+
export declare interface WidgetAudioApi {
|
|
14
|
+
readonly state: WidgetAudioState;
|
|
15
|
+
/** Subscribes to availability changes; the returned function unsubscribes. */
|
|
16
|
+
onStateChange(listener: (state: WidgetAudioState) => void): () => void;
|
|
17
|
+
/** Starts a continuous oscillator. */
|
|
18
|
+
tone(options: WidgetAudioToneOptions): WidgetAudioTone;
|
|
19
|
+
/** Opens a PCM stream. */
|
|
20
|
+
createStream(options: WidgetAudioStreamOptions): WidgetAudioStream;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export declare interface WidgetAudioEnded {
|
|
24
|
+
readonly reason: WidgetAudioEndReason;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Why a tone or stream stopped. */
|
|
28
|
+
export declare type WidgetAudioEndReason = 'finished' | 'stopped' | 'dropped' | 'error' | 'unavailable' | 'disposed';
|
|
29
|
+
|
|
30
|
+
/** Sample layout of a PCM stream. */
|
|
31
|
+
export declare type WidgetAudioFormat = 's16' | 'f32';
|
|
32
|
+
|
|
33
|
+
/** Attached to `mount` so the factory can read `api.audio.state` synchronously. */
|
|
34
|
+
export declare interface WidgetAudioMountInfo {
|
|
35
|
+
readonly state: WidgetAudioState;
|
|
36
|
+
/** One-shot token this mount generation uses on the direct port. */
|
|
37
|
+
readonly session?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export declare interface WidgetAudioRampOptions {
|
|
41
|
+
/** Smoothing window in milliseconds, `0`–`10000`; `0` when omitted. */
|
|
42
|
+
readonly rampMs?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Guest-side audio state shared by every instance of one sandbox. */
|
|
46
|
+
export declare interface WidgetAudioRuntime {
|
|
47
|
+
readonly state: WidgetAudioState;
|
|
48
|
+
/** API for one instance; the same object is returned on every call. */
|
|
49
|
+
api(instanceId: string): WidgetAudioApi;
|
|
50
|
+
/** Applies the state and mount token the host sent with `mount`. */
|
|
51
|
+
open(instanceId: string, info: WidgetAudioMountInfo | undefined): void;
|
|
52
|
+
setState(state: WidgetAudioState): void;
|
|
53
|
+
/** Takes the direct worker ↔ worklet port; every audio call rides on its RPC endpoint. */
|
|
54
|
+
attachPort(port: RpcPort): void;
|
|
55
|
+
/** Instance teardown: every handle ends with `disposed`. */
|
|
56
|
+
release(instanceId: string): void;
|
|
57
|
+
dispose(): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare interface WidgetAudioRuntimeOptions {
|
|
61
|
+
/** Guest-side callback failures surface as `widget/deliver-failed`, like every other callback. */
|
|
62
|
+
readonly onListenerError?: (instanceId: string, error: unknown) => void;
|
|
63
|
+
readonly maxVoices?: number;
|
|
64
|
+
readonly maxStreams?: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Availability of the host audio path. */
|
|
68
|
+
export declare type WidgetAudioState = 'unavailable' | 'suspended' | 'running';
|
|
69
|
+
|
|
70
|
+
export declare interface WidgetAudioStopOptions {
|
|
71
|
+
/** Fade-out in milliseconds, `0`–`10000`; `0` when omitted. */
|
|
72
|
+
readonly fadeMs?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Handle for one PCM stream. Every method is safe to call after the stream closed. */
|
|
76
|
+
export declare interface WidgetAudioStream {
|
|
77
|
+
readonly id: string;
|
|
78
|
+
readonly sampleRate: number;
|
|
79
|
+
readonly channels: 1 | 2;
|
|
80
|
+
readonly format: WidgetAudioFormat;
|
|
81
|
+
readonly capacityFrames: number;
|
|
82
|
+
readonly startFrames: number;
|
|
83
|
+
readonly volume: number;
|
|
84
|
+
/** Accepted frames the worklet has not reported as consumed yet. */
|
|
85
|
+
readonly bufferedFrames: number;
|
|
86
|
+
readonly underruns: number;
|
|
87
|
+
readonly dropped: number;
|
|
88
|
+
readonly state: WidgetAudioStreamState;
|
|
89
|
+
/** Settles when the stream finished, was closed or its instance went away. */
|
|
90
|
+
readonly ended: Promise<WidgetAudioEnded>;
|
|
91
|
+
/**
|
|
92
|
+
* Appends one block of interleaved samples.
|
|
93
|
+
*
|
|
94
|
+
* The promise resolves with the worklet's answer: `true` when the whole block entered the ring
|
|
95
|
+
* buffer, `false` when it lacked room and the block was discarded whole. It never means "played".
|
|
96
|
+
* Shape errors are thrown synchronously, before anything is sent.
|
|
97
|
+
*/
|
|
98
|
+
write(samples: Int16Array | Float32Array): Promise<boolean>;
|
|
99
|
+
/** Drops everything that has not played yet and keeps the stream open. */
|
|
100
|
+
clear(): void;
|
|
101
|
+
/** Plays the remaining data and then ends with `finished`. Idempotent. */
|
|
102
|
+
end(): void;
|
|
103
|
+
/** Stops immediately and discards the remaining data. Idempotent. */
|
|
104
|
+
close(): void;
|
|
105
|
+
/** Fires once each time the buffer drains to half its capacity. */
|
|
106
|
+
onDrain(listener: () => void): () => void;
|
|
107
|
+
/** Fires when `state` or `underruns` changed. */
|
|
108
|
+
onStatus(listener: (status: WidgetAudioStreamStatus) => void): () => void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export declare interface WidgetAudioStreamOptions {
|
|
112
|
+
/** Integer source rate in Hz, `8000`–`96000`; required. */
|
|
113
|
+
readonly sampleRate: number;
|
|
114
|
+
/** `1` (default) or `2`; multi-channel samples are interleaved. */
|
|
115
|
+
readonly channels?: 1 | 2;
|
|
116
|
+
/** `f32` (default) or `s16`. */
|
|
117
|
+
readonly format?: WidgetAudioFormat;
|
|
118
|
+
/** Worklet buffer window in milliseconds, `50`–`2000`; `500` when omitted. */
|
|
119
|
+
readonly bufferMs?: number;
|
|
120
|
+
/** Frames that must be buffered before (re)starting playback, **not** more than `bufferMs`. */
|
|
121
|
+
readonly startMs?: number;
|
|
122
|
+
/** `0`–`1`; `1` when omitted. */
|
|
123
|
+
readonly volume?: number;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Playback state of a PCM stream, as the guest sees it. */
|
|
127
|
+
export declare type WidgetAudioStreamState = 'buffering' | 'playing' | 'ended' | 'closed';
|
|
128
|
+
|
|
129
|
+
/** Read-only snapshot of one stream. */
|
|
130
|
+
export declare interface WidgetAudioStreamStatus {
|
|
131
|
+
readonly state: WidgetAudioStreamState;
|
|
132
|
+
readonly underruns: number;
|
|
133
|
+
readonly dropped: number;
|
|
134
|
+
readonly bufferedFrames: number;
|
|
135
|
+
readonly capacityFrames: number;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Voice handle of a continuous oscillator, with live parameter updates. */
|
|
139
|
+
export declare interface WidgetAudioTone extends WidgetAudioVoice {
|
|
140
|
+
set(settings: WidgetAudioVoiceSettings, options?: WidgetAudioRampOptions): void;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export declare interface WidgetAudioToneOptions {
|
|
144
|
+
/** `sine` when omitted. */
|
|
145
|
+
readonly type?: WidgetOscillatorType;
|
|
146
|
+
/** Hz, `(0, 24000]`. */
|
|
147
|
+
readonly frequency: number;
|
|
148
|
+
/** `0`–`1`; `1` when omitted. */
|
|
149
|
+
readonly volume?: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Handle for one voice; closing it is idempotent. */
|
|
153
|
+
export declare interface WidgetAudioVoice {
|
|
154
|
+
readonly id: string;
|
|
155
|
+
/** Settles once the worklet (or the guest, when audio is unavailable) stopped the voice. */
|
|
156
|
+
readonly ended: Promise<WidgetAudioEnded>;
|
|
157
|
+
stop(options?: WidgetAudioStopOptions): void;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export declare interface WidgetAudioVoiceSettings {
|
|
161
|
+
readonly volume?: number;
|
|
162
|
+
readonly frequency?: number;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Waveforms the worklet synthesizes for `api.audio.tone()`. */
|
|
166
|
+
export declare type WidgetOscillatorType = 'sine' | 'square' | 'triangle' | 'sawtooth';
|
|
167
|
+
|
|
168
|
+
export { }
|