@siteed/expo-audio-stream 1.1.2 → 1.1.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/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 +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/{src/ExpoAudioStream.native.ts → build/ExpoAudioStream.native.js} +3 -3
- package/build/ExpoAudioStream.native.js.map +1 -0
- package/build/ExpoAudioStream.types.d.ts +76 -0
- package/build/ExpoAudioStream.types.d.ts.map +1 -0
- package/build/ExpoAudioStream.types.js +2 -0
- package/build/ExpoAudioStream.types.js.map +1 -0
- package/build/ExpoAudioStream.web.d.ts +42 -0
- package/build/ExpoAudioStream.web.d.ts.map +1 -0
- package/build/ExpoAudioStream.web.js +203 -0
- package/build/ExpoAudioStream.web.js.map +1 -0
- package/build/ExpoAudioStreamModule.d.ts +3 -0
- package/build/ExpoAudioStreamModule.d.ts.map +1 -0
- package/build/ExpoAudioStreamModule.js +25 -0
- package/build/ExpoAudioStreamModule.js.map +1 -0
- package/build/WebRecorder.web.d.ts +51 -0
- package/build/WebRecorder.web.d.ts.map +1 -0
- package/build/WebRecorder.web.js +298 -0
- package/build/WebRecorder.web.js.map +1 -0
- package/build/constants.d.ts +11 -0
- package/build/constants.d.ts.map +1 -0
- package/build/constants.js +14 -0
- package/build/constants.js.map +1 -0
- package/build/events.d.ts +18 -0
- package/build/events.d.ts.map +1 -0
- package/build/events.js +15 -0
- package/build/events.js.map +1 -0
- package/build/index.d.ts +11 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js.map +1 -0
- package/build/logger.d.ts +9 -0
- package/build/logger.d.ts.map +1 -0
- package/build/logger.js +13 -0
- package/build/logger.js.map +1 -0
- package/build/useAudioRecorder.d.ts +20 -0
- package/build/useAudioRecorder.d.ts.map +1 -0
- package/build/useAudioRecorder.js +271 -0
- package/build/useAudioRecorder.js.map +1 -0
- package/build/utils/BlobFix.d.ts +9 -0
- package/build/utils/BlobFix.d.ts.map +1 -0
- package/{src/utils/BlobFix.ts → build/utils/BlobFix.js} +66 -122
- package/build/utils/BlobFix.js.map +1 -0
- package/build/utils/concatenateBuffers.d.ts +8 -0
- package/build/utils/concatenateBuffers.d.ts.map +1 -0
- package/{src/utils/concatenateBuffers.ts → build/utils/concatenateBuffers.js} +10 -13
- package/build/utils/concatenateBuffers.js.map +1 -0
- package/build/utils/convertPCMToFloat32.d.ts +11 -0
- package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
- package/build/utils/convertPCMToFloat32.js +54 -0
- package/build/utils/convertPCMToFloat32.js.map +1 -0
- package/build/utils/encodingToBitDepth.d.ts +5 -0
- package/build/utils/encodingToBitDepth.d.ts.map +1 -0
- package/build/utils/encodingToBitDepth.js +13 -0
- package/build/utils/encodingToBitDepth.js.map +1 -0
- package/build/utils/getWavFileInfo.d.ts +26 -0
- package/build/utils/getWavFileInfo.d.ts.map +1 -0
- package/build/utils/getWavFileInfo.js +92 -0
- package/build/utils/getWavFileInfo.js.map +1 -0
- package/build/utils/writeWavHeader.d.ts +9 -0
- package/build/utils/writeWavHeader.d.ts.map +1 -0
- package/build/utils/writeWavHeader.js +45 -0
- package/build/utils/writeWavHeader.js.map +1 -0
- package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
- package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
- package/{src/workers/InlineFeaturesExtractor.web.tsx → build/workers/InlineFeaturesExtractor.web.js} +2 -1
- package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
- package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
- package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
- package/{src/workers/inlineAudioWebWorker.web.tsx → build/workers/inlineAudioWebWorker.web.js} +2 -1
- package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
- package/package.json +96 -96
- package/src/AudioAnalysis/AudioAnalysis.types.ts +0 -84
- package/src/AudioAnalysis/extractAudioAnalysis.ts +0 -147
- package/src/AudioAnalysis/extractWaveform.ts +0 -25
- package/src/AudioRecorder.provider.tsx +0 -70
- package/src/ExpoAudioStream.types.ts +0 -80
- package/src/ExpoAudioStream.web.ts +0 -255
- package/src/ExpoAudioStreamModule.ts +0 -31
- package/src/WebRecorder.web.ts +0 -433
- package/src/constants.ts +0 -18
- package/src/events.ts +0 -39
- package/src/index.ts +0 -24
- package/src/logger.ts +0 -22
- package/src/useAudioRecorder.tsx +0 -420
- package/src/utils/convertPCMToFloat32.ts +0 -75
- package/src/utils/encodingToBitDepth.ts +0 -18
- package/src/utils/getWavFileInfo.ts +0 -132
- package/src/utils/writeWavHeader.ts +0 -61
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BitDepth, SampleRate } from '../ExpoAudioStream.types';
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing the metadata of a WAV file.
|
|
4
|
+
*/
|
|
5
|
+
export interface WavFileInfo {
|
|
6
|
+
sampleRate: SampleRate;
|
|
7
|
+
numChannels: number;
|
|
8
|
+
bitDepth: BitDepth;
|
|
9
|
+
size: number;
|
|
10
|
+
durationMs: number;
|
|
11
|
+
audioFormatDescription: string;
|
|
12
|
+
byteRate: number;
|
|
13
|
+
blockAlign: number;
|
|
14
|
+
creationDateTime?: string;
|
|
15
|
+
comments?: string;
|
|
16
|
+
compressionType?: string;
|
|
17
|
+
dataChunkOffset: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Extracts metadata from a WAV buffer.
|
|
21
|
+
*
|
|
22
|
+
* @param arrayBuffer - The array buffer containing the WAV data.
|
|
23
|
+
* @returns A promise that resolves to the extracted metadata.
|
|
24
|
+
*/
|
|
25
|
+
export declare const getWavFileInfo: (arrayBuffer: ArrayBuffer) => Promise<WavFileInfo>;
|
|
26
|
+
//# sourceMappingURL=getWavFileInfo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getWavFileInfo.d.ts","sourceRoot":"","sources":["../../src/utils/getWavFileInfo.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAoB/D;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,UAAU,EAAE,UAAU,CAAA;IACtB,WAAW,EAAE,MAAM,CAAA;IACnB,QAAQ,EAAE,QAAQ,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,MAAM,CAAA;IAClB,sBAAsB,EAAE,MAAM,CAAA;IAC9B,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,eAAe,EAAE,MAAM,CAAA;CAC1B;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,gBACV,WAAW,KACzB,OAAO,CAAC,WAAW,CAmFrB,CAAA"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// packages/expo-audio-stream/src/utils/getWavFileInfo.ts
|
|
2
|
+
import { DATA_CHUNK_ID, DEFAULT_BIT_DEPTH, DEFAULT_SAMPLE_RATE, FMT_CHUNK_ID, INFO_CHUNK_ID, RIFF_HEADER, WAVE_HEADER, } from '../constants';
|
|
3
|
+
// Audio format descriptions
|
|
4
|
+
const AUDIO_FORMATS = {
|
|
5
|
+
1: 'PCM',
|
|
6
|
+
3: 'IEEE float',
|
|
7
|
+
6: '8-bit ITU-T G.711 A-law',
|
|
8
|
+
7: '8-bit ITU-T G.711 µ-law',
|
|
9
|
+
65534: 'WAVE_FORMAT_EXTENSIBLE',
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Extracts metadata from a WAV buffer.
|
|
13
|
+
*
|
|
14
|
+
* @param arrayBuffer - The array buffer containing the WAV data.
|
|
15
|
+
* @returns A promise that resolves to the extracted metadata.
|
|
16
|
+
*/
|
|
17
|
+
export const getWavFileInfo = async (arrayBuffer) => {
|
|
18
|
+
const view = new DataView(arrayBuffer);
|
|
19
|
+
// Check if the file is a valid RIFF/WAVE file
|
|
20
|
+
const riffHeader = view.getUint32(0, false);
|
|
21
|
+
const waveHeader = view.getUint32(8, false);
|
|
22
|
+
if (riffHeader !== RIFF_HEADER || waveHeader !== WAVE_HEADER) {
|
|
23
|
+
throw new Error('Invalid WAV file');
|
|
24
|
+
}
|
|
25
|
+
// Initialize variables for the metadata
|
|
26
|
+
let fmtChunkOffset = 12;
|
|
27
|
+
let sampleRate = DEFAULT_SAMPLE_RATE;
|
|
28
|
+
let numChannels = 0;
|
|
29
|
+
let bitDepth = DEFAULT_BIT_DEPTH;
|
|
30
|
+
let dataChunkSize = 0;
|
|
31
|
+
let audioFormat = 0;
|
|
32
|
+
let byteRate = 0;
|
|
33
|
+
let blockAlign = 0;
|
|
34
|
+
let creationDateTime = '';
|
|
35
|
+
let comments = '';
|
|
36
|
+
let dataChunkOffset = 0;
|
|
37
|
+
// Parse chunks to find the "fmt " and "data" chunks
|
|
38
|
+
while (fmtChunkOffset < view.byteLength) {
|
|
39
|
+
const chunkId = view.getUint32(fmtChunkOffset, false);
|
|
40
|
+
const chunkSize = view.getUint32(fmtChunkOffset + 4, true);
|
|
41
|
+
if (chunkId === FMT_CHUNK_ID) {
|
|
42
|
+
// "fmt "
|
|
43
|
+
audioFormat = view.getUint16(fmtChunkOffset + 8, true);
|
|
44
|
+
if (!AUDIO_FORMATS[audioFormat]) {
|
|
45
|
+
throw new Error('Unsupported WAV file format');
|
|
46
|
+
}
|
|
47
|
+
numChannels = view.getUint16(fmtChunkOffset + 10, true);
|
|
48
|
+
sampleRate = view.getUint32(fmtChunkOffset + 12, true);
|
|
49
|
+
byteRate = view.getUint32(fmtChunkOffset + 16, true);
|
|
50
|
+
blockAlign = view.getUint16(fmtChunkOffset + 20, true);
|
|
51
|
+
bitDepth = view.getUint16(fmtChunkOffset + 22, true);
|
|
52
|
+
}
|
|
53
|
+
else if (chunkId === DATA_CHUNK_ID) {
|
|
54
|
+
// "data"
|
|
55
|
+
dataChunkSize = chunkSize;
|
|
56
|
+
dataChunkOffset = fmtChunkOffset + 8; // Position after chunk header
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
else if (chunkId === INFO_CHUNK_ID) {
|
|
60
|
+
// "INFO"
|
|
61
|
+
// Read INFO chunk (assuming it contains a text-based creation date/time and comments)
|
|
62
|
+
const infoStart = fmtChunkOffset + 8;
|
|
63
|
+
const infoText = new TextDecoder().decode(new Uint8Array(arrayBuffer.slice(infoStart, infoStart + chunkSize)));
|
|
64
|
+
const infoParts = infoText.split('\0');
|
|
65
|
+
creationDateTime = infoParts[0];
|
|
66
|
+
comments = infoParts[1];
|
|
67
|
+
}
|
|
68
|
+
fmtChunkOffset += 8 + chunkSize;
|
|
69
|
+
}
|
|
70
|
+
if (!sampleRate || !numChannels || !bitDepth || !dataChunkSize) {
|
|
71
|
+
throw new Error('Incomplete WAV file information');
|
|
72
|
+
}
|
|
73
|
+
// Calculate duration
|
|
74
|
+
const bytesPerSample = bitDepth / 8;
|
|
75
|
+
const numSamples = dataChunkSize / (numChannels * bytesPerSample);
|
|
76
|
+
const durationMs = (numSamples / sampleRate) * 1000;
|
|
77
|
+
return {
|
|
78
|
+
sampleRate,
|
|
79
|
+
numChannels,
|
|
80
|
+
bitDepth,
|
|
81
|
+
size: arrayBuffer.byteLength,
|
|
82
|
+
durationMs,
|
|
83
|
+
audioFormatDescription: AUDIO_FORMATS[audioFormat],
|
|
84
|
+
byteRate,
|
|
85
|
+
blockAlign,
|
|
86
|
+
creationDateTime: creationDateTime || undefined,
|
|
87
|
+
comments: comments || undefined,
|
|
88
|
+
compressionType: audioFormat === 1 ? 'None' : AUDIO_FORMATS[audioFormat],
|
|
89
|
+
dataChunkOffset,
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=getWavFileInfo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getWavFileInfo.js","sourceRoot":"","sources":["../../src/utils/getWavFileInfo.ts"],"names":[],"mappings":"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","sourcesContent":["// packages/expo-audio-stream/src/utils/getWavFileInfo.ts\n\nimport { BitDepth, SampleRate } from '../ExpoAudioStream.types'\nimport {\n DATA_CHUNK_ID,\n DEFAULT_BIT_DEPTH,\n DEFAULT_SAMPLE_RATE,\n FMT_CHUNK_ID,\n INFO_CHUNK_ID,\n RIFF_HEADER,\n WAVE_HEADER,\n} from '../constants'\n\n// Audio format descriptions\nconst AUDIO_FORMATS: { [key: number]: string } = {\n 1: 'PCM',\n 3: 'IEEE float',\n 6: '8-bit ITU-T G.711 A-law',\n 7: '8-bit ITU-T G.711 µ-law',\n 65534: 'WAVE_FORMAT_EXTENSIBLE',\n}\n\n/**\n * Interface representing the metadata of a WAV file.\n */\nexport interface WavFileInfo {\n sampleRate: SampleRate\n numChannels: number\n bitDepth: BitDepth\n size: number // in bytes\n durationMs: number // in ms\n audioFormatDescription: string // Description of the audio format\n byteRate: number // Average bytes per second\n blockAlign: number // Number of bytes for one sample including all channels\n creationDateTime?: string // Optional creation date and time\n comments?: string // Optional comments or tags\n compressionType?: string // Optional compression type\n dataChunkOffset: number // Position of the first data chunk\n}\n\n/**\n * Extracts metadata from a WAV buffer.\n *\n * @param arrayBuffer - The array buffer containing the WAV data.\n * @returns A promise that resolves to the extracted metadata.\n */\nexport const getWavFileInfo = async (\n arrayBuffer: ArrayBuffer\n): Promise<WavFileInfo> => {\n const view = new DataView(arrayBuffer)\n\n // Check if the file is a valid RIFF/WAVE file\n const riffHeader = view.getUint32(0, false)\n const waveHeader = view.getUint32(8, false)\n if (riffHeader !== RIFF_HEADER || waveHeader !== WAVE_HEADER) {\n throw new Error('Invalid WAV file')\n }\n\n // Initialize variables for the metadata\n let fmtChunkOffset = 12\n let sampleRate: SampleRate = DEFAULT_SAMPLE_RATE\n let numChannels = 0\n let bitDepth: BitDepth = DEFAULT_BIT_DEPTH\n let dataChunkSize = 0\n let audioFormat = 0\n let byteRate = 0\n let blockAlign = 0\n let creationDateTime = ''\n let comments = ''\n let dataChunkOffset = 0\n\n // Parse chunks to find the \"fmt \" and \"data\" chunks\n while (fmtChunkOffset < view.byteLength) {\n const chunkId = view.getUint32(fmtChunkOffset, false)\n const chunkSize = view.getUint32(fmtChunkOffset + 4, true)\n if (chunkId === FMT_CHUNK_ID) {\n // \"fmt \"\n audioFormat = view.getUint16(fmtChunkOffset + 8, true)\n if (!AUDIO_FORMATS[audioFormat]) {\n throw new Error('Unsupported WAV file format')\n }\n numChannels = view.getUint16(fmtChunkOffset + 10, true)\n sampleRate = view.getUint32(fmtChunkOffset + 12, true) as SampleRate\n byteRate = view.getUint32(fmtChunkOffset + 16, true)\n blockAlign = view.getUint16(fmtChunkOffset + 20, true)\n bitDepth = view.getUint16(fmtChunkOffset + 22, true) as BitDepth\n } else if (chunkId === DATA_CHUNK_ID) {\n // \"data\"\n dataChunkSize = chunkSize\n dataChunkOffset = fmtChunkOffset + 8 // Position after chunk header\n break\n } else if (chunkId === INFO_CHUNK_ID) {\n // \"INFO\"\n // Read INFO chunk (assuming it contains a text-based creation date/time and comments)\n const infoStart = fmtChunkOffset + 8\n const infoText = new TextDecoder().decode(\n new Uint8Array(\n arrayBuffer.slice(infoStart, infoStart + chunkSize)\n )\n )\n const infoParts = infoText.split('\\0')\n creationDateTime = infoParts[0]\n comments = infoParts[1]\n }\n fmtChunkOffset += 8 + chunkSize\n }\n\n if (!sampleRate || !numChannels || !bitDepth || !dataChunkSize) {\n throw new Error('Incomplete WAV file information')\n }\n\n // Calculate duration\n const bytesPerSample = bitDepth / 8\n const numSamples = dataChunkSize / (numChannels * bytesPerSample)\n const durationMs = (numSamples / sampleRate) * 1000\n\n return {\n sampleRate,\n numChannels,\n bitDepth,\n size: arrayBuffer.byteLength,\n durationMs,\n audioFormatDescription: AUDIO_FORMATS[audioFormat],\n byteRate,\n blockAlign,\n creationDateTime: creationDateTime || undefined,\n comments: comments || undefined,\n compressionType:\n audioFormat === 1 ? 'None' : AUDIO_FORMATS[audioFormat],\n dataChunkOffset,\n }\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
interface WavHeaderOptions {
|
|
2
|
+
buffer: ArrayBuffer;
|
|
3
|
+
sampleRate: number;
|
|
4
|
+
numChannels: number;
|
|
5
|
+
bitDepth: number;
|
|
6
|
+
}
|
|
7
|
+
export declare const writeWavHeader: ({ buffer, sampleRate, numChannels, bitDepth, }: WavHeaderOptions) => ArrayBuffer;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=writeWavHeader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writeWavHeader.d.ts","sourceRoot":"","sources":["../../src/utils/writeWavHeader.ts"],"names":[],"mappings":"AACA,UAAU,gBAAgB;IACtB,MAAM,EAAE,WAAW,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;IACnB,QAAQ,EAAE,MAAM,CAAA;CACnB;AAED,eAAO,MAAM,cAAc,mDAKxB,gBAAgB,KAAG,WA+CrB,CAAA"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export const writeWavHeader = ({ buffer, sampleRate, numChannels, bitDepth, }) => {
|
|
2
|
+
const bytesPerSample = bitDepth / 8;
|
|
3
|
+
const numSamples = buffer.byteLength / (numChannels * bytesPerSample);
|
|
4
|
+
const view = new DataView(buffer);
|
|
5
|
+
const blockAlign = numChannels * bytesPerSample;
|
|
6
|
+
const byteRate = sampleRate * blockAlign;
|
|
7
|
+
// Function to write a string to the DataView
|
|
8
|
+
const writeString = (view, offset, string) => {
|
|
9
|
+
for (let i = 0; i < string.length; i++) {
|
|
10
|
+
view.setUint8(offset + i, string.charCodeAt(i));
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
// Check if the buffer already has a WAV header by looking for "RIFF" at the start
|
|
14
|
+
const existingHeader = view.getUint32(0, false) === 0x52494646; // "RIFF" in ASCII
|
|
15
|
+
if (!existingHeader) {
|
|
16
|
+
// Ensure the buffer is large enough for the WAV header
|
|
17
|
+
if (buffer.byteLength < 44) {
|
|
18
|
+
throw new Error('Buffer is too small to contain a valid WAV header');
|
|
19
|
+
}
|
|
20
|
+
// Write the WAV header
|
|
21
|
+
writeString(view, 0, 'RIFF'); // ChunkID
|
|
22
|
+
view.setUint32(4, 36 + numSamples * blockAlign, true); // ChunkSize
|
|
23
|
+
writeString(view, 8, 'WAVE'); // Format
|
|
24
|
+
writeString(view, 12, 'fmt '); // Subchunk1ID
|
|
25
|
+
view.setUint32(16, 16, true); // Subchunk1Size (16 for PCM)
|
|
26
|
+
view.setUint16(20, bitDepth === 32 ? 3 : 1, true); // AudioFormat (3 for float, 1 for PCM)
|
|
27
|
+
view.setUint16(22, numChannels, true); // NumChannels
|
|
28
|
+
view.setUint32(24, sampleRate, true); // SampleRate
|
|
29
|
+
view.setUint32(28, byteRate, true); // ByteRate
|
|
30
|
+
view.setUint16(32, blockAlign, true); // BlockAlign
|
|
31
|
+
view.setUint16(34, bitDepth, true); // BitsPerSample
|
|
32
|
+
writeString(view, 36, 'data'); // Subchunk2ID
|
|
33
|
+
view.setUint32(40, numSamples * blockAlign, true); // Subchunk2Size
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// Update the existing WAV header if necessary
|
|
37
|
+
view.setUint32(4, 36 + numSamples * blockAlign, true); // Update ChunkSize
|
|
38
|
+
view.setUint32(24, sampleRate, true); // Update SampleRate
|
|
39
|
+
view.setUint32(28, byteRate, true); // Update ByteRate
|
|
40
|
+
view.setUint16(32, blockAlign, true); // Update BlockAlign
|
|
41
|
+
view.setUint32(40, numSamples * blockAlign, true); // Update Subchunk2Size
|
|
42
|
+
}
|
|
43
|
+
return buffer;
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=writeWavHeader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writeWavHeader.js","sourceRoot":"","sources":["../../src/utils/writeWavHeader.ts"],"names":[],"mappings":"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","sourcesContent":["// packages/expo-audio-stream/src/utils/writeWavHeader.ts\ninterface WavHeaderOptions {\n buffer: ArrayBuffer\n sampleRate: number\n numChannels: number\n bitDepth: number\n}\n\nexport const writeWavHeader = ({\n buffer,\n sampleRate,\n numChannels,\n bitDepth,\n}: WavHeaderOptions): ArrayBuffer => {\n const bytesPerSample = bitDepth / 8\n const numSamples = buffer.byteLength / (numChannels * bytesPerSample)\n const view = new DataView(buffer)\n const blockAlign = numChannels * bytesPerSample\n const byteRate = sampleRate * blockAlign\n\n // Function to write a string to the DataView\n const writeString = (view: DataView, offset: number, string: string) => {\n for (let i = 0; i < string.length; i++) {\n view.setUint8(offset + i, string.charCodeAt(i))\n }\n }\n\n // Check if the buffer already has a WAV header by looking for \"RIFF\" at the start\n const existingHeader = view.getUint32(0, false) === 0x52494646 // \"RIFF\" in ASCII\n\n if (!existingHeader) {\n // Ensure the buffer is large enough for the WAV header\n if (buffer.byteLength < 44) {\n throw new Error('Buffer is too small to contain a valid WAV header')\n }\n\n // Write the WAV header\n writeString(view, 0, 'RIFF') // ChunkID\n view.setUint32(4, 36 + numSamples * blockAlign, true) // ChunkSize\n writeString(view, 8, 'WAVE') // Format\n writeString(view, 12, 'fmt ') // Subchunk1ID\n view.setUint32(16, 16, true) // Subchunk1Size (16 for PCM)\n view.setUint16(20, bitDepth === 32 ? 3 : 1, true) // AudioFormat (3 for float, 1 for PCM)\n view.setUint16(22, numChannels, true) // NumChannels\n view.setUint32(24, sampleRate, true) // SampleRate\n view.setUint32(28, byteRate, true) // ByteRate\n view.setUint16(32, blockAlign, true) // BlockAlign\n view.setUint16(34, bitDepth, true) // BitsPerSample\n writeString(view, 36, 'data') // Subchunk2ID\n view.setUint32(40, numSamples * blockAlign, true) // Subchunk2Size\n } else {\n // Update the existing WAV header if necessary\n view.setUint32(4, 36 + numSamples * blockAlign, true) // Update ChunkSize\n view.setUint32(24, sampleRate, true) // Update SampleRate\n view.setUint32(28, byteRate, true) // Update ByteRate\n view.setUint16(32, blockAlign, true) // Update BlockAlign\n view.setUint32(40, numSamples * blockAlign, true) // Update Subchunk2Size\n }\n\n return buffer\n}\n"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare 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\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 (\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";
|
|
2
|
+
//# sourceMappingURL=InlineFeaturesExtractor.web.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InlineFeaturesExtractor.web.d.ts","sourceRoot":"","sources":["../../src/workers/InlineFeaturesExtractor.web.tsx"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,4yWAuTnC,CAAA"}
|
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuTtC,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\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 (\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 @@
|
|
|
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,97 +1,97 @@
|
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
2
|
+
"name": "@siteed/expo-audio-stream",
|
|
3
|
+
"version": "1.1.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
|
+
"files": [
|
|
25
|
+
"android",
|
|
26
|
+
"ios",
|
|
27
|
+
"cpp",
|
|
28
|
+
"build",
|
|
29
|
+
"generated",
|
|
30
|
+
"README.md",
|
|
31
|
+
"package.json",
|
|
32
|
+
"*.podspec",
|
|
33
|
+
"!ios/build",
|
|
34
|
+
"!android/build",
|
|
35
|
+
"!android/gradle",
|
|
36
|
+
"!android/gradlew",
|
|
37
|
+
"!android/gradlew.bat",
|
|
38
|
+
"!android/local.properties",
|
|
39
|
+
"!**/__tests__",
|
|
40
|
+
"!**/__fixtures__",
|
|
41
|
+
"!**/__mocks__",
|
|
42
|
+
"!**/.*"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "expo-module build",
|
|
46
|
+
"clean": "expo-module clean",
|
|
47
|
+
"lint": "expo-module lint",
|
|
48
|
+
"test": "expo-module test",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"docgen": "typedoc src/index.ts --plugin typedoc-plugin-markdown --readme none --out ../../documentation_site/docs/api-reference/API",
|
|
51
|
+
"prepare": "expo-module prepare",
|
|
52
|
+
"prepublishOnly": "expo-module prepublishOnly",
|
|
53
|
+
"expo-module": "expo-module",
|
|
54
|
+
"open:ios": "open -a \"Xcode\" ../../apps/playground/ios",
|
|
55
|
+
"open:android": "open -a \"Android Studio\" ../../apps/playground/android",
|
|
56
|
+
"size": "bundle-size && size-limit",
|
|
57
|
+
"release": "./publish.sh"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@expo/config-plugins": "^7.9.1",
|
|
61
|
+
"@size-limit/preset-big-lib": "^11.1.4",
|
|
62
|
+
"@types/jest": "^29.5.12",
|
|
63
|
+
"@types/node": "^20.12.7",
|
|
64
|
+
"@types/react": "^18.0.25",
|
|
65
|
+
"@typescript-eslint/eslint-plugin": "^7.7.0",
|
|
66
|
+
"@typescript-eslint/parser": "^7.7.0",
|
|
67
|
+
"bundle-size": "^1.1.5",
|
|
68
|
+
"eslint": "^8.56.0",
|
|
69
|
+
"eslint-config-prettier": "^9.1.0",
|
|
70
|
+
"eslint-config-universe": "^12.0.0",
|
|
71
|
+
"eslint-plugin-import": "^2.29.1",
|
|
72
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
73
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
74
|
+
"eslint-plugin-react": "^7.34.1",
|
|
75
|
+
"expo-module-scripts": "^3.5.2",
|
|
76
|
+
"expo-modules-core": "^1.12.19",
|
|
77
|
+
"jest": "^29.7.0",
|
|
78
|
+
"prettier": "^3.2.5",
|
|
79
|
+
"react-native": "^0.74.4",
|
|
80
|
+
"size-limit": "^11.1.4",
|
|
81
|
+
"typedoc": "^0.26.5",
|
|
82
|
+
"typedoc-plugin-markdown": "^4.2.3",
|
|
83
|
+
"typescript": "^5.5.4"
|
|
84
|
+
},
|
|
85
|
+
"peerDependencies": {
|
|
86
|
+
"expo": "*",
|
|
87
|
+
"react": "*",
|
|
88
|
+
"react-native": "*"
|
|
89
|
+
},
|
|
90
|
+
"publishConfig": {
|
|
91
|
+
"access": "public",
|
|
92
|
+
"registry": "https://registry.npmjs.org"
|
|
93
|
+
},
|
|
94
|
+
"dependencies": {
|
|
95
|
+
"@siteed/react-native-logger": "^0.9.3"
|
|
96
|
+
}
|
|
97
|
+
}
|