@sorisdk/web-audio 0.1.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.
@@ -0,0 +1,241 @@
1
+ import { MatcherEphemeralAuthOptions, InitMatcherOptions, MatcherEphemeralAuthResult, PackSource, MatchResult, AudioFingerprintType } from '@sorisdk/matcher';
2
+ export { AudioFingerprintType, PackSource } from '@sorisdk/matcher';
3
+ import { InitAfpgenOptions } from '@sorisdk/afpgen';
4
+
5
+ declare const DEFAULT_BROWSER_AUDIOPACK_VERSION = "20220401000000";
6
+ interface BrowserAudioPackState {
7
+ version: string;
8
+ packUrl: string;
9
+ }
10
+ interface BrowserAudioPackStateStore {
11
+ load(): BrowserAudioPackState | null | Promise<BrowserAudioPackState | null>;
12
+ save(state: BrowserAudioPackState): void | Promise<void>;
13
+ clear?(): void | Promise<void>;
14
+ }
15
+ interface BrowserAudioPackLoader {
16
+ loadPack(source: string | URL): Promise<void>;
17
+ }
18
+ interface CreateLocalStorageAudioPackStoreOptions {
19
+ key: string;
20
+ storage?: Storage;
21
+ }
22
+ interface AuthenticateAndLoadAudioPackOptions {
23
+ loader: BrowserAudioPackLoader;
24
+ auth: Omit<MatcherEphemeralAuthOptions, "version">;
25
+ store: BrowserAudioPackStateStore;
26
+ initialVersion?: string;
27
+ matcherInit?: Omit<InitMatcherOptions, "auth">;
28
+ }
29
+ interface AuthenticateAndLoadAudioPackResult extends MatcherEphemeralAuthResult {
30
+ requestedVersion: string;
31
+ loadedPackUrl: string;
32
+ persistedVersion: string;
33
+ source: "cache" | "network";
34
+ cachedState: BrowserAudioPackState | null;
35
+ }
36
+ declare function createLocalStorageAudioPackStore(options: CreateLocalStorageAudioPackStoreOptions): BrowserAudioPackStateStore;
37
+ declare function authenticateAndLoadAudioPack(options: AuthenticateAndLoadAudioPackOptions): Promise<AuthenticateAndLoadAudioPackResult>;
38
+
39
+ interface BrowserSessionManager {
40
+ getSessionId(): string;
41
+ clear?(): void | Promise<void>;
42
+ }
43
+ interface CreateLocalStorageSessionManagerOptions {
44
+ key: string;
45
+ storage?: Storage;
46
+ generateSessionId?: () => string;
47
+ }
48
+ declare function createLocalStorageSessionManager(options: CreateLocalStorageSessionManagerOptions): BrowserSessionManager;
49
+
50
+ interface InitMatchWindowWasmOptions {
51
+ loader?: () => Promise<unknown>;
52
+ wasmInitInput?: unknown;
53
+ }
54
+
55
+ type DetectedMatch = Pick<MatchResult, "afpType" | "name" | "position" | "score">;
56
+ interface MaterialActivityRequestPayload {
57
+ type: "material";
58
+ material_id: string;
59
+ metadata?: unknown;
60
+ }
61
+ interface MicrophoneMatcherCampaignEvent {
62
+ match: DetectedMatch;
63
+ campaign: Record<string, unknown>;
64
+ activityId: string | null;
65
+ raw: Record<string, unknown>;
66
+ }
67
+ interface MicrophoneMatcherActivityReporterOptions {
68
+ endpoint: string | URL;
69
+ token: string | (() => string | Promise<string>);
70
+ fetch?: typeof fetch;
71
+ headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
72
+ mapMatchToRequest?: (match: DetectedMatch) => MaterialActivityRequestPayload | null | Promise<MaterialActivityRequestPayload | null>;
73
+ mapResponseToCampaign?: (response: Record<string, unknown>, match: DetectedMatch) => MicrophoneMatcherCampaignEvent | null | Promise<MicrophoneMatcherCampaignEvent | null>;
74
+ }
75
+ interface CreateMicrophoneMatcherWasmOptions {
76
+ afpgen?: InitAfpgenOptions;
77
+ matcher?: Omit<InitMatcherOptions, "auth">;
78
+ matchWindow?: InitMatchWindowWasmOptions;
79
+ }
80
+ interface CreateMicrophoneMatcherOptions {
81
+ packSource?: PackSource;
82
+ mediaConstraints?: MediaStreamConstraints;
83
+ stopTracksOnStop?: boolean;
84
+ suspendContextOnStop?: boolean;
85
+ activityReporter?: MicrophoneMatcherActivityReporterOptions;
86
+ wasm?: CreateMicrophoneMatcherWasmOptions;
87
+ }
88
+ interface MicrophoneMatcherEventMap extends Record<string, unknown> {
89
+ ready: {
90
+ sampleRate: number;
91
+ };
92
+ microphoneready: {
93
+ stream: MediaStream;
94
+ trackCount: number;
95
+ };
96
+ start: {};
97
+ match: {
98
+ best: DetectedMatch;
99
+ };
100
+ campaign: MicrophoneMatcherCampaignEvent;
101
+ nomatch: {};
102
+ stop: {};
103
+ destroy: {};
104
+ error: {
105
+ phase: string;
106
+ error: Error;
107
+ };
108
+ }
109
+ type MicrophoneMatcherEventName = keyof MicrophoneMatcherEventMap;
110
+ type MicrophoneMatcherListener<T extends MicrophoneMatcherEventName> = (payload: MicrophoneMatcherEventMap[T]) => void;
111
+
112
+ type Awaitable<T> = T | Promise<T>;
113
+ type AudioRecognizerMatcherOptions = Omit<CreateMicrophoneMatcherOptions, "activityReporter" | "packSource">;
114
+ declare const DEFAULT_SORI_API_ENDPOINT = "https://console.soriapi.com/api";
115
+ interface AudioRecognizerAuthOptions {
116
+ endpoint: string | URL;
117
+ appId: string;
118
+ fetch?: typeof fetch;
119
+ ephemeralKey?: string | (() => Awaitable<string>);
120
+ ephemeralKeyEndpoint?: string | URL;
121
+ ephemeralKeyFetch?: typeof fetch;
122
+ ephemeralKeyRequestInit?: RequestInit | (() => Awaitable<RequestInit | undefined>);
123
+ }
124
+ interface AudioRecognizerOptions extends AudioRecognizerMatcherOptions {
125
+ appId?: string;
126
+ apiEndpoint?: string | URL;
127
+ authEndpoint?: string | URL;
128
+ activityEndpoint?: string | URL;
129
+ fetch?: typeof fetch;
130
+ ephemeralKey?: string | (() => Awaitable<string>);
131
+ ephemeraKey?: string | (() => Awaitable<string>);
132
+ ephemeralKeyEndpoint?: string | URL;
133
+ ephemeralKeyFetch?: typeof fetch;
134
+ ephemeralKeyRequestInit?: RequestInit | (() => Awaitable<RequestInit | undefined>);
135
+ auth?: AudioRecognizerAuthOptions;
136
+ activityReporter?: false | (Omit<MicrophoneMatcherActivityReporterOptions, "endpoint" | "token"> & {
137
+ endpoint?: string | URL;
138
+ });
139
+ initialVersion?: string;
140
+ packStore?: BrowserAudioPackStateStore;
141
+ packStorageKey?: string;
142
+ sessionManager?: BrowserSessionManager;
143
+ sessionStorageKey?: string;
144
+ }
145
+ declare class AudioRecognizer {
146
+ private readonly events;
147
+ private readonly options;
148
+ private readonly packStore;
149
+ private readonly sessionManager;
150
+ private matcher;
151
+ private matcherPromise;
152
+ private activityToken;
153
+ private running;
154
+ private destroyed;
155
+ constructor(options: AudioRecognizerOptions);
156
+ on<T extends MicrophoneMatcherEventName>(eventName: T, listener: MicrophoneMatcherListener<T>): this;
157
+ off<T extends MicrophoneMatcherEventName>(eventName: T, listener: MicrophoneMatcherListener<T>): this;
158
+ once<T extends MicrophoneMatcherEventName>(eventName: T, listener: MicrophoneMatcherListener<T>): this;
159
+ start(): Promise<void>;
160
+ stop(): Promise<void>;
161
+ destroy(): Promise<void>;
162
+ getDiagnostics(): Readonly<{
163
+ sampleCallbacks: number;
164
+ inputSamples: number;
165
+ normalizedSamples: number;
166
+ processedChunks: number;
167
+ fingerprints: number;
168
+ readyQueries: number;
169
+ matchRequests: number;
170
+ }> | null;
171
+ private ensureMatcher;
172
+ private bindMatcherEvents;
173
+ private ensureNotDestroyed;
174
+ private setAuthResult;
175
+ }
176
+
177
+ declare class MicrophoneMatcher {
178
+ private readonly events;
179
+ private readonly matcherSession;
180
+ private readonly mediaDevices;
181
+ private readonly audioContext;
182
+ private readonly stopTracksOnStop;
183
+ private readonly suspendContextOnStop;
184
+ private readonly audioGraphFactory;
185
+ private readonly packSource;
186
+ private readonly options;
187
+ private extractor;
188
+ private graphController;
189
+ private matchWindow;
190
+ private stream;
191
+ private running;
192
+ private destroyed;
193
+ private loadedPack;
194
+ private matchInFlight;
195
+ private pendingMatch;
196
+ private captureGeneration;
197
+ private pcmBuffer;
198
+ private resampleBuffer;
199
+ private resampleOffset;
200
+ private hopSize;
201
+ private diagnostics;
202
+ constructor(options: CreateMicrophoneMatcherOptions);
203
+ on<T extends MicrophoneMatcherEventName>(eventName: T, listener: MicrophoneMatcherListener<T>): this;
204
+ off<T extends MicrophoneMatcherEventName>(eventName: T, listener: MicrophoneMatcherListener<T>): this;
205
+ once<T extends MicrophoneMatcherEventName>(eventName: T, listener: MicrophoneMatcherListener<T>): this;
206
+ prepare(): Promise<void>;
207
+ loadPack(): Promise<void>;
208
+ loadPack(source: NonNullable<CreateMicrophoneMatcherOptions["packSource"]>): Promise<void>;
209
+ private prepareInternal;
210
+ addEntry(name: string, fingerprint: Uint8Array, afpType?: AudioFingerprintType): Promise<void>;
211
+ addOrReplace(name: string, fingerprint: Uint8Array, afpType?: AudioFingerprintType): Promise<void>;
212
+ clear(): Promise<void>;
213
+ start(): Promise<void>;
214
+ stop(): Promise<void>;
215
+ destroy(): Promise<void>;
216
+ getDiagnostics(): Readonly<{
217
+ sampleCallbacks: number;
218
+ inputSamples: number;
219
+ normalizedSamples: number;
220
+ processedChunks: number;
221
+ fingerprints: number;
222
+ readyQueries: number;
223
+ matchRequests: number;
224
+ }>;
225
+ private handleAudioSamples;
226
+ private queueMatch;
227
+ private runMatchLoop;
228
+ private resetCaptureState;
229
+ private normalizeInputSamples;
230
+ private resampleToTargetRate;
231
+ private cleanupAfterStartFailure;
232
+ private resetDiagnostics;
233
+ private ensureNotDestroyed;
234
+ private matchWindowOrThrow;
235
+ private reportCampaign;
236
+ }
237
+ declare function createMicrophoneMatcher(options?: CreateMicrophoneMatcherOptions): Promise<MicrophoneMatcher>;
238
+
239
+ declare function requestMicrophoneStream(mediaDevices: MediaDevices, mediaConstraints?: MediaStreamConstraints): Promise<MediaStream>;
240
+
241
+ export { AudioRecognizer, type AudioRecognizerAuthOptions, type AudioRecognizerOptions, type AuthenticateAndLoadAudioPackOptions, type AuthenticateAndLoadAudioPackResult, type BrowserAudioPackLoader, type BrowserAudioPackState, type BrowserAudioPackStateStore, type BrowserSessionManager, type CreateLocalStorageAudioPackStoreOptions, type CreateLocalStorageSessionManagerOptions, type CreateMicrophoneMatcherOptions, type CreateMicrophoneMatcherWasmOptions, DEFAULT_BROWSER_AUDIOPACK_VERSION, DEFAULT_SORI_API_ENDPOINT, type DetectedMatch, type MaterialActivityRequestPayload, MicrophoneMatcher, type MicrophoneMatcherActivityReporterOptions, type MicrophoneMatcherCampaignEvent, type MicrophoneMatcherEventMap, type MicrophoneMatcherEventName, type MicrophoneMatcherListener, authenticateAndLoadAudioPack, createLocalStorageAudioPackStore, createLocalStorageSessionManager, createMicrophoneMatcher, requestMicrophoneStream };