@siteed/expo-audio-stream 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +7 -0
  2. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +74 -0
  3. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  4. package/build/AudioAnalysis/AudioAnalysis.types.js +3 -0
  5. package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  6. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +20 -0
  7. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  8. package/build/AudioAnalysis/extractAudioAnalysis.js +88 -0
  9. package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  10. package/build/AudioAnalysis/extractWaveform.d.ts +8 -0
  11. package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  12. package/build/AudioAnalysis/extractWaveform.js +14 -0
  13. package/build/AudioAnalysis/extractWaveform.js.map +1 -0
  14. package/build/AudioRecorder.provider.d.ts +23 -0
  15. package/build/AudioRecorder.provider.d.ts.map +1 -0
  16. package/build/AudioRecorder.provider.js +36 -0
  17. package/build/AudioRecorder.provider.js.map +1 -0
  18. package/build/ExpoAudioStream.native.d.ts +3 -0
  19. package/build/ExpoAudioStream.native.d.ts.map +1 -0
  20. package/{src/ExpoAudioStream.native.ts → build/ExpoAudioStream.native.js} +3 -3
  21. package/build/ExpoAudioStream.native.js.map +1 -0
  22. package/build/ExpoAudioStream.types.d.ts +65 -0
  23. package/build/ExpoAudioStream.types.d.ts.map +1 -0
  24. package/build/ExpoAudioStream.types.js +2 -0
  25. package/build/ExpoAudioStream.types.js.map +1 -0
  26. package/build/ExpoAudioStream.web.d.ts +42 -0
  27. package/build/ExpoAudioStream.web.d.ts.map +1 -0
  28. package/build/ExpoAudioStream.web.js +203 -0
  29. package/build/ExpoAudioStream.web.js.map +1 -0
  30. package/build/ExpoAudioStreamModule.d.ts +3 -0
  31. package/build/ExpoAudioStreamModule.d.ts.map +1 -0
  32. package/build/ExpoAudioStreamModule.js +25 -0
  33. package/build/ExpoAudioStreamModule.js.map +1 -0
  34. package/build/WebRecorder.web.d.ts +51 -0
  35. package/build/WebRecorder.web.d.ts.map +1 -0
  36. package/build/WebRecorder.web.js +298 -0
  37. package/build/WebRecorder.web.js.map +1 -0
  38. package/build/constants.d.ts +11 -0
  39. package/build/constants.d.ts.map +1 -0
  40. package/build/constants.js +14 -0
  41. package/build/constants.js.map +1 -0
  42. package/build/events.d.ts +18 -0
  43. package/build/events.d.ts.map +1 -0
  44. package/build/events.js +15 -0
  45. package/build/events.js.map +1 -0
  46. package/build/index.d.ts +11 -0
  47. package/build/index.d.ts.map +1 -0
  48. package/build/index.js.map +1 -0
  49. package/build/logger.d.ts +9 -0
  50. package/build/logger.d.ts.map +1 -0
  51. package/build/logger.js +13 -0
  52. package/build/logger.js.map +1 -0
  53. package/build/useAudioRecorder.d.ts +20 -0
  54. package/build/useAudioRecorder.d.ts.map +1 -0
  55. package/build/useAudioRecorder.js +271 -0
  56. package/build/useAudioRecorder.js.map +1 -0
  57. package/build/utils/BlobFix.d.ts +9 -0
  58. package/build/utils/BlobFix.d.ts.map +1 -0
  59. package/{src/utils/BlobFix.ts → build/utils/BlobFix.js} +66 -122
  60. package/build/utils/BlobFix.js.map +1 -0
  61. package/build/utils/concatenateBuffers.d.ts +8 -0
  62. package/build/utils/concatenateBuffers.d.ts.map +1 -0
  63. package/{src/utils/concatenateBuffers.ts → build/utils/concatenateBuffers.js} +10 -13
  64. package/build/utils/concatenateBuffers.js.map +1 -0
  65. package/build/utils/convertPCMToFloat32.d.ts +11 -0
  66. package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
  67. package/build/utils/convertPCMToFloat32.js +54 -0
  68. package/build/utils/convertPCMToFloat32.js.map +1 -0
  69. package/build/utils/encodingToBitDepth.d.ts +5 -0
  70. package/build/utils/encodingToBitDepth.d.ts.map +1 -0
  71. package/build/utils/encodingToBitDepth.js +13 -0
  72. package/build/utils/encodingToBitDepth.js.map +1 -0
  73. package/build/utils/getWavFileInfo.d.ts +26 -0
  74. package/build/utils/getWavFileInfo.d.ts.map +1 -0
  75. package/build/utils/getWavFileInfo.js +92 -0
  76. package/build/utils/getWavFileInfo.js.map +1 -0
  77. package/build/utils/writeWavHeader.d.ts +9 -0
  78. package/build/utils/writeWavHeader.d.ts.map +1 -0
  79. package/build/utils/writeWavHeader.js +45 -0
  80. package/build/utils/writeWavHeader.js.map +1 -0
  81. package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
  82. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  83. package/{src/workers/InlineFeaturesExtractor.web.tsx → build/workers/InlineFeaturesExtractor.web.js} +2 -1
  84. package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
  85. package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
  86. package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  87. package/{src/workers/inlineAudioWebWorker.web.tsx → build/workers/inlineAudioWebWorker.web.js} +2 -1
  88. package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
  89. package/package.json +2 -2
  90. package/src/AudioAnalysis/AudioAnalysis.types.ts +0 -84
  91. package/src/AudioAnalysis/extractAudioAnalysis.ts +0 -147
  92. package/src/AudioAnalysis/extractWaveform.ts +0 -25
  93. package/src/AudioRecorder.provider.tsx +0 -70
  94. package/src/ExpoAudioStream.types.ts +0 -80
  95. package/src/ExpoAudioStream.web.ts +0 -255
  96. package/src/ExpoAudioStreamModule.ts +0 -31
  97. package/src/WebRecorder.web.ts +0 -433
  98. package/src/constants.ts +0 -18
  99. package/src/events.ts +0 -39
  100. package/src/index.ts +0 -24
  101. package/src/logger.ts +0 -22
  102. package/src/useAudioRecorder.tsx +0 -420
  103. package/src/utils/convertPCMToFloat32.ts +0 -75
  104. package/src/utils/encodingToBitDepth.ts +0 -18
  105. package/src/utils/getWavFileInfo.ts +0 -132
  106. 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
+ {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.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":"AAQA,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,EAC3B,MAAM,EACN,UAAU,EACV,WAAW,EACX,QAAQ,GACO,EAAe,EAAE;IAChC,MAAM,cAAc,GAAG,QAAQ,GAAG,CAAC,CAAA;IACnC,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,GAAG,CAAC,WAAW,GAAG,cAAc,CAAC,CAAA;IACrE,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAA;IACjC,MAAM,UAAU,GAAG,WAAW,GAAG,cAAc,CAAA;IAC/C,MAAM,QAAQ,GAAG,UAAU,GAAG,UAAU,CAAA;IAExC,6CAA6C;IAC7C,MAAM,WAAW,GAAG,CAAC,IAAc,EAAE,MAAc,EAAE,MAAc,EAAE,EAAE;QACnE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MA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CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAA,CAAC,WAAW;QAC9C,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA,CAAC,aAAa;QAClD,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAA,CAAC,gBAAgB;QACnD,WAAW,CAAC,IAAI,EAAE,EAAE,EAAE,MAAM,CAAC,CAAA,CAAC,cAAc;QAC5C,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,UAAU,GAAG,UAAU,EAAE,IAAI,CAAC,CAAA,CAAC,gBAAgB;IACtE,CAAC;SAAM,CAAC;QACJ,8CAA8C;QAC9C,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,GAAG,UAAU,GAAG,UAAU,EAAE,IAAI,CAAC,CAAA,CAAC,mBAAmB;QACzE,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA,CAAC,oBAAoB;QACzD,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAA,CAAC,kBAAkB;QACrD,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA,CAAC,oBAAoB;QACzD,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,UAAU,GAAG,UAAU,EAAE,IAAI,CAAC,CAAA,CAAC,uBAAuB;IAC7E,CAAC;IAED,OAAO,MAAM,CAAA;AACjB,CAAC,CAAA","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"}
@@ -309,4 +309,5 @@ self.onmessage = function (event) {
309
309
  // self.close();
310
310
  }
311
311
  }
312
- `
312
+ `;
313
+ //# sourceMappingURL=InlineFeaturesExtractor.web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InlineFeaturesExtractor.web.js","sourceRoot":"","sources":["../../src/workers/InlineFeaturesExtractor.web.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,uBAAuB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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"}
@@ -247,4 +247,5 @@ class RecorderProcessor extends AudioWorkletProcessor {
247
247
  }
248
248
 
249
249
  registerProcessor('recorder-processor', RecorderProcessor)
250
- `
250
+ `;
251
+ //# sourceMappingURL=inlineAudioWebWorker.web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inlineAudioWebWorker.web.js","sourceRoot":"","sources":["../../src/workers/inlineAudioWebWorker.web.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyPnC,CAAA","sourcesContent":["export const InlineAudioWebWorker = `\nconst DEFAULT_BIT_DEPTH = 32\nconst DEFAULT_SAMPLE_RATE = 44100\n\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super()\n this.recordedBuffers = [] // Float32Array\n this.newRecBuffer = [] // Float32Array\n this.resampledBuffer = [] // Float32Array\n this.exportIntervalSamples = 0\n this.samplesSinceLastExport = 0\n this.recordSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten\n this.exportSampleRate = DEFAULT_SAMPLE_RATE // To be overwritten\n this.recordBitDepth = DEFAULT_BIT_DEPTH // Default to 32-bit depth\n this.exportBitDepth = DEFAULT_BIT_DEPTH // To be overwritten\n this.numberOfChannels = 1 // Default to 1 channel (mono)\n this.isRecording = true\n this.port.onmessage = this.handleMessage.bind(this)\n }\n\n handleMessage(event) {\n switch (event.data.command) {\n case 'init':\n this.recordSampleRate = event.data.recordSampleRate\n this.exportSampleRate =\n event.data.exportSampleRate || event.data.recordSampleRate\n this.exportIntervalSamples =\n this.recordSampleRate * (event.data.interval / 1000)\n if (event.data.numberOfChannels) {\n this.numberOfChannels = event.data.numberOfChannels\n }\n if (event.data.recordBitDepth) {\n this.recordBitDepth = event.data.recordBitDepth\n }\n this.exportBitDepth =\n event.data.exportBitDepth ||\n this.recordBitDepth ||\n DEFAULT_BIT_DEPTH\n break\n case 'stop':\n this.isRecording = false\n this.getAllRecordedData()\n .then((fullRecordedData) => {\n this.port.postMessage({\n command: 'recordedData',\n recordedData: fullRecordedData,\n bitDepth: this.exportBitDepth,\n sampleRate: this.exportSampleRate,\n })\n return fullRecordedData\n })\n .catch((error) => {\n console.error(\n 'RecorderProcessor Error extracting recorded data:',\n error\n )\n })\n break\n }\n }\n\n process(inputs, _outputs, _parameters) {\n if (!this.isRecording) return true\n const input = inputs[0]\n if (input.length > 0) {\n const newBuffer = new Float32Array(input[0])\n this.newRecBuffer.push(newBuffer)\n this.recordedBuffers.push(newBuffer)\n this.samplesSinceLastExport += newBuffer.length\n\n if (this.samplesSinceLastExport >= this.exportIntervalSamples) {\n this.exportNewData()\n this.samplesSinceLastExport = 0\n }\n }\n return true\n }\n\n mergeBuffers(bufferArray, recLength) {\n const result = new Float32Array(recLength)\n let offset = 0\n for (let i = 0; i < bufferArray.length; i++) {\n result.set(bufferArray[i], offset)\n offset += bufferArray[i].length\n }\n return result\n }\n\n floatTo16BitPCM(input) {\n const output = new Int16Array(input.length)\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]))\n output[i] = s < 0 ? s * 0x8000 : s * 0x7fff\n }\n console.debug(\n 'RecorderProcessor Float to 16-bit PCM conversion complete. Output byte length:',\n output.byteLength\n )\n return output\n }\n\n floatTo32BitPCM(input) {\n const output = new Int32Array(input.length)\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]))\n output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff\n }\n console.debug(\n 'RecorderProcessor Float to 32-bit PCM conversion complete. Output byte length:',\n output.byteLength\n )\n return output\n }\n\n resample(samples, targetSampleRate) {\n if (this.recordSampleRate === targetSampleRate) {\n return samples\n }\n const resampledBuffer = new Float32Array(\n (samples.length * targetSampleRate) / this.recordSampleRate\n )\n const ratio = this.recordSampleRate / targetSampleRate\n let offset = 0\n for (let i = 0; i < resampledBuffer.length; i++) {\n const nextOffset = Math.floor((i + 1) * ratio)\n let accum = 0\n let count = 0\n for (let j = offset; j < nextOffset && j < samples.length; j++) {\n accum += samples[j]\n count++\n }\n resampledBuffer[i] = accum / count\n offset = nextOffset\n }\n return resampledBuffer\n }\n\n async resampleBuffer(buffer, targetSampleRate) {\n if (typeof OfflineAudioContext === 'undefined') {\n return this.resample(buffer, targetSampleRate)\n }\n\n if (this.recordSampleRate === targetSampleRate) {\n return buffer\n }\n const offlineContext = new OfflineAudioContext(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate\n )\n const sourceBuffer = offlineContext.createBuffer(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate\n )\n sourceBuffer.copyToChannel(buffer, 0)\n\n const bufferSource = offlineContext.createBufferSource()\n bufferSource.buffer = sourceBuffer\n bufferSource.connect(offlineContext.destination)\n bufferSource.start()\n\n const renderedBuffer = await offlineContext.startRendering()\n\n const resampledBuffer = new Float32Array(renderedBuffer.length)\n renderedBuffer.copyFromChannel(resampledBuffer, 0)\n\n return resampledBuffer\n }\n\n async exportNewData() {\n // Calculate the total length of the new recorded buffers\n const length = this.newRecBuffer.reduce(\n (acc, buffer) => acc + buffer.length,\n 0\n )\n\n // Merge all new recorded buffers into a single buffer\n const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length)\n\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate\n )\n\n let finalBuffer = resampledBuffer // Float32Array\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer)\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer)\n }\n }\n\n const originalSize = mergedBuffer.byteLength\n const resampledSize = resampledBuffer.byteLength\n const finalSize = finalBuffer.byteLength\n\n // Clear the new recorded buffers after they have been processed\n this.newRecBuffer.length = 0\n\n // Post the message to the main thread\n // The first argument is the message data, containing the encoded WAV buffer\n // The second argument is the transfer list, which transfers ownership of the ArrayBuffer\n // to the main thread, avoiding the need to copy the buffer and improving performance\n // this.port.postMessage({ recordedData: encodedWav.buffer, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);\n this.port.postMessage(\n {\n command: 'newData',\n recordedData: finalBuffer,\n sampleRate: this.exportSampleRate,\n bitDepth: this.exportBitDepth,\n },\n []\n )\n }\n\n async getAllRecordedData() {\n const length = this.recordedBuffers.reduce(\n (acc, buffer) => acc + buffer.length,\n 0\n )\n const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length)\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate\n )\n // Convert to the desired bit depth if necessary\n let finalBuffer = resampledBuffer\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer)\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer)\n }\n }\n\n const originalSize = mergedBuffer.byteLength\n const resampledSize = resampledBuffer.byteLength\n const finalSize = finalBuffer.byteLength\n\n this.recordedBuffers.length = 0 // Clear the buffers after extraction\n\n return finalBuffer\n }\n}\n\nregisterProcessor('recorder-processor', RecorderProcessor)\n`\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@siteed/expo-audio-stream",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "stream audio crossplatform",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",
@@ -22,10 +22,10 @@
22
22
  "ExpoAudioStream"
23
23
  ],
24
24
  "files": [
25
- "src",
26
25
  "android",
27
26
  "ios",
28
27
  "cpp",
28
+ "build",
29
29
  "generated",
30
30
  "README.md",
31
31
  "package.json",
@@ -1,84 +0,0 @@
1
- // packages/expo-audio-stream/src/AudioAnalysis/AudioAnalysis.types.ts
2
-
3
- /**
4
- * Represents various audio features extracted from an audio signal.
5
- */
6
- export interface AudioFeatures {
7
- energy: number // The infinite integral of the squared signal, representing the overall energy of the audio.
8
- mfcc: number[] // Mel-frequency cepstral coefficients, describing the short-term power spectrum of a sound.
9
- rms: number // Root mean square value, indicating the amplitude of the audio signal.
10
- minAmplitude: number // Minimum amplitude value in the audio signal.
11
- maxAmplitude: number // Maximum amplitude value in the audio signal.
12
- zcr: number // Zero-crossing rate, indicating the rate at which the signal changes sign.
13
- spectralCentroid: number // The center of mass of the spectrum, indicating the brightness of the sound.
14
- spectralFlatness: number // Measure of the flatness of the spectrum, indicating how noise-like the signal is.
15
- spectralRolloff: number // The frequency below which a specified percentage (usually 85%) of the total spectral energy lies.
16
- spectralBandwidth: number // The width of the spectrum, indicating the range of frequencies present.
17
- chromagram: number[] // Chromagram, representing the 12 different pitch classes of the audio.
18
- tempo: number // Estimated tempo of the audio signal, measured in beats per minute (BPM).
19
- hnr: number // Harmonics-to-noise ratio, indicating the proportion of harmonics to noise in the audio signal.
20
- }
21
-
22
- /**
23
- * Options to specify which audio features to extract.
24
- */
25
- export interface AudioFeaturesOptions {
26
- energy?: boolean
27
- mfcc?: boolean
28
- rms?: boolean
29
- zcr?: boolean
30
- spectralCentroid?: boolean
31
- spectralFlatness?: boolean
32
- spectralRolloff?: boolean
33
- spectralBandwidth?: boolean
34
- chromagram?: boolean
35
- tempo?: boolean
36
- hnr?: boolean
37
- }
38
-
39
- /**
40
- * Represents a single data point in the audio analysis.
41
- */
42
- export interface DataPoint {
43
- id: number
44
- amplitude: number
45
- activeSpeech?: boolean
46
- dB?: number
47
- silent?: boolean
48
- features?: AudioFeatures
49
- startTime?: number
50
- endTime?: number
51
- // start / end position in bytes
52
- startPosition?: number
53
- endPosition?: number
54
- // number of audio samples for this point (samples size depends on bit depth)
55
- samples?: number
56
- // TODO: speaker detection
57
- speaker?: number
58
- }
59
-
60
- export type AmplitudeAlgorithm = 'peak' | 'rms'
61
-
62
- /**
63
- * Represents the complete data from the audio analysis.
64
- */
65
- export interface AudioAnalysis {
66
- pointsPerSecond: number // How many consolidated value per second
67
- durationMs: number // Duration of the audio in milliseconds
68
- bitDepth: number // Bit depth of the audio
69
- samples: number // Size of the audio in bytes
70
- numberOfChannels: number // Number of audio channels
71
- sampleRate: number // Sample rate of the audio
72
- dataPoints: DataPoint[] // Array of data points from the analysis.
73
- amplitudeAlgorithm: AmplitudeAlgorithm // Algorithm used to calculate amplitude values.
74
- amplitudeRange: {
75
- min: number
76
- max: number
77
- }
78
- // TODO: speaker detection
79
- speakerChanges?: {
80
- timestamp: number // Timestamp of the speaker change in milliseconds.
81
- speaker: number // Speaker identifier.
82
- }[]
83
- }
84
-
@@ -1,147 +0,0 @@
1
- // packages/expo-audio-stream/src/AudioAnalysis/extractAudioAnalysis.ts
2
- import {
3
- AmplitudeAlgorithm,
4
- AudioAnalysis,
5
- AudioFeaturesOptions,
6
- } from './AudioAnalysis.types'
7
- import ExpoAudioStreamModule from '../ExpoAudioStreamModule'
8
- import { isWeb } from '../constants'
9
- import { getLogger } from '../logger'
10
- import { convertPCMToFloat32 } from '../utils/convertPCMToFloat32'
11
- import { getWavFileInfo, WavFileInfo } from '../utils/getWavFileInfo'
12
- import { InlineFeaturesExtractor } from '../workers/InlineFeaturesExtractor.web'
13
-
14
- const logger = getLogger('extractAudioAnalysis')
15
-
16
- export interface ExtractAudioAnalysisProps {
17
- fileUri?: string // should provide either fileUri or arrayBuffer
18
- wavMetadata?: WavFileInfo
19
- arrayBuffer?: ArrayBuffer
20
- bitDepth?: number
21
- skipWavHeader?: boolean
22
- durationMs?: number
23
- sampleRate?: number
24
- numberOfChannels?: number
25
- algorithm?: AmplitudeAlgorithm
26
- position?: number // Optional number of bytes to skip. Default is 0
27
- length?: number // Optional number of bytes to read.
28
- pointsPerSecond?: number // Optional number of points per second. Use to reduce the number of points and compute the number of datapoints to return.
29
- features?: AudioFeaturesOptions
30
- featuresExtratorUrl?: string
31
- }
32
-
33
- export const extractAudioAnalysis = async ({
34
- fileUri,
35
- pointsPerSecond = 20,
36
- arrayBuffer,
37
- bitDepth,
38
- skipWavHeader = true,
39
- durationMs,
40
- sampleRate,
41
- numberOfChannels,
42
- algorithm = 'rms',
43
- features,
44
- featuresExtratorUrl,
45
- }: ExtractAudioAnalysisProps): Promise<AudioAnalysis> => {
46
- if (isWeb) {
47
- if (!arrayBuffer && !fileUri) {
48
- throw new Error('Either arrayBuffer or fileUri must be provided')
49
- }
50
-
51
- if (!arrayBuffer) {
52
- logger.log(`fetching fileUri`, fileUri)
53
- const response = await fetch(fileUri!)
54
-
55
- if (!response.ok) {
56
- throw new Error(
57
- `Failed to fetch fileUri: ${response.statusText}`
58
- )
59
- }
60
-
61
- arrayBuffer = await response.arrayBuffer()
62
- logger.log(`fetched fileUri`, arrayBuffer.byteLength, arrayBuffer)
63
- }
64
-
65
- // Create a new copy of the ArrayBuffer to avoid detachment issues
66
- const bufferCopy = arrayBuffer.slice(0)
67
- logger.log(
68
- `extractAudioAnalysis skipWavHeader=${skipWavHeader} bitDepth=${bitDepth} len=${bufferCopy.byteLength}`,
69
- bufferCopy.slice(0, 100)
70
- )
71
-
72
- let actualBitDepth = bitDepth
73
- if (!actualBitDepth) {
74
- logger.log(
75
- `extractAudioAnalysis bitDepth not provided -- getting wav file info`
76
- )
77
- const fileInfo = await getWavFileInfo(bufferCopy)
78
- actualBitDepth = fileInfo.bitDepth
79
- }
80
- logger.log(`extractAudioAnalysis actualBitDepth=${actualBitDepth}`)
81
-
82
- const {
83
- pcmValues: channelData,
84
- min,
85
- max,
86
- } = await convertPCMToFloat32({
87
- buffer: arrayBuffer,
88
- bitDepth: actualBitDepth,
89
- skipWavHeader,
90
- })
91
- logger.log(
92
- `extractAudioAnalysis skipWaveHeader=${skipWavHeader} convertPCMToFloat32 length=${channelData.length} range: [ ${min} :: ${max} ]`
93
- )
94
-
95
- return new Promise((resolve, reject) => {
96
- let worker: Worker
97
- if (featuresExtratorUrl) {
98
- worker = new Worker(
99
- new URL(featuresExtratorUrl, window.location.href)
100
- )
101
- } else {
102
- const blob = new Blob([InlineFeaturesExtractor], {
103
- type: 'application/javascript',
104
- })
105
- const url = URL.createObjectURL(blob)
106
- worker = new Worker(url)
107
- }
108
-
109
- worker.onmessage = (event) => {
110
- resolve(event.data.result)
111
- }
112
-
113
- worker.onerror = (error) => {
114
- reject(error)
115
- }
116
-
117
- worker.postMessage({
118
- command: 'process',
119
- channelData,
120
- sampleRate,
121
- pointsPerSecond,
122
- algorithm,
123
- bitDepth,
124
- fullAudioDurationMs: durationMs,
125
- numberOfChannels,
126
- })
127
- })
128
- } else {
129
- if (!fileUri) {
130
- throw new Error('fileUri is required')
131
- }
132
- logger.log(`extractAudioAnalysis`, {
133
- fileUri,
134
- pointsPerSecond,
135
- algorithm,
136
- })
137
- const res = await ExpoAudioStreamModule.extractAudioAnalysis({
138
- fileUri,
139
- pointsPerSecond,
140
- skipWavHeader,
141
- algorithm,
142
- features,
143
- })
144
- logger.log(`extractAudioAnalysis`, res)
145
- return res
146
- }
147
- }
@@ -1,25 +0,0 @@
1
- import ExpoAudioStreamModule from '../ExpoAudioStreamModule'
2
- import { getLogger } from '../logger'
3
-
4
- const logger = getLogger('extractWaveform')
5
- export interface ExtractWaveformProps {
6
- fileUri: string
7
- numberOfSamples: number
8
- offset?: number
9
- length?: number
10
- }
11
- export const extractWaveform = async ({
12
- fileUri,
13
- numberOfSamples,
14
- offset = 0,
15
- length,
16
- }: ExtractWaveformProps): Promise<unknown> => {
17
- const res = await ExpoAudioStreamModule.extractAudioAnalysis({
18
- fileUri,
19
- numberOfSamples,
20
- offset,
21
- length,
22
- })
23
- logger.log(`extractWaveform`, res)
24
- return res
25
- }