@siteed/expo-audio-stream 1.0.1 → 1.0.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 (142) hide show
  1. package/.size-limit.json +6 -0
  2. package/README.md +6 -6
  3. package/android/build.gradle +5 -0
  4. package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +120 -0
  5. package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +34 -4
  6. package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +635 -0
  7. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +194 -79
  8. package/android/src/main/java/net/siteed/audiostream/Constants.kt +1 -0
  9. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +48 -2
  10. package/android/src/main/java/net/siteed/audiostream/FFT.kt +44 -0
  11. package/android/src/main/java/net/siteed/audiostream/Features.kt +56 -0
  12. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +12 -0
  13. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +56 -0
  14. package/app.plugin.js +1 -1
  15. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +76 -0
  16. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  17. package/build/AudioAnalysis/AudioAnalysis.types.js +3 -0
  18. package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  19. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +4 -0
  20. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  21. package/build/AudioAnalysis/extractAudioAnalysis.js +101 -0
  22. package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  23. package/build/AudioAnalysis/extractWaveform.d.ts +8 -0
  24. package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  25. package/build/AudioAnalysis/extractWaveform.js +14 -0
  26. package/build/AudioAnalysis/extractWaveform.js.map +1 -0
  27. package/build/AudioRecorder.provider.d.ts +14 -1
  28. package/build/AudioRecorder.provider.d.ts.map +1 -1
  29. package/build/AudioRecorder.provider.js +18 -5
  30. package/build/AudioRecorder.provider.js.map +1 -1
  31. package/build/ExpoAudioStream.native.d.ts +3 -0
  32. package/build/ExpoAudioStream.native.d.ts.map +1 -0
  33. package/build/ExpoAudioStream.native.js +6 -0
  34. package/build/ExpoAudioStream.native.js.map +1 -0
  35. package/build/ExpoAudioStream.types.d.ts +35 -20
  36. package/build/ExpoAudioStream.types.d.ts.map +1 -1
  37. package/build/ExpoAudioStream.types.js.map +1 -1
  38. package/build/ExpoAudioStream.web.d.ts +42 -0
  39. package/build/ExpoAudioStream.web.d.ts.map +1 -0
  40. package/build/ExpoAudioStream.web.js +185 -0
  41. package/build/ExpoAudioStream.web.js.map +1 -0
  42. package/build/ExpoAudioStreamModule.d.ts +2 -2
  43. package/build/ExpoAudioStreamModule.d.ts.map +1 -1
  44. package/build/ExpoAudioStreamModule.js +16 -3
  45. package/build/ExpoAudioStreamModule.js.map +1 -1
  46. package/build/WebRecorder.web.d.ts +51 -0
  47. package/build/WebRecorder.web.d.ts.map +1 -0
  48. package/build/WebRecorder.web.js +288 -0
  49. package/build/WebRecorder.web.js.map +1 -0
  50. package/build/constants.d.ts +11 -0
  51. package/build/constants.d.ts.map +1 -0
  52. package/build/constants.js +14 -0
  53. package/build/constants.js.map +1 -0
  54. package/build/events.d.ts +6 -0
  55. package/build/events.d.ts.map +1 -0
  56. package/build/events.js +15 -0
  57. package/build/events.js.map +1 -0
  58. package/build/index.d.ts +8 -7
  59. package/build/index.d.ts.map +1 -1
  60. package/build/index.js +7 -14
  61. package/build/index.js.map +1 -1
  62. package/build/logger.d.ts +9 -0
  63. package/build/logger.d.ts.map +1 -0
  64. package/build/logger.js +17 -0
  65. package/build/logger.js.map +1 -0
  66. package/build/useAudioRecorder.d.ts +37 -0
  67. package/build/useAudioRecorder.d.ts.map +1 -0
  68. package/build/useAudioRecorder.js +271 -0
  69. package/build/useAudioRecorder.js.map +1 -0
  70. package/build/utils/convertPCMToFloat32.d.ts +11 -0
  71. package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
  72. package/build/utils/convertPCMToFloat32.js +41 -0
  73. package/build/utils/convertPCMToFloat32.js.map +1 -0
  74. package/build/utils/encodingToBitDepth.d.ts +5 -0
  75. package/build/utils/encodingToBitDepth.d.ts.map +1 -0
  76. package/build/utils/encodingToBitDepth.js +13 -0
  77. package/build/utils/encodingToBitDepth.js.map +1 -0
  78. package/build/utils/getWavFileInfo.d.ts +25 -0
  79. package/build/utils/getWavFileInfo.d.ts.map +1 -0
  80. package/build/utils/getWavFileInfo.js +89 -0
  81. package/build/utils/getWavFileInfo.js.map +1 -0
  82. package/build/utils/writeWavHeader.d.ts +9 -0
  83. package/build/utils/writeWavHeader.d.ts.map +1 -0
  84. package/build/utils/writeWavHeader.js +41 -0
  85. package/build/utils/writeWavHeader.js.map +1 -0
  86. package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
  87. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  88. package/build/workers/InlineFeaturesExtractor.web.js +303 -0
  89. package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
  90. package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
  91. package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  92. package/build/workers/inlineAudioWebWorker.web.js +243 -0
  93. package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
  94. package/expo-module.config.json +13 -4
  95. package/ios/AudioAnalysisData.swift +39 -0
  96. package/ios/AudioProcessingHelpers.swift +59 -0
  97. package/ios/AudioProcessor.swift +317 -0
  98. package/ios/AudioStreamError.swift +7 -0
  99. package/ios/AudioStreamManager.swift +243 -54
  100. package/ios/AudioStreamManagerDelegate.swift +4 -0
  101. package/ios/DataPoint.swift +41 -0
  102. package/ios/ExpoAudioStreamModule.swift +198 -6
  103. package/ios/Features.swift +44 -0
  104. package/ios/RecordingResult.swift +19 -0
  105. package/ios/RecordingSettings.swift +13 -0
  106. package/ios/WaveformExtractor.swift +105 -0
  107. package/package.json +13 -12
  108. package/plugin/tsconfig.json +13 -8
  109. package/publish.sh +8 -0
  110. package/src/AudioAnalysis/AudioAnalysis.types.ts +85 -0
  111. package/src/AudioAnalysis/extractAudioAnalysis.ts +136 -0
  112. package/src/AudioAnalysis/extractWaveform.ts +25 -0
  113. package/src/AudioRecorder.provider.tsx +36 -8
  114. package/src/ExpoAudioStream.native.ts +6 -0
  115. package/src/ExpoAudioStream.types.ts +50 -25
  116. package/src/ExpoAudioStream.web.ts +229 -0
  117. package/src/ExpoAudioStreamModule.ts +22 -3
  118. package/src/WebRecorder.web.ts +416 -0
  119. package/src/constants.ts +18 -0
  120. package/src/events.ts +25 -0
  121. package/src/index.ts +14 -29
  122. package/src/logger.ts +26 -0
  123. package/src/useAudioRecorder.tsx +415 -0
  124. package/src/utils/convertPCMToFloat32.ts +48 -0
  125. package/src/utils/encodingToBitDepth.ts +18 -0
  126. package/src/utils/getWavFileInfo.ts +125 -0
  127. package/src/utils/writeWavHeader.ts +56 -0
  128. package/src/workers/InlineFeaturesExtractor.web.tsx +302 -0
  129. package/src/workers/inlineAudioWebWorker.web.tsx +242 -0
  130. package/build/ExpoAudioStreamModule.web.d.ts +0 -37
  131. package/build/ExpoAudioStreamModule.web.d.ts.map +0 -1
  132. package/build/ExpoAudioStreamModule.web.js +0 -156
  133. package/build/ExpoAudioStreamModule.web.js.map +0 -1
  134. package/build/useAudioRecording.d.ts +0 -23
  135. package/build/useAudioRecording.d.ts.map +0 -1
  136. package/build/useAudioRecording.js +0 -189
  137. package/build/useAudioRecording.js.map +0 -1
  138. package/docs/demo.gif +0 -0
  139. package/release-it.js +0 -18
  140. package/src/ExpoAudioStreamModule.web.ts +0 -181
  141. package/src/useAudioRecording.ts +0 -268
  142. package/yarn-error.log +0 -7793
@@ -0,0 +1,185 @@
1
+ // src/ExpoAudioStreamModule.web.ts
2
+ import { EventEmitter } from "expo-modules-core";
3
+ import { WebRecorder } from "./WebRecorder.web";
4
+ import { getLogger } from "./logger";
5
+ import { encodingToBitDepth } from "./utils/encodingToBitDepth";
6
+ const logger = getLogger("ExpoAudioStreamWeb");
7
+ export class ExpoAudioStreamWeb extends EventEmitter {
8
+ customRecorder;
9
+ audioChunks;
10
+ isRecording;
11
+ isPaused;
12
+ recordingStartTime;
13
+ pausedTime;
14
+ currentDurationMs;
15
+ currentSize;
16
+ currentInterval;
17
+ lastEmittedSize;
18
+ lastEmittedTime;
19
+ streamUuid;
20
+ extension = "wav"; // Default extension is 'webm'
21
+ recordingConfig;
22
+ bitDepth; // Bit depth of the audio
23
+ audioWorkletUrl;
24
+ featuresExtratorUrl;
25
+ constructor({ audioWorkletUrl, featuresExtratorUrl, }) {
26
+ const mockNativeModule = {
27
+ addListener: (eventName) => {
28
+ // Not used on web
29
+ },
30
+ removeListeners: (count) => {
31
+ // Not used on web
32
+ },
33
+ };
34
+ super(mockNativeModule); // Pass the mock native module to the parent class
35
+ this.customRecorder = null;
36
+ this.audioChunks = [];
37
+ this.isRecording = false;
38
+ this.isPaused = false;
39
+ this.recordingStartTime = 0;
40
+ this.pausedTime = 0;
41
+ this.currentDurationMs = 0;
42
+ this.currentSize = 0;
43
+ this.bitDepth = 32; // Default
44
+ this.currentInterval = 1000; // Default interval in ms
45
+ this.lastEmittedSize = 0;
46
+ this.lastEmittedTime = 0;
47
+ this.streamUuid = null; // Initialize UUID on first recording start
48
+ this.audioWorkletUrl = audioWorkletUrl;
49
+ this.featuresExtratorUrl = featuresExtratorUrl;
50
+ }
51
+ // Utility to handle user media stream
52
+ async getMediaStream() {
53
+ try {
54
+ return await navigator.mediaDevices.getUserMedia({ audio: true });
55
+ }
56
+ catch (error) {
57
+ console.error("Failed to get media stream:", error);
58
+ throw error;
59
+ }
60
+ }
61
+ // Start recording with options
62
+ async startRecording(recordingConfig = {}) {
63
+ if (this.isRecording) {
64
+ throw new Error("Recording is already in progress");
65
+ }
66
+ this.bitDepth = encodingToBitDepth({
67
+ encoding: recordingConfig.encoding ?? "pcm_32bit",
68
+ });
69
+ const audioContext = new (window.AudioContext ||
70
+ // @ts-ignore - Allow webkitAudioContext for Safari
71
+ window.webkitAudioContext)();
72
+ const stream = await this.getMediaStream();
73
+ const source = audioContext.createMediaStreamSource(stream);
74
+ this.customRecorder = new WebRecorder({
75
+ audioContext,
76
+ source,
77
+ recordingConfig,
78
+ audioWorkletUrl: this.audioWorkletUrl,
79
+ featuresExtratorUrl: this.featuresExtratorUrl,
80
+ emitAudioEventCallback: ({ data, position }) => {
81
+ this.audioChunks.push(data);
82
+ this.currentSize += data.byteLength;
83
+ this.emitAudioEvent({ data, position });
84
+ this.lastEmittedTime = Date.now();
85
+ this.lastEmittedSize = this.currentSize;
86
+ },
87
+ emitAudioAnalysisCallback: (audioAnalysisData) => {
88
+ logger.log(`Emitted AudioAnalysis:`, audioAnalysisData);
89
+ this.emit("AudioAnalysis", audioAnalysisData);
90
+ },
91
+ });
92
+ await this.customRecorder.init();
93
+ this.customRecorder.start();
94
+ // // Set a timer to stop recording after 5 seconds
95
+ // setTimeout(() => {
96
+ // logger.log("AUTO Stopping recording");
97
+ // this.customRecorder?.stopAndPlay();
98
+ // this.isRecording = false;
99
+ // }, 3000);
100
+ this.isRecording = true;
101
+ this.recordingConfig = recordingConfig;
102
+ this.recordingStartTime = Date.now();
103
+ this.pausedTime = 0;
104
+ this.lastEmittedSize = 0;
105
+ this.lastEmittedTime = 0;
106
+ this.streamUuid = Date.now().toString();
107
+ const fileUri = `${this.streamUuid}.${this.extension}`;
108
+ const streamConfig = {
109
+ fileUri,
110
+ mimeType: `audio/${this.extension}`,
111
+ bitDepth: this.bitDepth,
112
+ channels: recordingConfig.channels ?? 1,
113
+ sampleRate: recordingConfig.sampleRate ?? 44100,
114
+ };
115
+ return streamConfig;
116
+ }
117
+ emitAudioEvent({ data, position }) {
118
+ const fileUri = `${this.streamUuid}.${this.extension}`;
119
+ const audioEventPayload = {
120
+ fileUri,
121
+ mimeType: `audio/${this.extension}`,
122
+ lastEmittedSize: this.lastEmittedSize, // Since this might be continuously streaming, adjust accordingly
123
+ deltaSize: data.byteLength,
124
+ position,
125
+ totalSize: this.currentSize,
126
+ buffer: data,
127
+ streamUuid: this.streamUuid ?? "", // Generate or manage UUID for stream identification
128
+ };
129
+ this.emit("AudioData", audioEventPayload);
130
+ }
131
+ // Stop recording
132
+ async stopRecording() {
133
+ if (this.customRecorder) {
134
+ const fullPcmBuffer = await this.customRecorder.stop();
135
+ logger.debug(`Stopped recording`, fullPcmBuffer);
136
+ }
137
+ this.isRecording = false;
138
+ this.currentDurationMs = Date.now() - this.recordingStartTime;
139
+ const result = {
140
+ fileUri: `${this.streamUuid}.${this.extension}`,
141
+ bitDepth: this.bitDepth,
142
+ channels: this.recordingConfig?.channels ?? 1,
143
+ sampleRate: this.recordingConfig?.sampleRate ?? 44100,
144
+ durationMs: this.currentDurationMs,
145
+ size: this.currentSize,
146
+ mimeType: `audio/${this.extension}`,
147
+ };
148
+ return result;
149
+ }
150
+ // Pause recording
151
+ async pauseRecording() {
152
+ if (!this.isRecording || this.isPaused) {
153
+ throw new Error("Recording is not active or already paused");
154
+ }
155
+ if (this.customRecorder) {
156
+ this.customRecorder.pause();
157
+ }
158
+ this.isPaused = true;
159
+ this.pausedTime = Date.now();
160
+ }
161
+ // Resume recording
162
+ async resumeRecording() {
163
+ if (!this.isPaused) {
164
+ throw new Error("Recording is not paused");
165
+ }
166
+ if (this.customRecorder) {
167
+ this.customRecorder.resume();
168
+ }
169
+ this.isPaused = false;
170
+ this.recordingStartTime += Date.now() - this.pausedTime;
171
+ }
172
+ // Get current status
173
+ status() {
174
+ const status = {
175
+ isRecording: this.isRecording,
176
+ isPaused: this.isPaused,
177
+ durationMs: Date.now() - this.recordingStartTime,
178
+ size: this.currentSize,
179
+ interval: this.currentInterval,
180
+ mimeType: `audio/${this.extension}`,
181
+ };
182
+ return status;
183
+ }
184
+ }
185
+ //# sourceMappingURL=ExpoAudioStream.web.js.map
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AC,cAAc,CAAC,MAAM,EAAE,CAAC;QAC/B,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACtB,IAAI,CAAC,kBAAkB,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC;IAC1D,CAAC;IAED,qBAAqB;IACrB,MAAM;QACJ,MAAM,MAAM,GAAsB;YAChC,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,kBAAkB;YAChD,IAAI,EAAE,IAAI,CAAC,WAAW;YACtB,QAAQ,EAAE,IAAI,CAAC,eAAe;YAC9B,QAAQ,EAAE,SAAS,IAAI,CAAC,SAAS,EAAE;SACpC,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["// src/ExpoAudioStreamModule.web.ts\nimport { EventEmitter } from \"expo-modules-core\";\n\nimport { AudioAnalysisData } from \"./AudioAnalysis/AudioAnalysis.types\";\nimport {\n AudioEventPayload,\n AudioRecordingResult,\n AudioStreamStatus,\n BitDepth,\n RecordingConfig,\n StartRecordingResult,\n} from \"./ExpoAudioStream.types\";\nimport { WebRecorder } from \"./WebRecorder.web\";\nimport { getLogger } from \"./logger\";\nimport { encodingToBitDepth } from \"./utils/encodingToBitDepth\";\n\nexport interface EmitAudioEventProps {\n data: ArrayBuffer;\n position: number;\n}\nexport type EmitAudioEventFunction = (_: EmitAudioEventProps) => void;\nexport type EmitAudioAnalysisFunction = (_: AudioAnalysisData) => void;\n\nexport interface ExpoAudioStreamWebProps {\n audioWorkletUrl: string;\n featuresExtratorUrl: string;\n}\n\nconst logger = getLogger(\"ExpoAudioStreamWeb\");\n\nexport class ExpoAudioStreamWeb extends EventEmitter {\n customRecorder: WebRecorder | null;\n audioChunks: ArrayBuffer[];\n isRecording: boolean;\n isPaused: boolean;\n recordingStartTime: number;\n pausedTime: number;\n currentDurationMs: number;\n currentSize: number;\n currentInterval: number;\n lastEmittedSize: number;\n lastEmittedTime: number;\n streamUuid: string | null;\n extension: \"webm\" | \"wav\" = \"wav\"; // Default extension is 'webm'\n recordingConfig?: RecordingConfig;\n bitDepth: BitDepth; // Bit depth of the audio\n audioWorkletUrl: string;\n featuresExtratorUrl: string;\n\n constructor({\n audioWorkletUrl,\n featuresExtratorUrl,\n }: ExpoAudioStreamWebProps) {\n const mockNativeModule = {\n addListener: (eventName: string) => {\n // Not used on web\n },\n removeListeners: (count: number) => {\n // Not used on web\n },\n };\n super(mockNativeModule); // Pass the mock native module to the parent class\n\n this.customRecorder = null;\n this.audioChunks = [];\n this.isRecording = false;\n this.isPaused = false;\n this.recordingStartTime = 0;\n this.pausedTime = 0;\n this.currentDurationMs = 0;\n this.currentSize = 0;\n this.bitDepth = 32; // Default\n this.currentInterval = 1000; // Default interval in ms\n this.lastEmittedSize = 0;\n this.lastEmittedTime = 0;\n this.streamUuid = null; // Initialize UUID on first recording start\n this.audioWorkletUrl = audioWorkletUrl;\n this.featuresExtratorUrl = featuresExtratorUrl;\n }\n\n // Utility to handle user media stream\n async getMediaStream() {\n try {\n return await navigator.mediaDevices.getUserMedia({ audio: true });\n } catch (error) {\n console.error(\"Failed to get media stream:\", error);\n throw error;\n }\n }\n\n // Start recording with options\n async startRecording(recordingConfig: RecordingConfig = {}) {\n if (this.isRecording) {\n throw new Error(\"Recording is already in progress\");\n }\n\n this.bitDepth = encodingToBitDepth({\n encoding: recordingConfig.encoding ?? \"pcm_32bit\",\n });\n\n const audioContext = new (window.AudioContext ||\n // @ts-ignore - Allow webkitAudioContext for Safari\n window.webkitAudioContext)();\n const stream = await this.getMediaStream();\n\n const source = audioContext.createMediaStreamSource(stream);\n\n this.customRecorder = new WebRecorder({\n audioContext,\n source,\n recordingConfig,\n audioWorkletUrl: this.audioWorkletUrl,\n featuresExtratorUrl: this.featuresExtratorUrl,\n emitAudioEventCallback: ({ data, position }: EmitAudioEventProps) => {\n this.audioChunks.push(data);\n this.currentSize += data.byteLength;\n this.emitAudioEvent({ data, position });\n this.lastEmittedTime = Date.now();\n this.lastEmittedSize = this.currentSize;\n },\n emitAudioAnalysisCallback: (audioAnalysisData: AudioAnalysisData) => {\n logger.log(`Emitted AudioAnalysis:`, audioAnalysisData);\n this.emit(\"AudioAnalysis\", audioAnalysisData);\n },\n });\n await this.customRecorder.init();\n this.customRecorder.start();\n\n // // Set a timer to stop recording after 5 seconds\n // setTimeout(() => {\n // logger.log(\"AUTO Stopping recording\");\n // this.customRecorder?.stopAndPlay();\n // this.isRecording = false;\n // }, 3000);\n\n this.isRecording = true;\n this.recordingConfig = recordingConfig;\n this.recordingStartTime = Date.now();\n this.pausedTime = 0;\n this.lastEmittedSize = 0;\n this.lastEmittedTime = 0;\n this.streamUuid = Date.now().toString();\n const fileUri = `${this.streamUuid}.${this.extension}`;\n const streamConfig: StartRecordingResult = {\n fileUri,\n mimeType: `audio/${this.extension}`,\n bitDepth: this.bitDepth,\n channels: recordingConfig.channels ?? 1,\n sampleRate: recordingConfig.sampleRate ?? 44100,\n };\n return streamConfig;\n }\n\n emitAudioEvent({ data, position }: EmitAudioEventProps) {\n const fileUri = `${this.streamUuid}.${this.extension}`;\n const audioEventPayload: AudioEventPayload = {\n fileUri,\n mimeType: `audio/${this.extension}`,\n lastEmittedSize: this.lastEmittedSize, // Since this might be continuously streaming, adjust accordingly\n deltaSize: data.byteLength,\n position,\n totalSize: this.currentSize,\n buffer: data,\n streamUuid: this.streamUuid ?? \"\", // Generate or manage UUID for stream identification\n };\n\n this.emit(\"AudioData\", audioEventPayload);\n }\n\n // Stop recording\n async stopRecording(): Promise<AudioRecordingResult | null> {\n if (this.customRecorder) {\n const fullPcmBuffer = await this.customRecorder.stop();\n logger.debug(`Stopped recording`, fullPcmBuffer);\n }\n this.isRecording = false;\n this.currentDurationMs = Date.now() - this.recordingStartTime;\n const result: AudioRecordingResult = {\n fileUri: `${this.streamUuid}.${this.extension}`,\n bitDepth: this.bitDepth,\n channels: this.recordingConfig?.channels ?? 1,\n sampleRate: this.recordingConfig?.sampleRate ?? 44100,\n durationMs: this.currentDurationMs,\n size: this.currentSize,\n mimeType: `audio/${this.extension}`,\n };\n\n return result;\n }\n\n // Pause recording\n async pauseRecording() {\n if (!this.isRecording || this.isPaused) {\n throw new Error(\"Recording is not active or already paused\");\n }\n\n if (this.customRecorder) {\n this.customRecorder.pause();\n }\n this.isPaused = true;\n this.pausedTime = Date.now();\n }\n\n // Resume recording\n async resumeRecording() {\n if (!this.isPaused) {\n throw new Error(\"Recording is not paused\");\n }\n\n if (this.customRecorder) {\n this.customRecorder.resume();\n }\n this.isPaused = false;\n this.recordingStartTime += Date.now() - this.pausedTime;\n }\n\n // Get current status\n status() {\n const status: AudioStreamStatus = {\n isRecording: this.isRecording,\n isPaused: this.isPaused,\n durationMs: Date.now() - this.recordingStartTime,\n size: this.currentSize,\n interval: this.currentInterval,\n mimeType: `audio/${this.extension}`,\n };\n return status;\n }\n}\n"]}
@@ -1,3 +1,3 @@
1
- declare const _default: any;
2
- export default _default;
1
+ declare let ExpoAudioStreamModule: any;
2
+ export default ExpoAudioStreamModule;
3
3
  //# sourceMappingURL=ExpoAudioStreamModule.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoAudioStreamModule.d.ts","sourceRoot":"","sources":["../src/ExpoAudioStreamModule.ts"],"names":[],"mappings":";AAIA,wBAAsD"}
1
+ {"version":3,"file":"ExpoAudioStreamModule.d.ts","sourceRoot":"","sources":["../src/ExpoAudioStreamModule.ts"],"names":[],"mappings":"AAQA,QAAA,IAAI,qBAAqB,EAAE,GAAG,CAAC;AAe/B,eAAe,qBAAqB,CAAC"}
@@ -1,5 +1,18 @@
1
1
  import { requireNativeModule } from "expo-modules-core";
2
- // It loads the native module object from the JSI or falls back to
3
- // the bridge module (from NativeModulesProxy) if the remote debugger is on.
4
- export default requireNativeModule("ExpoAudioStream");
2
+ import { Platform } from "react-native";
3
+ import { ExpoAudioStreamWeb, } from "./ExpoAudioStream.web";
4
+ let ExpoAudioStreamModule;
5
+ if (Platform.OS === "web") {
6
+ let instance = null;
7
+ ExpoAudioStreamModule = (webProps) => {
8
+ if (!instance) {
9
+ instance = new ExpoAudioStreamWeb(webProps);
10
+ }
11
+ return instance;
12
+ };
13
+ }
14
+ else {
15
+ ExpoAudioStreamModule = requireNativeModule("ExpoAudioStream");
16
+ }
17
+ export default ExpoAudioStreamModule;
5
18
  //# sourceMappingURL=ExpoAudioStreamModule.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoAudioStreamModule.js","sourceRoot":"","sources":["../src/ExpoAudioStreamModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,kEAAkE;AAClE,4EAA4E;AAC5E,eAAe,mBAAmB,CAAC,iBAAiB,CAAC,CAAC","sourcesContent":["import { requireNativeModule } from \"expo-modules-core\";\n\n// It loads the native module object from the JSI or falls back to\n// the bridge module (from NativeModulesProxy) if the remote debugger is on.\nexport default requireNativeModule(\"ExpoAudioStream\");\n"]}
1
+ {"version":3,"file":"ExpoAudioStreamModule.js","sourceRoot":"","sources":["../src/ExpoAudioStreamModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,EACL,kBAAkB,GAEnB,MAAM,uBAAuB,CAAC;AAE/B,IAAI,qBAA0B,CAAC;AAE/B,IAAI,QAAQ,CAAC,EAAE,KAAK,KAAK,EAAE,CAAC;IAC1B,IAAI,QAAQ,GAA8B,IAAI,CAAC;IAE/C,qBAAqB,GAAG,CAAC,QAAiC,EAAE,EAAE;QAC5D,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,QAAQ,GAAG,IAAI,kBAAkB,CAAC,QAAQ,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;AACJ,CAAC;KAAM,CAAC;IACN,qBAAqB,GAAG,mBAAmB,CAAC,iBAAiB,CAAC,CAAC;AACjE,CAAC;AAED,eAAe,qBAAqB,CAAC","sourcesContent":["import { requireNativeModule } from \"expo-modules-core\";\nimport { Platform } from \"react-native\";\n\nimport {\n ExpoAudioStreamWeb,\n ExpoAudioStreamWebProps,\n} from \"./ExpoAudioStream.web\";\n\nlet ExpoAudioStreamModule: any;\n\nif (Platform.OS === \"web\") {\n let instance: ExpoAudioStreamWeb | null = null;\n\n ExpoAudioStreamModule = (webProps: ExpoAudioStreamWebProps) => {\n if (!instance) {\n instance = new ExpoAudioStreamWeb(webProps);\n }\n return instance;\n };\n} else {\n ExpoAudioStreamModule = requireNativeModule(\"ExpoAudioStream\");\n}\n\nexport default ExpoAudioStreamModule;\n"]}
@@ -0,0 +1,51 @@
1
+ import { AudioAnalysisData } from "./AudioAnalysis/AudioAnalysis.types";
2
+ import { RecordingConfig } from "./ExpoAudioStream.types";
3
+ import { EmitAudioAnalysisFunction, EmitAudioEventFunction } from "./ExpoAudioStream.web";
4
+ interface AudioFeaturesEvent {
5
+ data: {
6
+ command: string;
7
+ result: AudioAnalysisData;
8
+ };
9
+ }
10
+ export declare class WebRecorder {
11
+ private audioContext;
12
+ private audioWorkletNode;
13
+ private featureExtractorWorker?;
14
+ private source;
15
+ private audioWorkletUrl;
16
+ private emitAudioEventCallback;
17
+ private emitAudioAnalysisCallback;
18
+ private config;
19
+ private position;
20
+ private numberOfChannels;
21
+ private bitDepth;
22
+ private exportBitDepth;
23
+ private buffers;
24
+ private audioAnalysisData;
25
+ constructor({ audioContext, source, recordingConfig, featuresExtratorUrl, audioWorkletUrl, emitAudioEventCallback, emitAudioAnalysisCallback, }: {
26
+ audioContext: AudioContext;
27
+ source: MediaStreamAudioSourceNode;
28
+ recordingConfig: RecordingConfig;
29
+ featuresExtratorUrl: string;
30
+ audioWorkletUrl: string;
31
+ emitAudioEventCallback: EmitAudioEventFunction;
32
+ emitAudioAnalysisCallback: EmitAudioAnalysisFunction;
33
+ });
34
+ init(): Promise<void>;
35
+ initFeatureExtractorWorker(featuresExtratorUrl?: string): void;
36
+ initFallbackWorker(): void;
37
+ handleWorkerError(error: ErrorEvent): void;
38
+ handleFeatureExtractorMessage(event: AudioFeaturesEvent): void;
39
+ start(): void;
40
+ stop(): Promise<unknown>;
41
+ pause(): void;
42
+ stopMediaStreamTracks(): void;
43
+ playRecordedData({ recordedData, }: {
44
+ recordedData: ArrayBuffer;
45
+ mimeType?: string;
46
+ }): Promise<void>;
47
+ private checkAudioContextFormat;
48
+ resume(): void;
49
+ }
50
+ export {};
51
+ //# sourceMappingURL=WebRecorder.web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebRecorder.web.d.ts","sourceRoot":"","sources":["../src/WebRecorder.web.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EACL,yBAAyB,EACzB,sBAAsB,EACvB,MAAM,uBAAuB,CAAC;AAc/B,UAAU,kBAAkB;IAC1B,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,iBAAiB,CAAC;KAC3B,CAAC;CACH;AAUD,qBAAa,WAAW;IACtB,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,gBAAgB,CAAoB;IAC5C,OAAO,CAAC,sBAAsB,CAAC,CAAS;IACxC,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,sBAAsB,CAAyB;IACvD,OAAO,CAAC,yBAAyB,CAA4B;IAC7D,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,gBAAgB,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,iBAAiB,CAAoB;gBAEjC,EACV,YAAY,EACZ,MAAM,EACN,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,sBAAsB,EACtB,yBAAyB,GAC1B,EAAE;QACD,YAAY,EAAE,YAAY,CAAC;QAC3B,MAAM,EAAE,0BAA0B,CAAC;QACnC,eAAe,EAAE,eAAe,CAAC;QACjC,mBAAmB,EAAE,MAAM,CAAC;QAC5B,eAAe,EAAE,MAAM,CAAC;QACxB,sBAAsB,EAAE,sBAAsB,CAAC;QAC/C,yBAAyB,EAAE,yBAAyB,CAAC;KACtD;IA+CK,IAAI;IA8FV,0BAA0B,CAAC,mBAAmB,CAAC,EAAE,MAAM;IAyBvD,kBAAkB;IAqBlB,iBAAiB,CAAC,KAAK,EAAE,UAAU;IAInC,6BAA6B,CAAC,KAAK,EAAE,kBAAkB;IA6BvD,KAAK;IAKL,IAAI;IA4DJ,KAAK;IAML,qBAAqB;IAMf,gBAAgB,CAAC,EACrB,YAAY,GACb,EAAE;QACD,YAAY,EAAE,WAAW,CAAC;QAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB;IAqBD,OAAO,CAAC,uBAAuB;IAoB/B,MAAM;CAKP"}
@@ -0,0 +1,288 @@
1
+ import { getLogger } from "./logger";
2
+ import { encodingToBitDepth } from "./utils/encodingToBitDepth";
3
+ import { InlineFeaturesExtractor } from "./workers/InlineFeaturesExtractor.web";
4
+ import { InlineAudioWebWorker } from "./workers/inlineAudioWebWorker.web";
5
+ const DEFAULT_WEB_BITDEPTH = 32;
6
+ const DEFAULT_WEB_POINTS_PER_SECOND = 10;
7
+ const DEFAULT_WEB_INTERVAL = 500;
8
+ const DEFAULT_WEB_NUMBER_OF_CHANNELS = 1;
9
+ const TAG = "WebRecorder";
10
+ const logger = getLogger(TAG);
11
+ export class WebRecorder {
12
+ audioContext;
13
+ audioWorkletNode;
14
+ featureExtractorWorker;
15
+ source;
16
+ audioWorkletUrl;
17
+ emitAudioEventCallback;
18
+ emitAudioAnalysisCallback;
19
+ config;
20
+ position; // Track the cumulative position
21
+ numberOfChannels; // Number of audio channels
22
+ bitDepth; // Bit depth of the audio
23
+ exportBitDepth; // Bit depth of the audio
24
+ buffers; // Array to store the buffers
25
+ audioAnalysisData; // Keep updating the full audio analysis data with latest events
26
+ constructor({ audioContext, source, recordingConfig, featuresExtratorUrl, audioWorkletUrl, emitAudioEventCallback, emitAudioAnalysisCallback, }) {
27
+ this.audioContext = audioContext;
28
+ this.source = source;
29
+ this.audioWorkletUrl = audioWorkletUrl;
30
+ this.emitAudioEventCallback = emitAudioEventCallback;
31
+ this.emitAudioAnalysisCallback = emitAudioAnalysisCallback;
32
+ this.config = recordingConfig;
33
+ this.position = 0;
34
+ this.buffers = []; // Initialize the buffers array
35
+ const audioContextFormat = this.checkAudioContextFormat({
36
+ sampleRate: this.audioContext.sampleRate,
37
+ });
38
+ logger.debug("Initialized WebRecorder with config:", {
39
+ sampleRate: audioContextFormat.sampleRate,
40
+ bitDepth: audioContextFormat.bitDepth,
41
+ numberOfChannels: audioContextFormat.numberOfChannels,
42
+ });
43
+ this.bitDepth = audioContextFormat.bitDepth;
44
+ this.numberOfChannels =
45
+ audioContextFormat.numberOfChannels || DEFAULT_WEB_NUMBER_OF_CHANNELS; // Default to 1 if not available
46
+ this.exportBitDepth =
47
+ encodingToBitDepth({
48
+ encoding: recordingConfig.encoding ?? "pcm_32bit",
49
+ }) ||
50
+ audioContextFormat.bitDepth ||
51
+ DEFAULT_WEB_BITDEPTH;
52
+ this.audioAnalysisData = {
53
+ amplitudeRange: { min: 0, max: 0 },
54
+ dataPoints: [],
55
+ durationMs: 0,
56
+ samples: 0,
57
+ bitDepth: this.bitDepth,
58
+ numberOfChannels: this.numberOfChannels,
59
+ sampleRate: this.config.sampleRate || this.audioContext.sampleRate,
60
+ pointsPerSecond: this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,
61
+ speakerChanges: [],
62
+ };
63
+ if (recordingConfig.enableProcessing) {
64
+ this.initFeatureExtractorWorker();
65
+ }
66
+ }
67
+ async init() {
68
+ try {
69
+ if (!this.audioWorkletUrl) {
70
+ const blob = new Blob([InlineAudioWebWorker], {
71
+ type: "application/javascript",
72
+ });
73
+ const url = URL.createObjectURL(blob);
74
+ await this.audioContext.audioWorklet.addModule(url);
75
+ }
76
+ else {
77
+ await this.audioContext.audioWorklet.addModule(this.audioWorkletUrl);
78
+ }
79
+ this.audioWorkletNode = new AudioWorkletNode(this.audioContext, "recorder-processor");
80
+ this.audioWorkletNode.port.onmessage = async (event) => {
81
+ const command = event.data.command;
82
+ if (command !== "newData") {
83
+ return;
84
+ }
85
+ // Handle the audio blob (e.g., send it to the server or process it further)
86
+ logger.debug("Received audio blob from processor", event);
87
+ const pcmBuffer = event.data.recordedData;
88
+ if (!pcmBuffer) {
89
+ return;
90
+ }
91
+ this.buffers.push(pcmBuffer); // Store the buffer
92
+ const sampleRate = event.data.sampleRate ?? this.audioContext.sampleRate;
93
+ const otherSampleRate = this.audioContext.sampleRate;
94
+ // Pass the intermediary buffer to the feature extractor worker
95
+ const pcmBufferCopy = pcmBuffer.slice(0);
96
+ const channelData = new Float32Array(pcmBufferCopy);
97
+ const duration = channelData.length / sampleRate; // Calculate duration of the current buffer
98
+ const otherDuration = pcmBuffer.byteLength /
99
+ (otherSampleRate * (this.exportBitDepth / this.numberOfChannels)); // Calculate duration of the current buffer
100
+ logger.debug(`sampleRate=${sampleRate} Duration: ${duration} -- otherSampleRate=${otherSampleRate} Other duration: ${otherDuration}`);
101
+ this.emitAudioEventCallback({
102
+ data: pcmBuffer,
103
+ position: this.position,
104
+ });
105
+ this.position += duration; // Update position
106
+ this.featureExtractorWorker?.postMessage({
107
+ command: "process",
108
+ channelData,
109
+ sampleRate: this.audioContext.sampleRate,
110
+ pointsPerSecond: this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,
111
+ algorithm: this.config.algorithm || "rms",
112
+ bitDepth: this.bitDepth,
113
+ fullAudioDurationMs: this.position * 1000,
114
+ numberOfChannels: this.numberOfChannels,
115
+ features: this.config.features,
116
+ }, []);
117
+ };
118
+ logger.debug(`WebRecorder initialized -- recordSampleRate=${this.audioContext.sampleRate}`, this.config);
119
+ this.audioWorkletNode.port.postMessage({
120
+ command: "init",
121
+ recordSampleRate: this.audioContext.sampleRate, // Pass the original sample rate
122
+ exportSampleRate: this.config.sampleRate ?? this.audioContext.sampleRate,
123
+ bitDepth: this.bitDepth,
124
+ exportBitDepth: this.exportBitDepth,
125
+ channels: this.numberOfChannels,
126
+ interval: this.config.interval ?? DEFAULT_WEB_INTERVAL,
127
+ });
128
+ // Connect the source to the AudioWorkletNode and start recording
129
+ this.source.connect(this.audioWorkletNode);
130
+ this.audioWorkletNode.connect(this.audioContext.destination);
131
+ }
132
+ catch (error) {
133
+ console.error(`[${TAG}] Failed to initialize WebRecorder`, error);
134
+ }
135
+ }
136
+ initFeatureExtractorWorker(featuresExtratorUrl) {
137
+ try {
138
+ if (featuresExtratorUrl) {
139
+ // Initialize the feature extractor worker
140
+ //TODO: create audio feature extractor from a Blob instead of url since we cannot include the url directly in the library
141
+ // We keep the url during dev and use the blob in production.
142
+ this.featureExtractorWorker = new Worker(new URL(featuresExtratorUrl, window.location.href));
143
+ this.featureExtractorWorker.onmessage =
144
+ this.handleFeatureExtractorMessage.bind(this);
145
+ this.featureExtractorWorker.onerror = this.handleWorkerError.bind(this);
146
+ }
147
+ else {
148
+ // Fallback to the inline worker if the URL is not provided
149
+ this.initFallbackWorker();
150
+ }
151
+ }
152
+ catch (error) {
153
+ console.error(`[${TAG}] Failed to initialize feature extractor worker`, error);
154
+ this.initFallbackWorker();
155
+ }
156
+ }
157
+ initFallbackWorker() {
158
+ try {
159
+ const blob = new Blob([InlineFeaturesExtractor], {
160
+ type: "application/javascript",
161
+ });
162
+ const url = URL.createObjectURL(blob);
163
+ this.featureExtractorWorker = new Worker(url);
164
+ this.featureExtractorWorker.onmessage =
165
+ this.handleFeatureExtractorMessage.bind(this);
166
+ this.featureExtractorWorker.onerror = (error) => {
167
+ console.error(`[${TAG}] Default Inline worker failed`, error);
168
+ };
169
+ logger.log("Inline worker initialized successfully");
170
+ }
171
+ catch (error) {
172
+ console.error(`[${TAG}] Failed to initialize Inline Feature Extractor worker`, error);
173
+ }
174
+ }
175
+ handleWorkerError(error) {
176
+ console.error(`[${TAG}] Feature extractor worker error:`, error);
177
+ }
178
+ handleFeatureExtractorMessage(event) {
179
+ if (event.data.command === "features") {
180
+ const segmentResult = event.data.result;
181
+ // Merge the segment result with the full audio analysis data
182
+ this.audioAnalysisData.dataPoints.push(...segmentResult.dataPoints);
183
+ this.audioAnalysisData.speakerChanges?.push(...(segmentResult.speakerChanges ?? []));
184
+ this.audioAnalysisData.durationMs = segmentResult.durationMs;
185
+ if (segmentResult.amplitudeRange) {
186
+ this.audioAnalysisData.amplitudeRange = {
187
+ min: Math.min(this.audioAnalysisData.amplitudeRange.min, segmentResult.amplitudeRange.min),
188
+ max: Math.max(this.audioAnalysisData.amplitudeRange.max, segmentResult.amplitudeRange.max),
189
+ };
190
+ }
191
+ // Handle the extracted features (e.g., emit an event or log them)
192
+ logger.debug("features event segmentResult", segmentResult);
193
+ logger.debug("features event audioAnalysisData", this.audioAnalysisData);
194
+ this.emitAudioAnalysisCallback(segmentResult);
195
+ }
196
+ }
197
+ start() {
198
+ this.source.connect(this.audioWorkletNode);
199
+ this.audioWorkletNode.connect(this.audioContext.destination);
200
+ }
201
+ stop() {
202
+ return new Promise((resolve, reject) => {
203
+ try {
204
+ if (this.audioWorkletNode) {
205
+ // this.source.disconnect(this.audioWorkletNode);
206
+ // this.audioWorkletNode.disconnect(this.audioContext.destination);
207
+ this.audioWorkletNode.port.postMessage({ command: "stop" });
208
+ // Set a timeout to reject the promise if no message is received within 5 seconds
209
+ const timeout = setTimeout(() => {
210
+ this.audioWorkletNode.port.removeEventListener("message", onMessage);
211
+ reject(new Error("Timeout error, audioWorkletNode didn't complete."));
212
+ }, 5000);
213
+ // Listen for the recordedData message to confirm stopping
214
+ const onMessage = async (event) => {
215
+ const command = event.data.command;
216
+ if (command === "recordedData") {
217
+ clearTimeout(timeout); // Clear the timeout
218
+ const rawPCMDataFull = event.data.recordedData?.slice(0);
219
+ // Compute duration of the recorded data
220
+ const duration = rawPCMDataFull.byteLength /
221
+ (this.audioContext.sampleRate *
222
+ (this.exportBitDepth / this.numberOfChannels));
223
+ logger.debug(`Received recorded data -- Duration: ${duration} vs ${rawPCMDataFull.byteLength / this.audioContext.sampleRate} seconds`);
224
+ logger.debug(`recordedData.length=${rawPCMDataFull.byteLength} vs transmittedData.length=${this.buffers[0].byteLength}`);
225
+ // Remove the event listener after receiving the final data
226
+ this.audioWorkletNode.port.removeEventListener("message", onMessage);
227
+ resolve(this.buffers); // Resolve the promise with the collected buffers
228
+ }
229
+ };
230
+ this.audioWorkletNode.port.addEventListener("message", onMessage);
231
+ }
232
+ // Stop all media stream tracks to stop the browser recording
233
+ this.stopMediaStreamTracks();
234
+ }
235
+ catch (error) {
236
+ reject(error);
237
+ }
238
+ });
239
+ }
240
+ pause() {
241
+ this.source.disconnect(this.audioWorkletNode); // Disconnect the source from the AudioWorkletNode
242
+ this.audioWorkletNode.disconnect(this.audioContext.destination); // Disconnect the AudioWorkletNode from the destination
243
+ this.audioWorkletNode.port.postMessage({ command: "pause" });
244
+ }
245
+ stopMediaStreamTracks() {
246
+ // Stop all audio tracks to stop the recording icon
247
+ const tracks = this.source.mediaStream.getTracks();
248
+ tracks.forEach((track) => track.stop());
249
+ }
250
+ async playRecordedData({ recordedData, }) {
251
+ try {
252
+ const blob = new Blob([recordedData]);
253
+ const url = URL.createObjectURL(blob);
254
+ const response = await fetch(url);
255
+ const arrayBuffer = await response.arrayBuffer();
256
+ // Decode the audio data
257
+ const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
258
+ // Create a buffer source node and play the audio
259
+ const bufferSource = this.audioContext.createBufferSource();
260
+ bufferSource.buffer = audioBuffer;
261
+ bufferSource.connect(this.audioContext.destination);
262
+ bufferSource.start();
263
+ logger.debug("Playing recorded data", recordedData);
264
+ }
265
+ catch (error) {
266
+ console.error(`[${TAG}] Failed to play recorded data:`, error);
267
+ }
268
+ }
269
+ checkAudioContextFormat({ sampleRate }) {
270
+ // Create a silent AudioBuffer
271
+ const frameCount = sampleRate * 1.0; // 1 second buffer
272
+ const audioBuffer = this.audioContext.createBuffer(1, frameCount, sampleRate);
273
+ // Check the format
274
+ const channelData = audioBuffer.getChannelData(0);
275
+ const bitDepth = channelData.BYTES_PER_ELEMENT * 8; // 4 bytes per element means 32-bit
276
+ return {
277
+ sampleRate: audioBuffer.sampleRate,
278
+ bitDepth,
279
+ numberOfChannels: audioBuffer.numberOfChannels,
280
+ };
281
+ }
282
+ resume() {
283
+ this.source.connect(this.audioWorkletNode);
284
+ this.audioWorkletNode.connect(this.audioContext.destination);
285
+ this.audioWorkletNode.port.postMessage({ command: "resume" });
286
+ }
287
+ }
288
+ //# sourceMappingURL=WebRecorder.web.js.map