@micdrop/client 1.0.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/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # @micdrop/client
2
+
3
+ A browser library for handling real-time voice conversations with microphone and speaker management.
4
+
5
+ For server implementation, see [@micdrop/server](../server/README.md) package.
6
+
7
+ ## Features
8
+
9
+ - 🎤 Real-time microphone recording with voice activity detection (VAD)
10
+ - 🔊 Audio playback management
11
+ - 🌐 WebSocket-based audio streaming
12
+ - 📊 Audio analysis and volume monitoring
13
+ - 🎛️ Configurable speech detection settings
14
+ - ⚡ Event-based architecture
15
+ - 🔇 Mute/unmute functionality
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install @micdrop/client
21
+ # or
22
+ yarn add @micdrop/client
23
+ # or
24
+ pnpm add @micdrop/client
25
+ ```
26
+
27
+ ## Quick Start
28
+
29
+ ```typescript
30
+ import { CallHandler } from '@micdrop/client'
31
+
32
+ // Create a new call handler instance
33
+ const call = new CallHandler()
34
+
35
+ // Listen for state changes
36
+ call.on('StateChange', () => {
37
+ console.log('Call state:', call)
38
+ /*
39
+ {
40
+ "conversation": [
41
+ { "role": "assistant", "content": "Hello!" },
42
+ { "role": "user", "content": "User Message 1" },
43
+ { "role": "assistant", "content": "Assistant Message 1" }
44
+ ],
45
+ "isMicStarted": false,
46
+ "isStarted": false,
47
+ "isStarting": false,
48
+ "isWSStarted": false,
49
+ "isWSStarting": false
50
+ }
51
+ */
52
+
53
+ console.log('MicRecorder state:', call.micRecorder.state)
54
+ /*
55
+ {
56
+ "isStarting": false,
57
+ "isStarted": false,
58
+ "isMuted": false,
59
+ "isSpeaking": false,
60
+ "threshold": -50
61
+ }
62
+ */
63
+ })
64
+
65
+ // Start the call
66
+ await call.start()
67
+
68
+ // Pause/resume microphone
69
+ call.pause()
70
+ call.resume()
71
+
72
+ // Listen for end of interview
73
+ call.on('EndInterview', () => {
74
+ console.log('Interview ended')
75
+ })
76
+
77
+ // Listen for errors
78
+ call.on('Error', (error) => {
79
+ console.error('Error occurred:', error)
80
+ })
81
+
82
+ // Stop the call when done
83
+ await call.stop()
84
+ ```
85
+
86
+ ## Documentation
87
+
88
+ - **[CallHandler](./docs/CallHandler.md)** - Manages WebSocket connections, audio streaming, and conversation state
89
+
90
+ - **[MicRecorder](./docs/MicRecorder.md)** - Records audio from microphone with voice activity detection and speech events
91
+
92
+ - **[Mic](./docs/Mic.md)** - Manages microphone input devices and audio recording with real-time analysis
93
+
94
+ - **[Speaker](./docs/Speaker.md)** - Handles audio output devices and playback with analysis capabilities
95
+
96
+ ## Browser Support
97
+
98
+ Requires browsers with support for:
99
+
100
+ - WebSocket API
101
+ - MediaRecorder API
102
+ - Web Audio API
103
+ - getUserMedia API
104
+
105
+ ## License
106
+
107
+ MIT
108
+
109
+ ## Author
110
+
111
+ [Lonestone](https://www.lonestone.io) ([GitHub](https://github.com/lonestone))
@@ -0,0 +1,135 @@
1
+ export * from '@micdrop/server/src/types';
2
+ import EventEmitter from 'eventemitter3';
3
+ import { Conversation } from '@micdrop/server';
4
+
5
+ declare enum LocalStorageKeys {
6
+ MicDevice = "micdrop.micDevice",
7
+ SpeakerDevice = "micdrop.speakerDevice",
8
+ MicThreshold = "micdrop.micThreshold"
9
+ }
10
+
11
+ interface MicRecorderState {
12
+ isStarting: boolean;
13
+ isStarted: boolean;
14
+ isMuted: boolean;
15
+ isSpeaking: boolean;
16
+ threshold: number;
17
+ }
18
+ interface MicRecorderEvents {
19
+ Chunk: [Blob];
20
+ StartSpeaking: void;
21
+ StopSpeaking: void;
22
+ StateChange: [MicRecorderState];
23
+ }
24
+ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
25
+ state: MicRecorderState;
26
+ private audioInfo;
27
+ private hark;
28
+ private recorder;
29
+ private delayedStream;
30
+ constructor();
31
+ start(stream: MediaStream): Promise<void>;
32
+ mute(): void;
33
+ unmute(): void;
34
+ stop(): void;
35
+ setThreshold(threshold: number): void;
36
+ private startSpeakingDetection;
37
+ private stopSpeakingDetection;
38
+ private onStartSpeaking;
39
+ private onStopSpeaking;
40
+ private onDataAvailable;
41
+ private changeState;
42
+ private getAudioInfo;
43
+ }
44
+
45
+ interface AudioAnalyserEvents {
46
+ volume: [number];
47
+ }
48
+ declare class AudioAnalyser extends EventEmitter<AudioAnalyserEvents> {
49
+ volumeInterval: number;
50
+ node: AnalyserNode;
51
+ volume: number;
52
+ private fftSize;
53
+ private fftBins;
54
+ private interval;
55
+ constructor(audioContext: AudioContext, volumeInterval?: number);
56
+ stop(): void;
57
+ private getMaxVolume;
58
+ }
59
+
60
+ declare let micAnalyser: AudioAnalyser | undefined;
61
+ declare const defaultMicThreshold = -50;
62
+ declare function getMicConstraints(deviceId?: string): MediaStreamConstraints;
63
+ declare function startMicrophone(deviceId?: string): Promise<MediaStream>;
64
+ declare function stopMicrophone(): void;
65
+ declare function createDelayedStream(stream: MediaStream): MediaStream;
66
+
67
+ declare const microphone_createDelayedStream: typeof createDelayedStream;
68
+ declare const microphone_defaultMicThreshold: typeof defaultMicThreshold;
69
+ declare const microphone_getMicConstraints: typeof getMicConstraints;
70
+ declare const microphone_micAnalyser: typeof micAnalyser;
71
+ declare const microphone_startMicrophone: typeof startMicrophone;
72
+ declare const microphone_stopMicrophone: typeof stopMicrophone;
73
+ declare namespace microphone {
74
+ export { microphone_createDelayedStream as createDelayedStream, microphone_defaultMicThreshold as defaultMicThreshold, microphone_getMicConstraints as getMicConstraints, microphone_micAnalyser as micAnalyser, microphone_startMicrophone as startMicrophone, microphone_stopMicrophone as stopMicrophone };
75
+ }
76
+
77
+ declare let speakerAnalyser: AudioAnalyser | undefined;
78
+ declare function canChangeSpeakerDevice(): boolean;
79
+ declare function changeSpeakerDevice(speakerId: string): Promise<void>;
80
+ declare function playAudioBlob(blob: Blob): Promise<void>;
81
+ declare function stopAudioBlob(): void;
82
+
83
+ declare const speaker_canChangeSpeakerDevice: typeof canChangeSpeakerDevice;
84
+ declare const speaker_changeSpeakerDevice: typeof changeSpeakerDevice;
85
+ declare const speaker_playAudioBlob: typeof playAudioBlob;
86
+ declare const speaker_speakerAnalyser: typeof speakerAnalyser;
87
+ declare const speaker_stopAudioBlob: typeof stopAudioBlob;
88
+ declare namespace speaker {
89
+ export { speaker_canChangeSpeakerDevice as canChangeSpeakerDevice, speaker_changeSpeakerDevice as changeSpeakerDevice, speaker_playAudioBlob as playAudioBlob, speaker_speakerAnalyser as speakerAnalyser, speaker_stopAudioBlob as stopAudioBlob };
90
+ }
91
+
92
+ declare enum CallHandlerErrorCode {
93
+ Mic = "Mic",
94
+ Unauthorized = "Unauthorized",
95
+ Error = "Error"
96
+ }
97
+ declare class CallHandlerError extends Error {
98
+ code: CallHandlerErrorCode;
99
+ constructor(code?: CallHandlerErrorCode);
100
+ }
101
+
102
+ interface CallHandlerEvents {
103
+ EndInterview: [];
104
+ Error: [CallHandlerError];
105
+ StateChange: void;
106
+ }
107
+ declare class CallHandler<Params extends {}> extends EventEmitter<CallHandlerEvents> {
108
+ url?: string;
109
+ params?: Params;
110
+ micRecorder: MicRecorder;
111
+ conversation: Conversation;
112
+ private ws?;
113
+ private micStream?;
114
+ private startTime;
115
+ private lastAudioBlob?;
116
+ constructor();
117
+ get isStarted(): boolean;
118
+ get isStarting(): boolean;
119
+ get isWSStarted(): boolean;
120
+ get isWSStarting(): boolean;
121
+ get isMicStarted(): boolean;
122
+ start(): Promise<void>;
123
+ stop(): Promise<void>;
124
+ pause(): void;
125
+ resume(): void;
126
+ startMic(deviceId?: string, record?: boolean): Promise<void>;
127
+ private stopMic;
128
+ private startWS;
129
+ private stopWS;
130
+ private addMessage;
131
+ private notifyStateChange;
132
+ private debug;
133
+ }
134
+
135
+ export { CallHandler, CallHandlerError, CallHandlerErrorCode, type CallHandlerEvents, LocalStorageKeys, microphone as Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, speaker as Speaker };
@@ -0,0 +1,135 @@
1
+ export * from '@micdrop/server/src/types';
2
+ import EventEmitter from 'eventemitter3';
3
+ import { Conversation } from '@micdrop/server';
4
+
5
+ declare enum LocalStorageKeys {
6
+ MicDevice = "micdrop.micDevice",
7
+ SpeakerDevice = "micdrop.speakerDevice",
8
+ MicThreshold = "micdrop.micThreshold"
9
+ }
10
+
11
+ interface MicRecorderState {
12
+ isStarting: boolean;
13
+ isStarted: boolean;
14
+ isMuted: boolean;
15
+ isSpeaking: boolean;
16
+ threshold: number;
17
+ }
18
+ interface MicRecorderEvents {
19
+ Chunk: [Blob];
20
+ StartSpeaking: void;
21
+ StopSpeaking: void;
22
+ StateChange: [MicRecorderState];
23
+ }
24
+ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
25
+ state: MicRecorderState;
26
+ private audioInfo;
27
+ private hark;
28
+ private recorder;
29
+ private delayedStream;
30
+ constructor();
31
+ start(stream: MediaStream): Promise<void>;
32
+ mute(): void;
33
+ unmute(): void;
34
+ stop(): void;
35
+ setThreshold(threshold: number): void;
36
+ private startSpeakingDetection;
37
+ private stopSpeakingDetection;
38
+ private onStartSpeaking;
39
+ private onStopSpeaking;
40
+ private onDataAvailable;
41
+ private changeState;
42
+ private getAudioInfo;
43
+ }
44
+
45
+ interface AudioAnalyserEvents {
46
+ volume: [number];
47
+ }
48
+ declare class AudioAnalyser extends EventEmitter<AudioAnalyserEvents> {
49
+ volumeInterval: number;
50
+ node: AnalyserNode;
51
+ volume: number;
52
+ private fftSize;
53
+ private fftBins;
54
+ private interval;
55
+ constructor(audioContext: AudioContext, volumeInterval?: number);
56
+ stop(): void;
57
+ private getMaxVolume;
58
+ }
59
+
60
+ declare let micAnalyser: AudioAnalyser | undefined;
61
+ declare const defaultMicThreshold = -50;
62
+ declare function getMicConstraints(deviceId?: string): MediaStreamConstraints;
63
+ declare function startMicrophone(deviceId?: string): Promise<MediaStream>;
64
+ declare function stopMicrophone(): void;
65
+ declare function createDelayedStream(stream: MediaStream): MediaStream;
66
+
67
+ declare const microphone_createDelayedStream: typeof createDelayedStream;
68
+ declare const microphone_defaultMicThreshold: typeof defaultMicThreshold;
69
+ declare const microphone_getMicConstraints: typeof getMicConstraints;
70
+ declare const microphone_micAnalyser: typeof micAnalyser;
71
+ declare const microphone_startMicrophone: typeof startMicrophone;
72
+ declare const microphone_stopMicrophone: typeof stopMicrophone;
73
+ declare namespace microphone {
74
+ export { microphone_createDelayedStream as createDelayedStream, microphone_defaultMicThreshold as defaultMicThreshold, microphone_getMicConstraints as getMicConstraints, microphone_micAnalyser as micAnalyser, microphone_startMicrophone as startMicrophone, microphone_stopMicrophone as stopMicrophone };
75
+ }
76
+
77
+ declare let speakerAnalyser: AudioAnalyser | undefined;
78
+ declare function canChangeSpeakerDevice(): boolean;
79
+ declare function changeSpeakerDevice(speakerId: string): Promise<void>;
80
+ declare function playAudioBlob(blob: Blob): Promise<void>;
81
+ declare function stopAudioBlob(): void;
82
+
83
+ declare const speaker_canChangeSpeakerDevice: typeof canChangeSpeakerDevice;
84
+ declare const speaker_changeSpeakerDevice: typeof changeSpeakerDevice;
85
+ declare const speaker_playAudioBlob: typeof playAudioBlob;
86
+ declare const speaker_speakerAnalyser: typeof speakerAnalyser;
87
+ declare const speaker_stopAudioBlob: typeof stopAudioBlob;
88
+ declare namespace speaker {
89
+ export { speaker_canChangeSpeakerDevice as canChangeSpeakerDevice, speaker_changeSpeakerDevice as changeSpeakerDevice, speaker_playAudioBlob as playAudioBlob, speaker_speakerAnalyser as speakerAnalyser, speaker_stopAudioBlob as stopAudioBlob };
90
+ }
91
+
92
+ declare enum CallHandlerErrorCode {
93
+ Mic = "Mic",
94
+ Unauthorized = "Unauthorized",
95
+ Error = "Error"
96
+ }
97
+ declare class CallHandlerError extends Error {
98
+ code: CallHandlerErrorCode;
99
+ constructor(code?: CallHandlerErrorCode);
100
+ }
101
+
102
+ interface CallHandlerEvents {
103
+ EndInterview: [];
104
+ Error: [CallHandlerError];
105
+ StateChange: void;
106
+ }
107
+ declare class CallHandler<Params extends {}> extends EventEmitter<CallHandlerEvents> {
108
+ url?: string;
109
+ params?: Params;
110
+ micRecorder: MicRecorder;
111
+ conversation: Conversation;
112
+ private ws?;
113
+ private micStream?;
114
+ private startTime;
115
+ private lastAudioBlob?;
116
+ constructor();
117
+ get isStarted(): boolean;
118
+ get isStarting(): boolean;
119
+ get isWSStarted(): boolean;
120
+ get isWSStarting(): boolean;
121
+ get isMicStarted(): boolean;
122
+ start(): Promise<void>;
123
+ stop(): Promise<void>;
124
+ pause(): void;
125
+ resume(): void;
126
+ startMic(deviceId?: string, record?: boolean): Promise<void>;
127
+ private stopMic;
128
+ private startWS;
129
+ private stopWS;
130
+ private addMessage;
131
+ private notifyStateChange;
132
+ private debug;
133
+ }
134
+
135
+ export { CallHandler, CallHandlerError, CallHandlerErrorCode, type CallHandlerEvents, LocalStorageKeys, microphone as Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, speaker as Speaker };