@siteed/expo-audio-stream 1.1.2 → 1.1.3
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/build/AudioAnalysis/AudioAnalysis.types.d.ts +74 -0
- package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
- package/build/AudioAnalysis/AudioAnalysis.types.js +3 -0
- package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
- package/build/AudioAnalysis/extractAudioAnalysis.d.ts +20 -0
- package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
- package/build/AudioAnalysis/extractAudioAnalysis.js +88 -0
- package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
- package/build/AudioAnalysis/extractWaveform.d.ts +8 -0
- package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -0
- package/build/AudioAnalysis/extractWaveform.js +14 -0
- package/build/AudioAnalysis/extractWaveform.js.map +1 -0
- package/build/AudioRecorder.provider.d.ts +23 -0
- package/build/AudioRecorder.provider.d.ts.map +1 -0
- package/build/AudioRecorder.provider.js +36 -0
- package/build/AudioRecorder.provider.js.map +1 -0
- package/build/ExpoAudioStream.native.d.ts +3 -0
- package/build/ExpoAudioStream.native.d.ts.map +1 -0
- package/{src/ExpoAudioStream.native.ts → build/ExpoAudioStream.native.js} +3 -3
- package/build/ExpoAudioStream.native.js.map +1 -0
- package/build/ExpoAudioStream.types.d.ts +65 -0
- package/build/ExpoAudioStream.types.d.ts.map +1 -0
- package/build/ExpoAudioStream.types.js +2 -0
- package/build/ExpoAudioStream.types.js.map +1 -0
- package/build/ExpoAudioStream.web.d.ts +42 -0
- package/build/ExpoAudioStream.web.d.ts.map +1 -0
- package/build/ExpoAudioStream.web.js +203 -0
- package/build/ExpoAudioStream.web.js.map +1 -0
- package/build/ExpoAudioStreamModule.d.ts +3 -0
- package/build/ExpoAudioStreamModule.d.ts.map +1 -0
- package/build/ExpoAudioStreamModule.js +25 -0
- package/build/ExpoAudioStreamModule.js.map +1 -0
- package/build/WebRecorder.web.d.ts +51 -0
- package/build/WebRecorder.web.d.ts.map +1 -0
- package/build/WebRecorder.web.js +298 -0
- package/build/WebRecorder.web.js.map +1 -0
- package/build/constants.d.ts +11 -0
- package/build/constants.d.ts.map +1 -0
- package/build/constants.js +14 -0
- package/build/constants.js.map +1 -0
- package/build/events.d.ts +18 -0
- package/build/events.d.ts.map +1 -0
- package/build/events.js +15 -0
- package/build/events.js.map +1 -0
- package/build/index.d.ts +11 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js.map +1 -0
- package/build/logger.d.ts +9 -0
- package/build/logger.d.ts.map +1 -0
- package/build/logger.js +13 -0
- package/build/logger.js.map +1 -0
- package/build/useAudioRecorder.d.ts +20 -0
- package/build/useAudioRecorder.d.ts.map +1 -0
- package/build/useAudioRecorder.js +271 -0
- package/build/useAudioRecorder.js.map +1 -0
- package/build/utils/BlobFix.d.ts +9 -0
- package/build/utils/BlobFix.d.ts.map +1 -0
- package/{src/utils/BlobFix.ts → build/utils/BlobFix.js} +66 -122
- package/build/utils/BlobFix.js.map +1 -0
- package/build/utils/concatenateBuffers.d.ts +8 -0
- package/build/utils/concatenateBuffers.d.ts.map +1 -0
- package/{src/utils/concatenateBuffers.ts → build/utils/concatenateBuffers.js} +10 -13
- package/build/utils/concatenateBuffers.js.map +1 -0
- package/build/utils/convertPCMToFloat32.d.ts +11 -0
- package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
- package/build/utils/convertPCMToFloat32.js +54 -0
- package/build/utils/convertPCMToFloat32.js.map +1 -0
- package/build/utils/encodingToBitDepth.d.ts +5 -0
- package/build/utils/encodingToBitDepth.d.ts.map +1 -0
- package/build/utils/encodingToBitDepth.js +13 -0
- package/build/utils/encodingToBitDepth.js.map +1 -0
- package/build/utils/getWavFileInfo.d.ts +26 -0
- package/build/utils/getWavFileInfo.d.ts.map +1 -0
- package/build/utils/getWavFileInfo.js +92 -0
- package/build/utils/getWavFileInfo.js.map +1 -0
- package/build/utils/writeWavHeader.d.ts +9 -0
- package/build/utils/writeWavHeader.d.ts.map +1 -0
- package/build/utils/writeWavHeader.js +45 -0
- package/build/utils/writeWavHeader.js.map +1 -0
- package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
- package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
- package/{src/workers/InlineFeaturesExtractor.web.tsx → build/workers/InlineFeaturesExtractor.web.js} +2 -1
- package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
- package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
- package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
- package/{src/workers/inlineAudioWebWorker.web.tsx → build/workers/inlineAudioWebWorker.web.js} +2 -1
- package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
- package/package.json +2 -2
- package/src/AudioAnalysis/AudioAnalysis.types.ts +0 -84
- package/src/AudioAnalysis/extractAudioAnalysis.ts +0 -147
- package/src/AudioAnalysis/extractWaveform.ts +0 -25
- package/src/AudioRecorder.provider.tsx +0 -70
- package/src/ExpoAudioStream.types.ts +0 -80
- package/src/ExpoAudioStream.web.ts +0 -255
- package/src/ExpoAudioStreamModule.ts +0 -31
- package/src/WebRecorder.web.ts +0 -433
- package/src/constants.ts +0 -18
- package/src/events.ts +0 -39
- package/src/index.ts +0 -24
- package/src/logger.ts +0 -22
- package/src/useAudioRecorder.tsx +0 -420
- package/src/utils/convertPCMToFloat32.ts +0 -75
- package/src/utils/encodingToBitDepth.ts +0 -18
- package/src/utils/getWavFileInfo.ts +0 -132
- package/src/utils/writeWavHeader.ts +0 -61
package/src/WebRecorder.web.ts
DELETED
|
@@ -1,433 +0,0 @@
|
|
|
1
|
-
// src/WebRecorder.ts
|
|
2
|
-
import { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types'
|
|
3
|
-
import { RecordingConfig } from './ExpoAudioStream.types'
|
|
4
|
-
import {
|
|
5
|
-
EmitAudioAnalysisFunction,
|
|
6
|
-
EmitAudioEventFunction,
|
|
7
|
-
} from './ExpoAudioStream.web'
|
|
8
|
-
import { getLogger } from './logger'
|
|
9
|
-
import { encodingToBitDepth } from './utils/encodingToBitDepth'
|
|
10
|
-
import { InlineFeaturesExtractor } from './workers/InlineFeaturesExtractor.web'
|
|
11
|
-
import { InlineAudioWebWorker } from './workers/inlineAudioWebWorker.web'
|
|
12
|
-
|
|
13
|
-
interface AudioWorkletEvent {
|
|
14
|
-
data: {
|
|
15
|
-
command: string
|
|
16
|
-
recordedData?: Float32Array
|
|
17
|
-
sampleRate?: number
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface AudioFeaturesEvent {
|
|
22
|
-
data: {
|
|
23
|
-
command: string
|
|
24
|
-
result: AudioAnalysis
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const DEFAULT_WEB_BITDEPTH = 32
|
|
29
|
-
const DEFAULT_WEB_POINTS_PER_SECOND = 10
|
|
30
|
-
const DEFAULT_WEB_INTERVAL = 500
|
|
31
|
-
const DEFAULT_WEB_NUMBER_OF_CHANNELS = 1
|
|
32
|
-
const DEFAULT_ALGORITHM = 'rms'
|
|
33
|
-
|
|
34
|
-
const TAG = 'WebRecorder'
|
|
35
|
-
const logger = getLogger(TAG)
|
|
36
|
-
|
|
37
|
-
export class WebRecorder {
|
|
38
|
-
private audioContext: AudioContext
|
|
39
|
-
private audioWorkletNode!: AudioWorkletNode
|
|
40
|
-
private featureExtractorWorker?: Worker
|
|
41
|
-
private source: MediaStreamAudioSourceNode
|
|
42
|
-
private audioWorkletUrl: string
|
|
43
|
-
private emitAudioEventCallback: EmitAudioEventFunction
|
|
44
|
-
private emitAudioAnalysisCallback: EmitAudioAnalysisFunction
|
|
45
|
-
private config: RecordingConfig
|
|
46
|
-
private position: number // Track the cumulative position
|
|
47
|
-
private numberOfChannels: number // Number of audio channels
|
|
48
|
-
private bitDepth: number // Bit depth of the audio
|
|
49
|
-
private exportBitDepth: number // Bit depth of the audio
|
|
50
|
-
private buffer: Float32Array // Single buffer to store the audio data
|
|
51
|
-
private bufferSize: number // Keep track of the buffer size
|
|
52
|
-
private audioAnalysisData: AudioAnalysis // Keep updating the full audio analysis data with latest events
|
|
53
|
-
|
|
54
|
-
constructor({
|
|
55
|
-
audioContext,
|
|
56
|
-
source,
|
|
57
|
-
recordingConfig,
|
|
58
|
-
audioWorkletUrl,
|
|
59
|
-
emitAudioEventCallback,
|
|
60
|
-
emitAudioAnalysisCallback,
|
|
61
|
-
}: {
|
|
62
|
-
audioContext: AudioContext
|
|
63
|
-
source: MediaStreamAudioSourceNode
|
|
64
|
-
recordingConfig: RecordingConfig
|
|
65
|
-
audioWorkletUrl: string
|
|
66
|
-
emitAudioEventCallback: EmitAudioEventFunction
|
|
67
|
-
emitAudioAnalysisCallback: EmitAudioAnalysisFunction
|
|
68
|
-
}) {
|
|
69
|
-
this.audioContext = audioContext
|
|
70
|
-
this.source = source
|
|
71
|
-
this.audioWorkletUrl = audioWorkletUrl
|
|
72
|
-
this.emitAudioEventCallback = emitAudioEventCallback
|
|
73
|
-
this.emitAudioAnalysisCallback = emitAudioAnalysisCallback
|
|
74
|
-
this.config = recordingConfig
|
|
75
|
-
this.position = 0
|
|
76
|
-
this.bufferSize = 0
|
|
77
|
-
this.buffer = new Float32Array(0) // Initialize the buffer
|
|
78
|
-
|
|
79
|
-
const audioContextFormat = this.checkAudioContextFormat({
|
|
80
|
-
sampleRate: this.audioContext.sampleRate,
|
|
81
|
-
})
|
|
82
|
-
logger.debug('Initialized WebRecorder with config:', {
|
|
83
|
-
sampleRate: audioContextFormat.sampleRate,
|
|
84
|
-
bitDepth: audioContextFormat.bitDepth,
|
|
85
|
-
numberOfChannels: audioContextFormat.numberOfChannels,
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
this.bitDepth = audioContextFormat.bitDepth
|
|
89
|
-
this.numberOfChannels =
|
|
90
|
-
audioContextFormat.numberOfChannels ||
|
|
91
|
-
DEFAULT_WEB_NUMBER_OF_CHANNELS // Default to 1 if not available
|
|
92
|
-
this.exportBitDepth =
|
|
93
|
-
encodingToBitDepth({
|
|
94
|
-
encoding: recordingConfig.encoding ?? 'pcm_32bit',
|
|
95
|
-
}) ||
|
|
96
|
-
audioContextFormat.bitDepth ||
|
|
97
|
-
DEFAULT_WEB_BITDEPTH
|
|
98
|
-
|
|
99
|
-
this.audioAnalysisData = {
|
|
100
|
-
amplitudeRange: { min: 0, max: 0 },
|
|
101
|
-
dataPoints: [],
|
|
102
|
-
durationMs: 0,
|
|
103
|
-
samples: 0,
|
|
104
|
-
amplitudeAlgorithm: recordingConfig.algorithm || DEFAULT_ALGORITHM,
|
|
105
|
-
bitDepth: this.bitDepth,
|
|
106
|
-
numberOfChannels: this.numberOfChannels,
|
|
107
|
-
sampleRate: this.config.sampleRate || this.audioContext.sampleRate,
|
|
108
|
-
pointsPerSecond:
|
|
109
|
-
this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,
|
|
110
|
-
speakerChanges: [],
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (recordingConfig.enableProcessing) {
|
|
114
|
-
this.initFeatureExtractorWorker()
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async init() {
|
|
119
|
-
try {
|
|
120
|
-
if (!this.audioWorkletUrl) {
|
|
121
|
-
const blob = new Blob([InlineAudioWebWorker], {
|
|
122
|
-
type: 'application/javascript',
|
|
123
|
-
})
|
|
124
|
-
const url = URL.createObjectURL(blob)
|
|
125
|
-
await this.audioContext.audioWorklet.addModule(url)
|
|
126
|
-
} else {
|
|
127
|
-
await this.audioContext.audioWorklet.addModule(
|
|
128
|
-
this.audioWorkletUrl
|
|
129
|
-
)
|
|
130
|
-
}
|
|
131
|
-
this.audioWorkletNode = new AudioWorkletNode(
|
|
132
|
-
this.audioContext,
|
|
133
|
-
'recorder-processor'
|
|
134
|
-
)
|
|
135
|
-
|
|
136
|
-
this.audioWorkletNode.port.onmessage = async (
|
|
137
|
-
event: AudioWorkletEvent
|
|
138
|
-
) => {
|
|
139
|
-
const command = event.data.command
|
|
140
|
-
if (command !== 'newData') {
|
|
141
|
-
return
|
|
142
|
-
}
|
|
143
|
-
// Handle the audio blob (e.g., send it to the server or process it further)
|
|
144
|
-
logger.debug('Received audio blob from processor', event)
|
|
145
|
-
const pcmBufferFloat = event.data.recordedData
|
|
146
|
-
|
|
147
|
-
if (!pcmBufferFloat) {
|
|
148
|
-
return
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Concatenate the incoming Float32Array to the existing buffer
|
|
152
|
-
const newBuffer = new Float32Array(
|
|
153
|
-
this.bufferSize + pcmBufferFloat.length
|
|
154
|
-
)
|
|
155
|
-
newBuffer.set(this.buffer, 0)
|
|
156
|
-
newBuffer.set(pcmBufferFloat, this.bufferSize)
|
|
157
|
-
this.buffer = newBuffer
|
|
158
|
-
this.bufferSize += pcmBufferFloat.length
|
|
159
|
-
|
|
160
|
-
const sampleRate =
|
|
161
|
-
event.data.sampleRate ?? this.audioContext.sampleRate
|
|
162
|
-
const duration = pcmBufferFloat.length / sampleRate // Calculate duration of the current buffer
|
|
163
|
-
|
|
164
|
-
this.emitAudioEventCallback({
|
|
165
|
-
data: pcmBufferFloat,
|
|
166
|
-
position: this.position,
|
|
167
|
-
})
|
|
168
|
-
this.position += duration // Update position
|
|
169
|
-
|
|
170
|
-
this.featureExtractorWorker?.postMessage(
|
|
171
|
-
{
|
|
172
|
-
command: 'process',
|
|
173
|
-
channelData: pcmBufferFloat,
|
|
174
|
-
sampleRate,
|
|
175
|
-
pointsPerSecond:
|
|
176
|
-
this.config.pointsPerSecond ||
|
|
177
|
-
DEFAULT_WEB_POINTS_PER_SECOND,
|
|
178
|
-
algorithm: this.config.algorithm || 'rms',
|
|
179
|
-
bitDepth: this.bitDepth,
|
|
180
|
-
fullAudioDurationMs: this.position * 1000,
|
|
181
|
-
numberOfChannels: this.numberOfChannels,
|
|
182
|
-
features: this.config.features,
|
|
183
|
-
},
|
|
184
|
-
[]
|
|
185
|
-
)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
logger.debug(
|
|
189
|
-
`WebRecorder initialized -- recordSampleRate=${this.audioContext.sampleRate}`,
|
|
190
|
-
this.config
|
|
191
|
-
)
|
|
192
|
-
this.audioWorkletNode.port.postMessage({
|
|
193
|
-
command: 'init',
|
|
194
|
-
recordSampleRate: this.audioContext.sampleRate, // Pass the original sample rate
|
|
195
|
-
exportSampleRate:
|
|
196
|
-
this.config.sampleRate ?? this.audioContext.sampleRate,
|
|
197
|
-
bitDepth: this.bitDepth,
|
|
198
|
-
exportBitDepth: this.exportBitDepth,
|
|
199
|
-
channels: this.numberOfChannels,
|
|
200
|
-
interval: this.config.interval ?? DEFAULT_WEB_INTERVAL,
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
// Connect the source to the AudioWorkletNode and start recording
|
|
204
|
-
this.source.connect(this.audioWorkletNode)
|
|
205
|
-
this.audioWorkletNode.connect(this.audioContext.destination)
|
|
206
|
-
} catch (error) {
|
|
207
|
-
console.error(`[${TAG}] Failed to initialize WebRecorder`, error)
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
initFeatureExtractorWorker(featuresExtratorUrl?: string) {
|
|
212
|
-
try {
|
|
213
|
-
if (featuresExtratorUrl) {
|
|
214
|
-
// Initialize the feature extractor worker
|
|
215
|
-
//TODO: create audio feature extractor from a Blob instead of url since we cannot include the url directly in the library
|
|
216
|
-
// We keep the url during dev and use the blob in production.
|
|
217
|
-
this.featureExtractorWorker = new Worker(
|
|
218
|
-
new URL(featuresExtratorUrl, window.location.href)
|
|
219
|
-
)
|
|
220
|
-
this.featureExtractorWorker.onmessage =
|
|
221
|
-
this.handleFeatureExtractorMessage.bind(this)
|
|
222
|
-
this.featureExtractorWorker.onerror =
|
|
223
|
-
this.handleWorkerError.bind(this)
|
|
224
|
-
} else {
|
|
225
|
-
// Fallback to the inline worker if the URL is not provided
|
|
226
|
-
this.initFallbackWorker()
|
|
227
|
-
}
|
|
228
|
-
} catch (error) {
|
|
229
|
-
console.error(
|
|
230
|
-
`[${TAG}] Failed to initialize feature extractor worker`,
|
|
231
|
-
error
|
|
232
|
-
)
|
|
233
|
-
this.initFallbackWorker()
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
initFallbackWorker() {
|
|
238
|
-
try {
|
|
239
|
-
const blob = new Blob([InlineFeaturesExtractor], {
|
|
240
|
-
type: 'application/javascript',
|
|
241
|
-
})
|
|
242
|
-
const url = URL.createObjectURL(blob)
|
|
243
|
-
this.featureExtractorWorker = new Worker(url)
|
|
244
|
-
this.featureExtractorWorker.onmessage =
|
|
245
|
-
this.handleFeatureExtractorMessage.bind(this)
|
|
246
|
-
this.featureExtractorWorker.onerror = (error) => {
|
|
247
|
-
console.error(`[${TAG}] Default Inline worker failed`, error)
|
|
248
|
-
}
|
|
249
|
-
logger.log('Inline worker initialized successfully')
|
|
250
|
-
} catch (error) {
|
|
251
|
-
console.error(
|
|
252
|
-
`[${TAG}] Failed to initialize Inline Feature Extractor worker`,
|
|
253
|
-
error
|
|
254
|
-
)
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
handleWorkerError(error: ErrorEvent) {
|
|
259
|
-
console.error(`[${TAG}] Feature extractor worker error:`, error)
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
handleFeatureExtractorMessage(event: AudioFeaturesEvent) {
|
|
263
|
-
if (event.data.command === 'features') {
|
|
264
|
-
const segmentResult = event.data.result
|
|
265
|
-
|
|
266
|
-
// Merge the segment result with the full audio analysis data
|
|
267
|
-
this.audioAnalysisData.dataPoints.push(...segmentResult.dataPoints)
|
|
268
|
-
this.audioAnalysisData.speakerChanges?.push(
|
|
269
|
-
...(segmentResult.speakerChanges ?? [])
|
|
270
|
-
)
|
|
271
|
-
this.audioAnalysisData.durationMs = segmentResult.durationMs
|
|
272
|
-
if (segmentResult.amplitudeRange) {
|
|
273
|
-
this.audioAnalysisData.amplitudeRange = {
|
|
274
|
-
min: Math.min(
|
|
275
|
-
this.audioAnalysisData.amplitudeRange.min,
|
|
276
|
-
segmentResult.amplitudeRange.min
|
|
277
|
-
),
|
|
278
|
-
max: Math.max(
|
|
279
|
-
this.audioAnalysisData.amplitudeRange.max,
|
|
280
|
-
segmentResult.amplitudeRange.max
|
|
281
|
-
),
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
// Handle the extracted features (e.g., emit an event or log them)
|
|
285
|
-
logger.debug('features event segmentResult', segmentResult)
|
|
286
|
-
logger.debug(
|
|
287
|
-
'features event audioAnalysisData',
|
|
288
|
-
this.audioAnalysisData
|
|
289
|
-
)
|
|
290
|
-
this.emitAudioAnalysisCallback(segmentResult)
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
start() {
|
|
295
|
-
this.source.connect(this.audioWorkletNode)
|
|
296
|
-
this.audioWorkletNode.connect(this.audioContext.destination)
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
stop(): Promise<Float32Array> {
|
|
300
|
-
return new Promise((resolve, reject) => {
|
|
301
|
-
try {
|
|
302
|
-
if (this.audioWorkletNode) {
|
|
303
|
-
// this.source.disconnect(this.audioWorkletNode);
|
|
304
|
-
// this.audioWorkletNode.disconnect(this.audioContext.destination);
|
|
305
|
-
this.audioWorkletNode.port.postMessage({ command: 'stop' })
|
|
306
|
-
|
|
307
|
-
// Set a timeout to reject the promise if no message is received within 5 seconds
|
|
308
|
-
const timeout = setTimeout(() => {
|
|
309
|
-
this.audioWorkletNode.port.removeEventListener(
|
|
310
|
-
'message',
|
|
311
|
-
onMessage
|
|
312
|
-
)
|
|
313
|
-
reject(
|
|
314
|
-
new Error(
|
|
315
|
-
"Timeout error, audioWorkletNode didn't complete."
|
|
316
|
-
)
|
|
317
|
-
)
|
|
318
|
-
}, 5000)
|
|
319
|
-
|
|
320
|
-
// Listen for the recordedData message to confirm stopping
|
|
321
|
-
const onMessage = async (event: AudioWorkletEvent) => {
|
|
322
|
-
const command = event.data.command
|
|
323
|
-
if (command === 'recordedData') {
|
|
324
|
-
clearTimeout(timeout) // Clear the timeout
|
|
325
|
-
|
|
326
|
-
const rawPCMDataFull =
|
|
327
|
-
event.data.recordedData?.slice(0)
|
|
328
|
-
|
|
329
|
-
if (!rawPCMDataFull) {
|
|
330
|
-
reject(new Error('Failed to get recorded data'))
|
|
331
|
-
return
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// Compute duration of the recorded data
|
|
335
|
-
const duration =
|
|
336
|
-
rawPCMDataFull.byteLength /
|
|
337
|
-
(this.audioContext.sampleRate *
|
|
338
|
-
(this.exportBitDepth /
|
|
339
|
-
this.numberOfChannels))
|
|
340
|
-
logger.debug(
|
|
341
|
-
`Received recorded data -- Duration: ${duration} vs ${rawPCMDataFull.byteLength / this.audioContext.sampleRate} seconds`
|
|
342
|
-
)
|
|
343
|
-
logger.debug(
|
|
344
|
-
`recordedData.length=${rawPCMDataFull.byteLength} vs transmittedData.length=${this.bufferSize}`
|
|
345
|
-
)
|
|
346
|
-
|
|
347
|
-
// Remove the event listener after receiving the final data
|
|
348
|
-
this.audioWorkletNode.port.removeEventListener(
|
|
349
|
-
'message',
|
|
350
|
-
onMessage
|
|
351
|
-
)
|
|
352
|
-
resolve(this.buffer) // Resolve the promise with the collected buffers
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
this.audioWorkletNode.port.addEventListener(
|
|
356
|
-
'message',
|
|
357
|
-
onMessage
|
|
358
|
-
)
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
// Stop all media stream tracks to stop the browser recording
|
|
362
|
-
this.stopMediaStreamTracks()
|
|
363
|
-
} catch (error) {
|
|
364
|
-
reject(error)
|
|
365
|
-
}
|
|
366
|
-
})
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
pause() {
|
|
370
|
-
this.source.disconnect(this.audioWorkletNode) // Disconnect the source from the AudioWorkletNode
|
|
371
|
-
this.audioWorkletNode.disconnect(this.audioContext.destination) // Disconnect the AudioWorkletNode from the destination
|
|
372
|
-
this.audioWorkletNode.port.postMessage({ command: 'pause' })
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
stopMediaStreamTracks() {
|
|
376
|
-
// Stop all audio tracks to stop the recording icon
|
|
377
|
-
const tracks = this.source.mediaStream.getTracks()
|
|
378
|
-
tracks.forEach((track) => track.stop())
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
async playRecordedData({
|
|
382
|
-
recordedData,
|
|
383
|
-
}: {
|
|
384
|
-
recordedData: ArrayBuffer
|
|
385
|
-
mimeType?: string
|
|
386
|
-
}) {
|
|
387
|
-
try {
|
|
388
|
-
const blob = new Blob([recordedData])
|
|
389
|
-
const url = URL.createObjectURL(blob)
|
|
390
|
-
const response = await fetch(url)
|
|
391
|
-
const arrayBuffer = await response.arrayBuffer()
|
|
392
|
-
|
|
393
|
-
// Decode the audio data
|
|
394
|
-
const audioBuffer =
|
|
395
|
-
await this.audioContext.decodeAudioData(arrayBuffer)
|
|
396
|
-
|
|
397
|
-
// Create a buffer source node and play the audio
|
|
398
|
-
const bufferSource = this.audioContext.createBufferSource()
|
|
399
|
-
bufferSource.buffer = audioBuffer
|
|
400
|
-
bufferSource.connect(this.audioContext.destination)
|
|
401
|
-
bufferSource.start()
|
|
402
|
-
logger.debug('Playing recorded data', recordedData)
|
|
403
|
-
} catch (error) {
|
|
404
|
-
console.error(`[${TAG}] Failed to play recorded data:`, error)
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
private checkAudioContextFormat({ sampleRate }: { sampleRate: number }) {
|
|
409
|
-
// Create a silent AudioBuffer
|
|
410
|
-
const frameCount = sampleRate * 1.0 // 1 second buffer
|
|
411
|
-
const audioBuffer = this.audioContext.createBuffer(
|
|
412
|
-
1,
|
|
413
|
-
frameCount,
|
|
414
|
-
sampleRate
|
|
415
|
-
)
|
|
416
|
-
|
|
417
|
-
// Check the format
|
|
418
|
-
const channelData = audioBuffer.getChannelData(0)
|
|
419
|
-
const bitDepth = channelData.BYTES_PER_ELEMENT * 8 // 4 bytes per element means 32-bit
|
|
420
|
-
|
|
421
|
-
return {
|
|
422
|
-
sampleRate: audioBuffer.sampleRate,
|
|
423
|
-
bitDepth,
|
|
424
|
-
numberOfChannels: audioBuffer.numberOfChannels,
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
resume() {
|
|
429
|
-
this.source.connect(this.audioWorkletNode)
|
|
430
|
-
this.audioWorkletNode.connect(this.audioContext.destination)
|
|
431
|
-
this.audioWorkletNode.port.postMessage({ command: 'resume' })
|
|
432
|
-
}
|
|
433
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// packages/expo-audio-stream/src/constants.ts
|
|
2
|
-
import { Platform } from 'react-native'
|
|
3
|
-
|
|
4
|
-
import { BitDepth, SampleRate } from './ExpoAudioStream.types'
|
|
5
|
-
|
|
6
|
-
export const isWeb = Platform.OS === 'web'
|
|
7
|
-
export const DEBUG_NAMESPACE = 'expo-audio-stream'
|
|
8
|
-
|
|
9
|
-
// Constants for identifying chunks in a WAV file
|
|
10
|
-
export const RIFF_HEADER = 0x52494646 // "RIFF"
|
|
11
|
-
export const WAVE_HEADER = 0x57415645 // "WAVE"
|
|
12
|
-
export const FMT_CHUNK_ID = 0x666d7420 // "fmt "
|
|
13
|
-
export const DATA_CHUNK_ID = 0x64617461 // "data"
|
|
14
|
-
export const INFO_CHUNK_ID = 0x494e464f // "INFO"
|
|
15
|
-
|
|
16
|
-
// Default values
|
|
17
|
-
export const DEFAULT_SAMPLE_RATE: SampleRate = 16000
|
|
18
|
-
export const DEFAULT_BIT_DEPTH: BitDepth = 32
|
package/src/events.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
// packages/expo-audio-stream/src/events.ts
|
|
2
|
-
|
|
3
|
-
import { EventEmitter, type Subscription } from 'expo-modules-core'
|
|
4
|
-
|
|
5
|
-
import { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types'
|
|
6
|
-
import ExpoAudioStreamModule from './ExpoAudioStreamModule'
|
|
7
|
-
import { getLogger } from './logger'
|
|
8
|
-
|
|
9
|
-
const emitter = new EventEmitter(ExpoAudioStreamModule)
|
|
10
|
-
const logger = getLogger('events')
|
|
11
|
-
|
|
12
|
-
export interface AudioEventPayload {
|
|
13
|
-
encoded?: string
|
|
14
|
-
buffer?: Float32Array
|
|
15
|
-
fileUri: string
|
|
16
|
-
lastEmittedSize: number
|
|
17
|
-
position: number
|
|
18
|
-
deltaSize: number
|
|
19
|
-
totalSize: number
|
|
20
|
-
mimeType: string
|
|
21
|
-
streamUuid: string
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function addAudioEventListener(
|
|
25
|
-
listener: (event: AudioEventPayload) => Promise<void>
|
|
26
|
-
): Subscription {
|
|
27
|
-
logger.log('Adding listener for AudioData event')
|
|
28
|
-
return emitter.addListener<AudioEventPayload>('AudioData', listener)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Only aliasing the AudioAnalysis type for the event payload
|
|
32
|
-
export interface AudioAnalysisEvent extends AudioAnalysis {}
|
|
33
|
-
|
|
34
|
-
export function addAudioAnalysisListener(
|
|
35
|
-
listener: (event: AudioAnalysisEvent) => Promise<void>
|
|
36
|
-
): Subscription {
|
|
37
|
-
logger.log('Adding listener for AudioAnalysis event')
|
|
38
|
-
return emitter.addListener<AudioAnalysisEvent>('AudioAnalysis', listener)
|
|
39
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
|
|
3
|
-
import { extractAudioAnalysis } from './AudioAnalysis/extractAudioAnalysis'
|
|
4
|
-
import {
|
|
5
|
-
AudioRecorderProvider,
|
|
6
|
-
useSharedAudioRecorder,
|
|
7
|
-
} from './AudioRecorder.provider'
|
|
8
|
-
import ExpoAudioStreamModule from './ExpoAudioStreamModule'
|
|
9
|
-
import { useAudioRecorder } from './useAudioRecorder'
|
|
10
|
-
|
|
11
|
-
export * from './utils/convertPCMToFloat32'
|
|
12
|
-
export * from './utils/getWavFileInfo'
|
|
13
|
-
export * from './utils/writeWavHeader'
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
AudioRecorderProvider,
|
|
17
|
-
ExpoAudioStreamModule,
|
|
18
|
-
extractAudioAnalysis,
|
|
19
|
-
useAudioRecorder,
|
|
20
|
-
useSharedAudioRecorder,
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type * from './AudioAnalysis/AudioAnalysis.types'
|
|
24
|
-
export type * from './ExpoAudioStream.types'
|
package/src/logger.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// packages/expo-audio-stream/src/logger.ts
|
|
2
|
-
import { getLogger as siteedGetLogger } from '@siteed/react-native-logger'
|
|
3
|
-
|
|
4
|
-
import { DEBUG_NAMESPACE } from './constants'
|
|
5
|
-
|
|
6
|
-
type ConsoleLike = {
|
|
7
|
-
log: (message: string, ...args: unknown[]) => void
|
|
8
|
-
debug: (message: string, ...args: unknown[]) => void
|
|
9
|
-
warn: (message: string, ...args: unknown[]) => void
|
|
10
|
-
error: (message: string, ...args: unknown[]) => void
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const getLogger = (tag: string): ConsoleLike => {
|
|
14
|
-
const baseLogger = siteedGetLogger(`${DEBUG_NAMESPACE}:${tag}`)
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
log: (...args: unknown[]) => baseLogger.log(...(args as [unknown])),
|
|
18
|
-
debug: (...args: unknown[]) => baseLogger.debug(...(args as [unknown])),
|
|
19
|
-
error: (...args: unknown[]) => baseLogger.error(...(args as [unknown])),
|
|
20
|
-
warn: (...args: unknown[]) => baseLogger.warn(...(args as [unknown])),
|
|
21
|
-
}
|
|
22
|
-
}
|