@siteed/expo-audio-studio 2.8.0 → 2.8.2

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 (241) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/app.plugin.cjs +15 -2
  3. package/build/cjs/AudioAnalysis/AudioAnalysis.types.js +4 -0
  4. package/build/cjs/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  5. package/build/cjs/AudioAnalysis/extractAudioAnalysis.js +205 -0
  6. package/build/cjs/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  7. package/build/cjs/AudioAnalysis/extractAudioData.js +12 -0
  8. package/build/cjs/AudioAnalysis/extractAudioData.js.map +1 -0
  9. package/build/cjs/AudioAnalysis/extractMelSpectrogram.js +89 -0
  10. package/build/cjs/AudioAnalysis/extractMelSpectrogram.js.map +1 -0
  11. package/build/cjs/AudioAnalysis/extractPreview.js +29 -0
  12. package/build/cjs/AudioAnalysis/extractPreview.js.map +1 -0
  13. package/build/cjs/AudioAnalysis/extractWaveform.js +18 -0
  14. package/build/cjs/AudioAnalysis/extractWaveform.js.map +1 -0
  15. package/build/cjs/AudioDeviceManager.js +500 -0
  16. package/build/cjs/AudioDeviceManager.js.map +1 -0
  17. package/build/cjs/AudioRecorder.provider.js +68 -0
  18. package/build/cjs/AudioRecorder.provider.js.map +1 -0
  19. package/build/cjs/ExpoAudioStream.native.js +8 -0
  20. package/build/cjs/ExpoAudioStream.native.js.map +1 -0
  21. package/build/cjs/ExpoAudioStream.types.js +11 -0
  22. package/build/cjs/ExpoAudioStream.types.js.map +1 -0
  23. package/build/cjs/ExpoAudioStream.web.js +705 -0
  24. package/build/cjs/ExpoAudioStream.web.js.map +1 -0
  25. package/build/cjs/ExpoAudioStreamModule.js +718 -0
  26. package/build/cjs/ExpoAudioStreamModule.js.map +1 -0
  27. package/build/cjs/WebRecorder.web.js +756 -0
  28. package/build/cjs/WebRecorder.web.js.map +1 -0
  29. package/build/cjs/constants.js +17 -0
  30. package/build/cjs/constants.js.map +1 -0
  31. package/build/cjs/events.js +30 -0
  32. package/build/cjs/events.js.map +1 -0
  33. package/build/cjs/hooks/useAudioDevices.js +155 -0
  34. package/build/cjs/hooks/useAudioDevices.js.map +1 -0
  35. package/build/cjs/index.js +50 -0
  36. package/build/cjs/index.js.map +1 -0
  37. package/build/cjs/trimAudio.js +75 -0
  38. package/build/cjs/trimAudio.js.map +1 -0
  39. package/build/cjs/useAudioRecorder.js +453 -0
  40. package/build/cjs/useAudioRecorder.js.map +1 -0
  41. package/build/cjs/utils/BlobFix.js +502 -0
  42. package/build/cjs/utils/BlobFix.js.map +1 -0
  43. package/build/cjs/utils/audioProcessing.js +137 -0
  44. package/build/cjs/utils/audioProcessing.js.map +1 -0
  45. package/build/cjs/utils/concatenateBuffers.js +25 -0
  46. package/build/cjs/utils/concatenateBuffers.js.map +1 -0
  47. package/build/cjs/utils/convertPCMToFloat32.js +124 -0
  48. package/build/cjs/utils/convertPCMToFloat32.js.map +1 -0
  49. package/build/cjs/utils/crc32.js +52 -0
  50. package/build/cjs/utils/crc32.js.map +1 -0
  51. package/build/cjs/utils/encodingToBitDepth.js +17 -0
  52. package/build/cjs/utils/encodingToBitDepth.js.map +1 -0
  53. package/build/cjs/utils/getWavFileInfo.js +96 -0
  54. package/build/cjs/utils/getWavFileInfo.js.map +1 -0
  55. package/build/cjs/utils/writeWavHeader.js +88 -0
  56. package/build/cjs/utils/writeWavHeader.js.map +1 -0
  57. package/build/cjs/workers/InlineFeaturesExtractor.web.js +853 -0
  58. package/build/cjs/workers/InlineFeaturesExtractor.web.js.map +1 -0
  59. package/build/cjs/workers/inlineAudioWebWorker.web.js +184 -0
  60. package/build/cjs/workers/inlineAudioWebWorker.web.js.map +1 -0
  61. package/build/esm/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  62. package/build/esm/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  63. package/build/esm/AudioAnalysis/extractAudioData.js.map +1 -0
  64. package/build/esm/AudioAnalysis/extractMelSpectrogram.js.map +1 -0
  65. package/build/esm/AudioAnalysis/extractPreview.js.map +1 -0
  66. package/build/esm/AudioAnalysis/extractWaveform.js.map +1 -0
  67. package/build/esm/AudioDeviceManager.js.map +1 -0
  68. package/build/esm/AudioRecorder.provider.js.map +1 -0
  69. package/build/esm/ExpoAudioStream.native.js.map +1 -0
  70. package/build/esm/ExpoAudioStream.types.js.map +1 -0
  71. package/build/esm/ExpoAudioStream.web.js.map +1 -0
  72. package/build/esm/ExpoAudioStreamModule.js.map +1 -0
  73. package/build/esm/WebRecorder.web.js.map +1 -0
  74. package/build/esm/constants.js.map +1 -0
  75. package/build/esm/events.js.map +1 -0
  76. package/build/esm/hooks/useAudioDevices.js.map +1 -0
  77. package/build/esm/index.js.map +1 -0
  78. package/build/esm/trimAudio.js.map +1 -0
  79. package/build/esm/useAudioRecorder.js.map +1 -0
  80. package/build/esm/utils/BlobFix.js.map +1 -0
  81. package/build/esm/utils/audioProcessing.js.map +1 -0
  82. package/build/esm/utils/concatenateBuffers.js.map +1 -0
  83. package/build/esm/utils/convertPCMToFloat32.js.map +1 -0
  84. package/build/esm/utils/crc32.js.map +1 -0
  85. package/build/esm/utils/encodingToBitDepth.js.map +1 -0
  86. package/build/esm/utils/getWavFileInfo.js.map +1 -0
  87. package/build/esm/utils/writeWavHeader.js.map +1 -0
  88. package/build/esm/workers/InlineFeaturesExtractor.web.js.map +1 -0
  89. package/build/esm/workers/inlineAudioWebWorker.web.js.map +1 -0
  90. package/build/types/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  91. package/build/types/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  92. package/build/types/AudioAnalysis/extractAudioData.d.ts.map +1 -0
  93. package/build/types/AudioAnalysis/extractMelSpectrogram.d.ts.map +1 -0
  94. package/build/types/AudioAnalysis/extractPreview.d.ts.map +1 -0
  95. package/build/types/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  96. package/build/types/AudioDeviceManager.d.ts.map +1 -0
  97. package/build/types/AudioRecorder.provider.d.ts.map +1 -0
  98. package/build/types/ExpoAudioStream.native.d.ts.map +1 -0
  99. package/build/types/ExpoAudioStream.types.d.ts.map +1 -0
  100. package/build/types/ExpoAudioStream.web.d.ts.map +1 -0
  101. package/build/types/ExpoAudioStreamModule.d.ts.map +1 -0
  102. package/build/types/WebRecorder.web.d.ts.map +1 -0
  103. package/build/types/constants.d.ts.map +1 -0
  104. package/build/types/events.d.ts.map +1 -0
  105. package/build/types/hooks/useAudioDevices.d.ts.map +1 -0
  106. package/build/types/index.d.ts.map +1 -0
  107. package/build/types/trimAudio.d.ts.map +1 -0
  108. package/build/types/useAudioRecorder.d.ts.map +1 -0
  109. package/build/types/utils/BlobFix.d.ts.map +1 -0
  110. package/build/types/utils/audioProcessing.d.ts.map +1 -0
  111. package/build/types/utils/concatenateBuffers.d.ts.map +1 -0
  112. package/build/types/utils/convertPCMToFloat32.d.ts.map +1 -0
  113. package/build/types/utils/crc32.d.ts.map +1 -0
  114. package/build/types/utils/encodingToBitDepth.d.ts.map +1 -0
  115. package/build/types/utils/getWavFileInfo.d.ts.map +1 -0
  116. package/build/types/utils/writeWavHeader.d.ts.map +1 -0
  117. package/build/types/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  118. package/build/types/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  119. package/package.json +26 -7
  120. package/plugin/build/{index.js → cjs/index.js} +2 -1
  121. package/plugin/build/{index.cjs → esm/index.js} +6 -7
  122. package/plugin/src/index.ts +1 -0
  123. package/plugin/tsconfig.cjs.json +11 -0
  124. package/plugin/tsconfig.esm.json +11 -0
  125. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +0 -1
  126. package/build/AudioAnalysis/AudioAnalysis.types.js.map +0 -1
  127. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +0 -1
  128. package/build/AudioAnalysis/extractAudioAnalysis.js.map +0 -1
  129. package/build/AudioAnalysis/extractAudioData.d.ts.map +0 -1
  130. package/build/AudioAnalysis/extractAudioData.js.map +0 -1
  131. package/build/AudioAnalysis/extractMelSpectrogram.d.ts.map +0 -1
  132. package/build/AudioAnalysis/extractMelSpectrogram.js.map +0 -1
  133. package/build/AudioAnalysis/extractPreview.d.ts.map +0 -1
  134. package/build/AudioAnalysis/extractPreview.js.map +0 -1
  135. package/build/AudioAnalysis/extractWaveform.d.ts.map +0 -1
  136. package/build/AudioAnalysis/extractWaveform.js.map +0 -1
  137. package/build/AudioDeviceManager.d.ts.map +0 -1
  138. package/build/AudioDeviceManager.js.map +0 -1
  139. package/build/AudioRecorder.provider.d.ts.map +0 -1
  140. package/build/AudioRecorder.provider.js.map +0 -1
  141. package/build/ExpoAudioStream.native.d.ts.map +0 -1
  142. package/build/ExpoAudioStream.native.js.map +0 -1
  143. package/build/ExpoAudioStream.types.d.ts.map +0 -1
  144. package/build/ExpoAudioStream.types.js.map +0 -1
  145. package/build/ExpoAudioStream.web.d.ts.map +0 -1
  146. package/build/ExpoAudioStream.web.js.map +0 -1
  147. package/build/ExpoAudioStreamModule.d.ts.map +0 -1
  148. package/build/ExpoAudioStreamModule.js.map +0 -1
  149. package/build/WebRecorder.web.d.ts.map +0 -1
  150. package/build/WebRecorder.web.js.map +0 -1
  151. package/build/constants.d.ts.map +0 -1
  152. package/build/constants.js.map +0 -1
  153. package/build/events.d.ts.map +0 -1
  154. package/build/events.js.map +0 -1
  155. package/build/hooks/useAudioDevices.d.ts.map +0 -1
  156. package/build/hooks/useAudioDevices.js.map +0 -1
  157. package/build/index.d.ts.map +0 -1
  158. package/build/index.js.map +0 -1
  159. package/build/trimAudio.d.ts.map +0 -1
  160. package/build/trimAudio.js.map +0 -1
  161. package/build/useAudioRecorder.d.ts.map +0 -1
  162. package/build/useAudioRecorder.js.map +0 -1
  163. package/build/utils/BlobFix.d.ts.map +0 -1
  164. package/build/utils/BlobFix.js.map +0 -1
  165. package/build/utils/audioProcessing.d.ts.map +0 -1
  166. package/build/utils/audioProcessing.js.map +0 -1
  167. package/build/utils/concatenateBuffers.d.ts.map +0 -1
  168. package/build/utils/concatenateBuffers.js.map +0 -1
  169. package/build/utils/convertPCMToFloat32.d.ts.map +0 -1
  170. package/build/utils/convertPCMToFloat32.js.map +0 -1
  171. package/build/utils/crc32.d.ts.map +0 -1
  172. package/build/utils/crc32.js.map +0 -1
  173. package/build/utils/encodingToBitDepth.d.ts.map +0 -1
  174. package/build/utils/encodingToBitDepth.js.map +0 -1
  175. package/build/utils/getWavFileInfo.d.ts.map +0 -1
  176. package/build/utils/getWavFileInfo.js.map +0 -1
  177. package/build/utils/writeWavHeader.d.ts.map +0 -1
  178. package/build/utils/writeWavHeader.js.map +0 -1
  179. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +0 -1
  180. package/build/workers/InlineFeaturesExtractor.web.js.map +0 -1
  181. package/build/workers/inlineAudioWebWorker.web.d.ts.map +0 -1
  182. package/build/workers/inlineAudioWebWorker.web.js.map +0 -1
  183. /package/build/{AudioAnalysis → esm/AudioAnalysis}/AudioAnalysis.types.js +0 -0
  184. /package/build/{AudioAnalysis → esm/AudioAnalysis}/extractAudioAnalysis.js +0 -0
  185. /package/build/{AudioAnalysis → esm/AudioAnalysis}/extractAudioData.js +0 -0
  186. /package/build/{AudioAnalysis → esm/AudioAnalysis}/extractMelSpectrogram.js +0 -0
  187. /package/build/{AudioAnalysis → esm/AudioAnalysis}/extractPreview.js +0 -0
  188. /package/build/{AudioAnalysis → esm/AudioAnalysis}/extractWaveform.js +0 -0
  189. /package/build/{AudioDeviceManager.js → esm/AudioDeviceManager.js} +0 -0
  190. /package/build/{AudioRecorder.provider.js → esm/AudioRecorder.provider.js} +0 -0
  191. /package/build/{ExpoAudioStream.native.js → esm/ExpoAudioStream.native.js} +0 -0
  192. /package/build/{ExpoAudioStream.types.js → esm/ExpoAudioStream.types.js} +0 -0
  193. /package/build/{ExpoAudioStream.web.js → esm/ExpoAudioStream.web.js} +0 -0
  194. /package/build/{ExpoAudioStreamModule.js → esm/ExpoAudioStreamModule.js} +0 -0
  195. /package/build/{WebRecorder.web.js → esm/WebRecorder.web.js} +0 -0
  196. /package/build/{constants.js → esm/constants.js} +0 -0
  197. /package/build/{events.js → esm/events.js} +0 -0
  198. /package/build/{hooks → esm/hooks}/useAudioDevices.js +0 -0
  199. /package/build/{index.js → esm/index.js} +0 -0
  200. /package/build/{trimAudio.js → esm/trimAudio.js} +0 -0
  201. /package/build/{useAudioRecorder.js → esm/useAudioRecorder.js} +0 -0
  202. /package/build/{utils → esm/utils}/BlobFix.js +0 -0
  203. /package/build/{utils → esm/utils}/audioProcessing.js +0 -0
  204. /package/build/{utils → esm/utils}/concatenateBuffers.js +0 -0
  205. /package/build/{utils → esm/utils}/convertPCMToFloat32.js +0 -0
  206. /package/build/{utils → esm/utils}/crc32.js +0 -0
  207. /package/build/{utils → esm/utils}/encodingToBitDepth.js +0 -0
  208. /package/build/{utils → esm/utils}/getWavFileInfo.js +0 -0
  209. /package/build/{utils → esm/utils}/writeWavHeader.js +0 -0
  210. /package/build/{workers → esm/workers}/InlineFeaturesExtractor.web.js +0 -0
  211. /package/build/{workers → esm/workers}/inlineAudioWebWorker.web.js +0 -0
  212. /package/build/{AudioAnalysis → types/AudioAnalysis}/AudioAnalysis.types.d.ts +0 -0
  213. /package/build/{AudioAnalysis → types/AudioAnalysis}/extractAudioAnalysis.d.ts +0 -0
  214. /package/build/{AudioAnalysis → types/AudioAnalysis}/extractAudioData.d.ts +0 -0
  215. /package/build/{AudioAnalysis → types/AudioAnalysis}/extractMelSpectrogram.d.ts +0 -0
  216. /package/build/{AudioAnalysis → types/AudioAnalysis}/extractPreview.d.ts +0 -0
  217. /package/build/{AudioAnalysis → types/AudioAnalysis}/extractWaveform.d.ts +0 -0
  218. /package/build/{AudioDeviceManager.d.ts → types/AudioDeviceManager.d.ts} +0 -0
  219. /package/build/{AudioRecorder.provider.d.ts → types/AudioRecorder.provider.d.ts} +0 -0
  220. /package/build/{ExpoAudioStream.native.d.ts → types/ExpoAudioStream.native.d.ts} +0 -0
  221. /package/build/{ExpoAudioStream.types.d.ts → types/ExpoAudioStream.types.d.ts} +0 -0
  222. /package/build/{ExpoAudioStream.web.d.ts → types/ExpoAudioStream.web.d.ts} +0 -0
  223. /package/build/{ExpoAudioStreamModule.d.ts → types/ExpoAudioStreamModule.d.ts} +0 -0
  224. /package/build/{WebRecorder.web.d.ts → types/WebRecorder.web.d.ts} +0 -0
  225. /package/build/{constants.d.ts → types/constants.d.ts} +0 -0
  226. /package/build/{events.d.ts → types/events.d.ts} +0 -0
  227. /package/build/{hooks → types/hooks}/useAudioDevices.d.ts +0 -0
  228. /package/build/{index.d.ts → types/index.d.ts} +0 -0
  229. /package/build/{trimAudio.d.ts → types/trimAudio.d.ts} +0 -0
  230. /package/build/{useAudioRecorder.d.ts → types/useAudioRecorder.d.ts} +0 -0
  231. /package/build/{utils → types/utils}/BlobFix.d.ts +0 -0
  232. /package/build/{utils → types/utils}/audioProcessing.d.ts +0 -0
  233. /package/build/{utils → types/utils}/concatenateBuffers.d.ts +0 -0
  234. /package/build/{utils → types/utils}/convertPCMToFloat32.d.ts +0 -0
  235. /package/build/{utils → types/utils}/crc32.d.ts +0 -0
  236. /package/build/{utils → types/utils}/encodingToBitDepth.d.ts +0 -0
  237. /package/build/{utils → types/utils}/getWavFileInfo.d.ts +0 -0
  238. /package/build/{utils → types/utils}/writeWavHeader.d.ts +0 -0
  239. /package/build/{workers → types/workers}/InlineFeaturesExtractor.web.d.ts +0 -0
  240. /package/build/{workers → types/workers}/inlineAudioWebWorker.web.d.ts +0 -0
  241. /package/plugin/build/{index.d.ts → types/index.d.ts} +0 -0
@@ -0,0 +1,705 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExpoAudioStreamWeb = void 0;
4
+ // src/ExpoAudioStreamModule.web.ts
5
+ const expo_modules_core_1 = require("expo-modules-core");
6
+ const WebRecorder_web_1 = require("./WebRecorder.web");
7
+ const encodingToBitDepth_1 = require("./utils/encodingToBitDepth");
8
+ class ExpoAudioStreamWeb extends expo_modules_core_1.LegacyEventEmitter {
9
+ customRecorder;
10
+ audioChunks;
11
+ isRecording;
12
+ isPaused;
13
+ recordingStartTime;
14
+ pausedTime;
15
+ currentDurationMs;
16
+ currentSize;
17
+ currentInterval;
18
+ currentIntervalAnalysis;
19
+ lastEmittedSize;
20
+ lastEmittedTime;
21
+ lastEmittedCompressionSize;
22
+ lastEmittedAnalysisTime;
23
+ streamUuid;
24
+ extension = 'wav'; // Default extension is 'wav'
25
+ recordingConfig;
26
+ bitDepth; // Bit depth of the audio
27
+ audioWorkletUrl;
28
+ featuresExtratorUrl;
29
+ logger;
30
+ latestPosition = 0;
31
+ totalCompressedSize = 0;
32
+ maxBufferSize;
33
+ eventCallback;
34
+ constructor({ audioWorkletUrl, featuresExtratorUrl, logger, maxBufferSize = 100, // Default to storing last 100 chunks (1 chunk = 0.5 seconds)
35
+ }) {
36
+ const mockNativeModule = {
37
+ addListener: () => { },
38
+ removeListeners: () => { },
39
+ };
40
+ super(mockNativeModule); // Pass the mock native module to the parent class
41
+ this.logger = logger;
42
+ this.customRecorder = null;
43
+ this.audioChunks = [];
44
+ this.isRecording = false;
45
+ this.isPaused = false;
46
+ this.recordingStartTime = 0;
47
+ this.pausedTime = 0;
48
+ this.currentDurationMs = 0;
49
+ this.currentSize = 0;
50
+ this.bitDepth = 32; // Default
51
+ this.currentInterval = 1000; // Default interval in ms
52
+ this.currentIntervalAnalysis = 500; // Default analysis interval in ms
53
+ this.lastEmittedSize = 0;
54
+ this.lastEmittedTime = 0;
55
+ this.latestPosition = 0;
56
+ this.lastEmittedCompressionSize = 0;
57
+ this.lastEmittedAnalysisTime = 0;
58
+ this.streamUuid = null; // Initialize UUID on first recording start
59
+ this.audioWorkletUrl = audioWorkletUrl;
60
+ this.featuresExtratorUrl = featuresExtratorUrl;
61
+ this.maxBufferSize = maxBufferSize;
62
+ }
63
+ // Utility to handle user media stream
64
+ async getMediaStream() {
65
+ try {
66
+ this.logger?.debug('Requesting user media (microphone)...');
67
+ // First check if the browser supports the necessary audio APIs
68
+ if (!navigator?.mediaDevices?.getUserMedia) {
69
+ this.logger?.error('Browser does not support mediaDevices.getUserMedia');
70
+ throw new Error('Browser does not support audio recording');
71
+ }
72
+ // Get media with detailed audio constraints for better diagnostics
73
+ const constraints = {
74
+ audio: {
75
+ echoCancellation: true,
76
+ noiseSuppression: true,
77
+ autoGainControl: true,
78
+ // Add deviceId constraint if specified
79
+ ...(this.recordingConfig?.deviceId
80
+ ? {
81
+ deviceId: {
82
+ exact: this.recordingConfig.deviceId,
83
+ },
84
+ }
85
+ : {}),
86
+ },
87
+ };
88
+ this.logger?.debug('Media constraints:', constraints);
89
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
90
+ // Get detailed info about the audio track for debugging
91
+ const audioTracks = stream.getAudioTracks();
92
+ if (audioTracks.length > 0) {
93
+ const track = audioTracks[0];
94
+ const settings = track.getSettings();
95
+ this.logger?.debug('Audio track obtained:', {
96
+ label: track.label,
97
+ id: track.id,
98
+ enabled: track.enabled,
99
+ muted: track.muted,
100
+ readyState: track.readyState,
101
+ settings,
102
+ });
103
+ }
104
+ else {
105
+ this.logger?.warn('Stream has no audio tracks!');
106
+ }
107
+ return stream;
108
+ }
109
+ catch (error) {
110
+ this.logger?.error('Failed to get media stream:', error);
111
+ throw error;
112
+ }
113
+ }
114
+ // Prepare recording with options
115
+ async prepareRecording(recordingConfig = {}) {
116
+ if (this.isRecording) {
117
+ this.logger?.warn('Cannot prepare: Recording is already in progress');
118
+ return false;
119
+ }
120
+ try {
121
+ // Check permissions and initialize basic settings
122
+ await this.getMediaStream().then((stream) => {
123
+ // Just verify we can access the microphone by getting a stream, then release it
124
+ stream.getTracks().forEach((track) => track.stop());
125
+ });
126
+ this.bitDepth = (0, encodingToBitDepth_1.encodingToBitDepth)({
127
+ encoding: recordingConfig.encoding ?? 'pcm_32bit',
128
+ });
129
+ // Store recording configuration for later use
130
+ this.recordingConfig = recordingConfig;
131
+ // Use custom filename if provided, otherwise fallback to timestamp
132
+ if (recordingConfig.filename) {
133
+ // Remove any existing extension from the filename
134
+ this.streamUuid = recordingConfig.filename.replace(/\.[^/.]+$/, '');
135
+ }
136
+ else {
137
+ this.streamUuid = Date.now().toString();
138
+ }
139
+ this.logger?.debug('Recording preparation completed successfully');
140
+ return true;
141
+ }
142
+ catch (error) {
143
+ this.logger?.error('Error preparing recording:', error);
144
+ return false;
145
+ }
146
+ }
147
+ // Start recording with options
148
+ async startRecording(recordingConfig = {}) {
149
+ if (this.isRecording) {
150
+ throw new Error('Recording is already in progress');
151
+ }
152
+ // If we haven't prepared or have different settings, prepare now
153
+ if (!this.recordingConfig ||
154
+ this.recordingConfig.sampleRate !== recordingConfig.sampleRate ||
155
+ this.recordingConfig.channels !== recordingConfig.channels ||
156
+ this.recordingConfig.encoding !== recordingConfig.encoding) {
157
+ await this.prepareRecording(recordingConfig);
158
+ }
159
+ else {
160
+ this.logger?.debug('Using previously prepared recording configuration');
161
+ }
162
+ // Save recording config for reference
163
+ this.recordingConfig = recordingConfig;
164
+ const audioContext = new (window.AudioContext ||
165
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
166
+ // @ts-ignore - Allow webkitAudioContext for Safari
167
+ window.webkitAudioContext)();
168
+ const stream = await this.getMediaStream();
169
+ const source = audioContext.createMediaStreamSource(stream);
170
+ this.customRecorder = new WebRecorder_web_1.WebRecorder({
171
+ logger: this.logger,
172
+ audioContext,
173
+ source,
174
+ recordingConfig,
175
+ emitAudioEventCallback: this.customRecorderEventCallback.bind(this),
176
+ emitAudioAnalysisCallback: this.customRecorderAnalysisCallback.bind(this),
177
+ onInterruption: this.handleRecordingInterruption.bind(this),
178
+ });
179
+ await this.customRecorder.init();
180
+ this.customRecorder.start();
181
+ this.isRecording = true;
182
+ this.recordingStartTime = Date.now();
183
+ this.pausedTime = 0;
184
+ this.isPaused = false;
185
+ this.lastEmittedSize = 0;
186
+ this.lastEmittedTime = 0;
187
+ this.lastEmittedCompressionSize = 0;
188
+ this.currentInterval = recordingConfig.interval ?? 1000;
189
+ this.currentIntervalAnalysis = recordingConfig.intervalAnalysis ?? 500;
190
+ this.lastEmittedAnalysisTime = Date.now();
191
+ // Use custom filename if provided, otherwise fallback to timestamp
192
+ if (recordingConfig.filename) {
193
+ // Remove any existing extension from the filename
194
+ this.streamUuid = recordingConfig.filename.replace(/\.[^/.]+$/, '');
195
+ }
196
+ else {
197
+ this.streamUuid = Date.now().toString();
198
+ }
199
+ const fileUri = `${this.streamUuid}.${this.extension}`;
200
+ const streamConfig = {
201
+ fileUri,
202
+ mimeType: `audio/${this.extension}`,
203
+ bitDepth: this.bitDepth,
204
+ channels: recordingConfig.channels ?? 1,
205
+ sampleRate: recordingConfig.sampleRate ?? 44100,
206
+ compression: recordingConfig.compression
207
+ ? {
208
+ ...recordingConfig.compression,
209
+ bitrate: recordingConfig.compression?.bitrate ?? 128000,
210
+ size: 0,
211
+ mimeType: 'audio/webm',
212
+ format: recordingConfig.compression?.format ?? 'opus',
213
+ compressedFileUri: '',
214
+ }
215
+ : undefined,
216
+ };
217
+ return streamConfig;
218
+ }
219
+ /**
220
+ * Centralized handler for recording interruptions
221
+ */
222
+ handleRecordingInterruption(event) {
223
+ this.logger?.debug(`Received recording interruption: ${event.reason}`);
224
+ // Update local state if the interruption should pause recording
225
+ if (event.isPaused) {
226
+ this.isPaused = true;
227
+ // If this is a device disconnection, handle according to behavior setting
228
+ if (event.reason === 'deviceDisconnected') {
229
+ this.pausedTime = Date.now();
230
+ // Check if we should try fallback to another device
231
+ if (this.recordingConfig?.deviceDisconnectionBehavior ===
232
+ 'fallback') {
233
+ this.logger?.debug('Device disconnected with fallback behavior - attempting to switch to default device');
234
+ // Try to restart with default device
235
+ this.handleDeviceFallback().catch((error) => {
236
+ // If fallback fails, emit warning
237
+ this.logger?.error('Device fallback failed:', error);
238
+ this.emit('onRecordingInterrupted', {
239
+ reason: 'deviceSwitchFailed',
240
+ isPaused: true,
241
+ timestamp: Date.now(),
242
+ message: 'Failed to switch to fallback device. Recording paused.',
243
+ });
244
+ });
245
+ }
246
+ else {
247
+ // Just warn about disconnection if fallback not enabled
248
+ this.logger?.warn('Device disconnected - recording paused automatically');
249
+ this.emit('onRecordingInterrupted', event);
250
+ }
251
+ }
252
+ else {
253
+ // For other interruption types, just emit the event
254
+ this.emit('onRecordingInterrupted', event);
255
+ }
256
+ }
257
+ else {
258
+ // If not causing a pause, just forward the event
259
+ this.emit('onRecordingInterrupted', event);
260
+ }
261
+ }
262
+ /**
263
+ * Handler for audio events from the WebRecorder
264
+ */
265
+ customRecorderEventCallback({ data, position, compression, }) {
266
+ // Keep only the latest chunks based on maxBufferSize
267
+ this.audioChunks.push(new Float32Array(data));
268
+ if (this.audioChunks.length > this.maxBufferSize) {
269
+ this.audioChunks.shift(); // Remove oldest chunk
270
+ }
271
+ this.currentSize += data.byteLength;
272
+ this.emitAudioEvent({ data, position, compression });
273
+ this.lastEmittedTime = Date.now();
274
+ this.lastEmittedSize = this.currentSize;
275
+ this.lastEmittedCompressionSize = compression?.size ?? 0;
276
+ }
277
+ /**
278
+ * Handler for audio analysis events from the WebRecorder
279
+ */
280
+ customRecorderAnalysisCallback(audioAnalysisData) {
281
+ this.emit('AudioAnalysis', audioAnalysisData);
282
+ }
283
+ // Get recording duration
284
+ getRecordingDuration() {
285
+ if (!this.isRecording) {
286
+ return 0;
287
+ }
288
+ return this.currentDurationMs;
289
+ }
290
+ emitAudioEvent({ data, position, compression }) {
291
+ const fileUri = `${this.streamUuid}.${this.extension}`;
292
+ if (compression?.size) {
293
+ this.lastEmittedCompressionSize = compression.size;
294
+ this.totalCompressedSize = compression.totalSize;
295
+ }
296
+ // Update latest position for tracking
297
+ this.latestPosition = position;
298
+ // Calculate duration of this chunk in ms
299
+ const sampleRate = this.recordingConfig?.sampleRate || 44100;
300
+ const chunkDurationMs = (data.length / sampleRate) * 1000;
301
+ // Handle duration calculation
302
+ if (this.customRecorder?.isFirstChunkAfterSwitch) {
303
+ this.logger?.debug(`Processing first chunk after device switch, duration preserved at ${this.currentDurationMs}ms`);
304
+ this.customRecorder.isFirstChunkAfterSwitch = false;
305
+ }
306
+ else {
307
+ this.currentDurationMs += chunkDurationMs;
308
+ }
309
+ const audioEventPayload = {
310
+ fileUri,
311
+ mimeType: `audio/${this.extension}`,
312
+ lastEmittedSize: this.lastEmittedSize,
313
+ deltaSize: data.byteLength,
314
+ position,
315
+ totalSize: this.currentSize,
316
+ buffer: data,
317
+ streamUuid: this.streamUuid ?? '',
318
+ compression: compression
319
+ ? {
320
+ data: compression?.data,
321
+ totalSize: this.totalCompressedSize,
322
+ eventDataSize: compression?.size ?? 0,
323
+ position,
324
+ }
325
+ : undefined,
326
+ };
327
+ this.emit('AudioData', audioEventPayload);
328
+ }
329
+ // Stop recording
330
+ async stopRecording() {
331
+ if (!this.customRecorder) {
332
+ throw new Error('Recorder is not initialized');
333
+ }
334
+ this.logger?.debug('Starting stop process');
335
+ try {
336
+ const { compressedBlob, uncompressedBlob } = await this.customRecorder.stop();
337
+ this.isRecording = false;
338
+ this.isPaused = false;
339
+ let compression;
340
+ let fileUri = `${this.streamUuid}.${this.extension}`;
341
+ let mimeType = `audio/${this.extension}`;
342
+ // Handle both compressed and uncompressed blobs according to configuration
343
+ const compressionEnabled = this.recordingConfig?.compression?.enabled ?? false;
344
+ // Process compressed blob if available
345
+ if (compressedBlob) {
346
+ const compressedUri = URL.createObjectURL(compressedBlob);
347
+ const compressedInfo = {
348
+ compressedFileUri: compressedUri,
349
+ size: compressedBlob.size,
350
+ mimeType: 'audio/webm',
351
+ format: 'opus',
352
+ bitrate: this.recordingConfig?.compression?.bitrate ?? 128000,
353
+ };
354
+ // If compression is enabled, use compressed blob as primary format
355
+ if (compressionEnabled) {
356
+ this.logger?.debug('Using compressed audio as primary output');
357
+ fileUri = compressedUri;
358
+ mimeType = 'audio/webm';
359
+ // Store compression info
360
+ compression = compressedInfo;
361
+ }
362
+ else {
363
+ // Compression was enabled during recording but not set as primary
364
+ // Store as alternate format
365
+ compression = compressedInfo;
366
+ }
367
+ }
368
+ // Process uncompressed WAV if available
369
+ if (uncompressedBlob) {
370
+ const wavUri = URL.createObjectURL(uncompressedBlob);
371
+ // If compression is disabled or no compressed blob is available,
372
+ // use WAV as primary format
373
+ if (!compressionEnabled || !compressedBlob) {
374
+ this.logger?.debug('Using uncompressed WAV as primary output');
375
+ fileUri = wavUri;
376
+ mimeType = 'audio/wav';
377
+ }
378
+ }
379
+ // Use the stored streamUuid for the final filename
380
+ const filename = `${this.streamUuid}.${this.extension}`;
381
+ const result = {
382
+ fileUri,
383
+ filename,
384
+ bitDepth: this.bitDepth,
385
+ createdAt: this.recordingStartTime,
386
+ channels: this.recordingConfig?.channels ?? 1,
387
+ sampleRate: this.recordingConfig?.sampleRate ?? 44100,
388
+ durationMs: this.currentDurationMs,
389
+ size: this.currentSize,
390
+ mimeType,
391
+ compression,
392
+ };
393
+ // Reset after creating the result
394
+ this.streamUuid = null;
395
+ // Reset recording state variables to prepare for next recording
396
+ this.currentDurationMs = 0;
397
+ this.currentSize = 0;
398
+ this.lastEmittedSize = 0;
399
+ this.totalCompressedSize = 0;
400
+ this.lastEmittedCompressionSize = 0;
401
+ this.audioChunks = [];
402
+ return result;
403
+ }
404
+ catch (error) {
405
+ this.logger?.error('Error stopping recording:', error);
406
+ throw error;
407
+ }
408
+ }
409
+ // Pause recording
410
+ async pauseRecording() {
411
+ if (!this.isRecording) {
412
+ throw new Error('Recording is not active');
413
+ }
414
+ if (this.isPaused) {
415
+ this.logger?.debug('Recording already paused, skipping');
416
+ return;
417
+ }
418
+ try {
419
+ if (this.customRecorder) {
420
+ this.customRecorder.pause();
421
+ }
422
+ this.isPaused = true;
423
+ this.pausedTime = Date.now();
424
+ }
425
+ catch (error) {
426
+ this.logger?.error('Error in pauseRecording', error);
427
+ // Even if the pause operation failed, make sure our state is consistent
428
+ this.isPaused = true;
429
+ this.pausedTime = Date.now();
430
+ }
431
+ }
432
+ // Resume recording
433
+ async resumeRecording() {
434
+ if (!this.isPaused) {
435
+ throw new Error('Recording is not paused');
436
+ }
437
+ this.logger?.debug('Resuming recording', {
438
+ deviceDisconnectionBehavior: this.recordingConfig?.deviceDisconnectionBehavior,
439
+ isDeviceDisconnected: this.customRecorder?.isDeviceDisconnected,
440
+ });
441
+ try {
442
+ // If we have no recorder, or if the device is disconnected, always attempt fallback
443
+ if (!this.customRecorder ||
444
+ this.customRecorder.isDeviceDisconnected) {
445
+ this.logger?.debug('No recorder exists or device disconnected - attempting fallback on resume');
446
+ await this.handleDeviceFallback();
447
+ // handleDeviceFallback will manage resuming if successful, or emit error if failed.
448
+ return;
449
+ }
450
+ // Normal resume path - device is still connected
451
+ this.customRecorder.resume();
452
+ this.isPaused = false;
453
+ // Adjust the recording start time to account for the pause duration
454
+ const pauseDuration = Date.now() - this.pausedTime;
455
+ this.recordingStartTime += pauseDuration;
456
+ this.pausedTime = 0;
457
+ this.emit('onRecordingInterrupted', {
458
+ reason: 'userResumed',
459
+ isPaused: false,
460
+ timestamp: Date.now(),
461
+ });
462
+ }
463
+ catch (error) {
464
+ this.logger?.error('Resume failed:', error);
465
+ // Fallback to emitting a general failure if resume fails unexpectedly
466
+ this.emit('onRecordingInterrupted', {
467
+ reason: 'resumeFailed', // Use a more specific reason
468
+ isPaused: true, // Remain paused if resume fails
469
+ timestamp: Date.now(),
470
+ message: 'Failed to resume recording. Please stop and start again.',
471
+ });
472
+ }
473
+ }
474
+ // Get current status
475
+ status() {
476
+ const durationMs = this.getRecordingDuration();
477
+ const status = {
478
+ isRecording: this.isRecording,
479
+ isPaused: this.isPaused,
480
+ durationMs,
481
+ size: this.currentSize,
482
+ interval: this.currentInterval,
483
+ intervalAnalysis: this.currentIntervalAnalysis,
484
+ mimeType: `audio/${this.extension}`,
485
+ compression: this.recordingConfig?.compression?.enabled
486
+ ? {
487
+ size: this.totalCompressedSize,
488
+ mimeType: 'audio/webm',
489
+ format: this.recordingConfig.compression.format ?? 'opus',
490
+ bitrate: this.recordingConfig.compression.bitrate ?? 128000,
491
+ compressedFileUri: `${this.streamUuid}.webm`,
492
+ }
493
+ : undefined,
494
+ };
495
+ return status;
496
+ }
497
+ /**
498
+ * Handles device fallback when the current device is disconnected
499
+ */
500
+ async handleDeviceFallback() {
501
+ this.logger?.debug('Starting device fallback procedure');
502
+ if (!this.isRecording) {
503
+ return false;
504
+ }
505
+ try {
506
+ // Save important state before switching
507
+ const currentPosition = this.latestPosition;
508
+ const existingAudioChunks = [...this.audioChunks];
509
+ // Save compressed chunks if available
510
+ let compressedChunks = [];
511
+ if (this.customRecorder) {
512
+ try {
513
+ compressedChunks = this.customRecorder.getCompressedChunks();
514
+ }
515
+ catch (err) {
516
+ this.logger?.warn('Failed to get compressed chunks:', err);
517
+ }
518
+ }
519
+ // Save the current counter value for continuity
520
+ let currentDataPointCounter = 0;
521
+ if (this.customRecorder) {
522
+ currentDataPointCounter =
523
+ this.customRecorder.getDataPointCounter();
524
+ }
525
+ // Clean up existing recorder
526
+ if (this.customRecorder) {
527
+ try {
528
+ this.customRecorder.cleanup();
529
+ }
530
+ catch (cleanupError) {
531
+ this.logger?.warn('Error during cleanup:', cleanupError);
532
+ }
533
+ }
534
+ // Keep recording state true but mark as paused
535
+ this.isPaused = true;
536
+ this.pausedTime = Date.now();
537
+ // Store current size and other stats
538
+ const previousTotalSize = this.currentSize;
539
+ const previousLastEmittedSize = this.lastEmittedSize;
540
+ const previousCompressedSize = this.totalCompressedSize;
541
+ // Try to get a fallback device
542
+ const fallbackDeviceInfo = await this.getFallbackDevice();
543
+ if (!fallbackDeviceInfo) {
544
+ this.emit('onRecordingInterrupted', {
545
+ reason: 'deviceSwitchFailed',
546
+ isPaused: true,
547
+ timestamp: Date.now(),
548
+ message: 'Failed to switch to fallback device. Recording paused.',
549
+ });
550
+ return false;
551
+ }
552
+ // Start recording with the new device
553
+ try {
554
+ const stream = await this.requestPermissionsAndGetUserMedia(fallbackDeviceInfo.deviceId);
555
+ const audioContext = new (window.AudioContext ||
556
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
557
+ // @ts-ignore - Allow webkitAudioContext for Safari
558
+ window.webkitAudioContext)();
559
+ const source = audioContext.createMediaStreamSource(stream);
560
+ // Create a new recorder with the fallback device
561
+ this.customRecorder = new WebRecorder_web_1.WebRecorder({
562
+ logger: this.logger,
563
+ audioContext,
564
+ source,
565
+ recordingConfig: this.recordingConfig || {},
566
+ emitAudioEventCallback: this.customRecorderEventCallback.bind(this),
567
+ emitAudioAnalysisCallback: this.customRecorderAnalysisCallback.bind(this),
568
+ onInterruption: this.handleRecordingInterruption.bind(this),
569
+ });
570
+ await this.customRecorder.init();
571
+ // Set the initial position to continue from the previous device
572
+ this.customRecorder.setPosition(currentPosition);
573
+ // Reset the data point counter to continue from where the previous device left off
574
+ if (currentDataPointCounter > 0) {
575
+ this.customRecorder.resetDataPointCounter(currentDataPointCounter);
576
+ }
577
+ // Prepare the recorder to handle the device switch properly
578
+ this.customRecorder.prepareForDeviceSwitch();
579
+ // Restore the existing audio chunks
580
+ if (existingAudioChunks.length > 0) {
581
+ this.audioChunks = existingAudioChunks;
582
+ }
583
+ // Restore compressed chunks if available
584
+ if (compressedChunks.length > 0) {
585
+ this.customRecorder.setCompressedChunks(compressedChunks);
586
+ }
587
+ // Start the new recorder while preserving counters
588
+ this.customRecorder.start(true);
589
+ // Update recording state
590
+ this.isPaused = false;
591
+ this.recordingStartTime = Date.now();
592
+ // Restore size counters to maintain continuity
593
+ this.currentSize = previousTotalSize;
594
+ this.lastEmittedSize = previousLastEmittedSize;
595
+ this.totalCompressedSize = previousCompressedSize;
596
+ // Notify that we switched to a fallback device
597
+ if (this.eventCallback) {
598
+ this.eventCallback({
599
+ type: 'deviceFallback',
600
+ device: fallbackDeviceInfo.deviceId,
601
+ timestamp: new Date(),
602
+ });
603
+ }
604
+ return true;
605
+ }
606
+ catch (error) {
607
+ this.logger?.error('Failed to start recording with fallback device', error);
608
+ this.isPaused = true;
609
+ this.emit('onRecordingInterrupted', {
610
+ reason: 'deviceSwitchFailed',
611
+ isPaused: true,
612
+ timestamp: Date.now(),
613
+ message: 'Failed to switch to fallback device. Recording paused.',
614
+ });
615
+ return false;
616
+ }
617
+ }
618
+ catch (error) {
619
+ this.logger?.error('Failed to use fallback device', error);
620
+ this.isPaused = true;
621
+ this.emit('onRecordingInterrupted', {
622
+ reason: 'deviceSwitchFailed',
623
+ isPaused: true,
624
+ timestamp: Date.now(),
625
+ message: 'Failed to switch to fallback device. Recording paused.',
626
+ });
627
+ return false;
628
+ }
629
+ }
630
+ /**
631
+ * Attempts to get a fallback audio device
632
+ */
633
+ async getFallbackDevice() {
634
+ try {
635
+ // Get list of available audio input devices
636
+ const devices = await navigator.mediaDevices.enumerateDevices();
637
+ const audioInputDevices = devices.filter((device) => device.kind === 'audioinput');
638
+ if (audioInputDevices.length === 0) {
639
+ return null;
640
+ }
641
+ // Try to find a device that's not the current one
642
+ if (this.customRecorder) {
643
+ try {
644
+ // Use mediaDevices.enumerateDevices to find the current active device
645
+ const tracks = navigator.mediaDevices
646
+ .getUserMedia({ audio: true })
647
+ .then((stream) => {
648
+ const track = stream.getAudioTracks()[0];
649
+ return track ? track.label : '';
650
+ })
651
+ .catch(() => '');
652
+ const currentTrackLabel = await tracks;
653
+ if (currentTrackLabel) {
654
+ // Find a device with a different label
655
+ const differentDevice = audioInputDevices.find((device) => device.label &&
656
+ device.label !== currentTrackLabel);
657
+ if (differentDevice) {
658
+ return differentDevice;
659
+ }
660
+ }
661
+ }
662
+ catch (err) {
663
+ this.logger?.warn('Error determining current device, using default');
664
+ }
665
+ }
666
+ // Return the first available device (default device)
667
+ return audioInputDevices[0];
668
+ }
669
+ catch (error) {
670
+ this.logger?.error('Error finding fallback device:', error);
671
+ return null;
672
+ }
673
+ }
674
+ /**
675
+ * Gets user media with specific device ID
676
+ */
677
+ async requestPermissionsAndGetUserMedia(deviceId) {
678
+ try {
679
+ // Request the specific device
680
+ return await navigator.mediaDevices.getUserMedia({
681
+ audio: {
682
+ deviceId: { exact: deviceId },
683
+ },
684
+ });
685
+ }
686
+ catch (error) {
687
+ this.logger?.error(`Failed to get media for device ${deviceId}`, error);
688
+ // Try with default constraints as fallback
689
+ return await navigator.mediaDevices.getUserMedia({ audio: true });
690
+ }
691
+ }
692
+ init(options) {
693
+ try {
694
+ this.logger = options?.logger;
695
+ this.eventCallback = options?.eventCallback;
696
+ return Promise.resolve();
697
+ }
698
+ catch (error) {
699
+ this.logger?.error('Error initializing ExpoAudioStream', error);
700
+ return Promise.reject(error);
701
+ }
702
+ }
703
+ }
704
+ exports.ExpoAudioStreamWeb = ExpoAudioStreamWeb;
705
+ //# sourceMappingURL=ExpoAudioStream.web.js.map