@siteed/expo-audio-stream 1.5.1 → 1.6.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/CHANGELOG.md +12 -24
- package/app.plugin.js +1 -0
- 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 +22 -0
- package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
- package/build/AudioAnalysis/extractAudioAnalysis.js +86 -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 +11 -0
- package/build/AudioAnalysis/extractWaveform.js.map +1 -0
- package/build/AudioRecorder.provider.d.ts +11 -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/build/ExpoAudioStream.native.js +6 -0
- package/build/ExpoAudioStream.native.js.map +1 -0
- package/build/ExpoAudioStream.types.d.ts +127 -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 +44 -0
- package/build/ExpoAudioStream.web.d.ts.map +1 -0
- package/build/ExpoAudioStream.web.js +206 -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 +35 -0
- package/build/ExpoAudioStreamModule.js.map +1 -0
- package/build/WebRecorder.web.d.ts +54 -0
- package/build/WebRecorder.web.d.ts.map +1 -0
- package/build/WebRecorder.web.js +336 -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 +11 -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/useAudioRecorder.d.ts +20 -0
- package/build/useAudioRecorder.d.ts.map +1 -0
- package/build/useAudioRecorder.js +311 -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/build/utils/BlobFix.js +498 -0
- 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/build/utils/concatenateBuffers.js +21 -0
- package/build/utils/concatenateBuffers.js.map +1 -0
- package/build/utils/convertPCMToFloat32.d.ts +13 -0
- package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
- package/build/utils/convertPCMToFloat32.js +120 -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 +49 -0
- package/build/utils/writeWavHeader.d.ts.map +1 -0
- package/build/utils/writeWavHeader.js +91 -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/build/workers/InlineFeaturesExtractor.web.js +311 -0
- 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/build/workers/inlineAudioWebWorker.web.js +251 -0
- package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/package.json +7 -1
- package/plugin/build/index.d.ts +3 -0
- package/plugin/build/index.js +132 -0
- package/plugin/src/index.ts +176 -0
- package/plugin/tsconfig.json +10 -0
- package/plugin/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
export const InlineFeaturesExtractor = `
|
|
2
|
+
// Unique ID counter
|
|
3
|
+
let uniqueIdCounter = 0
|
|
4
|
+
|
|
5
|
+
self.onmessage = function (event) {
|
|
6
|
+
const {
|
|
7
|
+
channelData, // this is only the newly recorded data when live recording.
|
|
8
|
+
sampleRate,
|
|
9
|
+
pointsPerSecond,
|
|
10
|
+
algorithm,
|
|
11
|
+
bitDepth,
|
|
12
|
+
fullAudioDurationMs,
|
|
13
|
+
numberOfChannels,
|
|
14
|
+
features: _features,
|
|
15
|
+
} = event.data
|
|
16
|
+
const features = _features || {}
|
|
17
|
+
|
|
18
|
+
const SILENCE_THRESHOLD = 0.01
|
|
19
|
+
const MIN_SILENCE_DURATION = 1.5 * sampleRate // 1.5 seconds of silence
|
|
20
|
+
const SPEECH_INERTIA_DURATION = 0.1 * sampleRate // Speech inertia duration in samples
|
|
21
|
+
const RMS_THRESHOLD = 0.01
|
|
22
|
+
const ZCR_THRESHOLD = 0.1
|
|
23
|
+
|
|
24
|
+
// Placeholder functions for feature extraction
|
|
25
|
+
const extractMFCC = (segmentData, sampleRate) => {
|
|
26
|
+
// Implement MFCC extraction logic here
|
|
27
|
+
return []
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const extractSpectralCentroid = (segmentData, sampleRate) => {
|
|
31
|
+
const magnitudeSpectrum = segmentData.map((v) => v * v)
|
|
32
|
+
const sum = magnitudeSpectrum.reduce((a, b) => a + b, 0)
|
|
33
|
+
if (sum === 0) return 0
|
|
34
|
+
|
|
35
|
+
const weightedSum = magnitudeSpectrum.reduce(
|
|
36
|
+
(acc, value, index) => acc + index * value,
|
|
37
|
+
0
|
|
38
|
+
)
|
|
39
|
+
return (
|
|
40
|
+
((weightedSum / sum) * (sampleRate / 2)) / magnitudeSpectrum.length
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const extractSpectralFlatness = (segmentData) => {
|
|
45
|
+
const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))
|
|
46
|
+
const geometricMean = Math.exp(
|
|
47
|
+
magnitudeSpectrum
|
|
48
|
+
.map((v) => Math.log(v + Number.MIN_VALUE))
|
|
49
|
+
.reduce((a, b) => a + b) / magnitudeSpectrum.length
|
|
50
|
+
)
|
|
51
|
+
const arithmeticMean =
|
|
52
|
+
magnitudeSpectrum.reduce((a, b) => a + b) / magnitudeSpectrum.length
|
|
53
|
+
return arithmeticMean === 0 ? 0 : geometricMean / arithmeticMean
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const extractSpectralRollOff = (segmentData, sampleRate) => {
|
|
57
|
+
const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))
|
|
58
|
+
const totalEnergy = magnitudeSpectrum.reduce((a, b) => a + b, 0)
|
|
59
|
+
const rollOffThreshold = totalEnergy * 0.85
|
|
60
|
+
let cumulativeEnergy = 0
|
|
61
|
+
|
|
62
|
+
for (let i = 0; i < magnitudeSpectrum.length; i++) {
|
|
63
|
+
cumulativeEnergy += magnitudeSpectrum[i]
|
|
64
|
+
if (cumulativeEnergy >= rollOffThreshold) {
|
|
65
|
+
return (i / magnitudeSpectrum.length) * (sampleRate / 2)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return 0
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const extractSpectralBandwidth = (segmentData, sampleRate) => {
|
|
73
|
+
const centroid = extractSpectralCentroid(segmentData, sampleRate)
|
|
74
|
+
const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))
|
|
75
|
+
const sum = magnitudeSpectrum.reduce((a, b) => a + b, 0)
|
|
76
|
+
if (sum === 0) return 0
|
|
77
|
+
|
|
78
|
+
const weightedSum = magnitudeSpectrum.reduce(
|
|
79
|
+
(acc, value, index) => acc + value * Math.pow(index - centroid, 2),
|
|
80
|
+
0
|
|
81
|
+
)
|
|
82
|
+
return Math.sqrt(weightedSum / sum)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const extractChromagram = (segmentData, sampleRate) => {
|
|
86
|
+
return [] // TODO implement
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const extractHNR = (segmentData) => {
|
|
90
|
+
const frameSize = segmentData.length
|
|
91
|
+
const autocorrelation = new Float32Array(frameSize)
|
|
92
|
+
|
|
93
|
+
// Compute the autocorrelation of the segment data
|
|
94
|
+
for (let i = 0; i < frameSize; i++) {
|
|
95
|
+
let sum = 0
|
|
96
|
+
for (let j = 0; j < frameSize - i; j++) {
|
|
97
|
+
sum += segmentData[j] * segmentData[j + i]
|
|
98
|
+
}
|
|
99
|
+
autocorrelation[i] = sum
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Find the maximum autocorrelation value (excluding the zero lag)
|
|
103
|
+
const maxAutocorrelation = Math.max(...autocorrelation.subarray(1))
|
|
104
|
+
|
|
105
|
+
// Compute the HNR
|
|
106
|
+
return autocorrelation[0] !== 0
|
|
107
|
+
? 10 *
|
|
108
|
+
Math.log10(
|
|
109
|
+
maxAutocorrelation /
|
|
110
|
+
(autocorrelation[0] - maxAutocorrelation)
|
|
111
|
+
)
|
|
112
|
+
: 0
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const extractWaveform = (
|
|
116
|
+
channelData, // Float32Array
|
|
117
|
+
sampleRate, // number
|
|
118
|
+
pointsPerSecond, // number
|
|
119
|
+
algorithm // string
|
|
120
|
+
) => {
|
|
121
|
+
const totalSamples = channelData.length
|
|
122
|
+
const segmentDuration = totalSamples / sampleRate
|
|
123
|
+
const totalPoints = Math.max(
|
|
124
|
+
Math.ceil(segmentDuration * pointsPerSecond),
|
|
125
|
+
1
|
|
126
|
+
)
|
|
127
|
+
const pointInterval = Math.ceil(totalSamples / totalPoints)
|
|
128
|
+
const dataPoints = []
|
|
129
|
+
let minAmplitude = Infinity
|
|
130
|
+
let maxAmplitude = -Infinity
|
|
131
|
+
let silenceStart = null
|
|
132
|
+
let lastSpeechEnd = -Infinity
|
|
133
|
+
let isSpeech = false
|
|
134
|
+
|
|
135
|
+
const expectedPoints = segmentDuration * pointsPerSecond
|
|
136
|
+
const samplesPerPoint = Math.ceil(channelData.length / expectedPoints)
|
|
137
|
+
|
|
138
|
+
for (let i = 0; i < expectedPoints; i++) {
|
|
139
|
+
const start = i * samplesPerPoint
|
|
140
|
+
const end = Math.min(start + samplesPerPoint, totalSamples)
|
|
141
|
+
|
|
142
|
+
let sumSquares = 0
|
|
143
|
+
let zeroCrossings = 0
|
|
144
|
+
let prevValue = channelData[start]
|
|
145
|
+
let localMinAmplitude = Infinity
|
|
146
|
+
let localMaxAmplitude = -Infinity
|
|
147
|
+
let hasNonZeroValue = false
|
|
148
|
+
|
|
149
|
+
// compute values for the segment
|
|
150
|
+
for (let j = start; j < end; j++) {
|
|
151
|
+
const value = channelData[j]
|
|
152
|
+
sumSquares += value * value
|
|
153
|
+
if (j > start && value * prevValue < 0) {
|
|
154
|
+
zeroCrossings++
|
|
155
|
+
}
|
|
156
|
+
prevValue = value
|
|
157
|
+
|
|
158
|
+
// We need to keep absolute value otherwise we cannot visualize properly
|
|
159
|
+
const absValue = Math.abs(value)
|
|
160
|
+
localMinAmplitude = Math.min(localMinAmplitude, absValue)
|
|
161
|
+
localMaxAmplitude = Math.max(localMaxAmplitude, absValue)
|
|
162
|
+
|
|
163
|
+
if (value !== 0) {
|
|
164
|
+
hasNonZeroValue = true
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Post-processing checks
|
|
169
|
+
if (!hasNonZeroValue) {
|
|
170
|
+
// All values are zero
|
|
171
|
+
localMinAmplitude = 0
|
|
172
|
+
localMaxAmplitude = 0
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const rms = Math.sqrt(sumSquares / (end - start))
|
|
176
|
+
minAmplitude = Math.min(minAmplitude, localMinAmplitude)
|
|
177
|
+
maxAmplitude = Math.max(maxAmplitude, localMaxAmplitude)
|
|
178
|
+
|
|
179
|
+
const energy = sumSquares
|
|
180
|
+
const zcr = zeroCrossings / (end - start)
|
|
181
|
+
|
|
182
|
+
const silent = rms < SILENCE_THRESHOLD
|
|
183
|
+
const dB = 20 * Math.log10(rms)
|
|
184
|
+
|
|
185
|
+
if (silent) {
|
|
186
|
+
if (silenceStart === null) {
|
|
187
|
+
silenceStart = start
|
|
188
|
+
} else if (start - silenceStart > MIN_SILENCE_DURATION) {
|
|
189
|
+
// Silence detected for longer than the threshold, set amplitude to 0
|
|
190
|
+
localMaxAmplitude = 0
|
|
191
|
+
localMinAmplitude = 0
|
|
192
|
+
isSpeech = false
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
silenceStart = null
|
|
196
|
+
if (
|
|
197
|
+
!isSpeech &&
|
|
198
|
+
start - lastSpeechEnd < SPEECH_INERTIA_DURATION
|
|
199
|
+
) {
|
|
200
|
+
isSpeech = true
|
|
201
|
+
}
|
|
202
|
+
lastSpeechEnd = end
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const activeSpeech =
|
|
206
|
+
(rms > RMS_THRESHOLD && zcr > ZCR_THRESHOLD) ||
|
|
207
|
+
(isSpeech && start - lastSpeechEnd < SPEECH_INERTIA_DURATION)
|
|
208
|
+
|
|
209
|
+
if (activeSpeech) {
|
|
210
|
+
isSpeech = true
|
|
211
|
+
lastSpeechEnd = end
|
|
212
|
+
} else {
|
|
213
|
+
isSpeech = false
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const bytesPerSample = bitDepth / 8
|
|
217
|
+
const startPosition = start * bytesPerSample * numberOfChannels // Calculate start position in bytes
|
|
218
|
+
const endPosition = end * bytesPerSample * numberOfChannels // Calculate end position in bytes
|
|
219
|
+
|
|
220
|
+
// Compute features
|
|
221
|
+
const segmentData = channelData.slice(start, end)
|
|
222
|
+
const mfcc = features.mfcc
|
|
223
|
+
? extractMFCC(segmentData, sampleRate)
|
|
224
|
+
: []
|
|
225
|
+
const spectralCentroid = features.spectralCentroid
|
|
226
|
+
? extractSpectralCentroid(segmentData, sampleRate)
|
|
227
|
+
: 0
|
|
228
|
+
const spectralFlatness = features.spectralFlatness
|
|
229
|
+
? extractSpectralFlatness(segmentData)
|
|
230
|
+
: 0
|
|
231
|
+
const spectralRollOff = features.spectralRollOff
|
|
232
|
+
? extractSpectralRollOff(segmentData, sampleRate)
|
|
233
|
+
: 0
|
|
234
|
+
const spectralBandwidth = features.spectralBandwidth
|
|
235
|
+
? extractSpectralBandwidth(segmentData, sampleRate)
|
|
236
|
+
: 0
|
|
237
|
+
const chromagram = features.chromagram
|
|
238
|
+
? extractChromagram(segmentData, sampleRate)
|
|
239
|
+
: []
|
|
240
|
+
const hnr = features.hnr ? extractHNR(segmentData) : 0
|
|
241
|
+
|
|
242
|
+
const peakAmp = Math.max(Math.abs(localMaxAmplitude), Math.abs(localMinAmplitude))
|
|
243
|
+
const newData = {
|
|
244
|
+
id: uniqueIdCounter++, // Assign unique ID and increment the counter
|
|
245
|
+
amplitude: algorithm === 'peak' ? peakAmp : rms,
|
|
246
|
+
activeSpeech,
|
|
247
|
+
dB,
|
|
248
|
+
silent,
|
|
249
|
+
features: {
|
|
250
|
+
energy,
|
|
251
|
+
rms,
|
|
252
|
+
minAmplitude: localMinAmplitude,
|
|
253
|
+
maxAmplitude: localMaxAmplitude,
|
|
254
|
+
zcr,
|
|
255
|
+
mfcc: [], // Placeholder for MFCC features
|
|
256
|
+
spectralCentroid, // Computed spectral centroid
|
|
257
|
+
spectralFlatness, // Computed spectral flatness
|
|
258
|
+
spectralRollOff, // Computed spectral roll-off
|
|
259
|
+
spectralBandwidth, // Computed spectral bandwidth
|
|
260
|
+
chromagram, // Computed chromagram
|
|
261
|
+
hnr, // Computed HNR
|
|
262
|
+
},
|
|
263
|
+
startTime: start / sampleRate,
|
|
264
|
+
endTime: end / sampleRate,
|
|
265
|
+
startPosition,
|
|
266
|
+
endPosition,
|
|
267
|
+
samples: end - start,
|
|
268
|
+
speaker: 0, // Assuming speaker detection is to be handled later
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
dataPoints.push(newData)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
pointsPerSecond,
|
|
276
|
+
amplitudeAlgorithm: algorithm,
|
|
277
|
+
durationMs: fullAudioDurationMs,
|
|
278
|
+
bitDepth,
|
|
279
|
+
samples: totalSamples,
|
|
280
|
+
numberOfChannels,
|
|
281
|
+
sampleRate,
|
|
282
|
+
dataPoints,
|
|
283
|
+
amplitudeRange: {
|
|
284
|
+
min: minAmplitude,
|
|
285
|
+
max: maxAmplitude,
|
|
286
|
+
},
|
|
287
|
+
speakerChanges: [], // Placeholder for future speaker detection logic
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
try {
|
|
292
|
+
const result = extractWaveform(
|
|
293
|
+
channelData,
|
|
294
|
+
sampleRate,
|
|
295
|
+
pointsPerSecond,
|
|
296
|
+
algorithm
|
|
297
|
+
)
|
|
298
|
+
self.postMessage({
|
|
299
|
+
command: 'features',
|
|
300
|
+
result,
|
|
301
|
+
})
|
|
302
|
+
} catch (error) {
|
|
303
|
+
console.error('[AudioFeaturesExtractor] Error in processing', error)
|
|
304
|
+
self.postMessage({ error: error.message })
|
|
305
|
+
} finally {
|
|
306
|
+
// Do not close the worker so it can be re-used for subsequent messages
|
|
307
|
+
// self.close();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
`;
|
|
311
|
+
//# sourceMappingURL=InlineFeaturesExtractor.web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InlineFeaturesExtractor.web.js","sourceRoot":"","sources":["../../src/workers/InlineFeaturesExtractor.web.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,uBAAuB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqTtC,CAAA","sourcesContent":["export const InlineFeaturesExtractor = `\n// Unique ID counter\nlet uniqueIdCounter = 0\n\nself.onmessage = function (event) {\n const {\n channelData, // this is only the newly recorded data when live recording.\n sampleRate,\n pointsPerSecond,\n algorithm,\n bitDepth,\n fullAudioDurationMs,\n numberOfChannels,\n features: _features,\n } = event.data\n const features = _features || {}\n\n const SILENCE_THRESHOLD = 0.01\n const MIN_SILENCE_DURATION = 1.5 * sampleRate // 1.5 seconds of silence\n const SPEECH_INERTIA_DURATION = 0.1 * sampleRate // Speech inertia duration in samples\n const RMS_THRESHOLD = 0.01\n const ZCR_THRESHOLD = 0.1\n\n // Placeholder functions for feature extraction\n const extractMFCC = (segmentData, sampleRate) => {\n // Implement MFCC extraction logic here\n return []\n }\n\n const extractSpectralCentroid = (segmentData, sampleRate) => {\n const magnitudeSpectrum = segmentData.map((v) => v * v)\n const sum = magnitudeSpectrum.reduce((a, b) => a + b, 0)\n if (sum === 0) return 0\n\n const weightedSum = magnitudeSpectrum.reduce(\n (acc, value, index) => acc + index * value,\n 0\n )\n return (\n ((weightedSum / sum) * (sampleRate / 2)) / magnitudeSpectrum.length\n )\n }\n\n const extractSpectralFlatness = (segmentData) => {\n const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))\n const geometricMean = Math.exp(\n magnitudeSpectrum\n .map((v) => Math.log(v + Number.MIN_VALUE))\n .reduce((a, b) => a + b) / magnitudeSpectrum.length\n )\n const arithmeticMean =\n magnitudeSpectrum.reduce((a, b) => a + b) / magnitudeSpectrum.length\n return arithmeticMean === 0 ? 0 : geometricMean / arithmeticMean\n }\n\n const extractSpectralRollOff = (segmentData, sampleRate) => {\n const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))\n const totalEnergy = magnitudeSpectrum.reduce((a, b) => a + b, 0)\n const rollOffThreshold = totalEnergy * 0.85\n let cumulativeEnergy = 0\n\n for (let i = 0; i < magnitudeSpectrum.length; i++) {\n cumulativeEnergy += magnitudeSpectrum[i]\n if (cumulativeEnergy >= rollOffThreshold) {\n return (i / magnitudeSpectrum.length) * (sampleRate / 2)\n }\n }\n\n return 0\n }\n\n const extractSpectralBandwidth = (segmentData, sampleRate) => {\n const centroid = extractSpectralCentroid(segmentData, sampleRate)\n const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))\n const sum = magnitudeSpectrum.reduce((a, b) => a + b, 0)\n if (sum === 0) return 0\n\n const weightedSum = magnitudeSpectrum.reduce(\n (acc, value, index) => acc + value * Math.pow(index - centroid, 2),\n 0\n )\n return Math.sqrt(weightedSum / sum)\n }\n\n const extractChromagram = (segmentData, sampleRate) => {\n return [] // TODO implement\n }\n\n const extractHNR = (segmentData) => {\n const frameSize = segmentData.length\n const autocorrelation = new Float32Array(frameSize)\n\n // Compute the autocorrelation of the segment data\n for (let i = 0; i < frameSize; i++) {\n let sum = 0\n for (let j = 0; j < frameSize - i; j++) {\n sum += segmentData[j] * segmentData[j + i]\n }\n autocorrelation[i] = sum\n }\n\n // Find the maximum autocorrelation value (excluding the zero lag)\n const maxAutocorrelation = Math.max(...autocorrelation.subarray(1))\n\n // Compute the HNR\n return autocorrelation[0] !== 0\n ? 10 *\n Math.log10(\n maxAutocorrelation /\n (autocorrelation[0] - maxAutocorrelation)\n )\n : 0\n }\n\n const extractWaveform = (\n channelData, // Float32Array\n sampleRate, // number\n pointsPerSecond, // number\n algorithm // string\n ) => {\n const totalSamples = channelData.length\n const segmentDuration = totalSamples / sampleRate\n const totalPoints = Math.max(\n Math.ceil(segmentDuration * pointsPerSecond),\n 1\n )\n const pointInterval = Math.ceil(totalSamples / totalPoints)\n const dataPoints = []\n let minAmplitude = Infinity\n let maxAmplitude = -Infinity\n let silenceStart = null\n let lastSpeechEnd = -Infinity\n let isSpeech = false\n\n const expectedPoints = segmentDuration * pointsPerSecond\n const samplesPerPoint = Math.ceil(channelData.length / expectedPoints)\n\n for (let i = 0; i < expectedPoints; i++) {\n const start = i * samplesPerPoint\n const end = Math.min(start + samplesPerPoint, totalSamples)\n\n let sumSquares = 0\n let zeroCrossings = 0\n let prevValue = channelData[start]\n let localMinAmplitude = Infinity\n let localMaxAmplitude = -Infinity\n let hasNonZeroValue = false\n\n // compute values for the segment\n for (let j = start; j < end; j++) {\n const value = channelData[j]\n sumSquares += value * value\n if (j > start && value * prevValue < 0) {\n zeroCrossings++\n }\n prevValue = value\n\n // We need to keep absolute value otherwise we cannot visualize properly\n const absValue = Math.abs(value)\n localMinAmplitude = Math.min(localMinAmplitude, absValue)\n localMaxAmplitude = Math.max(localMaxAmplitude, absValue)\n\n if (value !== 0) {\n hasNonZeroValue = true\n }\n }\n\n // Post-processing checks\n if (!hasNonZeroValue) {\n // All values are zero\n localMinAmplitude = 0\n localMaxAmplitude = 0\n }\n\n const rms = Math.sqrt(sumSquares / (end - start))\n minAmplitude = Math.min(minAmplitude, localMinAmplitude)\n maxAmplitude = Math.max(maxAmplitude, localMaxAmplitude)\n\n const energy = sumSquares\n const zcr = zeroCrossings / (end - start)\n\n const silent = rms < SILENCE_THRESHOLD\n const dB = 20 * Math.log10(rms)\n\n if (silent) {\n if (silenceStart === null) {\n silenceStart = start\n } else if (start - silenceStart > MIN_SILENCE_DURATION) {\n // Silence detected for longer than the threshold, set amplitude to 0\n localMaxAmplitude = 0\n localMinAmplitude = 0\n isSpeech = false\n }\n } else {\n silenceStart = null\n if (\n !isSpeech &&\n start - lastSpeechEnd < SPEECH_INERTIA_DURATION\n ) {\n isSpeech = true\n }\n lastSpeechEnd = end\n }\n\n const activeSpeech =\n (rms > RMS_THRESHOLD && zcr > ZCR_THRESHOLD) ||\n (isSpeech && start - lastSpeechEnd < SPEECH_INERTIA_DURATION)\n\n if (activeSpeech) {\n isSpeech = true\n lastSpeechEnd = end\n } else {\n isSpeech = false\n }\n\n const bytesPerSample = bitDepth / 8\n const startPosition = start * bytesPerSample * numberOfChannels // Calculate start position in bytes\n const endPosition = end * bytesPerSample * numberOfChannels // Calculate end position in bytes\n\n // Compute features\n const segmentData = channelData.slice(start, end)\n const mfcc = features.mfcc\n ? extractMFCC(segmentData, sampleRate)\n : []\n const spectralCentroid = features.spectralCentroid\n ? extractSpectralCentroid(segmentData, sampleRate)\n : 0\n const spectralFlatness = features.spectralFlatness\n ? extractSpectralFlatness(segmentData)\n : 0\n const spectralRollOff = features.spectralRollOff\n ? extractSpectralRollOff(segmentData, sampleRate)\n : 0\n const spectralBandwidth = features.spectralBandwidth\n ? extractSpectralBandwidth(segmentData, sampleRate)\n : 0\n const chromagram = features.chromagram\n ? extractChromagram(segmentData, sampleRate)\n : []\n const hnr = features.hnr ? extractHNR(segmentData) : 0\n\n const peakAmp = Math.max(Math.abs(localMaxAmplitude), Math.abs(localMinAmplitude))\n const newData = {\n id: uniqueIdCounter++, // Assign unique ID and increment the counter\n amplitude: algorithm === 'peak' ? peakAmp : rms,\n activeSpeech,\n dB,\n silent,\n features: {\n energy,\n rms,\n minAmplitude: localMinAmplitude,\n maxAmplitude: localMaxAmplitude,\n zcr,\n mfcc: [], // Placeholder for MFCC features\n spectralCentroid, // Computed spectral centroid\n spectralFlatness, // Computed spectral flatness\n spectralRollOff, // Computed spectral roll-off\n spectralBandwidth, // Computed spectral bandwidth\n chromagram, // Computed chromagram\n hnr, // Computed HNR\n },\n startTime: start / sampleRate,\n endTime: end / sampleRate,\n startPosition,\n endPosition,\n samples: end - start,\n speaker: 0, // Assuming speaker detection is to be handled later\n }\n\n dataPoints.push(newData)\n }\n\n return {\n pointsPerSecond,\n amplitudeAlgorithm: algorithm,\n durationMs: fullAudioDurationMs,\n bitDepth,\n samples: totalSamples,\n numberOfChannels,\n sampleRate,\n dataPoints,\n amplitudeRange: {\n min: minAmplitude,\n max: maxAmplitude,\n },\n speakerChanges: [], // Placeholder for future speaker detection logic\n }\n }\n\n try {\n const result = extractWaveform(\n channelData,\n sampleRate,\n pointsPerSecond,\n algorithm\n )\n self.postMessage({\n command: 'features',\n result,\n })\n } catch (error) {\n console.error('[AudioFeaturesExtractor] Error in processing', error)\n self.postMessage({ error: error.message })\n } finally {\n // Do not close the worker so it can be re-used for subsequent messages\n // self.close();\n }\n}\n`\n"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const InlineAudioWebWorker = "\nconst DEFAULT_BIT_DEPTH = 32\nconst DEFAULT_SAMPLE_RATE = 44100\n\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super()\n this.recordedBuffers = [] // Float32Array\n this.newRecBuffer = [] // Float32Array\n this.resampledBuffer = [] // Float32Array\n this.exportIntervalSamples = 0\n this.samplesSinceLastExport = 0\n this.recordSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten\n this.exportSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten\n this.recordBitDepth = DEFAULT_BIT_DEPTH // Default to 32-bit depth\n this.exportBitDepth = DEFAULT_BIT_DEPTH // To be overwritten\n this.numberOfChannels = 1 // Default to 1 channel (mono)\n this.isRecording = true\n this.port.onmessage = this.handleMessage.bind(this)\n }\n\n handleMessage(event) {\n switch (event.data.command) {\n case 'init':\n this.recordSampleRate = event.data.recordSampleRate\n this.exportSampleRate =\n event.data.exportSampleRate || event.data.recordSampleRate\n this.exportIntervalSamples =\n this.recordSampleRate * (event.data.interval / 1000)\n if (event.data.numberOfChannels) {\n this.numberOfChannels = event.data.numberOfChannels\n }\n if (event.data.recordBitDepth) {\n this.recordBitDepth = event.data.recordBitDepth\n }\n this.exportBitDepth =\n event.data.exportBitDepth ||\n this.recordBitDepth ||\n DEFAULT_BIT_DEPTH\n break\n case 'stop':\n this.isRecording = false\n this.getAllRecordedData()\n .then((fullRecordedData) => {\n this.port.postMessage({\n command: 'recordedData',\n recordedData: fullRecordedData,\n bitDepth: this.exportBitDepth,\n sampleRate: this.exportSampleRate,\n })\n return fullRecordedData\n })\n .catch((error) => {\n console.error(\n 'RecorderProcessor Error extracting recorded data:',\n error\n )\n })\n break\n }\n }\n\n process(inputs, _outputs, _parameters) {\n if (!this.isRecording) return true\n const input = inputs[0]\n if (input.length > 0) {\n const newBuffer = new Float32Array(input[0])\n this.newRecBuffer.push(newBuffer)\n this.recordedBuffers.push(newBuffer)\n this.samplesSinceLastExport += newBuffer.length\n\n if (this.samplesSinceLastExport >= this.exportIntervalSamples) {\n this.exportNewData()\n this.samplesSinceLastExport = 0\n }\n }\n return true\n }\n\n mergeBuffers(bufferArray, recLength) {\n const result = new Float32Array(recLength)\n let offset = 0\n for (let i = 0; i < bufferArray.length; i++) {\n result.set(bufferArray[i], offset)\n offset += bufferArray[i].length\n }\n return result\n }\n\n floatTo16BitPCM(input) {\n const output = new Int16Array(input.length)\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]))\n output[i] = s < 0 ? s * 0x8000 : s * 0x7fff\n }\n console.debug(\n 'RecorderProcessor Float to 16-bit PCM conversion complete. Output byte length:',\n output.byteLength\n )\n return output\n }\n\n floatTo32BitPCM(input) {\n const output = new Int32Array(input.length)\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]))\n output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff\n }\n console.debug(\n 'RecorderProcessor Float to 32-bit PCM conversion complete. Output byte length:',\n output.byteLength\n )\n return output\n }\n\n resample(samples, targetSampleRate) {\n if (this.recordSampleRate === targetSampleRate) {\n return samples\n }\n const resampledBuffer = new Float32Array(\n (samples.length * targetSampleRate) / this.recordSampleRate\n )\n const ratio = this.recordSampleRate / targetSampleRate\n let offset = 0\n for (let i = 0; i < resampledBuffer.length; i++) {\n const nextOffset = Math.floor((i + 1) * ratio)\n let accum = 0\n let count = 0\n for (let j = offset; j < nextOffset && j < samples.length; j++) {\n accum += samples[j]\n count++\n }\n resampledBuffer[i] = accum / count\n offset = nextOffset\n }\n return resampledBuffer\n }\n\n async resampleBuffer(buffer, targetSampleRate) {\n if (typeof OfflineAudioContext === 'undefined') {\n return this.resample(buffer, targetSampleRate)\n }\n\n if (this.recordSampleRate === targetSampleRate) {\n return buffer\n }\n const offlineContext = new OfflineAudioContext(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate\n )\n const sourceBuffer = offlineContext.createBuffer(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate\n )\n sourceBuffer.copyToChannel(buffer, 0)\n\n const bufferSource = offlineContext.createBufferSource()\n bufferSource.buffer = sourceBuffer\n bufferSource.connect(offlineContext.destination)\n bufferSource.start()\n\n const renderedBuffer = await offlineContext.startRendering()\n\n const resampledBuffer = new Float32Array(renderedBuffer.length)\n renderedBuffer.copyFromChannel(resampledBuffer, 0)\n\n return resampledBuffer\n }\n\n async exportNewData() {\n // Calculate the total length of the new recorded buffers\n const length = this.newRecBuffer.reduce(\n (acc, buffer) => acc + buffer.length,\n 0\n )\n\n // Merge all new recorded buffers into a single buffer\n const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length)\n\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate\n )\n\n let finalBuffer = resampledBuffer // Float32Array\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer)\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer)\n }\n }\n\n const originalSize = mergedBuffer.byteLength\n const resampledSize = resampledBuffer.byteLength\n const finalSize = finalBuffer.byteLength\n\n // Clear the new recorded buffers after they have been processed\n this.newRecBuffer.length = 0\n\n // Post the message to the main thread\n // The first argument is the message data, containing the encoded WAV buffer\n // The second argument is the transfer list, which transfers ownership of the ArrayBuffer\n // to the main thread, avoiding the need to copy the buffer and improving performance\n // this.port.postMessage({ recordedData: encodedWav.buffer, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);\n this.port.postMessage(\n {\n command: 'newData',\n recordedData: finalBuffer,\n sampleRate: this.exportSampleRate,\n bitDepth: this.exportBitDepth,\n },\n []\n )\n }\n\n async getAllRecordedData() {\n const length = this.recordedBuffers.reduce(\n (acc, buffer) => acc + buffer.length,\n 0\n )\n const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length)\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate\n )\n // Convert to the desired bit depth if necessary\n let finalBuffer = resampledBuffer\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer)\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer)\n }\n }\n\n const originalSize = mergedBuffer.byteLength\n const resampledSize = resampledBuffer.byteLength\n const finalSize = finalBuffer.byteLength\n\n this.recordedBuffers.length = 0 // Clear the buffers after extraction\n\n return finalBuffer\n }\n}\n\nregisterProcessor('recorder-processor', RecorderProcessor)\n";
|
|
2
|
+
//# sourceMappingURL=inlineAudioWebWorker.web.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inlineAudioWebWorker.web.d.ts","sourceRoot":"","sources":["../../src/workers/inlineAudioWebWorker.web.tsx"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,urSAyPhC,CAAA"}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
export const InlineAudioWebWorker = `
|
|
2
|
+
const DEFAULT_BIT_DEPTH = 32
|
|
3
|
+
const DEFAULT_SAMPLE_RATE = 44100
|
|
4
|
+
|
|
5
|
+
class RecorderProcessor extends AudioWorkletProcessor {
|
|
6
|
+
constructor() {
|
|
7
|
+
super()
|
|
8
|
+
this.recordedBuffers = [] // Float32Array
|
|
9
|
+
this.newRecBuffer = [] // Float32Array
|
|
10
|
+
this.resampledBuffer = [] // Float32Array
|
|
11
|
+
this.exportIntervalSamples = 0
|
|
12
|
+
this.samplesSinceLastExport = 0
|
|
13
|
+
this.recordSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten
|
|
14
|
+
this.exportSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten
|
|
15
|
+
this.recordBitDepth = DEFAULT_BIT_DEPTH // Default to 32-bit depth
|
|
16
|
+
this.exportBitDepth = DEFAULT_BIT_DEPTH // To be overwritten
|
|
17
|
+
this.numberOfChannels = 1 // Default to 1 channel (mono)
|
|
18
|
+
this.isRecording = true
|
|
19
|
+
this.port.onmessage = this.handleMessage.bind(this)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
handleMessage(event) {
|
|
23
|
+
switch (event.data.command) {
|
|
24
|
+
case 'init':
|
|
25
|
+
this.recordSampleRate = event.data.recordSampleRate
|
|
26
|
+
this.exportSampleRate =
|
|
27
|
+
event.data.exportSampleRate || event.data.recordSampleRate
|
|
28
|
+
this.exportIntervalSamples =
|
|
29
|
+
this.recordSampleRate * (event.data.interval / 1000)
|
|
30
|
+
if (event.data.numberOfChannels) {
|
|
31
|
+
this.numberOfChannels = event.data.numberOfChannels
|
|
32
|
+
}
|
|
33
|
+
if (event.data.recordBitDepth) {
|
|
34
|
+
this.recordBitDepth = event.data.recordBitDepth
|
|
35
|
+
}
|
|
36
|
+
this.exportBitDepth =
|
|
37
|
+
event.data.exportBitDepth ||
|
|
38
|
+
this.recordBitDepth ||
|
|
39
|
+
DEFAULT_BIT_DEPTH
|
|
40
|
+
break
|
|
41
|
+
case 'stop':
|
|
42
|
+
this.isRecording = false
|
|
43
|
+
this.getAllRecordedData()
|
|
44
|
+
.then((fullRecordedData) => {
|
|
45
|
+
this.port.postMessage({
|
|
46
|
+
command: 'recordedData',
|
|
47
|
+
recordedData: fullRecordedData,
|
|
48
|
+
bitDepth: this.exportBitDepth,
|
|
49
|
+
sampleRate: this.exportSampleRate,
|
|
50
|
+
})
|
|
51
|
+
return fullRecordedData
|
|
52
|
+
})
|
|
53
|
+
.catch((error) => {
|
|
54
|
+
console.error(
|
|
55
|
+
'RecorderProcessor Error extracting recorded data:',
|
|
56
|
+
error
|
|
57
|
+
)
|
|
58
|
+
})
|
|
59
|
+
break
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
process(inputs, _outputs, _parameters) {
|
|
64
|
+
if (!this.isRecording) return true
|
|
65
|
+
const input = inputs[0]
|
|
66
|
+
if (input.length > 0) {
|
|
67
|
+
const newBuffer = new Float32Array(input[0])
|
|
68
|
+
this.newRecBuffer.push(newBuffer)
|
|
69
|
+
this.recordedBuffers.push(newBuffer)
|
|
70
|
+
this.samplesSinceLastExport += newBuffer.length
|
|
71
|
+
|
|
72
|
+
if (this.samplesSinceLastExport >= this.exportIntervalSamples) {
|
|
73
|
+
this.exportNewData()
|
|
74
|
+
this.samplesSinceLastExport = 0
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return true
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
mergeBuffers(bufferArray, recLength) {
|
|
81
|
+
const result = new Float32Array(recLength)
|
|
82
|
+
let offset = 0
|
|
83
|
+
for (let i = 0; i < bufferArray.length; i++) {
|
|
84
|
+
result.set(bufferArray[i], offset)
|
|
85
|
+
offset += bufferArray[i].length
|
|
86
|
+
}
|
|
87
|
+
return result
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
floatTo16BitPCM(input) {
|
|
91
|
+
const output = new Int16Array(input.length)
|
|
92
|
+
for (let i = 0; i < input.length; i++) {
|
|
93
|
+
const s = Math.max(-1, Math.min(1, input[i]))
|
|
94
|
+
output[i] = s < 0 ? s * 0x8000 : s * 0x7fff
|
|
95
|
+
}
|
|
96
|
+
console.debug(
|
|
97
|
+
'RecorderProcessor Float to 16-bit PCM conversion complete. Output byte length:',
|
|
98
|
+
output.byteLength
|
|
99
|
+
)
|
|
100
|
+
return output
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
floatTo32BitPCM(input) {
|
|
104
|
+
const output = new Int32Array(input.length)
|
|
105
|
+
for (let i = 0; i < input.length; i++) {
|
|
106
|
+
const s = Math.max(-1, Math.min(1, input[i]))
|
|
107
|
+
output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff
|
|
108
|
+
}
|
|
109
|
+
console.debug(
|
|
110
|
+
'RecorderProcessor Float to 32-bit PCM conversion complete. Output byte length:',
|
|
111
|
+
output.byteLength
|
|
112
|
+
)
|
|
113
|
+
return output
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
resample(samples, targetSampleRate) {
|
|
117
|
+
if (this.recordSampleRate === targetSampleRate) {
|
|
118
|
+
return samples
|
|
119
|
+
}
|
|
120
|
+
const resampledBuffer = new Float32Array(
|
|
121
|
+
(samples.length * targetSampleRate) / this.recordSampleRate
|
|
122
|
+
)
|
|
123
|
+
const ratio = this.recordSampleRate / targetSampleRate
|
|
124
|
+
let offset = 0
|
|
125
|
+
for (let i = 0; i < resampledBuffer.length; i++) {
|
|
126
|
+
const nextOffset = Math.floor((i + 1) * ratio)
|
|
127
|
+
let accum = 0
|
|
128
|
+
let count = 0
|
|
129
|
+
for (let j = offset; j < nextOffset && j < samples.length; j++) {
|
|
130
|
+
accum += samples[j]
|
|
131
|
+
count++
|
|
132
|
+
}
|
|
133
|
+
resampledBuffer[i] = accum / count
|
|
134
|
+
offset = nextOffset
|
|
135
|
+
}
|
|
136
|
+
return resampledBuffer
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async resampleBuffer(buffer, targetSampleRate) {
|
|
140
|
+
if (typeof OfflineAudioContext === 'undefined') {
|
|
141
|
+
return this.resample(buffer, targetSampleRate)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (this.recordSampleRate === targetSampleRate) {
|
|
145
|
+
return buffer
|
|
146
|
+
}
|
|
147
|
+
const offlineContext = new OfflineAudioContext(
|
|
148
|
+
this.numberOfChannels,
|
|
149
|
+
buffer.length,
|
|
150
|
+
this.recordSampleRate
|
|
151
|
+
)
|
|
152
|
+
const sourceBuffer = offlineContext.createBuffer(
|
|
153
|
+
this.numberOfChannels,
|
|
154
|
+
buffer.length,
|
|
155
|
+
this.recordSampleRate
|
|
156
|
+
)
|
|
157
|
+
sourceBuffer.copyToChannel(buffer, 0)
|
|
158
|
+
|
|
159
|
+
const bufferSource = offlineContext.createBufferSource()
|
|
160
|
+
bufferSource.buffer = sourceBuffer
|
|
161
|
+
bufferSource.connect(offlineContext.destination)
|
|
162
|
+
bufferSource.start()
|
|
163
|
+
|
|
164
|
+
const renderedBuffer = await offlineContext.startRendering()
|
|
165
|
+
|
|
166
|
+
const resampledBuffer = new Float32Array(renderedBuffer.length)
|
|
167
|
+
renderedBuffer.copyFromChannel(resampledBuffer, 0)
|
|
168
|
+
|
|
169
|
+
return resampledBuffer
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async exportNewData() {
|
|
173
|
+
// Calculate the total length of the new recorded buffers
|
|
174
|
+
const length = this.newRecBuffer.reduce(
|
|
175
|
+
(acc, buffer) => acc + buffer.length,
|
|
176
|
+
0
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
// Merge all new recorded buffers into a single buffer
|
|
180
|
+
const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length)
|
|
181
|
+
|
|
182
|
+
const resampledBuffer = await this.resampleBuffer(
|
|
183
|
+
mergedBuffer,
|
|
184
|
+
this.exportSampleRate
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
let finalBuffer = resampledBuffer // Float32Array
|
|
188
|
+
if (this.recordBitDepth !== this.exportBitDepth) {
|
|
189
|
+
if (this.exportBitDepth === 16) {
|
|
190
|
+
finalBuffer = this.floatTo16BitPCM(resampledBuffer)
|
|
191
|
+
} else if (this.exportBitDepth === 32) {
|
|
192
|
+
finalBuffer = this.floatTo32BitPCM(resampledBuffer)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const originalSize = mergedBuffer.byteLength
|
|
197
|
+
const resampledSize = resampledBuffer.byteLength
|
|
198
|
+
const finalSize = finalBuffer.byteLength
|
|
199
|
+
|
|
200
|
+
// Clear the new recorded buffers after they have been processed
|
|
201
|
+
this.newRecBuffer.length = 0
|
|
202
|
+
|
|
203
|
+
// Post the message to the main thread
|
|
204
|
+
// The first argument is the message data, containing the encoded WAV buffer
|
|
205
|
+
// The second argument is the transfer list, which transfers ownership of the ArrayBuffer
|
|
206
|
+
// to the main thread, avoiding the need to copy the buffer and improving performance
|
|
207
|
+
// this.port.postMessage({ recordedData: encodedWav.buffer, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);
|
|
208
|
+
this.port.postMessage(
|
|
209
|
+
{
|
|
210
|
+
command: 'newData',
|
|
211
|
+
recordedData: finalBuffer,
|
|
212
|
+
sampleRate: this.exportSampleRate,
|
|
213
|
+
bitDepth: this.exportBitDepth,
|
|
214
|
+
},
|
|
215
|
+
[]
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async getAllRecordedData() {
|
|
220
|
+
const length = this.recordedBuffers.reduce(
|
|
221
|
+
(acc, buffer) => acc + buffer.length,
|
|
222
|
+
0
|
|
223
|
+
)
|
|
224
|
+
const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length)
|
|
225
|
+
const resampledBuffer = await this.resampleBuffer(
|
|
226
|
+
mergedBuffer,
|
|
227
|
+
this.exportSampleRate
|
|
228
|
+
)
|
|
229
|
+
// Convert to the desired bit depth if necessary
|
|
230
|
+
let finalBuffer = resampledBuffer
|
|
231
|
+
if (this.recordBitDepth !== this.exportBitDepth) {
|
|
232
|
+
if (this.exportBitDepth === 16) {
|
|
233
|
+
finalBuffer = this.floatTo16BitPCM(resampledBuffer)
|
|
234
|
+
} else if (this.exportBitDepth === 32) {
|
|
235
|
+
finalBuffer = this.floatTo32BitPCM(resampledBuffer)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const originalSize = mergedBuffer.byteLength
|
|
240
|
+
const resampledSize = resampledBuffer.byteLength
|
|
241
|
+
const finalSize = finalBuffer.byteLength
|
|
242
|
+
|
|
243
|
+
this.recordedBuffers.length = 0 // Clear the buffers after extraction
|
|
244
|
+
|
|
245
|
+
return finalBuffer
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
registerProcessor('recorder-processor', RecorderProcessor)
|
|
250
|
+
`;
|
|
251
|
+
//# sourceMappingURL=inlineAudioWebWorker.web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inlineAudioWebWorker.web.js","sourceRoot":"","sources":["../../src/workers/inlineAudioWebWorker.web.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyPnC,CAAA","sourcesContent":["export const InlineAudioWebWorker = `\nconst DEFAULT_BIT_DEPTH = 32\nconst DEFAULT_SAMPLE_RATE = 44100\n\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super()\n this.recordedBuffers = [] // Float32Array\n this.newRecBuffer = [] // Float32Array\n this.resampledBuffer = [] // Float32Array\n this.exportIntervalSamples = 0\n this.samplesSinceLastExport = 0\n this.recordSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten\n this.exportSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten\n this.recordBitDepth = DEFAULT_BIT_DEPTH // Default to 32-bit depth\n this.exportBitDepth = DEFAULT_BIT_DEPTH // To be overwritten\n this.numberOfChannels = 1 // Default to 1 channel (mono)\n this.isRecording = true\n this.port.onmessage = this.handleMessage.bind(this)\n }\n\n handleMessage(event) {\n switch (event.data.command) {\n case 'init':\n this.recordSampleRate = event.data.recordSampleRate\n this.exportSampleRate =\n event.data.exportSampleRate || event.data.recordSampleRate\n this.exportIntervalSamples =\n this.recordSampleRate * (event.data.interval / 1000)\n if (event.data.numberOfChannels) {\n this.numberOfChannels = event.data.numberOfChannels\n }\n if (event.data.recordBitDepth) {\n this.recordBitDepth = event.data.recordBitDepth\n }\n this.exportBitDepth =\n event.data.exportBitDepth ||\n this.recordBitDepth ||\n DEFAULT_BIT_DEPTH\n break\n case 'stop':\n this.isRecording = false\n this.getAllRecordedData()\n .then((fullRecordedData) => {\n this.port.postMessage({\n command: 'recordedData',\n recordedData: fullRecordedData,\n bitDepth: this.exportBitDepth,\n sampleRate: this.exportSampleRate,\n })\n return fullRecordedData\n })\n .catch((error) => {\n console.error(\n 'RecorderProcessor Error extracting recorded data:',\n error\n )\n })\n break\n }\n }\n\n process(inputs, _outputs, _parameters) {\n if (!this.isRecording) return true\n const input = inputs[0]\n if (input.length > 0) {\n const newBuffer = new Float32Array(input[0])\n this.newRecBuffer.push(newBuffer)\n this.recordedBuffers.push(newBuffer)\n this.samplesSinceLastExport += newBuffer.length\n\n if (this.samplesSinceLastExport >= this.exportIntervalSamples) {\n this.exportNewData()\n this.samplesSinceLastExport = 0\n }\n }\n return true\n }\n\n mergeBuffers(bufferArray, recLength) {\n const result = new Float32Array(recLength)\n let offset = 0\n for (let i = 0; i < bufferArray.length; i++) {\n result.set(bufferArray[i], offset)\n offset += bufferArray[i].length\n }\n return result\n }\n\n floatTo16BitPCM(input) {\n const output = new Int16Array(input.length)\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]))\n output[i] = s < 0 ? s * 0x8000 : s * 0x7fff\n }\n console.debug(\n 'RecorderProcessor Float to 16-bit PCM conversion complete. Output byte length:',\n output.byteLength\n )\n return output\n }\n\n floatTo32BitPCM(input) {\n const output = new Int32Array(input.length)\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]))\n output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff\n }\n console.debug(\n 'RecorderProcessor Float to 32-bit PCM conversion complete. Output byte length:',\n output.byteLength\n )\n return output\n }\n\n resample(samples, targetSampleRate) {\n if (this.recordSampleRate === targetSampleRate) {\n return samples\n }\n const resampledBuffer = new Float32Array(\n (samples.length * targetSampleRate) / this.recordSampleRate\n )\n const ratio = this.recordSampleRate / targetSampleRate\n let offset = 0\n for (let i = 0; i < resampledBuffer.length; i++) {\n const nextOffset = Math.floor((i + 1) * ratio)\n let accum = 0\n let count = 0\n for (let j = offset; j < nextOffset && j < samples.length; j++) {\n accum += samples[j]\n count++\n }\n resampledBuffer[i] = accum / count\n offset = nextOffset\n }\n return resampledBuffer\n }\n\n async resampleBuffer(buffer, targetSampleRate) {\n if (typeof OfflineAudioContext === 'undefined') {\n return this.resample(buffer, targetSampleRate)\n }\n\n if (this.recordSampleRate === targetSampleRate) {\n return buffer\n }\n const offlineContext = new OfflineAudioContext(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate\n )\n const sourceBuffer = offlineContext.createBuffer(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate\n )\n sourceBuffer.copyToChannel(buffer, 0)\n\n const bufferSource = offlineContext.createBufferSource()\n bufferSource.buffer = sourceBuffer\n bufferSource.connect(offlineContext.destination)\n bufferSource.start()\n\n const renderedBuffer = await offlineContext.startRendering()\n\n const resampledBuffer = new Float32Array(renderedBuffer.length)\n renderedBuffer.copyFromChannel(resampledBuffer, 0)\n\n return resampledBuffer\n }\n\n async exportNewData() {\n // Calculate the total length of the new recorded buffers\n const length = this.newRecBuffer.reduce(\n (acc, buffer) => acc + buffer.length,\n 0\n )\n\n // Merge all new recorded buffers into a single buffer\n const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length)\n\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate\n )\n\n let finalBuffer = resampledBuffer // Float32Array\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer)\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer)\n }\n }\n\n const originalSize = mergedBuffer.byteLength\n const resampledSize = resampledBuffer.byteLength\n const finalSize = finalBuffer.byteLength\n\n // Clear the new recorded buffers after they have been processed\n this.newRecBuffer.length = 0\n\n // Post the message to the main thread\n // The first argument is the message data, containing the encoded WAV buffer\n // The second argument is the transfer list, which transfers ownership of the ArrayBuffer\n // to the main thread, avoiding the need to copy the buffer and improving performance\n // this.port.postMessage({ recordedData: encodedWav.buffer, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);\n this.port.postMessage(\n {\n command: 'newData',\n recordedData: finalBuffer,\n sampleRate: this.exportSampleRate,\n bitDepth: this.exportBitDepth,\n },\n []\n )\n }\n\n async getAllRecordedData() {\n const length = this.recordedBuffers.reduce(\n (acc, buffer) => acc + buffer.length,\n 0\n )\n const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length)\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate\n )\n // Convert to the desired bit depth if necessary\n let finalBuffer = resampledBuffer\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer)\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer)\n }\n }\n\n const originalSize = mergedBuffer.byteLength\n const resampledSize = resampledBuffer.byteLength\n const finalSize = finalBuffer.byteLength\n\n this.recordedBuffers.length = 0 // Clear the buffers after extraction\n\n return finalBuffer\n }\n}\n\nregisterProcessor('recorder-processor', RecorderProcessor)\n`\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@siteed/expo-audio-stream",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "stream audio crossplatform",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "build/index.js",
|
|
@@ -26,10 +26,16 @@
|
|
|
26
26
|
"android",
|
|
27
27
|
"ios",
|
|
28
28
|
"cpp",
|
|
29
|
+
"plugin",
|
|
30
|
+
"app.plugin.js",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"CHANGELOG.md",
|
|
29
33
|
"generated",
|
|
34
|
+
"expo-module.config.json",
|
|
30
35
|
"README.md",
|
|
31
36
|
"package.json",
|
|
32
37
|
"*.podspec",
|
|
38
|
+
"build",
|
|
33
39
|
"!ios/build",
|
|
34
40
|
"!android/build",
|
|
35
41
|
"!android/gradle",
|