@siteed/expo-audio-stream 1.0.3 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -175
- package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +47 -7
- package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +1 -0
- package/android/src/main/java/net/siteed/audiostream/Constants.kt +5 -0
- package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +12 -3
- package/build/index.js +8 -7
- package/ios/AudioProcessor.swift +7 -5
- package/ios/AudioStreamManager.swift +1 -0
- package/ios/ExpoAudioStream.podspec +1 -1
- package/ios/ExpoAudioStreamModule.swift +36 -0
- package/ios/RecordingResult.swift +1 -0
- package/package.json +95 -65
- package/src/AudioAnalysis/AudioAnalysis.types.ts +59 -60
- package/src/AudioAnalysis/extractAudioAnalysis.ts +132 -121
- package/src/AudioAnalysis/extractWaveform.ts +18 -18
- package/src/AudioRecorder.provider.tsx +53 -53
- package/src/ExpoAudioStream.native.ts +2 -2
- package/src/ExpoAudioStream.types.ts +59 -53
- package/src/ExpoAudioStream.web.ts +231 -205
- package/src/ExpoAudioStreamModule.ts +22 -15
- package/src/WebRecorder.web.ts +407 -390
- package/src/constants.ts +11 -11
- package/src/events.ts +27 -13
- package/src/index.ts +17 -15
- package/src/logger.ts +15 -19
- package/src/useAudioRecorder.tsx +394 -389
- package/src/utils/BlobFix.ts +550 -0
- package/src/utils/concatenateBuffers.ts +24 -0
- package/src/utils/convertPCMToFloat32.ts +72 -45
- package/src/utils/encodingToBitDepth.ts +14 -14
- package/src/utils/getWavFileInfo.ts +106 -99
- package/src/utils/writeWavHeader.ts +50 -45
- package/src/workers/InlineFeaturesExtractor.web.tsx +296 -286
- package/src/workers/inlineAudioWebWorker.web.tsx +230 -222
- package/.eslintrc.js +0 -2
- package/.size-limit.json +0 -6
- package/android/.gradle/8.1.1/checksums/checksums.lock +0 -0
- package/android/.gradle/8.1.1/dependencies-accessors/dependencies-accessors.lock +0 -0
- package/android/.gradle/8.1.1/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/8.1.1/fileChanges/last-build.bin +0 -0
- package/android/.gradle/8.1.1/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/8.1.1/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +0 -2
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/app.plugin.js +0 -1
- package/build/AudioAnalysis/AudioAnalysis.types.d.ts +0 -76
- package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +0 -1
- package/build/AudioAnalysis/AudioAnalysis.types.js +0 -3
- package/build/AudioAnalysis/AudioAnalysis.types.js.map +0 -1
- package/build/AudioAnalysis/extractAudioAnalysis.d.ts +0 -4
- package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +0 -1
- package/build/AudioAnalysis/extractAudioAnalysis.js +0 -101
- package/build/AudioAnalysis/extractAudioAnalysis.js.map +0 -1
- package/build/AudioAnalysis/extractWaveform.d.ts +0 -8
- package/build/AudioAnalysis/extractWaveform.d.ts.map +0 -1
- package/build/AudioAnalysis/extractWaveform.js +0 -14
- package/build/AudioAnalysis/extractWaveform.js.map +0 -1
- package/build/AudioRecorder.provider.d.ts +0 -23
- package/build/AudioRecorder.provider.d.ts.map +0 -1
- package/build/AudioRecorder.provider.js +0 -36
- package/build/AudioRecorder.provider.js.map +0 -1
- package/build/ExpoAudioStream.native.d.ts +0 -3
- package/build/ExpoAudioStream.native.d.ts.map +0 -1
- package/build/ExpoAudioStream.native.js +0 -6
- package/build/ExpoAudioStream.native.js.map +0 -1
- package/build/ExpoAudioStream.types.d.ts +0 -60
- package/build/ExpoAudioStream.types.d.ts.map +0 -1
- package/build/ExpoAudioStream.types.js +0 -2
- package/build/ExpoAudioStream.types.js.map +0 -1
- package/build/ExpoAudioStream.web.d.ts +0 -42
- package/build/ExpoAudioStream.web.d.ts.map +0 -1
- package/build/ExpoAudioStream.web.js +0 -185
- package/build/ExpoAudioStream.web.js.map +0 -1
- package/build/ExpoAudioStreamModule.d.ts +0 -3
- package/build/ExpoAudioStreamModule.d.ts.map +0 -1
- package/build/ExpoAudioStreamModule.js +0 -18
- package/build/ExpoAudioStreamModule.js.map +0 -1
- package/build/WebRecorder.web.d.ts +0 -51
- package/build/WebRecorder.web.d.ts.map +0 -1
- package/build/WebRecorder.web.js +0 -288
- package/build/WebRecorder.web.js.map +0 -1
- package/build/constants.d.ts +0 -11
- package/build/constants.d.ts.map +0 -1
- package/build/constants.js +0 -14
- package/build/constants.js.map +0 -1
- package/build/events.d.ts +0 -6
- package/build/events.d.ts.map +0 -1
- package/build/events.js +0 -15
- package/build/events.js.map +0 -1
- package/build/index.d.ts +0 -10
- package/build/index.d.ts.map +0 -1
- package/build/index.js.map +0 -1
- package/build/logger.d.ts +0 -9
- package/build/logger.d.ts.map +0 -1
- package/build/logger.js +0 -17
- package/build/logger.js.map +0 -1
- package/build/useAudioRecorder.d.ts +0 -37
- package/build/useAudioRecorder.d.ts.map +0 -1
- package/build/useAudioRecorder.js +0 -271
- package/build/useAudioRecorder.js.map +0 -1
- package/build/utils/convertPCMToFloat32.d.ts +0 -11
- package/build/utils/convertPCMToFloat32.d.ts.map +0 -1
- package/build/utils/convertPCMToFloat32.js +0 -41
- package/build/utils/convertPCMToFloat32.js.map +0 -1
- package/build/utils/encodingToBitDepth.d.ts +0 -5
- package/build/utils/encodingToBitDepth.d.ts.map +0 -1
- package/build/utils/encodingToBitDepth.js +0 -13
- package/build/utils/encodingToBitDepth.js.map +0 -1
- package/build/utils/getWavFileInfo.d.ts +0 -25
- package/build/utils/getWavFileInfo.d.ts.map +0 -1
- package/build/utils/getWavFileInfo.js +0 -89
- package/build/utils/getWavFileInfo.js.map +0 -1
- package/build/utils/writeWavHeader.d.ts +0 -9
- package/build/utils/writeWavHeader.d.ts.map +0 -1
- package/build/utils/writeWavHeader.js +0 -41
- package/build/utils/writeWavHeader.js.map +0 -1
- package/build/workers/InlineFeaturesExtractor.web.d.ts +0 -2
- package/build/workers/InlineFeaturesExtractor.web.d.ts.map +0 -1
- package/build/workers/InlineFeaturesExtractor.web.js +0 -303
- package/build/workers/InlineFeaturesExtractor.web.js.map +0 -1
- package/build/workers/inlineAudioWebWorker.web.d.ts +0 -2
- package/build/workers/inlineAudioWebWorker.web.d.ts.map +0 -1
- package/build/workers/inlineAudioWebWorker.web.js +0 -243
- package/build/workers/inlineAudioWebWorker.web.js.map +0 -1
- package/expo-module.config.json +0 -18
- package/plugin/build/index.d.ts +0 -5
- package/plugin/build/index.js +0 -28
- package/plugin/src/index.ts +0 -53
- package/plugin/tsconfig.json +0 -14
- package/publish.sh +0 -8
- package/tsconfig.json +0 -9
package/src/useAudioRecorder.tsx
CHANGED
|
@@ -1,415 +1,420 @@
|
|
|
1
1
|
// src/useAudioRecorder.ts
|
|
2
|
-
import { Platform, Subscription } from
|
|
3
|
-
import { useCallback, useEffect, useReducer, useRef } from
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
AudioDataEvent,
|
|
8
|
+
AudioRecording,
|
|
9
|
+
AudioStreamStatus,
|
|
10
|
+
RecordingConfig,
|
|
11
|
+
StartRecordingResult,
|
|
12
|
+
} from './ExpoAudioStream.types'
|
|
13
|
+
import ExpoAudioStreamModule from './ExpoAudioStreamModule'
|
|
10
14
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
24
|
+
debug?: boolean
|
|
25
|
+
audioWorkletUrl?: string
|
|
26
|
+
featuresExtratorUrl?: string
|
|
47
27
|
}
|
|
48
28
|
|
|
49
29
|
export interface UseAudioRecorderState {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
42
|
+
isRecording: boolean
|
|
43
|
+
isPaused: boolean
|
|
44
|
+
durationMs: number
|
|
45
|
+
size: number
|
|
46
|
+
analysisData?: AudioAnalysis
|
|
67
47
|
}
|
|
68
48
|
|
|
69
49
|
type RecorderAction =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const defaultAnalysis:
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
90
|
-
|
|
71
|
+
state: RecorderReducerState,
|
|
72
|
+
action: RecorderAction
|
|
91
73
|
): RecorderReducerState {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
112
|
+
debug = false,
|
|
113
|
+
audioWorkletUrl,
|
|
114
|
+
featuresExtratorUrl,
|
|
128
115
|
}: UseAudioRecorderProps = {}): UseAudioRecorderState {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
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
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
392
|
+
useEffect(() => {
|
|
393
|
+
logger.debug(`Registering audio event listener`)
|
|
394
|
+
const subscribeAudio = addAudioEventListener(handleAudioEvent)
|
|
347
395
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
}
|