@siteed/expo-audio-stream 1.0.3 → 1.0.5

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.
Files changed (114) hide show
  1. package/.size-limit.json +4 -4
  2. package/README.md +18 -176
  3. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +1 -0
  4. package/app.plugin.js +1 -1
  5. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +3 -5
  6. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -1
  7. package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -1
  8. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +19 -3
  9. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -1
  10. package/build/AudioAnalysis/extractAudioAnalysis.js +14 -27
  11. package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -1
  12. package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -1
  13. package/build/AudioAnalysis/extractWaveform.js +3 -3
  14. package/build/AudioAnalysis/extractWaveform.js.map +1 -1
  15. package/build/AudioRecorder.provider.d.ts +6 -6
  16. package/build/AudioRecorder.provider.d.ts.map +1 -1
  17. package/build/AudioRecorder.provider.js +9 -9
  18. package/build/AudioRecorder.provider.js.map +1 -1
  19. package/build/ExpoAudioStream.native.d.ts.map +1 -1
  20. package/build/ExpoAudioStream.native.js +2 -2
  21. package/build/ExpoAudioStream.native.js.map +1 -1
  22. package/build/ExpoAudioStream.types.d.ts +20 -18
  23. package/build/ExpoAudioStream.types.d.ts.map +1 -1
  24. package/build/ExpoAudioStream.types.js.map +1 -1
  25. package/build/ExpoAudioStream.web.d.ts +8 -8
  26. package/build/ExpoAudioStream.web.d.ts.map +1 -1
  27. package/build/ExpoAudioStream.web.js +40 -22
  28. package/build/ExpoAudioStream.web.js.map +1 -1
  29. package/build/ExpoAudioStreamModule.d.ts.map +1 -1
  30. package/build/ExpoAudioStreamModule.js +8 -7
  31. package/build/ExpoAudioStreamModule.js.map +1 -1
  32. package/build/WebRecorder.web.d.ts +8 -8
  33. package/build/WebRecorder.web.d.ts.map +1 -1
  34. package/build/WebRecorder.web.js +60 -50
  35. package/build/WebRecorder.web.js.map +1 -1
  36. package/build/constants.d.ts +1 -1
  37. package/build/constants.d.ts.map +1 -1
  38. package/build/constants.js +3 -3
  39. package/build/constants.js.map +1 -1
  40. package/build/events.d.ts +16 -4
  41. package/build/events.d.ts.map +1 -1
  42. package/build/events.js +8 -8
  43. package/build/events.js.map +1 -1
  44. package/build/index.d.ts +8 -8
  45. package/build/index.d.ts.map +1 -1
  46. package/build/index.js +6 -6
  47. package/build/index.js.map +1 -1
  48. package/build/logger.d.ts +2 -2
  49. package/build/logger.d.ts.map +1 -1
  50. package/build/logger.js +7 -11
  51. package/build/logger.js.map +1 -1
  52. package/build/useAudioRecorder.d.ts +4 -21
  53. package/build/useAudioRecorder.d.ts.map +1 -1
  54. package/build/useAudioRecorder.js +33 -33
  55. package/build/useAudioRecorder.js.map +1 -1
  56. package/build/utils/BlobFix.d.ts +9 -0
  57. package/build/utils/BlobFix.d.ts.map +1 -0
  58. package/build/utils/BlobFix.js +494 -0
  59. package/build/utils/BlobFix.js.map +1 -0
  60. package/build/utils/concatenateBuffers.d.ts +8 -0
  61. package/build/utils/concatenateBuffers.d.ts.map +1 -0
  62. package/build/utils/concatenateBuffers.js +21 -0
  63. package/build/utils/concatenateBuffers.js.map +1 -0
  64. package/build/utils/convertPCMToFloat32.d.ts +2 -2
  65. package/build/utils/convertPCMToFloat32.d.ts.map +1 -1
  66. package/build/utils/convertPCMToFloat32.js +49 -36
  67. package/build/utils/convertPCMToFloat32.js.map +1 -1
  68. package/build/utils/encodingToBitDepth.d.ts +1 -1
  69. package/build/utils/encodingToBitDepth.d.ts.map +1 -1
  70. package/build/utils/encodingToBitDepth.js +3 -3
  71. package/build/utils/encodingToBitDepth.js.map +1 -1
  72. package/build/utils/getWavFileInfo.d.ts +4 -3
  73. package/build/utils/getWavFileInfo.d.ts.map +1 -1
  74. package/build/utils/getWavFileInfo.js +18 -15
  75. package/build/utils/getWavFileInfo.js.map +1 -1
  76. package/build/utils/writeWavHeader.d.ts.map +1 -1
  77. package/build/utils/writeWavHeader.js +4 -4
  78. package/build/utils/writeWavHeader.js.map +1 -1
  79. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -1
  80. package/build/workers/InlineFeaturesExtractor.web.js.map +1 -1
  81. package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -1
  82. package/build/workers/inlineAudioWebWorker.web.js.map +1 -1
  83. package/expo-module.config.json +8 -17
  84. package/ios/AudioStreamManager.swift +1 -0
  85. package/ios/ExpoAudioStreamModule.swift +1 -0
  86. package/ios/RecordingResult.swift +1 -0
  87. package/package.json +72 -65
  88. package/plugin/build/index.d.ts +1 -1
  89. package/plugin/build/index.js +7 -7
  90. package/plugin/src/index.ts +47 -47
  91. package/plugin/tsconfig.json +8 -13
  92. package/src/AudioAnalysis/AudioAnalysis.types.ts +59 -60
  93. package/src/AudioAnalysis/extractAudioAnalysis.ts +132 -121
  94. package/src/AudioAnalysis/extractWaveform.ts +18 -18
  95. package/src/AudioRecorder.provider.tsx +53 -53
  96. package/src/ExpoAudioStream.native.ts +2 -2
  97. package/src/ExpoAudioStream.types.ts +56 -53
  98. package/src/ExpoAudioStream.web.ts +232 -205
  99. package/src/ExpoAudioStreamModule.ts +17 -16
  100. package/src/WebRecorder.web.ts +407 -390
  101. package/src/constants.ts +11 -11
  102. package/src/events.ts +27 -13
  103. package/src/index.ts +15 -15
  104. package/src/logger.ts +15 -18
  105. package/src/useAudioRecorder.tsx +394 -389
  106. package/src/utils/BlobFix.ts +550 -0
  107. package/src/utils/concatenateBuffers.ts +24 -0
  108. package/src/utils/convertPCMToFloat32.ts +72 -45
  109. package/src/utils/encodingToBitDepth.ts +14 -14
  110. package/src/utils/getWavFileInfo.ts +106 -99
  111. package/src/utils/writeWavHeader.ts +45 -45
  112. package/src/workers/InlineFeaturesExtractor.web.tsx +1 -1
  113. package/src/workers/inlineAudioWebWorker.web.tsx +1 -1
  114. package/tsconfig.json +12 -7
@@ -1,415 +1,420 @@
1
1
  // src/useAudioRecorder.ts
2
- import { Platform, Subscription } from "expo-modules-core";
3
- import { useCallback, useEffect, useReducer, useRef } from "react";
2
+ import { Platform, Subscription } from 'expo-modules-core'
3
+ import { useCallback, useEffect, useReducer, useRef } from 'react'
4
4
 
5
+ import { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types'
5
6
  import {
6
- AudioAnalysisData,
7
- AudioAnalysisEventPayload,
8
- AudioFeaturesOptions,
9
- } from "./AudioAnalysis/AudioAnalysis.types";
7
+ AudioDataEvent,
8
+ AudioRecording,
9
+ AudioStreamStatus,
10
+ RecordingConfig,
11
+ StartRecordingResult,
12
+ } from './ExpoAudioStream.types'
13
+ import ExpoAudioStreamModule from './ExpoAudioStreamModule'
10
14
  import {
11
- AudioDataEvent,
12
- AudioEventPayload,
13
- AudioRecordingResult,
14
- AudioStreamStatus,
15
- RecordingConfig,
16
- StartRecordingResult,
17
- } from "./ExpoAudioStream.types";
18
- import ExpoAudioStreamModule from "./ExpoAudioStreamModule";
19
- import { addAudioAnalysisListener, addAudioEventListener } from "./events";
20
- import { disableAllLoggers, enableAllLoggers, getLogger } from "./logger";
21
- import { WavFileInfo } from "./utils/getWavFileInfo";
22
-
23
- const TAG = "useAudioRecorder";
24
- const logger = getLogger(TAG);
25
-
26
- export interface ExtractMetadataProps {
27
- fileUri?: string; // should provide either fileUri or arrayBuffer
28
- wavMetadata?: WavFileInfo;
29
- arrayBuffer?: ArrayBuffer;
30
- bitDepth?: number;
31
- skipWavHeader?: boolean;
32
- durationMs?: number;
33
- sampleRate?: number;
34
- numberOfChannels?: number;
35
- algorithm?: "peak" | "rms";
36
- position?: number; // Optional number of bytes to skip. Default is 0
37
- length?: number; // Optional number of bytes to read.
38
- pointsPerSecond?: number; // Optional number of points per second. Use to reduce the number of points and compute the number of datapoints to return.
39
- features?: AudioFeaturesOptions;
40
- featuresExtratorUrl?: string;
41
- }
42
-
15
+ addAudioAnalysisListener,
16
+ addAudioEventListener,
17
+ AudioEventPayload,
18
+ } from './events'
19
+ import { getLogger } from './logger'
20
+
21
+ const TAG = 'useAudioRecorder'
22
+ const logger = getLogger(TAG)
43
23
  export interface UseAudioRecorderProps {
44
- debug?: boolean;
45
- audioWorkletUrl?: string;
46
- featuresExtratorUrl?: string;
24
+ debug?: boolean
25
+ audioWorkletUrl?: string
26
+ featuresExtratorUrl?: string
47
27
  }
48
28
 
49
29
  export interface UseAudioRecorderState {
50
- startRecording: (_: RecordingConfig) => Promise<StartRecordingResult>;
51
- stopRecording: () => Promise<AudioRecordingResult | null>;
52
- pauseRecording: () => void;
53
- resumeRecording: () => void;
54
- isRecording: boolean;
55
- isPaused: boolean;
56
- durationMs: number; // Duration of the recording
57
- size: number; // Size in bytes of the recorded audio
58
- analysisData?: AudioAnalysisData;
30
+ startRecording: (_: RecordingConfig) => Promise<StartRecordingResult>
31
+ stopRecording: () => Promise<AudioRecording>
32
+ pauseRecording: () => void
33
+ resumeRecording: () => void
34
+ isRecording: boolean
35
+ isPaused: boolean
36
+ durationMs: number // Duration of the recording
37
+ size: number // Size in bytes of the recorded audio
38
+ analysisData?: AudioAnalysis
59
39
  }
60
40
 
61
41
  interface RecorderReducerState {
62
- isRecording: boolean;
63
- isPaused: boolean;
64
- durationMs: number;
65
- size: number;
66
- analysisData?: AudioAnalysisData;
42
+ isRecording: boolean
43
+ isPaused: boolean
44
+ durationMs: number
45
+ size: number
46
+ analysisData?: AudioAnalysis
67
47
  }
68
48
 
69
49
  type RecorderAction =
70
- | { type: "START" | "STOP" | "PAUSE" | "RESUME" }
71
- | { type: "UPDATE_STATUS"; payload: { durationMs: number; size: number } }
72
- | { type: "UPDATE_ANALYSIS"; payload: AudioAnalysisData };
73
-
74
- const defaultAnalysis: AudioAnalysisData = {
75
- pointsPerSecond: 10,
76
- bitDepth: 32,
77
- numberOfChannels: 1,
78
- durationMs: 0,
79
- sampleRate: 44100,
80
- samples: 0,
81
- dataPoints: [],
82
- amplitudeRange: {
83
- min: Number.POSITIVE_INFINITY,
84
- max: Number.NEGATIVE_INFINITY,
85
- },
86
- };
50
+ | { type: 'START' | 'STOP' | 'PAUSE' | 'RESUME' }
51
+ | { type: 'UPDATE_STATUS'; payload: { durationMs: number; size: number } }
52
+ | { type: 'UPDATE_ANALYSIS'; payload: AudioAnalysis }
53
+
54
+ const defaultAnalysis: AudioAnalysis = {
55
+ pointsPerSecond: 10,
56
+ bitDepth: 32,
57
+ numberOfChannels: 1,
58
+ durationMs: 0,
59
+ sampleRate: 44100,
60
+ samples: 0,
61
+ dataPoints: [],
62
+ amplitudeAlgorithm: 'rms',
63
+ speakerChanges: [],
64
+ amplitudeRange: {
65
+ min: Number.POSITIVE_INFINITY,
66
+ max: Number.NEGATIVE_INFINITY,
67
+ },
68
+ }
87
69
 
88
70
  function audioRecorderReducer(
89
- state: RecorderReducerState,
90
- action: RecorderAction,
71
+ state: RecorderReducerState,
72
+ action: RecorderAction
91
73
  ): RecorderReducerState {
92
- switch (action.type) {
93
- case "START":
94
- return {
95
- ...state,
96
- isRecording: true,
97
- isPaused: false,
98
- durationMs: 0,
99
- size: 0,
100
- analysisData: defaultAnalysis, // Reset analysis data
101
- };
102
- case "STOP":
103
- return { ...state, isRecording: false, isPaused: false };
104
- case "PAUSE":
105
- return { ...state, isPaused: true, isRecording: false };
106
- case "RESUME":
107
- return { ...state, isPaused: false, isRecording: true };
108
- case "UPDATE_STATUS":
109
- return {
110
- ...state,
111
- durationMs: action.payload.durationMs,
112
- size: action.payload.size,
113
- };
114
- case "UPDATE_ANALYSIS":
115
- return {
116
- ...state,
117
- analysisData: action.payload,
118
- };
119
- default:
120
- return state;
121
- }
74
+ switch (action.type) {
75
+ case 'START':
76
+ return {
77
+ ...state,
78
+ isRecording: true,
79
+ isPaused: false,
80
+ durationMs: 0,
81
+ size: 0,
82
+ analysisData: defaultAnalysis, // Reset analysis data
83
+ }
84
+ case 'STOP':
85
+ return { ...state, isRecording: false, isPaused: false }
86
+ case 'PAUSE':
87
+ return { ...state, isPaused: true, isRecording: false }
88
+ case 'RESUME':
89
+ return { ...state, isPaused: false, isRecording: true }
90
+ case 'UPDATE_STATUS':
91
+ return {
92
+ ...state,
93
+ durationMs: action.payload.durationMs,
94
+ size: action.payload.size,
95
+ }
96
+ case 'UPDATE_ANALYSIS':
97
+ return {
98
+ ...state,
99
+ analysisData: action.payload,
100
+ }
101
+ default:
102
+ return state
103
+ }
104
+ }
105
+
106
+ interface HandleAudioAnalysisProps {
107
+ analysis: AudioAnalysis
108
+ visualizationDuration: number
122
109
  }
123
110
 
124
111
  export function useAudioRecorder({
125
- debug = false,
126
- audioWorkletUrl,
127
- featuresExtratorUrl,
112
+ debug = false,
113
+ audioWorkletUrl,
114
+ featuresExtratorUrl,
128
115
  }: UseAudioRecorderProps = {}): UseAudioRecorderState {
129
- const [state, dispatch] = useReducer(audioRecorderReducer, {
130
- isRecording: false,
131
- isPaused: false,
132
- durationMs: 0,
133
- size: 0,
134
- analysisData: undefined,
135
- });
136
-
137
- const analysisListenerRef = useRef<Subscription | null>(null);
138
- const analysisRef = useRef<AudioAnalysisData>({ ...defaultAnalysis });
139
-
140
- // Instantiate the module for web with URLs
141
- const ExpoAudioStream =
142
- Platform.OS === "web"
143
- ? ExpoAudioStreamModule({ audioWorkletUrl, featuresExtratorUrl })
144
- : ExpoAudioStreamModule;
145
-
146
- const onAudioStreamRef = useRef<
147
- ((_: AudioDataEvent) => Promise<void>) | null
148
- >(null);
149
-
150
- const handleAudioAnalysis = useCallback(
151
- async ({ analysis, visualizationDuration }: AudioAnalysisEventPayload) => {
152
- const savedAnalysisData = analysisRef.current || { ...defaultAnalysis };
153
-
154
- const maxDuration = visualizationDuration;
155
-
156
- logger.debug(
157
- `[handleAudioAnalysis] Received audio analysis: maxDuration=${maxDuration} analysis.dataPoints=${analysis.dataPoints.length} analysisData.dataPoints=${savedAnalysisData.dataPoints.length}`,
158
- analysis,
159
- );
160
-
161
- // Combine data points
162
- const combinedDataPoints = [
163
- ...savedAnalysisData.dataPoints,
164
- ...analysis.dataPoints,
165
- ];
166
-
167
- // Calculate the new duration
168
- const pointsPerSecond =
169
- analysis.pointsPerSecond || savedAnalysisData.pointsPerSecond;
170
- const maxDataPoints = (pointsPerSecond * visualizationDuration) / 1000;
171
-
172
- logger.debug(
173
- `[handleAudioAnalysis] Combined data points before trimming: pointsPerSecond=${pointsPerSecond} visualizationDuration=${visualizationDuration} combinedDataPointsLength=${combinedDataPoints.length} vs maxDataPoints=${maxDataPoints}`,
174
- );
175
-
176
- // Trim data points to keep within the maximum number of data points
177
- if (combinedDataPoints.length > maxDataPoints) {
178
- combinedDataPoints.splice(0, combinedDataPoints.length - maxDataPoints);
179
- }
180
-
181
- savedAnalysisData.dataPoints = combinedDataPoints;
182
- savedAnalysisData.bitDepth =
183
- analysis.bitDepth || savedAnalysisData.bitDepth;
184
- savedAnalysisData.durationMs =
185
- combinedDataPoints.length * (1000 / pointsPerSecond);
186
-
187
- // Update amplitude range
188
- const newMin = Math.min(
189
- savedAnalysisData.amplitudeRange.min,
190
- analysis.amplitudeRange.min,
191
- );
192
- const newMax = Math.max(
193
- savedAnalysisData.amplitudeRange.max,
194
- analysis.amplitudeRange.max,
195
- );
196
-
197
- savedAnalysisData.amplitudeRange = {
198
- min: newMin,
199
- max: newMax,
200
- };
201
-
202
- logger.debug(
203
- `[handleAudioAnalysis] Updated analysis data: durationMs=${savedAnalysisData.durationMs}`,
204
- savedAnalysisData,
205
- );
206
-
207
- // Update the ref
208
- analysisRef.current = savedAnalysisData;
209
-
210
- // Dispatch the updated analysis data to state to trigger re-render
211
- // need to use spread operator otherwise it doesnt trigger update.
212
- dispatch({ type: "UPDATE_ANALYSIS", payload: { ...savedAnalysisData } });
213
- },
214
- [dispatch],
215
- );
216
-
217
- const handleAudioEvent = useCallback(async (eventData: AudioEventPayload) => {
218
- const {
219
- fileUri,
220
- deltaSize,
221
- totalSize,
222
- lastEmittedSize,
223
- position,
224
- streamUuid,
225
- encoded,
226
- mimeType,
227
- buffer,
228
- } = eventData;
229
- logger.debug(`[handleAudioEvent] Received audio event:`, {
230
- fileUri,
231
- deltaSize,
232
- totalSize,
233
- position,
234
- mimeType,
235
- lastEmittedSize,
236
- streamUuid,
237
- encodedLength: encoded?.length,
238
- });
239
- if (deltaSize === 0) {
240
- // Ignore packet with no data
241
- return;
242
- }
243
- try {
244
- // Coming from native ( ios / android ) otherwise buffer is set
245
- if (Platform.OS !== "web") {
246
- // Read the audio file as a base64 string for comparison
247
- if (!encoded) {
248
- console.error(`${TAG} Encoded audio data is missing`);
249
- throw new Error("Encoded audio data is missing");
116
+ const [state, dispatch] = useReducer(audioRecorderReducer, {
117
+ isRecording: false,
118
+ isPaused: false,
119
+ durationMs: 0,
120
+ size: 0,
121
+ analysisData: undefined,
122
+ })
123
+
124
+ const analysisListenerRef = useRef<Subscription | null>(null)
125
+ const analysisRef = useRef<AudioAnalysis>({ ...defaultAnalysis })
126
+
127
+ // Instantiate the module for web with URLs
128
+ const ExpoAudioStream =
129
+ Platform.OS === 'web'
130
+ ? ExpoAudioStreamModule({ audioWorkletUrl, featuresExtratorUrl })
131
+ : ExpoAudioStreamModule
132
+
133
+ const onAudioStreamRef = useRef<
134
+ ((_: AudioDataEvent) => Promise<void>) | null
135
+ >(null)
136
+
137
+ const handleAudioAnalysis = useCallback(
138
+ async ({
139
+ analysis,
140
+ visualizationDuration,
141
+ }: HandleAudioAnalysisProps) => {
142
+ const savedAnalysisData = analysisRef.current || {
143
+ ...defaultAnalysis,
144
+ }
145
+
146
+ const maxDuration = visualizationDuration
147
+
148
+ logger.debug(
149
+ `[handleAudioAnalysis] Received audio analysis: maxDuration=${maxDuration} analysis.dataPoints=${analysis.dataPoints.length} analysisData.dataPoints=${savedAnalysisData.dataPoints.length}`,
150
+ analysis
151
+ )
152
+
153
+ // Combine data points
154
+ const combinedDataPoints = [
155
+ ...savedAnalysisData.dataPoints,
156
+ ...analysis.dataPoints,
157
+ ]
158
+
159
+ // Calculate the new duration
160
+ const pointsPerSecond =
161
+ analysis.pointsPerSecond || savedAnalysisData.pointsPerSecond
162
+ const maxDataPoints =
163
+ (pointsPerSecond * visualizationDuration) / 1000
164
+
165
+ logger.debug(
166
+ `[handleAudioAnalysis] Combined data points before trimming: pointsPerSecond=${pointsPerSecond} visualizationDuration=${visualizationDuration} combinedDataPointsLength=${combinedDataPoints.length} vs maxDataPoints=${maxDataPoints}`
167
+ )
168
+
169
+ // Trim data points to keep within the maximum number of data points
170
+ if (combinedDataPoints.length > maxDataPoints) {
171
+ combinedDataPoints.splice(
172
+ 0,
173
+ combinedDataPoints.length - maxDataPoints
174
+ )
175
+ }
176
+
177
+ savedAnalysisData.dataPoints = combinedDataPoints
178
+ savedAnalysisData.bitDepth =
179
+ analysis.bitDepth || savedAnalysisData.bitDepth
180
+ savedAnalysisData.durationMs =
181
+ combinedDataPoints.length * (1000 / pointsPerSecond)
182
+
183
+ // Update amplitude range
184
+ const newMin = Math.min(
185
+ savedAnalysisData.amplitudeRange.min,
186
+ analysis.amplitudeRange.min
187
+ )
188
+ const newMax = Math.max(
189
+ savedAnalysisData.amplitudeRange.max,
190
+ analysis.amplitudeRange.max
191
+ )
192
+
193
+ savedAnalysisData.amplitudeRange = {
194
+ min: newMin,
195
+ max: newMax,
196
+ }
197
+
198
+ logger.debug(
199
+ `[handleAudioAnalysis] Updated analysis data: durationMs=${savedAnalysisData.durationMs}`,
200
+ savedAnalysisData
201
+ )
202
+
203
+ // Update the ref
204
+ analysisRef.current = savedAnalysisData
205
+
206
+ // Dispatch the updated analysis data to state to trigger re-render
207
+ // need to use spread operator otherwise it doesnt trigger update.
208
+ dispatch({
209
+ type: 'UPDATE_ANALYSIS',
210
+ payload: { ...savedAnalysisData },
211
+ })
212
+ },
213
+ [dispatch]
214
+ )
215
+
216
+ const handleAudioEvent = useCallback(
217
+ async (eventData: AudioEventPayload) => {
218
+ const {
219
+ fileUri,
220
+ deltaSize,
221
+ totalSize,
222
+ lastEmittedSize,
223
+ position,
224
+ streamUuid,
225
+ encoded,
226
+ mimeType,
227
+ buffer,
228
+ } = eventData
229
+ logger.debug(`[handleAudioEvent] Received audio event:`, {
230
+ fileUri,
231
+ deltaSize,
232
+ totalSize,
233
+ position,
234
+ mimeType,
235
+ lastEmittedSize,
236
+ streamUuid,
237
+ encodedLength: encoded?.length,
238
+ })
239
+ if (deltaSize === 0) {
240
+ // Ignore packet with no data
241
+ return
242
+ }
243
+ try {
244
+ // Coming from native ( ios / android ) otherwise buffer is set
245
+ if (Platform.OS !== 'web') {
246
+ // Read the audio file as a base64 string for comparison
247
+ if (!encoded) {
248
+ console.error(`${TAG} Encoded audio data is missing`)
249
+ throw new Error('Encoded audio data is missing')
250
+ }
251
+ onAudioStreamRef.current?.({
252
+ data: encoded,
253
+ position,
254
+ fileUri,
255
+ eventDataSize: deltaSize,
256
+ totalSize,
257
+ })
258
+ } else if (buffer) {
259
+ // Coming from web
260
+ const webEvent: AudioDataEvent = {
261
+ data: buffer,
262
+ position,
263
+ fileUri,
264
+ eventDataSize: deltaSize,
265
+ totalSize,
266
+ }
267
+ onAudioStreamRef.current?.(webEvent)
268
+ logger.debug(
269
+ `[handleAudioEvent] Audio data sent to onAudioStream`,
270
+ webEvent
271
+ )
272
+ }
273
+ } catch (error) {
274
+ console.error(`${TAG} Error processing audio event:`, error)
275
+ }
276
+ },
277
+ []
278
+ )
279
+
280
+ const checkStatus = useCallback(async () => {
281
+ try {
282
+ if (!state.isRecording) {
283
+ logger.debug(`Not recording, exiting status check.`)
284
+ return
285
+ }
286
+
287
+ const status: AudioStreamStatus = ExpoAudioStream.status()
288
+ if (debug) {
289
+ logger.debug(`Status:`, status)
290
+ }
291
+
292
+ dispatch({
293
+ type: 'UPDATE_STATUS',
294
+ payload: { durationMs: status.durationMs, size: status.size },
295
+ })
296
+ } catch (error) {
297
+ console.error(`${TAG} Error getting status:`, error)
250
298
  }
251
- onAudioStreamRef.current?.({
252
- data: encoded,
253
- position,
254
- fileUri,
255
- eventDataSize: deltaSize,
256
- totalSize,
257
- });
258
- } else if (buffer) {
259
- // Coming from web
260
- const webEvent: AudioDataEvent = {
261
- data: buffer,
262
- position,
263
- fileUri,
264
- eventDataSize: deltaSize,
265
- totalSize,
266
- };
267
- onAudioStreamRef.current?.(webEvent);
268
- logger.debug(
269
- `[handleAudioEvent] Audio data sent to onAudioStream`,
270
- webEvent,
271
- );
272
- }
273
- } catch (error) {
274
- console.error(`${TAG} Error processing audio event:`, error);
275
- }
276
- }, []);
277
-
278
- const checkStatus = useCallback(async () => {
279
- try {
280
- if (!state.isRecording) {
281
- logger.debug(`Not recording, exiting status check.`);
282
- return;
283
- }
284
-
285
- const status: AudioStreamStatus = ExpoAudioStream.status();
286
- if (debug) {
287
- logger.debug(`Status:`, status);
288
- }
289
-
290
- dispatch({
291
- type: "UPDATE_STATUS",
292
- payload: { durationMs: status.durationMs, size: status.size },
293
- });
294
- } catch (error) {
295
- console.error(`${TAG} Error getting status:`, error);
296
- }
297
- }, [state.isRecording]);
298
-
299
- const startRecording = useCallback(
300
- async (recordingOptions: RecordingConfig) => {
301
- logger.debug(`start recoding`, recordingOptions);
302
-
303
- analysisRef.current = { ...defaultAnalysis }; // Reset analysis data
304
-
305
- const { onAudioStream, ...options } = recordingOptions;
306
- const { enableProcessing } = options;
307
-
308
- const maxRecentDataDuration = 10000; // TODO compute maxRecentDataDuration based on screen dimensions
309
- if (typeof onAudioStream === "function") {
310
- onAudioStreamRef.current = onAudioStream;
311
- } else {
312
- console.warn(`${TAG} onAudioStream is not a function`, onAudioStream);
313
- onAudioStreamRef.current = null;
314
- }
315
- const startResult: StartRecordingResult =
316
- await ExpoAudioStream.startRecording(options);
317
- dispatch({ type: "START" });
318
-
319
- if (enableProcessing) {
320
- logger.debug(`Enabling audio analysis listener`);
321
- const listener = addAudioAnalysisListener(async (analysisData) => {
322
- try {
323
- await handleAudioAnalysis({
324
- analysis: analysisData,
325
- visualizationDuration: maxRecentDataDuration,
326
- });
327
- } catch (error) {
328
- console.warn(`${TAG} Error processing audio analysis:`, error);
329
- }
330
- });
331
-
332
- analysisListenerRef.current = listener;
333
- }
334
-
335
- return startResult;
336
- },
337
- [handleAudioAnalysis, dispatch],
338
- );
339
-
340
- const stopRecording = useCallback(async () => {
341
- logger.debug(`stoping recording`);
299
+ }, [state.isRecording])
300
+
301
+ const startRecording = useCallback(
302
+ async (recordingOptions: RecordingConfig) => {
303
+ logger.debug(`start recoding`, recordingOptions)
304
+
305
+ analysisRef.current = { ...defaultAnalysis } // Reset analysis data
306
+
307
+ const { onAudioStream, ...options } = recordingOptions
308
+ const { enableProcessing } = options
309
+
310
+ const maxRecentDataDuration = 10000 // TODO compute maxRecentDataDuration based on screen dimensions
311
+ if (typeof onAudioStream === 'function') {
312
+ onAudioStreamRef.current = onAudioStream
313
+ } else {
314
+ console.warn(
315
+ `${TAG} onAudioStream is not a function`,
316
+ onAudioStream
317
+ )
318
+ onAudioStreamRef.current = null
319
+ }
320
+ const startResult: StartRecordingResult =
321
+ await ExpoAudioStream.startRecording(options)
322
+ dispatch({ type: 'START' })
323
+
324
+ if (enableProcessing) {
325
+ logger.debug(`Enabling audio analysis listener`)
326
+ const listener = addAudioAnalysisListener(
327
+ async (analysisData) => {
328
+ try {
329
+ await handleAudioAnalysis({
330
+ analysis: analysisData,
331
+ visualizationDuration: maxRecentDataDuration,
332
+ })
333
+ } catch (error) {
334
+ console.warn(
335
+ `${TAG} Error processing audio analysis:`,
336
+ error
337
+ )
338
+ }
339
+ }
340
+ )
341
+
342
+ analysisListenerRef.current = listener
343
+ }
344
+
345
+ return startResult
346
+ },
347
+ [handleAudioAnalysis, dispatch]
348
+ )
349
+
350
+ const stopRecording = useCallback(async () => {
351
+ logger.debug(`stoping recording`)
352
+
353
+ const stopResult: AudioRecording = await ExpoAudioStream.stopRecording()
354
+ stopResult.analysisData = analysisRef.current
355
+
356
+ if (analysisListenerRef.current) {
357
+ analysisListenerRef.current.remove()
358
+ analysisListenerRef.current = null
359
+ }
360
+ onAudioStreamRef.current = null
361
+ logger.debug(`recording stopped`, stopResult)
362
+ dispatch({ type: 'STOP' })
363
+ return stopResult
364
+ }, [dispatch])
365
+
366
+ const pauseRecording = useCallback(async () => {
367
+ logger.debug(`pause recording`)
368
+ const pauseResult = await ExpoAudioStream.pauseRecording()
369
+ dispatch({ type: 'PAUSE' })
370
+ return pauseResult
371
+ }, [dispatch])
372
+
373
+ const resumeRecording = useCallback(async () => {
374
+ logger.debug(`resume recording`)
375
+ const resumeResult = await ExpoAudioStream.resumeRecording()
376
+ dispatch({ type: 'RESUME' })
377
+ return resumeResult
378
+ }, [dispatch])
379
+
380
+ useEffect(() => {
381
+ let interval: ReturnType<typeof setTimeout>
382
+ if (state.isRecording) {
383
+ interval = setInterval(checkStatus, 1000)
384
+ }
385
+ return () => {
386
+ if (interval) {
387
+ clearInterval(interval)
388
+ }
389
+ }
390
+ }, [checkStatus, state.isRecording])
342
391
 
343
- if (analysisListenerRef.current) {
344
- analysisListenerRef.current.remove();
345
- analysisListenerRef.current = null;
346
- }
392
+ useEffect(() => {
393
+ logger.debug(`Registering audio event listener`)
394
+ const subscribeAudio = addAudioEventListener(handleAudioEvent)
347
395
 
348
- const stopResult: AudioRecordingResult =
349
- await ExpoAudioStream.stopRecording();
350
- onAudioStreamRef.current = null;
351
- logger.debug(`recording stopped`, stopResult);
352
- dispatch({ type: "STOP" });
353
- return stopResult;
354
- }, [dispatch]);
355
-
356
- const pauseRecording = useCallback(async () => {
357
- logger.debug(`pause recording`);
358
- const pauseResult = await ExpoAudioStream.pauseRecording();
359
- dispatch({ type: "PAUSE" });
360
- return pauseResult;
361
- }, [dispatch]);
362
-
363
- const resumeRecording = useCallback(async () => {
364
- logger.debug(`resume recording`);
365
- const resumeResult = await ExpoAudioStream.resumeRecording();
366
- dispatch({ type: "RESUME" });
367
- return resumeResult;
368
- }, [dispatch]);
369
-
370
- useEffect(() => {
371
- let interval: ReturnType<typeof setTimeout>;
372
- if (state.isRecording) {
373
- interval = setInterval(checkStatus, 1000);
374
- }
375
- return () => {
376
- if (interval) {
377
- clearInterval(interval);
378
- }
379
- };
380
- }, [checkStatus, state.isRecording]);
381
-
382
- useEffect(() => {
383
- logger.debug(`Registering audio event listener`);
384
- const subscribeAudio = addAudioEventListener(handleAudioEvent);
385
-
386
- logger.debug(`Subscribed to audio event listener and analysis listener`, {
387
- subscribeAudio,
388
- });
389
-
390
- return () => {
391
- logger.debug(`Removing audio event listener`);
392
- subscribeAudio.remove();
393
- };
394
- }, [handleAudioEvent, handleAudioAnalysis]);
395
-
396
- useEffect(() => {
397
- if (debug) {
398
- enableAllLoggers();
399
- } else {
400
- disableAllLoggers();
396
+ logger.debug(
397
+ `Subscribed to audio event listener and analysis listener`,
398
+ {
399
+ subscribeAudio,
400
+ }
401
+ )
402
+
403
+ return () => {
404
+ logger.debug(`Removing audio event listener`)
405
+ subscribeAudio.remove()
406
+ }
407
+ }, [handleAudioEvent, handleAudioAnalysis])
408
+
409
+ return {
410
+ startRecording,
411
+ stopRecording,
412
+ pauseRecording,
413
+ resumeRecording,
414
+ isPaused: state.isPaused,
415
+ isRecording: state.isRecording,
416
+ durationMs: state.durationMs,
417
+ size: state.size,
418
+ analysisData: state.analysisData,
401
419
  }
402
- }, [debug]);
403
-
404
- return {
405
- startRecording,
406
- stopRecording,
407
- pauseRecording,
408
- resumeRecording,
409
- isPaused: state.isPaused,
410
- isRecording: state.isRecording,
411
- durationMs: state.durationMs,
412
- size: state.size,
413
- analysisData: state.analysisData,
414
- };
415
420
  }