@siteed/expo-audio-stream 2.1.0 → 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 (187) hide show
  1. package/README.md +40 -222
  2. package/build/index.d.ts +11 -15
  3. package/build/index.js +44 -14
  4. package/package.json +49 -110
  5. package/src/index.ts +18 -32
  6. package/CHANGELOG.md +0 -206
  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 -2235
  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 -152
  17. package/android/src/main/java/net/siteed/audiostream/AudioTrimmer.kt +0 -1099
  18. package/android/src/main/java/net/siteed/audiostream/Constants.kt +0 -21
  19. package/android/src/main/java/net/siteed/audiostream/EventSender.kt +0 -7
  20. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +0 -739
  21. package/android/src/main/java/net/siteed/audiostream/FFT.kt +0 -99
  22. package/android/src/main/java/net/siteed/audiostream/Features.kt +0 -98
  23. package/android/src/main/java/net/siteed/audiostream/NotificationConfig.kt +0 -70
  24. package/android/src/main/java/net/siteed/audiostream/PermissionUtils.kt +0 -59
  25. package/android/src/main/java/net/siteed/audiostream/RecordingActionReceiver.kt +0 -59
  26. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +0 -205
  27. package/android/src/main/java/net/siteed/audiostream/WaveformConfig.kt +0 -19
  28. package/android/src/main/java/net/siteed/audiostream/WaveformRenderer.kt +0 -159
  29. package/android/src/main/res/drawable/ic_default_action_icon.xml +0 -16
  30. package/android/src/main/res/drawable/ic_microphone.xml +0 -13
  31. package/android/src/main/res/drawable/ic_pause.xml +0 -10
  32. package/android/src/main/res/drawable/ic_play.xml +0 -10
  33. package/android/src/main/res/drawable/ic_stop.xml +0 -10
  34. package/android/src/main/res/layout/notification_recording.xml +0 -37
  35. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +0 -56
  36. package/app.plugin.js +0 -1
  37. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +0 -179
  38. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +0 -1
  39. package/build/AudioAnalysis/AudioAnalysis.types.js +0 -3
  40. package/build/AudioAnalysis/AudioAnalysis.types.js.map +0 -1
  41. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +0 -68
  42. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +0 -1
  43. package/build/AudioAnalysis/extractAudioAnalysis.js +0 -203
  44. package/build/AudioAnalysis/extractAudioAnalysis.js.map +0 -1
  45. package/build/AudioAnalysis/extractAudioData.d.ts +0 -3
  46. package/build/AudioAnalysis/extractAudioData.d.ts.map +0 -1
  47. package/build/AudioAnalysis/extractAudioData.js +0 -5
  48. package/build/AudioAnalysis/extractAudioData.js.map +0 -1
  49. package/build/AudioAnalysis/extractMelSpectrogram.d.ts +0 -14
  50. package/build/AudioAnalysis/extractMelSpectrogram.d.ts.map +0 -1
  51. package/build/AudioAnalysis/extractMelSpectrogram.js +0 -85
  52. package/build/AudioAnalysis/extractMelSpectrogram.js.map +0 -1
  53. package/build/AudioAnalysis/extractPreview.d.ts +0 -11
  54. package/build/AudioAnalysis/extractPreview.d.ts.map +0 -1
  55. package/build/AudioAnalysis/extractPreview.js +0 -25
  56. package/build/AudioAnalysis/extractPreview.js.map +0 -1
  57. package/build/AudioAnalysis/extractWaveform.d.ts +0 -8
  58. package/build/AudioAnalysis/extractWaveform.d.ts.map +0 -1
  59. package/build/AudioAnalysis/extractWaveform.js +0 -11
  60. package/build/AudioAnalysis/extractWaveform.js.map +0 -1
  61. package/build/AudioRecorder.provider.d.ts +0 -11
  62. package/build/AudioRecorder.provider.d.ts.map +0 -1
  63. package/build/AudioRecorder.provider.js +0 -37
  64. package/build/AudioRecorder.provider.js.map +0 -1
  65. package/build/ExpoAudioStream.native.d.ts +0 -3
  66. package/build/ExpoAudioStream.native.d.ts.map +0 -1
  67. package/build/ExpoAudioStream.native.js +0 -6
  68. package/build/ExpoAudioStream.native.js.map +0 -1
  69. package/build/ExpoAudioStream.types.d.ts +0 -532
  70. package/build/ExpoAudioStream.types.d.ts.map +0 -1
  71. package/build/ExpoAudioStream.types.js +0 -2
  72. package/build/ExpoAudioStream.types.js.map +0 -1
  73. package/build/ExpoAudioStream.web.d.ts +0 -59
  74. package/build/ExpoAudioStream.web.d.ts.map +0 -1
  75. package/build/ExpoAudioStream.web.js +0 -285
  76. package/build/ExpoAudioStream.web.js.map +0 -1
  77. package/build/ExpoAudioStreamModule.d.ts +0 -3
  78. package/build/ExpoAudioStreamModule.d.ts.map +0 -1
  79. package/build/ExpoAudioStreamModule.js +0 -693
  80. package/build/ExpoAudioStreamModule.js.map +0 -1
  81. package/build/WebRecorder.web.d.ts +0 -119
  82. package/build/WebRecorder.web.d.ts.map +0 -1
  83. package/build/WebRecorder.web.js +0 -436
  84. package/build/WebRecorder.web.js.map +0 -1
  85. package/build/constants.d.ts +0 -11
  86. package/build/constants.d.ts.map +0 -1
  87. package/build/constants.js +0 -14
  88. package/build/constants.js.map +0 -1
  89. package/build/events.d.ts +0 -26
  90. package/build/events.d.ts.map +0 -1
  91. package/build/events.js +0 -21
  92. package/build/events.js.map +0 -1
  93. package/build/index.d.ts.map +0 -1
  94. package/build/index.js.map +0 -1
  95. package/build/trimAudio.d.ts +0 -25
  96. package/build/trimAudio.d.ts.map +0 -1
  97. package/build/trimAudio.js +0 -67
  98. package/build/trimAudio.js.map +0 -1
  99. package/build/useAudioRecorder.d.ts +0 -21
  100. package/build/useAudioRecorder.d.ts.map +0 -1
  101. package/build/useAudioRecorder.js +0 -427
  102. package/build/useAudioRecorder.js.map +0 -1
  103. package/build/utils/BlobFix.d.ts +0 -9
  104. package/build/utils/BlobFix.d.ts.map +0 -1
  105. package/build/utils/BlobFix.js +0 -498
  106. package/build/utils/BlobFix.js.map +0 -1
  107. package/build/utils/audioProcessing.d.ts +0 -24
  108. package/build/utils/audioProcessing.d.ts.map +0 -1
  109. package/build/utils/audioProcessing.js +0 -133
  110. package/build/utils/audioProcessing.js.map +0 -1
  111. package/build/utils/concatenateBuffers.d.ts +0 -8
  112. package/build/utils/concatenateBuffers.d.ts.map +0 -1
  113. package/build/utils/concatenateBuffers.js +0 -21
  114. package/build/utils/concatenateBuffers.js.map +0 -1
  115. package/build/utils/convertPCMToFloat32.d.ts +0 -13
  116. package/build/utils/convertPCMToFloat32.d.ts.map +0 -1
  117. package/build/utils/convertPCMToFloat32.js +0 -120
  118. package/build/utils/convertPCMToFloat32.js.map +0 -1
  119. package/build/utils/encodingToBitDepth.d.ts +0 -5
  120. package/build/utils/encodingToBitDepth.d.ts.map +0 -1
  121. package/build/utils/encodingToBitDepth.js +0 -13
  122. package/build/utils/encodingToBitDepth.js.map +0 -1
  123. package/build/utils/getWavFileInfo.d.ts +0 -26
  124. package/build/utils/getWavFileInfo.d.ts.map +0 -1
  125. package/build/utils/getWavFileInfo.js +0 -92
  126. package/build/utils/getWavFileInfo.js.map +0 -1
  127. package/build/utils/writeWavHeader.d.ts +0 -49
  128. package/build/utils/writeWavHeader.d.ts.map +0 -1
  129. package/build/utils/writeWavHeader.js +0 -91
  130. package/build/utils/writeWavHeader.js.map +0 -1
  131. package/build/workers/InlineFeaturesExtractor.web.d.ts +0 -2
  132. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +0 -1
  133. package/build/workers/InlineFeaturesExtractor.web.js +0 -828
  134. package/build/workers/InlineFeaturesExtractor.web.js.map +0 -1
  135. package/build/workers/inlineAudioWebWorker.web.d.ts +0 -2
  136. package/build/workers/inlineAudioWebWorker.web.d.ts.map +0 -1
  137. package/build/workers/inlineAudioWebWorker.web.js +0 -157
  138. package/build/workers/inlineAudioWebWorker.web.js.map +0 -1
  139. package/expo-module.config.json +0 -9
  140. package/ios/AudioAnalysisData.swift +0 -74
  141. package/ios/AudioNotificationManager.swift +0 -135
  142. package/ios/AudioProcessingHelpers.swift +0 -743
  143. package/ios/AudioProcessor.swift +0 -1313
  144. package/ios/AudioStreamError.swift +0 -7
  145. package/ios/AudioStreamManager.swift +0 -1708
  146. package/ios/AudioStreamManagerDelegate.swift +0 -16
  147. package/ios/DataPoint.swift +0 -54
  148. package/ios/DecodingConfig.swift +0 -47
  149. package/ios/ExpoAudioStream.podspec +0 -27
  150. package/ios/ExpoAudioStreamModule.swift +0 -805
  151. package/ios/FFT.swift +0 -62
  152. package/ios/Features.swift +0 -95
  153. package/ios/Logger.swift +0 -7
  154. package/ios/NotificationExtension.swift +0 -15
  155. package/ios/RecordingResult.swift +0 -22
  156. package/ios/RecordingSettings.swift +0 -265
  157. package/ios/WaveformExtractor.swift +0 -105
  158. package/plugin/build/index.d.ts +0 -21
  159. package/plugin/build/index.js +0 -191
  160. package/plugin/src/index.ts +0 -278
  161. package/plugin/tsconfig.json +0 -10
  162. package/plugin/tsconfig.tsbuildinfo +0 -1
  163. package/src/AudioAnalysis/AudioAnalysis.types.ts +0 -202
  164. package/src/AudioAnalysis/extractAudioAnalysis.ts +0 -333
  165. package/src/AudioAnalysis/extractAudioData.ts +0 -6
  166. package/src/AudioAnalysis/extractMelSpectrogram.ts +0 -144
  167. package/src/AudioAnalysis/extractPreview.ts +0 -34
  168. package/src/AudioAnalysis/extractWaveform.ts +0 -22
  169. package/src/AudioRecorder.provider.tsx +0 -54
  170. package/src/ExpoAudioStream.native.ts +0 -6
  171. package/src/ExpoAudioStream.types.ts +0 -641
  172. package/src/ExpoAudioStream.web.ts +0 -359
  173. package/src/ExpoAudioStreamModule.ts +0 -967
  174. package/src/WebRecorder.web.ts +0 -580
  175. package/src/constants.ts +0 -18
  176. package/src/events.ts +0 -60
  177. package/src/trimAudio.ts +0 -90
  178. package/src/useAudioRecorder.tsx +0 -620
  179. package/src/utils/BlobFix.ts +0 -559
  180. package/src/utils/audioProcessing.ts +0 -205
  181. package/src/utils/concatenateBuffers.ts +0 -24
  182. package/src/utils/convertPCMToFloat32.ts +0 -170
  183. package/src/utils/encodingToBitDepth.ts +0 -18
  184. package/src/utils/getWavFileInfo.ts +0 -132
  185. package/src/utils/writeWavHeader.ts +0 -114
  186. package/src/workers/InlineFeaturesExtractor.web.tsx +0 -827
  187. package/src/workers/inlineAudioWebWorker.web.tsx +0 -156
package/src/trimAudio.ts DELETED
@@ -1,90 +0,0 @@
1
- import { LegacyEventEmitter, type EventSubscription } from 'expo-modules-core'
2
-
3
- import {
4
- TrimAudioOptions,
5
- TrimAudioResult,
6
- TrimProgressEvent,
7
- } from './ExpoAudioStream.types'
8
- import ExpoAudioStreamModule from './ExpoAudioStreamModule'
9
-
10
- // Create a single emitter instance
11
- const emitter = new LegacyEventEmitter(ExpoAudioStreamModule)
12
-
13
- /**
14
- * Trims an audio file based on the provided options.
15
- *
16
- * @experimental This API is experimental and not fully optimized for production use.
17
- * Performance may vary based on file size and device capabilities.
18
- * Future versions may include breaking changes.
19
- *
20
- * @param options Configuration options for the trimming operation
21
- * @param progressCallback Optional callback to receive progress updates
22
- * @returns Promise resolving to the trimmed audio file information, including processing time
23
- */
24
- export async function trimAudio(
25
- options: TrimAudioOptions,
26
- progressCallback?: (event: TrimProgressEvent) => void
27
- ): Promise<TrimAudioResult> {
28
- // Validation
29
- if (!options.fileUri) {
30
- throw new Error('fileUri is required')
31
- }
32
- const mode = options.mode ?? 'single'
33
- if (mode === 'single') {
34
- if (
35
- options.startTimeMs === undefined &&
36
- options.endTimeMs === undefined
37
- ) {
38
- throw new Error(
39
- 'At least one of startTimeMs or endTimeMs must be provided in single mode'
40
- )
41
- }
42
- } else if (mode === 'keep' || mode === 'remove') {
43
- if (!options.ranges || options.ranges.length === 0) {
44
- throw new Error(
45
- 'ranges must be provided and non-empty for keep or remove modes'
46
- )
47
- }
48
- } else {
49
- throw new Error(
50
- `Invalid mode: ${mode}. Must be 'single', 'keep', or 'remove'`
51
- )
52
- }
53
-
54
- // Set up progress event listener if callback is provided
55
- let subscription: EventSubscription | undefined
56
- if (progressCallback) {
57
- subscription = emitter.addListener(
58
- 'TrimProgress',
59
- (event: TrimProgressEvent) => {
60
- progressCallback(event)
61
- }
62
- )
63
- }
64
-
65
- try {
66
- const result = await ExpoAudioStreamModule.trimAudio(options)
67
- return result
68
- } finally {
69
- if (subscription) {
70
- subscription.remove()
71
- }
72
- }
73
- }
74
-
75
- /**
76
- * Simplified version of trimAudio that returns only the URI of the trimmed file.
77
- *
78
- * @experimental This API is experimental and not fully optimized for production use.
79
- * Performance may vary based on file size and device capabilities.
80
- * Future versions may include breaking changes.
81
- *
82
- * @param options Configuration options for the trimming operation
83
- * @returns Promise resolving to the URI of the trimmed audio file
84
- */
85
- export async function trimAudioSimple(
86
- options: TrimAudioOptions
87
- ): Promise<string> {
88
- const result = await trimAudio(options)
89
- return result.uri
90
- }
@@ -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
- }