@siteed/expo-audio-stream 2.0.1 → 2.2.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.
Files changed (166) hide show
  1. package/README.md +46 -27
  2. package/build/index.d.ts +11 -12
  3. package/build/index.js +44 -10
  4. package/package.json +49 -110
  5. package/src/index.ts +18 -33
  6. package/CHANGELOG.md +0 -195
  7. package/android/build.gradle +0 -105
  8. package/android/src/main/AndroidManifest.xml +0 -27
  9. package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +0 -166
  10. package/android/src/main/java/net/siteed/audiostream/AudioDataEncoder.kt +0 -9
  11. package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +0 -131
  12. package/android/src/main/java/net/siteed/audiostream/AudioFormatUtils.kt +0 -103
  13. package/android/src/main/java/net/siteed/audiostream/AudioNotificationsManager.kt +0 -435
  14. package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +0 -1936
  15. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +0 -1437
  16. package/android/src/main/java/net/siteed/audiostream/AudioRecordingService.kt +0 -138
  17. package/android/src/main/java/net/siteed/audiostream/Constants.kt +0 -20
  18. package/android/src/main/java/net/siteed/audiostream/EventSender.kt +0 -7
  19. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +0 -509
  20. package/android/src/main/java/net/siteed/audiostream/FFT.kt +0 -99
  21. package/android/src/main/java/net/siteed/audiostream/Features.kt +0 -98
  22. package/android/src/main/java/net/siteed/audiostream/NotificationConfig.kt +0 -70
  23. package/android/src/main/java/net/siteed/audiostream/PermissionUtils.kt +0 -59
  24. package/android/src/main/java/net/siteed/audiostream/RecordingActionReceiver.kt +0 -59
  25. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +0 -205
  26. package/android/src/main/java/net/siteed/audiostream/WaveformConfig.kt +0 -19
  27. package/android/src/main/java/net/siteed/audiostream/WaveformRenderer.kt +0 -159
  28. package/android/src/main/res/drawable/ic_default_action_icon.xml +0 -16
  29. package/android/src/main/res/drawable/ic_microphone.xml +0 -13
  30. package/android/src/main/res/drawable/ic_pause.xml +0 -10
  31. package/android/src/main/res/drawable/ic_play.xml +0 -10
  32. package/android/src/main/res/drawable/ic_stop.xml +0 -10
  33. package/android/src/main/res/layout/notification_recording.xml +0 -37
  34. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +0 -56
  35. package/app.plugin.js +0 -1
  36. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +0 -144
  37. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +0 -1
  38. package/build/AudioAnalysis/AudioAnalysis.types.js +0 -3
  39. package/build/AudioAnalysis/AudioAnalysis.types.js.map +0 -1
  40. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +0 -78
  41. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +0 -1
  42. package/build/AudioAnalysis/extractAudioAnalysis.js +0 -229
  43. package/build/AudioAnalysis/extractAudioAnalysis.js.map +0 -1
  44. package/build/AudioAnalysis/extractWaveform.d.ts +0 -8
  45. package/build/AudioAnalysis/extractWaveform.d.ts.map +0 -1
  46. package/build/AudioAnalysis/extractWaveform.js +0 -11
  47. package/build/AudioAnalysis/extractWaveform.js.map +0 -1
  48. package/build/AudioRecorder.provider.d.ts +0 -11
  49. package/build/AudioRecorder.provider.d.ts.map +0 -1
  50. package/build/AudioRecorder.provider.js +0 -37
  51. package/build/AudioRecorder.provider.js.map +0 -1
  52. package/build/ExpoAudioStream.native.d.ts +0 -3
  53. package/build/ExpoAudioStream.native.d.ts.map +0 -1
  54. package/build/ExpoAudioStream.native.js +0 -6
  55. package/build/ExpoAudioStream.native.js.map +0 -1
  56. package/build/ExpoAudioStream.types.d.ts +0 -206
  57. package/build/ExpoAudioStream.types.d.ts.map +0 -1
  58. package/build/ExpoAudioStream.types.js +0 -2
  59. package/build/ExpoAudioStream.types.js.map +0 -1
  60. package/build/ExpoAudioStream.web.d.ts +0 -59
  61. package/build/ExpoAudioStream.web.d.ts.map +0 -1
  62. package/build/ExpoAudioStream.web.js +0 -285
  63. package/build/ExpoAudioStream.web.js.map +0 -1
  64. package/build/ExpoAudioStreamModule.d.ts +0 -3
  65. package/build/ExpoAudioStreamModule.d.ts.map +0 -1
  66. package/build/ExpoAudioStreamModule.js +0 -239
  67. package/build/ExpoAudioStreamModule.js.map +0 -1
  68. package/build/WebRecorder.web.d.ts +0 -119
  69. package/build/WebRecorder.web.d.ts.map +0 -1
  70. package/build/WebRecorder.web.js +0 -436
  71. package/build/WebRecorder.web.js.map +0 -1
  72. package/build/constants.d.ts +0 -11
  73. package/build/constants.d.ts.map +0 -1
  74. package/build/constants.js +0 -14
  75. package/build/constants.js.map +0 -1
  76. package/build/events.d.ts +0 -26
  77. package/build/events.d.ts.map +0 -1
  78. package/build/events.js +0 -21
  79. package/build/events.js.map +0 -1
  80. package/build/index.d.ts.map +0 -1
  81. package/build/index.js.map +0 -1
  82. package/build/useAudioRecorder.d.ts +0 -21
  83. package/build/useAudioRecorder.d.ts.map +0 -1
  84. package/build/useAudioRecorder.js +0 -427
  85. package/build/useAudioRecorder.js.map +0 -1
  86. package/build/utils/BlobFix.d.ts +0 -9
  87. package/build/utils/BlobFix.d.ts.map +0 -1
  88. package/build/utils/BlobFix.js +0 -498
  89. package/build/utils/BlobFix.js.map +0 -1
  90. package/build/utils/audioProcessing.d.ts +0 -24
  91. package/build/utils/audioProcessing.d.ts.map +0 -1
  92. package/build/utils/audioProcessing.js +0 -133
  93. package/build/utils/audioProcessing.js.map +0 -1
  94. package/build/utils/concatenateBuffers.d.ts +0 -8
  95. package/build/utils/concatenateBuffers.d.ts.map +0 -1
  96. package/build/utils/concatenateBuffers.js +0 -21
  97. package/build/utils/concatenateBuffers.js.map +0 -1
  98. package/build/utils/convertPCMToFloat32.d.ts +0 -13
  99. package/build/utils/convertPCMToFloat32.d.ts.map +0 -1
  100. package/build/utils/convertPCMToFloat32.js +0 -120
  101. package/build/utils/convertPCMToFloat32.js.map +0 -1
  102. package/build/utils/encodingToBitDepth.d.ts +0 -5
  103. package/build/utils/encodingToBitDepth.d.ts.map +0 -1
  104. package/build/utils/encodingToBitDepth.js +0 -13
  105. package/build/utils/encodingToBitDepth.js.map +0 -1
  106. package/build/utils/getWavFileInfo.d.ts +0 -26
  107. package/build/utils/getWavFileInfo.d.ts.map +0 -1
  108. package/build/utils/getWavFileInfo.js +0 -92
  109. package/build/utils/getWavFileInfo.js.map +0 -1
  110. package/build/utils/writeWavHeader.d.ts +0 -49
  111. package/build/utils/writeWavHeader.d.ts.map +0 -1
  112. package/build/utils/writeWavHeader.js +0 -91
  113. package/build/utils/writeWavHeader.js.map +0 -1
  114. package/build/workers/InlineFeaturesExtractor.web.d.ts +0 -2
  115. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +0 -1
  116. package/build/workers/InlineFeaturesExtractor.web.js +0 -828
  117. package/build/workers/InlineFeaturesExtractor.web.js.map +0 -1
  118. package/build/workers/inlineAudioWebWorker.web.d.ts +0 -2
  119. package/build/workers/inlineAudioWebWorker.web.d.ts.map +0 -1
  120. package/build/workers/inlineAudioWebWorker.web.js +0 -157
  121. package/build/workers/inlineAudioWebWorker.web.js.map +0 -1
  122. package/expo-module.config.json +0 -9
  123. package/ios/AudioAnalysisData.swift +0 -74
  124. package/ios/AudioNotificationManager.swift +0 -135
  125. package/ios/AudioProcessingHelpers.swift +0 -743
  126. package/ios/AudioProcessor.swift +0 -858
  127. package/ios/AudioStreamError.swift +0 -7
  128. package/ios/AudioStreamManager.swift +0 -1708
  129. package/ios/AudioStreamManagerDelegate.swift +0 -16
  130. package/ios/DataPoint.swift +0 -54
  131. package/ios/DecodingConfig.swift +0 -47
  132. package/ios/ExpoAudioStream.podspec +0 -27
  133. package/ios/ExpoAudioStreamModule.swift +0 -698
  134. package/ios/FFT.swift +0 -62
  135. package/ios/Features.swift +0 -95
  136. package/ios/Logger.swift +0 -7
  137. package/ios/NotificationExtension.swift +0 -15
  138. package/ios/RecordingResult.swift +0 -22
  139. package/ios/RecordingSettings.swift +0 -265
  140. package/ios/WaveformExtractor.swift +0 -105
  141. package/plugin/build/index.d.ts +0 -21
  142. package/plugin/build/index.js +0 -191
  143. package/plugin/src/index.ts +0 -278
  144. package/plugin/tsconfig.json +0 -10
  145. package/plugin/tsconfig.tsbuildinfo +0 -1
  146. package/src/AudioAnalysis/AudioAnalysis.types.ts +0 -165
  147. package/src/AudioAnalysis/extractAudioAnalysis.ts +0 -370
  148. package/src/AudioAnalysis/extractWaveform.ts +0 -22
  149. package/src/AudioRecorder.provider.tsx +0 -54
  150. package/src/ExpoAudioStream.native.ts +0 -6
  151. package/src/ExpoAudioStream.types.ts +0 -329
  152. package/src/ExpoAudioStream.web.ts +0 -359
  153. package/src/ExpoAudioStreamModule.ts +0 -286
  154. package/src/WebRecorder.web.ts +0 -580
  155. package/src/constants.ts +0 -18
  156. package/src/events.ts +0 -60
  157. package/src/useAudioRecorder.tsx +0 -620
  158. package/src/utils/BlobFix.ts +0 -559
  159. package/src/utils/audioProcessing.ts +0 -205
  160. package/src/utils/concatenateBuffers.ts +0 -24
  161. package/src/utils/convertPCMToFloat32.ts +0 -170
  162. package/src/utils/encodingToBitDepth.ts +0 -18
  163. package/src/utils/getWavFileInfo.ts +0 -132
  164. package/src/utils/writeWavHeader.ts +0 -114
  165. package/src/workers/InlineFeaturesExtractor.web.tsx +0 -827
  166. package/src/workers/inlineAudioWebWorker.web.tsx +0 -156
@@ -1,620 +0,0 @@
1
- // src/useAudioRecorder.ts
2
- import { EventSubscription, Platform } from 'expo-modules-core'
3
- import { useCallback, useEffect, useReducer, useRef } from 'react'
4
-
5
- import { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types'
6
- import {
7
- AudioDataEvent,
8
- AudioRecording,
9
- AudioStreamStatus,
10
- CompressionInfo,
11
- ConsoleLike,
12
- RecordingConfig,
13
- StartRecordingResult,
14
- } from './ExpoAudioStream.types'
15
- import ExpoAudioStreamModule from './ExpoAudioStreamModule'
16
- import {
17
- addAudioAnalysisListener,
18
- addAudioEventListener,
19
- AudioEventPayload,
20
- addRecordingInterruptionListener,
21
- } from './events'
22
-
23
- export interface UseAudioRecorderProps {
24
- logger?: ConsoleLike
25
- audioWorkletUrl?: string
26
- featuresExtratorUrl?: string
27
- }
28
-
29
- export interface UseAudioRecorderState {
30
- startRecording: (_: RecordingConfig) => Promise<StartRecordingResult>
31
- stopRecording: () => Promise<AudioRecording>
32
- pauseRecording: () => Promise<void>
33
- resumeRecording: () => Promise<void>
34
- isRecording: boolean
35
- isPaused: boolean
36
- durationMs: number
37
- size: number
38
- compression?: CompressionInfo
39
- analysisData?: AudioAnalysis
40
- }
41
-
42
- interface RecorderReducerState {
43
- isRecording: boolean
44
- isPaused: boolean
45
- durationMs: number
46
- size: number
47
- compression?: CompressionInfo
48
- analysisData?: AudioAnalysis
49
- }
50
-
51
- type RecorderAction =
52
- | { type: 'START' | 'STOP' | 'PAUSE' | 'RESUME' }
53
- | {
54
- type: 'UPDATE_RECORDING_STATE'
55
- payload: {
56
- isRecording: boolean
57
- isPaused: boolean
58
- }
59
- }
60
- | {
61
- type: 'UPDATE_STATUS'
62
- payload: {
63
- durationMs: number
64
- size: number
65
- compression?: CompressionInfo
66
- }
67
- }
68
- | { type: 'UPDATE_ANALYSIS'; payload: AudioAnalysis }
69
-
70
- const defaultAnalysis: AudioAnalysis = {
71
- segmentDurationMs: 100,
72
- bitDepth: 32,
73
- numberOfChannels: 1,
74
- durationMs: 0,
75
- sampleRate: 44100,
76
- samples: 0,
77
- dataPoints: [],
78
- rmsRange: {
79
- min: Number.POSITIVE_INFINITY,
80
- max: Number.NEGATIVE_INFINITY,
81
- },
82
- amplitudeRange: {
83
- min: Number.POSITIVE_INFINITY,
84
- max: Number.NEGATIVE_INFINITY,
85
- },
86
- }
87
-
88
- function audioRecorderReducer(
89
- state: RecorderReducerState,
90
- action: RecorderAction
91
- ): 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
- compression: undefined,
101
- analysisData: defaultAnalysis,
102
- }
103
- case 'STOP':
104
- return {
105
- ...state,
106
- isRecording: false,
107
- isPaused: false,
108
- durationMs: 0,
109
- size: 0,
110
- compression: undefined,
111
- analysisData: undefined,
112
- }
113
- case 'PAUSE':
114
- return { ...state, isPaused: true, isRecording: false }
115
- case 'RESUME':
116
- return { ...state, isPaused: false, isRecording: true }
117
- case 'UPDATE_RECORDING_STATE':
118
- return {
119
- ...state,
120
- isPaused: action.payload.isPaused,
121
- isRecording: action.payload.isRecording,
122
- }
123
- case 'UPDATE_STATUS': {
124
- const newState = {
125
- ...state,
126
- durationMs: action.payload.durationMs,
127
- size: action.payload.size,
128
- compression: action.payload.compression
129
- ? {
130
- size: action.payload.compression.size,
131
- mimeType: action.payload.compression.mimeType,
132
- bitrate: action.payload.compression.bitrate,
133
- format: action.payload.compression.format,
134
- }
135
- : undefined,
136
- }
137
- return newState
138
- }
139
- case 'UPDATE_ANALYSIS':
140
- return {
141
- ...state,
142
- analysisData: action.payload,
143
- }
144
- default:
145
- return state
146
- }
147
- }
148
-
149
- interface HandleAudioAnalysisProps {
150
- analysis: AudioAnalysis
151
- visualizationDuration: number
152
- }
153
-
154
- export function useAudioRecorder({
155
- logger,
156
- audioWorkletUrl,
157
- featuresExtratorUrl,
158
- }: UseAudioRecorderProps = {}): UseAudioRecorderState {
159
- const [state, dispatch] = useReducer(audioRecorderReducer, {
160
- isRecording: false,
161
- isPaused: false,
162
- durationMs: 0,
163
- size: 0,
164
- compression: undefined,
165
- analysisData: undefined,
166
- })
167
-
168
- const startResultRef = useRef<StartRecordingResult | null>(null)
169
-
170
- const analysisListenerRef = useRef<EventSubscription | null>(null)
171
- // analysisRef is the current analysis data (last 10 seconds by default)
172
- const analysisRef = useRef<AudioAnalysis>({ ...defaultAnalysis })
173
- // fullAnalysisRef is the full analysis data (all data points)
174
- const fullAnalysisRef = useRef<AudioAnalysis>({
175
- ...defaultAnalysis,
176
- })
177
-
178
- // Instantiate the module for web with URLs
179
- const ExpoAudioStream =
180
- Platform.OS === 'web'
181
- ? ExpoAudioStreamModule({
182
- audioWorkletUrl,
183
- featuresExtratorUrl,
184
- logger,
185
- })
186
- : ExpoAudioStreamModule
187
-
188
- const onAudioStreamRef = useRef<
189
- ((_: AudioDataEvent) => Promise<void>) | null
190
- >(null)
191
-
192
- const stateRef = useRef({
193
- isRecording: false,
194
- isPaused: false,
195
- durationMs: 0,
196
- size: 0,
197
- compression: undefined as CompressionInfo | undefined,
198
- })
199
-
200
- const recordingConfigRef = useRef<RecordingConfig | null>(null)
201
-
202
- const handleAudioAnalysis = useCallback(
203
- async ({
204
- analysis,
205
- visualizationDuration,
206
- }: HandleAudioAnalysisProps) => {
207
- const savedAnalysisData = analysisRef.current || {
208
- ...defaultAnalysis,
209
- }
210
-
211
- const maxDuration = visualizationDuration
212
-
213
- logger?.debug(
214
- `[handleAudioAnalysis] Received audio analysis: maxDuration=${maxDuration} analysis.dataPoints=${analysis.dataPoints.length} analysisData.dataPoints=${savedAnalysisData.dataPoints.length}`,
215
- analysis
216
- )
217
-
218
- // Combine data points
219
- const combinedDataPoints = [
220
- ...savedAnalysisData.dataPoints,
221
- ...analysis.dataPoints,
222
- ]
223
-
224
- const fullCombinedDataPoints = [
225
- ...(fullAnalysisRef.current?.dataPoints ?? []),
226
- ...analysis.dataPoints,
227
- ]
228
-
229
- // Calculate the new duration
230
- // The number of segments is based on how many segments of segmentDurationMs can fit in visualizationDuration
231
- const numberOfSegments = Math.ceil(
232
- visualizationDuration / analysis.segmentDurationMs
233
- )
234
- // maxDataPoints should be the number of data points, not milliseconds
235
- const maxDataPoints = numberOfSegments
236
-
237
- logger?.debug(
238
- `[handleAudioAnalysis] Combined data points before trimming: numberOfSegments=${numberOfSegments} visualizationDuration=${visualizationDuration} combinedDataPointsLength=${combinedDataPoints.length} vs maxDataPoints=${maxDataPoints}`
239
- )
240
-
241
- // Trim data points to keep within the maximum number of data points
242
- if (combinedDataPoints.length > maxDataPoints) {
243
- combinedDataPoints.splice(
244
- 0,
245
- combinedDataPoints.length - maxDataPoints
246
- )
247
- }
248
-
249
- // Keep the full data points
250
- fullAnalysisRef.current = {
251
- ...fullAnalysisRef.current,
252
- dataPoints: fullCombinedDataPoints,
253
- }
254
- fullAnalysisRef.current.durationMs =
255
- fullCombinedDataPoints.length * analysis.segmentDurationMs
256
- savedAnalysisData.dataPoints = combinedDataPoints
257
- savedAnalysisData.bitDepth =
258
- analysis.bitDepth || savedAnalysisData.bitDepth
259
- savedAnalysisData.durationMs =
260
- combinedDataPoints.length * analysis.segmentDurationMs
261
-
262
- // Update amplitude range
263
- const newMin = Math.min(
264
- savedAnalysisData.amplitudeRange.min,
265
- analysis.amplitudeRange.min
266
- )
267
- const newMax = Math.max(
268
- savedAnalysisData.amplitudeRange.max,
269
- analysis.amplitudeRange.max
270
- )
271
-
272
- savedAnalysisData.amplitudeRange = {
273
- min: newMin,
274
- max: newMax,
275
- }
276
- fullAnalysisRef.current.amplitudeRange = {
277
- min: newMin,
278
- max: newMax,
279
- }
280
-
281
- logger?.debug(
282
- `[handleAudioAnalysis] Updated analysis data: durationMs=${savedAnalysisData.durationMs}`,
283
- savedAnalysisData
284
- )
285
-
286
- // Call the onAudioAnalysis callback if it exists in the recording config
287
- if (recordingConfigRef.current?.onAudioAnalysis) {
288
- recordingConfigRef.current
289
- .onAudioAnalysis(analysis)
290
- .catch((error) => {
291
- logger?.warn(`Error processing audio analysis:`, error)
292
- })
293
- }
294
-
295
- // Update the ref
296
- analysisRef.current = savedAnalysisData
297
-
298
- // Dispatch the updated analysis data to state to trigger re-render
299
- dispatch({
300
- type: 'UPDATE_ANALYSIS',
301
- payload: { ...savedAnalysisData },
302
- })
303
- },
304
- [dispatch]
305
- )
306
-
307
- const handleAudioEvent = useCallback(
308
- async (eventData: AudioEventPayload) => {
309
- const {
310
- fileUri,
311
- deltaSize,
312
- totalSize,
313
- lastEmittedSize,
314
- position,
315
- streamUuid,
316
- encoded,
317
- mimeType,
318
- buffer,
319
- compression,
320
- } = eventData
321
- logger?.debug(`[handleAudioEvent] Received audio event:`, {
322
- fileUri,
323
- deltaSize,
324
- totalSize,
325
- position,
326
- mimeType,
327
- lastEmittedSize,
328
- streamUuid,
329
- encodedLength: encoded?.length,
330
- compression,
331
- })
332
- if (deltaSize === 0) {
333
- // Ignore packet with no data
334
- return
335
- }
336
- try {
337
- // Coming from native ( ios / android ) otherwise buffer is set
338
- if (Platform.OS !== 'web') {
339
- // Read the audio file as a base64 string for comparison
340
- if (!encoded) {
341
- logger?.error(`Encoded audio data is missing`)
342
- throw new Error('Encoded audio data is missing')
343
- }
344
- onAudioStreamRef.current?.({
345
- data: encoded,
346
- position,
347
- fileUri,
348
- eventDataSize: deltaSize,
349
- totalSize,
350
- compression:
351
- compression && startResultRef.current?.compression
352
- ? {
353
- data: compression.data,
354
- size: compression.totalSize,
355
- mimeType:
356
- startResultRef.current.compression
357
- ?.mimeType,
358
- bitrate:
359
- startResultRef.current.compression
360
- ?.bitrate,
361
- format: startResultRef.current.compression
362
- ?.format,
363
- }
364
- : undefined,
365
- })
366
- } else if (buffer) {
367
- // Coming from web
368
- const webEvent: AudioDataEvent = {
369
- data: buffer,
370
- position,
371
- fileUri,
372
- eventDataSize: deltaSize,
373
- totalSize,
374
- compression:
375
- compression && startResultRef.current?.compression
376
- ? {
377
- data: compression.data,
378
- size: compression.totalSize,
379
- mimeType:
380
- startResultRef.current.compression
381
- ?.mimeType,
382
- bitrate:
383
- startResultRef.current.compression
384
- ?.bitrate,
385
- format: startResultRef.current.compression
386
- ?.format,
387
- }
388
- : undefined,
389
- }
390
- onAudioStreamRef.current?.(webEvent)
391
- logger?.debug(
392
- `[handleAudioEvent] Audio data sent to onAudioStream`,
393
- webEvent
394
- )
395
- }
396
- } catch (error) {
397
- logger?.error(`Error processing audio event:`, error)
398
- }
399
- },
400
- []
401
- )
402
-
403
- const checkStatus = useCallback(async () => {
404
- try {
405
- const status: AudioStreamStatus = ExpoAudioStream.status()
406
- logger?.debug(
407
- `Status: paused: ${status.isPaused} isRecording: ${status.isRecording} durationMs: ${status.durationMs} size: ${status.size}`,
408
- status.compression
409
- )
410
-
411
- // Only dispatch if values actually changed
412
- if (
413
- status.isRecording !== stateRef.current.isRecording ||
414
- status.isPaused !== stateRef.current.isPaused
415
- ) {
416
- stateRef.current.isRecording = status.isRecording
417
- stateRef.current.isPaused = status.isPaused
418
- dispatch({
419
- type: 'UPDATE_RECORDING_STATE',
420
- payload: {
421
- isRecording: status.isRecording,
422
- isPaused: status.isPaused,
423
- },
424
- })
425
- }
426
-
427
- if (
428
- status.durationMs !== stateRef.current.durationMs ||
429
- status.size !== stateRef.current.size
430
- ) {
431
- stateRef.current.durationMs = status.durationMs
432
- stateRef.current.size = status.size
433
- stateRef.current.compression = status.compression
434
- dispatch({
435
- type: 'UPDATE_STATUS',
436
- payload: {
437
- durationMs: status.durationMs,
438
- size: status.size,
439
- compression: status.compression,
440
- },
441
- })
442
- }
443
- } catch (error) {
444
- logger?.error(`Error getting status:`, error)
445
- }
446
- }, [ExpoAudioStream, logger]) // Only depend on ExpoAudioStream and logger
447
-
448
- // Update ref when state changes
449
- useEffect(() => {
450
- stateRef.current = {
451
- isRecording: state.isRecording,
452
- isPaused: state.isPaused,
453
- durationMs: state.durationMs,
454
- size: state.size,
455
- compression: state.compression,
456
- }
457
- }, [
458
- state.isRecording,
459
- state.isPaused,
460
- state.durationMs,
461
- state.size,
462
- state.compression,
463
- ])
464
-
465
- const startRecording = useCallback(
466
- async (recordingOptions: RecordingConfig) => {
467
- recordingConfigRef.current = recordingOptions
468
- logger?.debug(`start recoding`, recordingOptions)
469
-
470
- analysisRef.current = { ...defaultAnalysis } // Reset analysis data
471
- fullAnalysisRef.current = { ...defaultAnalysis }
472
- const { onAudioStream, ...options } = recordingOptions
473
- const { enableProcessing } = options
474
-
475
- const maxRecentDataDuration = 10000 // TODO compute maxRecentDataDuration based on screen dimensions
476
- if (typeof onAudioStream === 'function') {
477
- onAudioStreamRef.current = onAudioStream
478
- } else {
479
- logger?.warn(`onAudioStream is not a function`, onAudioStream)
480
- onAudioStreamRef.current = null
481
- }
482
- const startResult: StartRecordingResult =
483
- await ExpoAudioStream.startRecording(options)
484
- dispatch({ type: 'START' })
485
-
486
- startResultRef.current = startResult
487
-
488
- if (enableProcessing) {
489
- logger?.debug(`Enabling audio analysis listener`)
490
- const listener = addAudioAnalysisListener(
491
- async (analysisData) => {
492
- try {
493
- await handleAudioAnalysis({
494
- analysis: analysisData,
495
- visualizationDuration: maxRecentDataDuration,
496
- })
497
- } catch (error) {
498
- logger?.warn(
499
- `Error processing audio analysis:`,
500
- error
501
- )
502
- }
503
- }
504
- )
505
-
506
- analysisListenerRef.current = listener
507
- }
508
-
509
- return startResult
510
- },
511
- [handleAudioAnalysis, dispatch]
512
- )
513
-
514
- const stopRecording = useCallback(async () => {
515
- logger?.debug(`stoping recording`)
516
-
517
- const stopResult: AudioRecording = await ExpoAudioStream.stopRecording()
518
- stopResult.analysisData = fullAnalysisRef.current
519
-
520
- if (analysisListenerRef.current) {
521
- analysisListenerRef.current.remove()
522
- analysisListenerRef.current = null
523
- }
524
- onAudioStreamRef.current = null
525
- logger?.debug(`recording stopped`, stopResult)
526
- dispatch({ type: 'STOP' })
527
- return stopResult
528
- }, [dispatch])
529
-
530
- const pauseRecording = useCallback(async () => {
531
- logger?.debug(`pause recording`)
532
- const pauseResult = await ExpoAudioStream.pauseRecording()
533
- dispatch({ type: 'PAUSE' })
534
- return pauseResult
535
- }, [dispatch])
536
-
537
- const resumeRecording = useCallback(async () => {
538
- logger?.debug(`resume recording`)
539
- const resumeResult = await ExpoAudioStream.resumeRecording()
540
- dispatch({ type: 'RESUME' })
541
- return resumeResult
542
- }, [dispatch])
543
-
544
- useEffect(() => {
545
- let intervalId: NodeJS.Timeout | undefined
546
-
547
- if (state.isRecording || state.isPaused) {
548
- // Immediately check status when starting
549
- checkStatus()
550
-
551
- // Start interval
552
- intervalId = setInterval(checkStatus, 1000)
553
- }
554
-
555
- return () => {
556
- if (intervalId) {
557
- clearInterval(intervalId)
558
- intervalId = undefined
559
- }
560
- }
561
- }, [checkStatus, state.isRecording, state.isPaused])
562
-
563
- useEffect(() => {
564
- logger?.debug(`Registering audio event listener`)
565
- const subscribeAudio = addAudioEventListener(handleAudioEvent)
566
-
567
- logger?.debug(
568
- `Subscribed to audio event listener and analysis listener`,
569
- {
570
- subscribeAudio,
571
- }
572
- )
573
-
574
- return () => {
575
- logger?.debug(`Removing audio event listener`)
576
- subscribeAudio.remove()
577
- }
578
- }, [handleAudioEvent, handleAudioAnalysis])
579
-
580
- useEffect(() => {
581
- // Add event subscription for recording interruptions
582
- logger?.debug('Setting up recording interruption listener')
583
-
584
- const subscription = addRecordingInterruptionListener((event) => {
585
- logger?.debug('Received recording interruption event:', event)
586
-
587
- // Check if we have a callback configured
588
- if (recordingConfigRef.current?.onRecordingInterrupted) {
589
- try {
590
- recordingConfigRef.current.onRecordingInterrupted(event)
591
- } catch (error) {
592
- logger?.error(
593
- 'Error in recording interruption callback:',
594
- error
595
- )
596
- }
597
- } else {
598
- logger?.warn('No recording interruption callback configured')
599
- }
600
- })
601
-
602
- return () => {
603
- logger?.debug('Removing recording interruption listener')
604
- subscription.remove()
605
- }
606
- }, []) // Empty dependency array since we want this to run once
607
-
608
- return {
609
- startRecording,
610
- stopRecording,
611
- pauseRecording,
612
- resumeRecording,
613
- isPaused: state.isPaused,
614
- isRecording: state.isRecording,
615
- durationMs: state.durationMs,
616
- size: state.size,
617
- compression: state.compression,
618
- analysisData: state.analysisData,
619
- }
620
- }