@siteed/expo-audio-stream 1.0.2 → 1.0.5
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/.size-limit.json +6 -0
- package/README.md +18 -176
- package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +1 -0
- package/app.plugin.js +1 -1
- 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 +15 -2
- package/build/AudioRecorder.provider.d.ts.map +1 -1
- package/build/AudioRecorder.provider.js +21 -8
- package/build/AudioRecorder.provider.js.map +1 -1
- package/build/ExpoAudioStream.native.d.ts.map +1 -1
- package/build/ExpoAudioStream.native.js +2 -2
- package/build/ExpoAudioStream.native.js.map +1 -1
- package/build/ExpoAudioStream.types.d.ts +33 -89
- package/build/ExpoAudioStream.types.d.ts.map +1 -1
- package/build/ExpoAudioStream.types.js.map +1 -1
- package/build/ExpoAudioStream.web.d.ts +10 -9
- package/build/ExpoAudioStream.web.d.ts.map +1 -1
- package/build/ExpoAudioStream.web.js +44 -25
- package/build/ExpoAudioStream.web.js.map +1 -1
- package/build/ExpoAudioStreamModule.d.ts.map +1 -1
- package/build/ExpoAudioStreamModule.js +13 -8
- package/build/ExpoAudioStreamModule.js.map +1 -1
- package/build/{WebRecorder.d.ts → WebRecorder.web.d.ts} +13 -9
- package/build/WebRecorder.web.d.ts.map +1 -0
- package/build/{WebRecorder.js → WebRecorder.web.js} +118 -63
- 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 +9 -17
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -113
- package/build/index.js.map +1 -1
- 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/{useAudioRecording.js → useAudioRecorder.js} +90 -86
- 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 +494 -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 +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 +41 -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 +303 -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 +243 -0
- package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
- package/expo-module.config.json +8 -17
- package/ios/AudioStreamManager.swift +40 -2
- package/ios/ExpoAudioStreamModule.swift +11 -0
- package/ios/RecordingResult.swift +1 -0
- package/package.json +72 -64
- package/plugin/build/index.d.ts +1 -1
- package/plugin/build/index.js +7 -7
- package/plugin/src/index.ts +47 -47
- package/plugin/tsconfig.json +8 -13
- package/publish.sh +0 -0
- package/src/AudioAnalysis/AudioAnalysis.types.ts +84 -0
- package/src/AudioAnalysis/extractAudioAnalysis.ts +147 -0
- package/src/AudioAnalysis/extractWaveform.ts +25 -0
- package/src/AudioRecorder.provider.tsx +59 -31
- package/src/ExpoAudioStream.native.ts +2 -2
- package/src/ExpoAudioStream.types.ts +58 -116
- package/src/ExpoAudioStream.web.ts +233 -205
- package/src/ExpoAudioStreamModule.ts +18 -12
- package/src/WebRecorder.web.ts +433 -0
- package/src/constants.ts +18 -0
- package/src/events.ts +39 -0
- package/src/index.ts +15 -176
- package/src/logger.ts +23 -0
- package/src/useAudioRecorder.tsx +420 -0
- package/src/utils/BlobFix.ts +550 -0
- package/src/utils/concatenateBuffers.ts +24 -0
- package/src/utils/convertPCMToFloat32.ts +75 -0
- package/src/utils/encodingToBitDepth.ts +18 -0
- package/src/utils/getWavFileInfo.ts +132 -0
- package/src/utils/writeWavHeader.ts +56 -0
- package/src/workers/InlineFeaturesExtractor.web.tsx +302 -0
- package/src/workers/inlineAudioWebWorker.web.tsx +242 -0
- package/tsconfig.json +12 -7
- package/build/WebRecorder.d.ts.map +0 -1
- package/build/WebRecorder.js.map +0 -1
- package/build/inlineAudioWebWorker.d.ts +0 -3
- package/build/inlineAudioWebWorker.d.ts.map +0 -1
- package/build/inlineAudioWebWorker.js +0 -340
- package/build/inlineAudioWebWorker.js.map +0 -1
- package/build/useAudioRecording.d.ts +0 -38
- package/build/useAudioRecording.d.ts.map +0 -1
- package/build/useAudioRecording.js.map +0 -1
- package/build/utils.d.ts +0 -31
- package/build/utils.d.ts.map +0 -1
- package/build/utils.js +0 -143
- package/build/utils.js.map +0 -1
- package/src/WebRecorder.ts +0 -364
- package/src/inlineAudioWebWorker.tsx +0 -340
- package/src/useAudioRecording.tsx +0 -410
- package/src/utils.ts +0 -189
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6StC,CAAA","sourcesContent":["export const InlineFeaturesExtractor = `\n\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\n console.log(\"[AudioFeaturesExtractor] Worker received message\", 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 ((weightedSum / sum) * (sampleRate / 2)) / magnitudeSpectrum.length;\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 / (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 const absValue = Math.abs(value);\n localMinAmplitude = Math.min(localMinAmplitude, absValue);\n localMaxAmplitude = Math.max(localMaxAmplitude, absValue);\n\n if (absValue !== 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, rms);\n maxAmplitude = Math.max(maxAmplitude, rms);\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 (!isSpeech && start - lastSpeechEnd < SPEECH_INERTIA_DURATION) {\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 ? extractMFCC(segmentData, sampleRate) : [];\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 newData = {\n id: uniqueIdCounter++, // Assign unique ID and increment the counter\n amplitude: algorithm === \"peak\" ? localMaxAmplitude : 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 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 || this.recordBitDepth || 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(\"Error extracting recorded data:\", error);\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 \"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 \"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 command: \"newData\",\n recordedData: finalBuffer.buffer,\n sampleRate: this.exportSampleRate,\n bitDepth: this.exportBitDepth,\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.buffer;\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,4/PAiPhC,CAAA"}
|
|
@@ -0,0 +1,243 @@
|
|
|
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 || this.recordBitDepth || DEFAULT_BIT_DEPTH;
|
|
38
|
+
break;
|
|
39
|
+
case "stop":
|
|
40
|
+
this.isRecording = false;
|
|
41
|
+
this.getAllRecordedData()
|
|
42
|
+
.then((fullRecordedData) => {
|
|
43
|
+
this.port.postMessage({
|
|
44
|
+
command: "recordedData",
|
|
45
|
+
recordedData: fullRecordedData,
|
|
46
|
+
bitDepth: this.exportBitDepth,
|
|
47
|
+
sampleRate: this.exportSampleRate,
|
|
48
|
+
});
|
|
49
|
+
return fullRecordedData;
|
|
50
|
+
})
|
|
51
|
+
.catch((error) => {
|
|
52
|
+
console.error("Error extracting recorded data:", error);
|
|
53
|
+
});
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
process(inputs, outputs, parameters) {
|
|
59
|
+
if (!this.isRecording) return true;
|
|
60
|
+
const input = inputs[0];
|
|
61
|
+
if (input.length > 0) {
|
|
62
|
+
const newBuffer = new Float32Array(input[0]);
|
|
63
|
+
this.newRecBuffer.push(newBuffer);
|
|
64
|
+
this.recordedBuffers.push(newBuffer);
|
|
65
|
+
this.samplesSinceLastExport += newBuffer.length;
|
|
66
|
+
|
|
67
|
+
if (this.samplesSinceLastExport >= this.exportIntervalSamples) {
|
|
68
|
+
this.exportNewData();
|
|
69
|
+
this.samplesSinceLastExport = 0;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
mergeBuffers(bufferArray, recLength) {
|
|
76
|
+
const result = new Float32Array(recLength);
|
|
77
|
+
let offset = 0;
|
|
78
|
+
for (let i = 0; i < bufferArray.length; i++) {
|
|
79
|
+
result.set(bufferArray[i], offset);
|
|
80
|
+
offset += bufferArray[i].length;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
floatTo16BitPCM(input) {
|
|
86
|
+
const output = new Int16Array(input.length);
|
|
87
|
+
for (let i = 0; i < input.length; i++) {
|
|
88
|
+
const s = Math.max(-1, Math.min(1, input[i]));
|
|
89
|
+
output[i] = s < 0 ? s * 0x8000 : s * 0x7fff;
|
|
90
|
+
}
|
|
91
|
+
console.debug(
|
|
92
|
+
"Float to 16-bit PCM conversion complete. Output byte length:",
|
|
93
|
+
output.byteLength,
|
|
94
|
+
);
|
|
95
|
+
return output;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
floatTo32BitPCM(input) {
|
|
99
|
+
const output = new Int32Array(input.length);
|
|
100
|
+
for (let i = 0; i < input.length; i++) {
|
|
101
|
+
const s = Math.max(-1, Math.min(1, input[i]));
|
|
102
|
+
output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff;
|
|
103
|
+
}
|
|
104
|
+
console.debug(
|
|
105
|
+
"Float to 32-bit PCM conversion complete. Output byte length:",
|
|
106
|
+
output.byteLength,
|
|
107
|
+
);
|
|
108
|
+
return output;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
resample(samples, targetSampleRate) {
|
|
112
|
+
if (this.recordSampleRate === targetSampleRate) {
|
|
113
|
+
return samples;
|
|
114
|
+
}
|
|
115
|
+
const resampledBuffer = new Float32Array(
|
|
116
|
+
(samples.length * targetSampleRate) / this.recordSampleRate,
|
|
117
|
+
);
|
|
118
|
+
const ratio = this.recordSampleRate / targetSampleRate;
|
|
119
|
+
let offset = 0;
|
|
120
|
+
for (let i = 0; i < resampledBuffer.length; i++) {
|
|
121
|
+
const nextOffset = Math.floor((i + 1) * ratio);
|
|
122
|
+
let accum = 0;
|
|
123
|
+
let count = 0;
|
|
124
|
+
for (let j = offset; j < nextOffset && j < samples.length; j++) {
|
|
125
|
+
accum += samples[j];
|
|
126
|
+
count++;
|
|
127
|
+
}
|
|
128
|
+
resampledBuffer[i] = accum / count;
|
|
129
|
+
offset = nextOffset;
|
|
130
|
+
}
|
|
131
|
+
return resampledBuffer;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async resampleBuffer(buffer, targetSampleRate) {
|
|
135
|
+
if (typeof OfflineAudioContext === "undefined") {
|
|
136
|
+
return this.resample(buffer, targetSampleRate);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (this.recordSampleRate === targetSampleRate) {
|
|
140
|
+
return buffer;
|
|
141
|
+
}
|
|
142
|
+
const offlineContext = new OfflineAudioContext(
|
|
143
|
+
this.numberOfChannels,
|
|
144
|
+
buffer.length,
|
|
145
|
+
this.recordSampleRate,
|
|
146
|
+
);
|
|
147
|
+
const sourceBuffer = offlineContext.createBuffer(
|
|
148
|
+
this.numberOfChannels,
|
|
149
|
+
buffer.length,
|
|
150
|
+
this.recordSampleRate,
|
|
151
|
+
);
|
|
152
|
+
sourceBuffer.copyToChannel(buffer, 0);
|
|
153
|
+
|
|
154
|
+
const bufferSource = offlineContext.createBufferSource();
|
|
155
|
+
bufferSource.buffer = sourceBuffer;
|
|
156
|
+
bufferSource.connect(offlineContext.destination);
|
|
157
|
+
bufferSource.start();
|
|
158
|
+
|
|
159
|
+
const renderedBuffer = await offlineContext.startRendering();
|
|
160
|
+
|
|
161
|
+
const resampledBuffer = new Float32Array(renderedBuffer.length);
|
|
162
|
+
renderedBuffer.copyFromChannel(resampledBuffer, 0);
|
|
163
|
+
|
|
164
|
+
return resampledBuffer;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async exportNewData() {
|
|
168
|
+
// Calculate the total length of the new recorded buffers
|
|
169
|
+
const length = this.newRecBuffer.reduce(
|
|
170
|
+
(acc, buffer) => acc + buffer.length,
|
|
171
|
+
0,
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
// Merge all new recorded buffers into a single buffer
|
|
175
|
+
const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length);
|
|
176
|
+
|
|
177
|
+
const resampledBuffer = await this.resampleBuffer(
|
|
178
|
+
mergedBuffer,
|
|
179
|
+
this.exportSampleRate,
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
let finalBuffer = resampledBuffer; // Float32Array
|
|
183
|
+
if (this.recordBitDepth !== this.exportBitDepth) {
|
|
184
|
+
if (this.exportBitDepth === 16) {
|
|
185
|
+
finalBuffer = this.floatTo16BitPCM(resampledBuffer);
|
|
186
|
+
} else if (this.exportBitDepth === 32) {
|
|
187
|
+
finalBuffer = this.floatTo32BitPCM(resampledBuffer);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const originalSize = mergedBuffer.byteLength;
|
|
192
|
+
const resampledSize = resampledBuffer.byteLength;
|
|
193
|
+
const finalSize = finalBuffer.byteLength;
|
|
194
|
+
|
|
195
|
+
// Clear the new recorded buffers after they have been processed
|
|
196
|
+
this.newRecBuffer.length = 0;
|
|
197
|
+
|
|
198
|
+
// Post the message to the main thread
|
|
199
|
+
// The first argument is the message data, containing the encoded WAV buffer
|
|
200
|
+
// The second argument is the transfer list, which transfers ownership of the ArrayBuffer
|
|
201
|
+
// to the main thread, avoiding the need to copy the buffer and improving performance
|
|
202
|
+
// this.port.postMessage({ recordedData: encodedWav.buffer, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);
|
|
203
|
+
this.port.postMessage({
|
|
204
|
+
command: "newData",
|
|
205
|
+
recordedData: finalBuffer.buffer,
|
|
206
|
+
sampleRate: this.exportSampleRate,
|
|
207
|
+
bitDepth: this.exportBitDepth,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async getAllRecordedData() {
|
|
212
|
+
const length = this.recordedBuffers.reduce(
|
|
213
|
+
(acc, buffer) => acc + buffer.length,
|
|
214
|
+
0,
|
|
215
|
+
);
|
|
216
|
+
const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length);
|
|
217
|
+
const resampledBuffer = await this.resampleBuffer(
|
|
218
|
+
mergedBuffer,
|
|
219
|
+
this.exportSampleRate,
|
|
220
|
+
);
|
|
221
|
+
// Convert to the desired bit depth if necessary
|
|
222
|
+
let finalBuffer = resampledBuffer;
|
|
223
|
+
if (this.recordBitDepth !== this.exportBitDepth) {
|
|
224
|
+
if (this.exportBitDepth === 16) {
|
|
225
|
+
finalBuffer = this.floatTo16BitPCM(resampledBuffer);
|
|
226
|
+
} else if (this.exportBitDepth === 32) {
|
|
227
|
+
finalBuffer = this.floatTo32BitPCM(resampledBuffer);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const originalSize = mergedBuffer.byteLength;
|
|
232
|
+
const resampledSize = resampledBuffer.byteLength;
|
|
233
|
+
const finalSize = finalBuffer.byteLength;
|
|
234
|
+
|
|
235
|
+
this.recordedBuffers.length = 0; // Clear the buffers after extraction
|
|
236
|
+
|
|
237
|
+
return finalBuffer.buffer;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
registerProcessor("recorder-processor", RecorderProcessor);
|
|
242
|
+
`;
|
|
243
|
+
//# 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiPnC,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 || this.recordBitDepth || 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(\"Error extracting recorded data:\", error);\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 \"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 \"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 command: \"newData\",\n recordedData: finalBuffer.buffer,\n sampleRate: this.exportSampleRate,\n bitDepth: this.exportBitDepth,\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.buffer;\n }\n}\n\nregisterProcessor(\"recorder-processor\", RecorderProcessor);\n`\n"]}
|
package/expo-module.config.json
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
"ios"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"ExpoAudioStreamModule"
|
|
11
|
-
]
|
|
12
|
-
},
|
|
13
|
-
"android": {
|
|
14
|
-
"modules": [
|
|
15
|
-
"net.siteed.audiostream.ExpoAudioStreamModule"
|
|
16
|
-
]
|
|
17
|
-
}
|
|
18
|
-
}
|
|
2
|
+
"platforms": ["ios", "tvos", "android", "web"],
|
|
3
|
+
"ios": {
|
|
4
|
+
"modules": ["ExpoAudioStreamModule"]
|
|
5
|
+
},
|
|
6
|
+
"android": {
|
|
7
|
+
"modules": ["net.siteed.audiostream.ExpoAudioStreamModule"]
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -32,6 +32,8 @@ class AudioStreamManager: NSObject {
|
|
|
32
32
|
internal var recordingFileURL: URL?
|
|
33
33
|
private var audioProcessor: AudioProcessor?
|
|
34
34
|
private var startTime: Date?
|
|
35
|
+
private var pauseStartTime: Date?
|
|
36
|
+
|
|
35
37
|
internal var lastEmissionTime: Date?
|
|
36
38
|
internal var lastEmittedSize: Int64 = 0
|
|
37
39
|
private var emissionInterval: TimeInterval = 1.0 // Default to 1 second
|
|
@@ -144,8 +146,8 @@ class AudioStreamManager: NSObject {
|
|
|
144
146
|
|
|
145
147
|
// Calculate the duration in seconds
|
|
146
148
|
let durationInSeconds = Double(totalDataSize) / (sampleRate * channels * (bitDepth / 8))
|
|
147
|
-
let durationInMilliseconds = Int(durationInSeconds * 1000)
|
|
148
|
-
|
|
149
|
+
let durationInMilliseconds = Int(durationInSeconds * 1000) - Int(pausedDuration * 1000)
|
|
150
|
+
|
|
149
151
|
return [
|
|
150
152
|
"durationMs": durationInMilliseconds,
|
|
151
153
|
"isRecording": isRecording,
|
|
@@ -192,6 +194,8 @@ class AudioStreamManager: NSObject {
|
|
|
192
194
|
lastEmissionTime = Date()
|
|
193
195
|
accumulatedData.removeAll()
|
|
194
196
|
totalDataSize = 0
|
|
197
|
+
pausedDuration = 0
|
|
198
|
+
isPaused = false
|
|
195
199
|
|
|
196
200
|
let session = AVAudioSession.sharedInstance()
|
|
197
201
|
do {
|
|
@@ -283,6 +287,39 @@ class AudioStreamManager: NSObject {
|
|
|
283
287
|
}
|
|
284
288
|
}
|
|
285
289
|
|
|
290
|
+
/// Pauses the current audio recording.
|
|
291
|
+
func pauseRecording() {
|
|
292
|
+
guard isRecording && !isPaused else {
|
|
293
|
+
Logger.debug("Recording is not in progress or already paused.")
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
audioEngine.pause()
|
|
298
|
+
isPaused = true
|
|
299
|
+
pauseStartTime = Date()
|
|
300
|
+
|
|
301
|
+
Logger.debug("Recording paused.")
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/// Resumes the current audio recording.
|
|
305
|
+
func resumeRecording() {
|
|
306
|
+
guard isRecording && isPaused else {
|
|
307
|
+
Logger.debug("Recording is not in progress or not paused.")
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
audioEngine.prepare()
|
|
312
|
+
do {
|
|
313
|
+
try audioEngine.start()
|
|
314
|
+
isPaused = false
|
|
315
|
+
if let pauseStartTime = pauseStartTime {
|
|
316
|
+
pausedDuration += Int(Date().timeIntervalSince(pauseStartTime))
|
|
317
|
+
}
|
|
318
|
+
Logger.debug("Recording resumed.")
|
|
319
|
+
} catch {
|
|
320
|
+
Logger.debug("Error: Failed to resume recording: \(error.localizedDescription)")
|
|
321
|
+
}
|
|
322
|
+
}
|
|
286
323
|
|
|
287
324
|
/// Describes the format of the given audio format.
|
|
288
325
|
/// - Parameter format: The AVAudioFormat object to describe.
|
|
@@ -342,6 +379,7 @@ class AudioStreamManager: NSObject {
|
|
|
342
379
|
|
|
343
380
|
let result = RecordingResult(
|
|
344
381
|
fileUri: fileURL.absoluteString,
|
|
382
|
+
filename: fileURL.lastPathComponent,
|
|
345
383
|
mimeType: mimeType,
|
|
346
384
|
duration: duration,
|
|
347
385
|
size: fileSize,
|
|
@@ -178,6 +178,16 @@ public class ExpoAudioStreamModule: Module, AudioStreamManagerDelegate {
|
|
|
178
178
|
return self.streamManager.getStatus()
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
+
/// Pauses audio recording.
|
|
182
|
+
Function("pauseRecording") {
|
|
183
|
+
self.streamManager.pauseRecording()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// Resumes audio recording.
|
|
187
|
+
Function("resumeRecording") {
|
|
188
|
+
self.streamManager.resumeRecording()
|
|
189
|
+
}
|
|
190
|
+
|
|
181
191
|
/// Asynchronously stops audio recording and retrieves the recording result.
|
|
182
192
|
///
|
|
183
193
|
/// - Parameters:
|
|
@@ -187,6 +197,7 @@ public class ExpoAudioStreamModule: Module, AudioStreamManagerDelegate {
|
|
|
187
197
|
// Convert RecordingResult to a dictionary
|
|
188
198
|
let resultDict: [String: Any] = [
|
|
189
199
|
"fileUri": recordingResult.fileUri,
|
|
200
|
+
"filename": recordingResult.filename,
|
|
190
201
|
"durationMs": recordingResult.duration,
|
|
191
202
|
"size": recordingResult.size,
|
|
192
203
|
"channels": recordingResult.channels,
|
package/package.json
CHANGED
|
@@ -1,66 +1,74 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
2
|
+
"name": "@siteed/expo-audio-stream",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "stream audio crossplatform",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "build/index.js",
|
|
7
|
+
"types": "build/index.d.ts",
|
|
8
|
+
"author": "Arthur Breton <abreton@siteed.net> (https://github.com/deeeed)",
|
|
9
|
+
"homepage": "https://github.com/deeeed/expo-audio-stream#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/deeeed/expo-audio-stream.git",
|
|
13
|
+
"directory": "packages/expo-audio-stream"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/deeeed/expo-audio-stream/issues"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react-native",
|
|
20
|
+
"expo",
|
|
21
|
+
"expo-audio-stream",
|
|
22
|
+
"ExpoAudioStream"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "expo-module build",
|
|
26
|
+
"clean": "expo-module clean",
|
|
27
|
+
"lint": "expo-module lint",
|
|
28
|
+
"test": "expo-module test",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"prepare": "expo-module prepare",
|
|
31
|
+
"prepublishOnly": "expo-module prepublishOnly",
|
|
32
|
+
"expo-module": "expo-module",
|
|
33
|
+
"open:ios": "open -a \"Xcode\" ../../apps/playground/ios",
|
|
34
|
+
"open:android": "open -a \"Android Studio\" ../../apps/playground/android",
|
|
35
|
+
"size": "bundle-size && size-limit",
|
|
36
|
+
"release": "./publish.sh"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@expo/config-plugins": "^7.9.1",
|
|
40
|
+
"@size-limit/preset-big-lib": "^11.1.4",
|
|
41
|
+
"@types/jest": "^29.5.12",
|
|
42
|
+
"@types/node": "^20.12.7",
|
|
43
|
+
"@types/react": "^18.0.25",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "^7.7.0",
|
|
45
|
+
"@typescript-eslint/parser": "^7.7.0",
|
|
46
|
+
"bundle-size": "^1.1.5",
|
|
47
|
+
"eslint": "^8.56.0",
|
|
48
|
+
"eslint-config-prettier": "^9.1.0",
|
|
49
|
+
"eslint-config-universe": "^12.0.0",
|
|
50
|
+
"eslint-plugin-import": "^2.29.1",
|
|
51
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
52
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
53
|
+
"eslint-plugin-react": "^7.34.1",
|
|
54
|
+
"expo-module-scripts": "^3.5.2",
|
|
55
|
+
"expo-modules-core": "^1.12.19",
|
|
56
|
+
"jest": "^29.7.0",
|
|
57
|
+
"prettier": "^3.2.5",
|
|
58
|
+
"react-native": "^0.74.3",
|
|
59
|
+
"size-limit": "^11.1.4",
|
|
60
|
+
"typescript": "^5.5.4"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"expo": "*",
|
|
64
|
+
"react": "*",
|
|
65
|
+
"react-native": "*"
|
|
66
|
+
},
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public",
|
|
69
|
+
"registry": "https://registry.npmjs.org"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@siteed/react-native-logger": "^0.9.2"
|
|
73
|
+
}
|
|
66
74
|
}
|
package/plugin/build/index.d.ts
CHANGED
package/plugin/build/index.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const config_plugins_1 = require("@expo/config-plugins");
|
|
4
|
-
const MICROPHONE_USAGE =
|
|
4
|
+
const MICROPHONE_USAGE = 'Allow $(PRODUCT_NAME) to access your microphone';
|
|
5
5
|
const withRecordingPermission = (config, existingPerms) => {
|
|
6
6
|
if (!existingPerms) {
|
|
7
|
-
console.warn(
|
|
7
|
+
console.warn('No previous permissions provided');
|
|
8
8
|
}
|
|
9
9
|
config = (0, config_plugins_1.withInfoPlist)(config, (config) => {
|
|
10
|
-
config.modResults[
|
|
10
|
+
config.modResults['NSMicrophoneUsageDescription'] = MICROPHONE_USAGE;
|
|
11
11
|
// Add audio to UIBackgroundModes to allow background audio recording
|
|
12
12
|
const existingBackgroundModes = config.modResults.UIBackgroundModes || [];
|
|
13
|
-
if (!existingBackgroundModes.includes(
|
|
14
|
-
existingBackgroundModes.push(
|
|
13
|
+
if (!existingBackgroundModes.includes('audio')) {
|
|
14
|
+
existingBackgroundModes.push('audio');
|
|
15
15
|
}
|
|
16
16
|
config.modResults.UIBackgroundModes = existingBackgroundModes;
|
|
17
17
|
return config;
|
|
18
18
|
});
|
|
19
19
|
config = (0, config_plugins_1.withAndroidManifest)(config, (config) => {
|
|
20
20
|
const mainApplication = config_plugins_1.AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults);
|
|
21
|
-
config_plugins_1.AndroidConfig.Manifest.addMetaDataItemToMainApplication(mainApplication,
|
|
21
|
+
config_plugins_1.AndroidConfig.Manifest.addMetaDataItemToMainApplication(mainApplication, 'android.permission.RECORD_AUDIO', MICROPHONE_USAGE);
|
|
22
22
|
// Add FOREGROUND_SERVICE permission for handling background recording
|
|
23
|
-
config_plugins_1.AndroidConfig.Manifest.addMetaDataItemToMainApplication(mainApplication,
|
|
23
|
+
config_plugins_1.AndroidConfig.Manifest.addMetaDataItemToMainApplication(mainApplication, 'android.permission.FOREGROUND_SERVICE', 'This apps needs access to the foreground service to record audio in the background');
|
|
24
24
|
return config;
|
|
25
25
|
});
|
|
26
26
|
return config;
|