@idealyst/microphone 1.1.2

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,43 @@
1
+ import { createUseMicrophoneHook } from './createUseMicrophoneHook';
2
+ import { createMicrophone } from '../microphone.web';
3
+
4
+ /**
5
+ * React hook for microphone access on web platforms.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { useMicrophone, AUDIO_PROFILES } from '@idealyst/microphone';
10
+ *
11
+ * function VoiceInput() {
12
+ * const {
13
+ * isRecording,
14
+ * level,
15
+ * start,
16
+ * stop,
17
+ * subscribeToAudioData
18
+ * } = useMicrophone({
19
+ * config: AUDIO_PROFILES.speech,
20
+ * autoRequestPermission: true,
21
+ * });
22
+ *
23
+ * useEffect(() => {
24
+ * if (!isRecording) return;
25
+ *
26
+ * return subscribeToAudioData((pcmData) => {
27
+ * // Process audio data
28
+ * console.log('Got', pcmData.samples.length, 'samples');
29
+ * });
30
+ * }, [isRecording, subscribeToAudioData]);
31
+ *
32
+ * return (
33
+ * <div>
34
+ * <button onClick={isRecording ? stop : start}>
35
+ * {isRecording ? 'Stop' : 'Start'}
36
+ * </button>
37
+ * <div>Level: {Math.round(level.current * 100)}%</div>
38
+ * </div>
39
+ * );
40
+ * }
41
+ * ```
42
+ */
43
+ export const useMicrophone = createUseMicrophoneHook(createMicrophone);
@@ -0,0 +1,37 @@
1
+ import { createUseRecorderHook } from './createUseRecorderHook';
2
+ import { createRecorder } from '../recorder/recorder.native';
3
+
4
+ /**
5
+ * React hook for recording audio to file on React Native platforms.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { useRecorder } from '@idealyst/microphone';
10
+ * import { View, Button, Text } from 'react-native';
11
+ *
12
+ * function AudioRecorder() {
13
+ * const { isRecording, duration, startRecording, stopRecording } = useRecorder();
14
+ * const [recording, setRecording] = useState<RecordingResult | null>(null);
15
+ *
16
+ * const handleStop = async () => {
17
+ * const result = await stopRecording();
18
+ * setRecording(result);
19
+ *
20
+ * // Get the base64 data for upload
21
+ * const base64Data = await result.getData();
22
+ * console.log('Recording size:', result.size, 'bytes');
23
+ * };
24
+ *
25
+ * return (
26
+ * <View>
27
+ * <Text>Duration: {Math.floor(duration / 1000)}s</Text>
28
+ * <Button
29
+ * title={isRecording ? 'Stop' : 'Record'}
30
+ * onPress={isRecording ? handleStop : () => startRecording()}
31
+ * />
32
+ * </View>
33
+ * );
34
+ * }
35
+ * ```
36
+ */
37
+ export const useRecorder = createUseRecorderHook(createRecorder);
@@ -0,0 +1,37 @@
1
+ import { createUseRecorderHook } from './createUseRecorderHook';
2
+ import { createRecorder } from '../recorder/recorder.web';
3
+
4
+ /**
5
+ * React hook for recording audio to file on web platforms.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { useRecorder } from '@idealyst/microphone';
10
+ *
11
+ * function AudioRecorder() {
12
+ * const { isRecording, duration, startRecording, stopRecording } = useRecorder();
13
+ * const [recording, setRecording] = useState<RecordingResult | null>(null);
14
+ *
15
+ * const handleStop = async () => {
16
+ * const result = await stopRecording();
17
+ * setRecording(result);
18
+ *
19
+ * // Download the recording
20
+ * const link = document.createElement('a');
21
+ * link.href = result.uri;
22
+ * link.download = 'recording.wav';
23
+ * link.click();
24
+ * };
25
+ *
26
+ * return (
27
+ * <div>
28
+ * <p>Duration: {Math.floor(duration / 1000)}s</p>
29
+ * <button onClick={isRecording ? handleStop : () => startRecording()}>
30
+ * {isRecording ? 'Stop' : 'Record'}
31
+ * </button>
32
+ * </div>
33
+ * );
34
+ * }
35
+ * ```
36
+ */
37
+ export const useRecorder = createUseRecorderHook(createRecorder);
@@ -0,0 +1,79 @@
1
+ // Types
2
+ export type {
3
+ // Audio config types
4
+ SampleRate,
5
+ BitDepth,
6
+ ChannelCount,
7
+ AudioConfig,
8
+
9
+ // PCM data types
10
+ PCMData,
11
+ AudioLevel,
12
+
13
+ // Permission types
14
+ PermissionStatus,
15
+ PermissionResult,
16
+
17
+ // Microphone state types
18
+ MicrophoneState,
19
+ MicrophoneStatus,
20
+
21
+ // Error types
22
+ MicrophoneErrorCode,
23
+ MicrophoneError,
24
+
25
+ // Callback types
26
+ AudioDataCallback,
27
+ AudioLevelCallback,
28
+ StateChangeCallback,
29
+ ErrorCallback,
30
+
31
+ // Interface types
32
+ IMicrophone,
33
+ IRecorder,
34
+
35
+ // Recording types
36
+ RecordingFormat,
37
+ RecordingOptions,
38
+ RecordingResult,
39
+
40
+ // Hook types
41
+ UseMicrophoneOptions,
42
+ UseMicrophoneResult,
43
+ UseRecorderOptions,
44
+ UseRecorderResult,
45
+ } from './types';
46
+
47
+ // Constants
48
+ export {
49
+ DEFAULT_AUDIO_CONFIG,
50
+ DEFAULT_AUDIO_LEVEL,
51
+ DEFAULT_LEVEL_UPDATE_INTERVAL,
52
+ AUDIO_PROFILES,
53
+ BIT_DEPTH_MAX_VALUES,
54
+ } from './constants';
55
+
56
+ // Utilities
57
+ export {
58
+ base64ToArrayBuffer,
59
+ arrayBufferToBase64,
60
+ createPCMTypedArray,
61
+ calculateAudioLevels,
62
+ float32ToInt16,
63
+ float32ToInt8,
64
+ createWavHeader,
65
+ createWavFile,
66
+ concatArrayBuffers,
67
+ } from './utils';
68
+
69
+ // Hooks
70
+ export { useMicrophone, useRecorder } from './hooks/index.native';
71
+
72
+ // Microphone class
73
+ export { NativeMicrophone, createMicrophone } from './microphone.native';
74
+
75
+ // Recorder class
76
+ export { NativeRecorder, createRecorder } from './recorder/recorder.native';
77
+
78
+ // Permissions
79
+ export { checkPermission, requestPermission } from './permissions/permissions.native';
package/src/index.ts ADDED
@@ -0,0 +1,79 @@
1
+ // Types
2
+ export type {
3
+ // Audio config types
4
+ SampleRate,
5
+ BitDepth,
6
+ ChannelCount,
7
+ AudioConfig,
8
+
9
+ // PCM data types
10
+ PCMData,
11
+ AudioLevel,
12
+
13
+ // Permission types
14
+ PermissionStatus,
15
+ PermissionResult,
16
+
17
+ // Microphone state types
18
+ MicrophoneState,
19
+ MicrophoneStatus,
20
+
21
+ // Error types
22
+ MicrophoneErrorCode,
23
+ MicrophoneError,
24
+
25
+ // Callback types
26
+ AudioDataCallback,
27
+ AudioLevelCallback,
28
+ StateChangeCallback,
29
+ ErrorCallback,
30
+
31
+ // Interface types
32
+ IMicrophone,
33
+ IRecorder,
34
+
35
+ // Recording types
36
+ RecordingFormat,
37
+ RecordingOptions,
38
+ RecordingResult,
39
+
40
+ // Hook types
41
+ UseMicrophoneOptions,
42
+ UseMicrophoneResult,
43
+ UseRecorderOptions,
44
+ UseRecorderResult,
45
+ } from './types';
46
+
47
+ // Constants
48
+ export {
49
+ DEFAULT_AUDIO_CONFIG,
50
+ DEFAULT_AUDIO_LEVEL,
51
+ DEFAULT_LEVEL_UPDATE_INTERVAL,
52
+ AUDIO_PROFILES,
53
+ BIT_DEPTH_MAX_VALUES,
54
+ } from './constants';
55
+
56
+ // Utilities
57
+ export {
58
+ base64ToArrayBuffer,
59
+ arrayBufferToBase64,
60
+ createPCMTypedArray,
61
+ calculateAudioLevels,
62
+ float32ToInt16,
63
+ float32ToInt8,
64
+ createWavHeader,
65
+ createWavFile,
66
+ concatArrayBuffers,
67
+ } from './utils';
68
+
69
+ // Hooks
70
+ export { useMicrophone, useRecorder } from './hooks/index.web';
71
+
72
+ // Microphone class
73
+ export { WebMicrophone, createMicrophone } from './microphone.web';
74
+
75
+ // Recorder class
76
+ export { WebRecorder, createRecorder } from './recorder/recorder.web';
77
+
78
+ // Permissions
79
+ export { checkPermission, requestPermission } from './permissions/permissions.web';
@@ -0,0 +1,79 @@
1
+ // Types
2
+ export type {
3
+ // Audio config types
4
+ SampleRate,
5
+ BitDepth,
6
+ ChannelCount,
7
+ AudioConfig,
8
+
9
+ // PCM data types
10
+ PCMData,
11
+ AudioLevel,
12
+
13
+ // Permission types
14
+ PermissionStatus,
15
+ PermissionResult,
16
+
17
+ // Microphone state types
18
+ MicrophoneState,
19
+ MicrophoneStatus,
20
+
21
+ // Error types
22
+ MicrophoneErrorCode,
23
+ MicrophoneError,
24
+
25
+ // Callback types
26
+ AudioDataCallback,
27
+ AudioLevelCallback,
28
+ StateChangeCallback,
29
+ ErrorCallback,
30
+
31
+ // Interface types
32
+ IMicrophone,
33
+ IRecorder,
34
+
35
+ // Recording types
36
+ RecordingFormat,
37
+ RecordingOptions,
38
+ RecordingResult,
39
+
40
+ // Hook types
41
+ UseMicrophoneOptions,
42
+ UseMicrophoneResult,
43
+ UseRecorderOptions,
44
+ UseRecorderResult,
45
+ } from './types';
46
+
47
+ // Constants
48
+ export {
49
+ DEFAULT_AUDIO_CONFIG,
50
+ DEFAULT_AUDIO_LEVEL,
51
+ DEFAULT_LEVEL_UPDATE_INTERVAL,
52
+ AUDIO_PROFILES,
53
+ BIT_DEPTH_MAX_VALUES,
54
+ } from './constants';
55
+
56
+ // Utilities
57
+ export {
58
+ base64ToArrayBuffer,
59
+ arrayBufferToBase64,
60
+ createPCMTypedArray,
61
+ calculateAudioLevels,
62
+ float32ToInt16,
63
+ float32ToInt8,
64
+ createWavHeader,
65
+ createWavFile,
66
+ concatArrayBuffers,
67
+ } from './utils';
68
+
69
+ // Hooks
70
+ export { useMicrophone, useRecorder } from './hooks/index.web';
71
+
72
+ // Microphone class
73
+ export { WebMicrophone, createMicrophone } from './microphone.web';
74
+
75
+ // Recorder class
76
+ export { WebRecorder, createRecorder } from './recorder/recorder.web';
77
+
78
+ // Permissions
79
+ export { checkPermission, requestPermission } from './permissions/permissions.web';