@lokutor/sdk 1.1.2 → 1.1.7

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/index.d.mts CHANGED
@@ -62,6 +62,40 @@ interface SynthesizeOptions {
62
62
  steps?: number;
63
63
  visemes?: boolean;
64
64
  }
65
+ /**
66
+ * Browser audio configuration options
67
+ */
68
+ interface BrowserAudioOptions {
69
+ inputSampleRate?: number;
70
+ outputSampleRate?: number;
71
+ autoGainControl?: boolean;
72
+ echoCancellation?: boolean;
73
+ noiseSuppression?: boolean;
74
+ analyserEnabled?: boolean;
75
+ onInputError?: (error: Error) => void;
76
+ }
77
+ /**
78
+ * Voice agent conversation options
79
+ */
80
+ interface VoiceAgentOptions {
81
+ prompt?: string;
82
+ voice?: VoiceStyle;
83
+ language?: Language;
84
+ serverUrl?: string;
85
+ visemes?: boolean;
86
+ onTranscription?: (text: string, isUser: boolean) => void;
87
+ onVisemes?: (visemes: Viseme[]) => void;
88
+ onStatusChange?: (status: string) => void;
89
+ onError?: (err: any) => void;
90
+ }
91
+ /**
92
+ * Viseme data for lip-sync animation
93
+ */
94
+ interface Viseme {
95
+ id: number;
96
+ char: string;
97
+ timestamp: number;
98
+ }
65
99
 
66
100
  /**
67
101
  * Main client for Lokutor Voice Agent SDK
@@ -77,14 +111,17 @@ declare class VoiceAgentClient {
77
111
  private onTranscription?;
78
112
  private onResponse?;
79
113
  private onAudioCallback?;
114
+ private onVisemesCallback?;
80
115
  private onStatus?;
81
116
  private onError?;
82
117
  private isConnected;
83
118
  private messages;
119
+ private visemeListeners;
84
120
  constructor(config: LokutorConfig & {
85
121
  prompt: string;
86
122
  voice?: VoiceStyle;
87
123
  language?: Language;
124
+ onVisemes?: (visemes: Viseme[]) => void;
88
125
  });
89
126
  /**
90
127
  * Connect to the Lokutor Voice Agent server
@@ -110,6 +147,7 @@ declare class VoiceAgentClient {
110
147
  private audioListeners;
111
148
  private emit;
112
149
  onAudio(callback: (data: Uint8Array) => void): void;
150
+ onVisemes(callback: (visemes: Viseme[]) => void): void;
113
151
  /**
114
152
  * Disconnect from the server
115
153
  */
@@ -171,4 +209,193 @@ declare function simpleTTS(options: SynthesizeOptions & {
171
209
  onAudio: (buf: Uint8Array) => void;
172
210
  }): Promise<void>;
173
211
 
174
- export { AUDIO_CONFIG, DEFAULT_URLS, Language, type LokutorConfig, type SynthesizeOptions, TTSClient, VoiceAgentClient, VoiceStyle, simpleConversation, simpleTTS };
212
+ /**
213
+ * Audio utility functions for format conversion, resampling, and PCM processing
214
+ */
215
+ /**
216
+ * Convert 16-bit PCM (Int16) to 32-bit Float
217
+ * @param int16Data Int16Array of PCM audio
218
+ * @returns Float32Array normalized to [-1, 1]
219
+ */
220
+ declare function pcm16ToFloat32(int16Data: Int16Array): Float32Array;
221
+ /**
222
+ * Convert 32-bit Float to 16-bit PCM (Int16)
223
+ * @param float32Data Float32Array normalized to [-1, 1]
224
+ * @returns Int16Array of PCM audio
225
+ */
226
+ declare function float32ToPcm16(float32Data: Float32Array): Int16Array;
227
+ /**
228
+ * Resample audio data from one sample rate to another using linear interpolation
229
+ * @param input Float32Array of input audio
230
+ * @param inputRate Original sample rate in Hz
231
+ * @param outputRate Target sample rate in Hz
232
+ * @returns Float32Array of resampled audio
233
+ */
234
+ declare function resample(input: Float32Array, inputRate: number, outputRate: number): Float32Array;
235
+ /**
236
+ * Apply a simple low-pass filter for anti-aliasing during downsampling
237
+ * @param data Float32Array of audio
238
+ * @param cutoffFreq Cutoff frequency in Hz
239
+ * @param sampleRate Sample rate in Hz
240
+ * @returns Filtered Float32Array
241
+ */
242
+ declare function applyLowPassFilter(data: Float32Array, cutoffFreq: number, sampleRate: number): Float32Array;
243
+ /**
244
+ * Resample audio with anti-aliasing low-pass filter
245
+ * Best used when downsampling to prevent aliasing artifacts
246
+ * @param input Float32Array of input audio
247
+ * @param inputRate Original sample rate in Hz
248
+ * @param outputRate Target sample rate in Hz
249
+ * @returns Float32Array of resampled and filtered audio
250
+ */
251
+ declare function resampleWithAntiAliasing(input: Float32Array, inputRate: number, outputRate: number): Float32Array;
252
+ /**
253
+ * Convert raw audio samples to Uint8Array (bytes)
254
+ * @param data Int16Array of PCM audio
255
+ * @returns Uint8Array containing PCM bytes
256
+ */
257
+ declare function pcm16ToBytes(data: Int16Array): Uint8Array;
258
+ /**
259
+ * Convert bytes to Int16Array
260
+ * @param bytes Uint8Array of PCM bytes
261
+ * @returns Int16Array of PCM audio
262
+ */
263
+ declare function bytesToPcm16(bytes: Uint8Array): Int16Array;
264
+ /**
265
+ * Normalize audio amplitude to prevent clipping
266
+ * @param data Float32Array of audio
267
+ * @param targetPeak Peak level to normalize to (0-1)
268
+ * @returns Normalized Float32Array
269
+ */
270
+ declare function normalizeAudio(data: Float32Array, targetPeak?: number): Float32Array;
271
+ /**
272
+ * Calculate RMS (Root Mean Square) amplitude
273
+ * @param data Float32Array or Uint8Array of audio
274
+ * @returns RMS value (0-1 for normalized float, 0-255 for byte data)
275
+ */
276
+ declare function calculateRMS(data: Float32Array | Uint8Array): number;
277
+ /**
278
+ * Create a resample function factory for streaming audio
279
+ * Useful for processing audio in chunks
280
+ */
281
+ declare class StreamResampler {
282
+ private inputBuffer;
283
+ private inputRate;
284
+ private outputRate;
285
+ constructor(inputRate: number, outputRate: number);
286
+ /**
287
+ * Process a chunk of audio and return resampled data
288
+ * @param inputChunk Float32Array chunk to process
289
+ * @param flush If true, output remaining buffered samples
290
+ * @returns Resampled Float32Array (may be empty if more data needed)
291
+ */
292
+ process(inputChunk: Float32Array, flush?: boolean): Float32Array;
293
+ reset(): void;
294
+ }
295
+
296
+ /**
297
+ * Configuration for browser audio handling
298
+ */
299
+ interface BrowserAudioConfig {
300
+ inputSampleRate?: number;
301
+ outputSampleRate?: number;
302
+ autoGainControl?: boolean;
303
+ echoCancellation?: boolean;
304
+ noiseSuppression?: boolean;
305
+ onInputError?: (error: Error) => void;
306
+ }
307
+ /**
308
+ * Analyser configuration for audio visualization
309
+ */
310
+ interface AnalyserConfig {
311
+ enabled?: boolean;
312
+ fftSize?: number;
313
+ }
314
+ /**
315
+ * Browser-based audio manager for Web Audio API operations
316
+ * Handles microphone input, speaker output, and visualization
317
+ */
318
+ declare class BrowserAudioManager {
319
+ private audioContext;
320
+ private mediaStreamAudioSourceNode;
321
+ private scriptProcessor;
322
+ private analyserNode;
323
+ private mediaStream;
324
+ private nextPlaybackTime;
325
+ private activeSources;
326
+ private playbackQueue;
327
+ private inputSampleRate;
328
+ private outputSampleRate;
329
+ private autoGainControl;
330
+ private echoCancellation;
331
+ private noiseSuppression;
332
+ private onAudioInput?;
333
+ private onInputError?;
334
+ private isMuted;
335
+ private isListening;
336
+ constructor(config?: BrowserAudioConfig);
337
+ /**
338
+ * Initialize the AudioContext and analyser
339
+ */
340
+ init(analyserConfig?: AnalyserConfig): Promise<void>;
341
+ /**
342
+ * Start capturing audio from the microphone
343
+ */
344
+ startMicrophone(onAudioInput: (pcm16Data: Uint8Array) => void): Promise<void>;
345
+ /**
346
+ * Internal method to process microphone audio data
347
+ */
348
+ private _processAudioInput;
349
+ /**
350
+ * Stop capturing microphone input
351
+ */
352
+ stopMicrophone(): void;
353
+ /**
354
+ * Play back audio received from the server
355
+ * @param pcm16Data Int16 PCM audio data at SPEAKER_SAMPLE_RATE
356
+ */
357
+ playAudio(pcm16Data: Uint8Array): void;
358
+ /**
359
+ * Internal method to schedule and play audio with sample-accurate timing
360
+ */
361
+ private _schedulePlayback;
362
+ /**
363
+ * Stop all currently playing audio and clear the queue
364
+ */
365
+ stopPlayback(): void;
366
+ /**
367
+ * Toggle mute state
368
+ */
369
+ setMuted(muted: boolean): void;
370
+ /**
371
+ * Get current mute state
372
+ */
373
+ isMicMuted(): boolean;
374
+ /**
375
+ * Get current amplitude from analyser (for visualization)
376
+ * Returns value between 0 and 1
377
+ */
378
+ getAmplitude(): number;
379
+ /**
380
+ * Get frequency data from analyser for visualization
381
+ */
382
+ getFrequencyData(): Uint8Array;
383
+ /**
384
+ * Get time-domain data from analyser for waveform visualization
385
+ */
386
+ getWaveformData(): Uint8Array;
387
+ /**
388
+ * Cleanup and close AudioContext
389
+ */
390
+ cleanup(): void;
391
+ /**
392
+ * Get current audio context state
393
+ */
394
+ getState(): 'running' | 'suspended' | 'closed' | 'interrupted' | null;
395
+ /**
396
+ * Check if microphone is currently listening
397
+ */
398
+ isRecording(): boolean;
399
+ }
400
+
401
+ export { AUDIO_CONFIG, type AnalyserConfig, type BrowserAudioConfig, BrowserAudioManager, type BrowserAudioOptions, DEFAULT_URLS, Language, type LokutorConfig, StreamResampler, type SynthesizeOptions, TTSClient, type Viseme, VoiceAgentClient, type VoiceAgentOptions, VoiceStyle, applyLowPassFilter, bytesToPcm16, calculateRMS, float32ToPcm16, normalizeAudio, pcm16ToBytes, pcm16ToFloat32, resample, resampleWithAntiAliasing, simpleConversation, simpleTTS };
package/dist/index.d.ts CHANGED
@@ -62,6 +62,40 @@ interface SynthesizeOptions {
62
62
  steps?: number;
63
63
  visemes?: boolean;
64
64
  }
65
+ /**
66
+ * Browser audio configuration options
67
+ */
68
+ interface BrowserAudioOptions {
69
+ inputSampleRate?: number;
70
+ outputSampleRate?: number;
71
+ autoGainControl?: boolean;
72
+ echoCancellation?: boolean;
73
+ noiseSuppression?: boolean;
74
+ analyserEnabled?: boolean;
75
+ onInputError?: (error: Error) => void;
76
+ }
77
+ /**
78
+ * Voice agent conversation options
79
+ */
80
+ interface VoiceAgentOptions {
81
+ prompt?: string;
82
+ voice?: VoiceStyle;
83
+ language?: Language;
84
+ serverUrl?: string;
85
+ visemes?: boolean;
86
+ onTranscription?: (text: string, isUser: boolean) => void;
87
+ onVisemes?: (visemes: Viseme[]) => void;
88
+ onStatusChange?: (status: string) => void;
89
+ onError?: (err: any) => void;
90
+ }
91
+ /**
92
+ * Viseme data for lip-sync animation
93
+ */
94
+ interface Viseme {
95
+ id: number;
96
+ char: string;
97
+ timestamp: number;
98
+ }
65
99
 
66
100
  /**
67
101
  * Main client for Lokutor Voice Agent SDK
@@ -77,14 +111,17 @@ declare class VoiceAgentClient {
77
111
  private onTranscription?;
78
112
  private onResponse?;
79
113
  private onAudioCallback?;
114
+ private onVisemesCallback?;
80
115
  private onStatus?;
81
116
  private onError?;
82
117
  private isConnected;
83
118
  private messages;
119
+ private visemeListeners;
84
120
  constructor(config: LokutorConfig & {
85
121
  prompt: string;
86
122
  voice?: VoiceStyle;
87
123
  language?: Language;
124
+ onVisemes?: (visemes: Viseme[]) => void;
88
125
  });
89
126
  /**
90
127
  * Connect to the Lokutor Voice Agent server
@@ -110,6 +147,7 @@ declare class VoiceAgentClient {
110
147
  private audioListeners;
111
148
  private emit;
112
149
  onAudio(callback: (data: Uint8Array) => void): void;
150
+ onVisemes(callback: (visemes: Viseme[]) => void): void;
113
151
  /**
114
152
  * Disconnect from the server
115
153
  */
@@ -171,4 +209,193 @@ declare function simpleTTS(options: SynthesizeOptions & {
171
209
  onAudio: (buf: Uint8Array) => void;
172
210
  }): Promise<void>;
173
211
 
174
- export { AUDIO_CONFIG, DEFAULT_URLS, Language, type LokutorConfig, type SynthesizeOptions, TTSClient, VoiceAgentClient, VoiceStyle, simpleConversation, simpleTTS };
212
+ /**
213
+ * Audio utility functions for format conversion, resampling, and PCM processing
214
+ */
215
+ /**
216
+ * Convert 16-bit PCM (Int16) to 32-bit Float
217
+ * @param int16Data Int16Array of PCM audio
218
+ * @returns Float32Array normalized to [-1, 1]
219
+ */
220
+ declare function pcm16ToFloat32(int16Data: Int16Array): Float32Array;
221
+ /**
222
+ * Convert 32-bit Float to 16-bit PCM (Int16)
223
+ * @param float32Data Float32Array normalized to [-1, 1]
224
+ * @returns Int16Array of PCM audio
225
+ */
226
+ declare function float32ToPcm16(float32Data: Float32Array): Int16Array;
227
+ /**
228
+ * Resample audio data from one sample rate to another using linear interpolation
229
+ * @param input Float32Array of input audio
230
+ * @param inputRate Original sample rate in Hz
231
+ * @param outputRate Target sample rate in Hz
232
+ * @returns Float32Array of resampled audio
233
+ */
234
+ declare function resample(input: Float32Array, inputRate: number, outputRate: number): Float32Array;
235
+ /**
236
+ * Apply a simple low-pass filter for anti-aliasing during downsampling
237
+ * @param data Float32Array of audio
238
+ * @param cutoffFreq Cutoff frequency in Hz
239
+ * @param sampleRate Sample rate in Hz
240
+ * @returns Filtered Float32Array
241
+ */
242
+ declare function applyLowPassFilter(data: Float32Array, cutoffFreq: number, sampleRate: number): Float32Array;
243
+ /**
244
+ * Resample audio with anti-aliasing low-pass filter
245
+ * Best used when downsampling to prevent aliasing artifacts
246
+ * @param input Float32Array of input audio
247
+ * @param inputRate Original sample rate in Hz
248
+ * @param outputRate Target sample rate in Hz
249
+ * @returns Float32Array of resampled and filtered audio
250
+ */
251
+ declare function resampleWithAntiAliasing(input: Float32Array, inputRate: number, outputRate: number): Float32Array;
252
+ /**
253
+ * Convert raw audio samples to Uint8Array (bytes)
254
+ * @param data Int16Array of PCM audio
255
+ * @returns Uint8Array containing PCM bytes
256
+ */
257
+ declare function pcm16ToBytes(data: Int16Array): Uint8Array;
258
+ /**
259
+ * Convert bytes to Int16Array
260
+ * @param bytes Uint8Array of PCM bytes
261
+ * @returns Int16Array of PCM audio
262
+ */
263
+ declare function bytesToPcm16(bytes: Uint8Array): Int16Array;
264
+ /**
265
+ * Normalize audio amplitude to prevent clipping
266
+ * @param data Float32Array of audio
267
+ * @param targetPeak Peak level to normalize to (0-1)
268
+ * @returns Normalized Float32Array
269
+ */
270
+ declare function normalizeAudio(data: Float32Array, targetPeak?: number): Float32Array;
271
+ /**
272
+ * Calculate RMS (Root Mean Square) amplitude
273
+ * @param data Float32Array or Uint8Array of audio
274
+ * @returns RMS value (0-1 for normalized float, 0-255 for byte data)
275
+ */
276
+ declare function calculateRMS(data: Float32Array | Uint8Array): number;
277
+ /**
278
+ * Create a resample function factory for streaming audio
279
+ * Useful for processing audio in chunks
280
+ */
281
+ declare class StreamResampler {
282
+ private inputBuffer;
283
+ private inputRate;
284
+ private outputRate;
285
+ constructor(inputRate: number, outputRate: number);
286
+ /**
287
+ * Process a chunk of audio and return resampled data
288
+ * @param inputChunk Float32Array chunk to process
289
+ * @param flush If true, output remaining buffered samples
290
+ * @returns Resampled Float32Array (may be empty if more data needed)
291
+ */
292
+ process(inputChunk: Float32Array, flush?: boolean): Float32Array;
293
+ reset(): void;
294
+ }
295
+
296
+ /**
297
+ * Configuration for browser audio handling
298
+ */
299
+ interface BrowserAudioConfig {
300
+ inputSampleRate?: number;
301
+ outputSampleRate?: number;
302
+ autoGainControl?: boolean;
303
+ echoCancellation?: boolean;
304
+ noiseSuppression?: boolean;
305
+ onInputError?: (error: Error) => void;
306
+ }
307
+ /**
308
+ * Analyser configuration for audio visualization
309
+ */
310
+ interface AnalyserConfig {
311
+ enabled?: boolean;
312
+ fftSize?: number;
313
+ }
314
+ /**
315
+ * Browser-based audio manager for Web Audio API operations
316
+ * Handles microphone input, speaker output, and visualization
317
+ */
318
+ declare class BrowserAudioManager {
319
+ private audioContext;
320
+ private mediaStreamAudioSourceNode;
321
+ private scriptProcessor;
322
+ private analyserNode;
323
+ private mediaStream;
324
+ private nextPlaybackTime;
325
+ private activeSources;
326
+ private playbackQueue;
327
+ private inputSampleRate;
328
+ private outputSampleRate;
329
+ private autoGainControl;
330
+ private echoCancellation;
331
+ private noiseSuppression;
332
+ private onAudioInput?;
333
+ private onInputError?;
334
+ private isMuted;
335
+ private isListening;
336
+ constructor(config?: BrowserAudioConfig);
337
+ /**
338
+ * Initialize the AudioContext and analyser
339
+ */
340
+ init(analyserConfig?: AnalyserConfig): Promise<void>;
341
+ /**
342
+ * Start capturing audio from the microphone
343
+ */
344
+ startMicrophone(onAudioInput: (pcm16Data: Uint8Array) => void): Promise<void>;
345
+ /**
346
+ * Internal method to process microphone audio data
347
+ */
348
+ private _processAudioInput;
349
+ /**
350
+ * Stop capturing microphone input
351
+ */
352
+ stopMicrophone(): void;
353
+ /**
354
+ * Play back audio received from the server
355
+ * @param pcm16Data Int16 PCM audio data at SPEAKER_SAMPLE_RATE
356
+ */
357
+ playAudio(pcm16Data: Uint8Array): void;
358
+ /**
359
+ * Internal method to schedule and play audio with sample-accurate timing
360
+ */
361
+ private _schedulePlayback;
362
+ /**
363
+ * Stop all currently playing audio and clear the queue
364
+ */
365
+ stopPlayback(): void;
366
+ /**
367
+ * Toggle mute state
368
+ */
369
+ setMuted(muted: boolean): void;
370
+ /**
371
+ * Get current mute state
372
+ */
373
+ isMicMuted(): boolean;
374
+ /**
375
+ * Get current amplitude from analyser (for visualization)
376
+ * Returns value between 0 and 1
377
+ */
378
+ getAmplitude(): number;
379
+ /**
380
+ * Get frequency data from analyser for visualization
381
+ */
382
+ getFrequencyData(): Uint8Array;
383
+ /**
384
+ * Get time-domain data from analyser for waveform visualization
385
+ */
386
+ getWaveformData(): Uint8Array;
387
+ /**
388
+ * Cleanup and close AudioContext
389
+ */
390
+ cleanup(): void;
391
+ /**
392
+ * Get current audio context state
393
+ */
394
+ getState(): 'running' | 'suspended' | 'closed' | 'interrupted' | null;
395
+ /**
396
+ * Check if microphone is currently listening
397
+ */
398
+ isRecording(): boolean;
399
+ }
400
+
401
+ export { AUDIO_CONFIG, type AnalyserConfig, type BrowserAudioConfig, BrowserAudioManager, type BrowserAudioOptions, DEFAULT_URLS, Language, type LokutorConfig, StreamResampler, type SynthesizeOptions, TTSClient, type Viseme, VoiceAgentClient, type VoiceAgentOptions, VoiceStyle, applyLowPassFilter, bytesToPcm16, calculateRMS, float32ToPcm16, normalizeAudio, pcm16ToBytes, pcm16ToFloat32, resample, resampleWithAntiAliasing, simpleConversation, simpleTTS };