@siteed/expo-audio-studio 2.1.1

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 (188) hide show
  1. package/CHANGELOG.md +210 -0
  2. package/LICENSE +21 -0
  3. package/README.md +269 -0
  4. package/android/build.gradle +105 -0
  5. package/android/src/main/AndroidManifest.xml +27 -0
  6. package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +166 -0
  7. package/android/src/main/java/net/siteed/audiostream/AudioDataEncoder.kt +9 -0
  8. package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +131 -0
  9. package/android/src/main/java/net/siteed/audiostream/AudioFormatUtils.kt +103 -0
  10. package/android/src/main/java/net/siteed/audiostream/AudioNotificationsManager.kt +435 -0
  11. package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +2235 -0
  12. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +1437 -0
  13. package/android/src/main/java/net/siteed/audiostream/AudioRecordingService.kt +166 -0
  14. package/android/src/main/java/net/siteed/audiostream/AudioTrimmer.kt +1099 -0
  15. package/android/src/main/java/net/siteed/audiostream/Constants.kt +21 -0
  16. package/android/src/main/java/net/siteed/audiostream/EventSender.kt +7 -0
  17. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +739 -0
  18. package/android/src/main/java/net/siteed/audiostream/FFT.kt +99 -0
  19. package/android/src/main/java/net/siteed/audiostream/Features.kt +98 -0
  20. package/android/src/main/java/net/siteed/audiostream/NotificationConfig.kt +70 -0
  21. package/android/src/main/java/net/siteed/audiostream/PermissionUtils.kt +59 -0
  22. package/android/src/main/java/net/siteed/audiostream/RecordingActionReceiver.kt +59 -0
  23. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +205 -0
  24. package/android/src/main/java/net/siteed/audiostream/WaveformConfig.kt +19 -0
  25. package/android/src/main/java/net/siteed/audiostream/WaveformRenderer.kt +159 -0
  26. package/android/src/main/res/drawable/ic_default_action_icon.xml +16 -0
  27. package/android/src/main/res/drawable/ic_microphone.xml +13 -0
  28. package/android/src/main/res/drawable/ic_pause.xml +10 -0
  29. package/android/src/main/res/drawable/ic_play.xml +10 -0
  30. package/android/src/main/res/drawable/ic_stop.xml +10 -0
  31. package/android/src/main/res/layout/notification_recording.xml +37 -0
  32. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +56 -0
  33. package/app.plugin.js +1 -0
  34. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +179 -0
  35. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  36. package/build/AudioAnalysis/AudioAnalysis.types.js +3 -0
  37. package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  38. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +68 -0
  39. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  40. package/build/AudioAnalysis/extractAudioAnalysis.js +203 -0
  41. package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  42. package/build/AudioAnalysis/extractAudioData.d.ts +3 -0
  43. package/build/AudioAnalysis/extractAudioData.d.ts.map +1 -0
  44. package/build/AudioAnalysis/extractAudioData.js +5 -0
  45. package/build/AudioAnalysis/extractAudioData.js.map +1 -0
  46. package/build/AudioAnalysis/extractMelSpectrogram.d.ts +14 -0
  47. package/build/AudioAnalysis/extractMelSpectrogram.d.ts.map +1 -0
  48. package/build/AudioAnalysis/extractMelSpectrogram.js +85 -0
  49. package/build/AudioAnalysis/extractMelSpectrogram.js.map +1 -0
  50. package/build/AudioAnalysis/extractPreview.d.ts +11 -0
  51. package/build/AudioAnalysis/extractPreview.d.ts.map +1 -0
  52. package/build/AudioAnalysis/extractPreview.js +25 -0
  53. package/build/AudioAnalysis/extractPreview.js.map +1 -0
  54. package/build/AudioAnalysis/extractWaveform.d.ts +8 -0
  55. package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  56. package/build/AudioAnalysis/extractWaveform.js +11 -0
  57. package/build/AudioAnalysis/extractWaveform.js.map +1 -0
  58. package/build/AudioRecorder.provider.d.ts +11 -0
  59. package/build/AudioRecorder.provider.d.ts.map +1 -0
  60. package/build/AudioRecorder.provider.js +37 -0
  61. package/build/AudioRecorder.provider.js.map +1 -0
  62. package/build/ExpoAudioStream.native.d.ts +3 -0
  63. package/build/ExpoAudioStream.native.d.ts.map +1 -0
  64. package/build/ExpoAudioStream.native.js +6 -0
  65. package/build/ExpoAudioStream.native.js.map +1 -0
  66. package/build/ExpoAudioStream.types.d.ts +532 -0
  67. package/build/ExpoAudioStream.types.d.ts.map +1 -0
  68. package/build/ExpoAudioStream.types.js +2 -0
  69. package/build/ExpoAudioStream.types.js.map +1 -0
  70. package/build/ExpoAudioStream.web.d.ts +59 -0
  71. package/build/ExpoAudioStream.web.d.ts.map +1 -0
  72. package/build/ExpoAudioStream.web.js +285 -0
  73. package/build/ExpoAudioStream.web.js.map +1 -0
  74. package/build/ExpoAudioStreamModule.d.ts +3 -0
  75. package/build/ExpoAudioStreamModule.d.ts.map +1 -0
  76. package/build/ExpoAudioStreamModule.js +693 -0
  77. package/build/ExpoAudioStreamModule.js.map +1 -0
  78. package/build/WebRecorder.web.d.ts +119 -0
  79. package/build/WebRecorder.web.d.ts.map +1 -0
  80. package/build/WebRecorder.web.js +436 -0
  81. package/build/WebRecorder.web.js.map +1 -0
  82. package/build/constants.d.ts +11 -0
  83. package/build/constants.d.ts.map +1 -0
  84. package/build/constants.js +14 -0
  85. package/build/constants.js.map +1 -0
  86. package/build/events.d.ts +26 -0
  87. package/build/events.d.ts.map +1 -0
  88. package/build/events.js +21 -0
  89. package/build/events.js.map +1 -0
  90. package/build/index.d.ts +15 -0
  91. package/build/index.d.ts.map +1 -0
  92. package/build/index.js +14 -0
  93. package/build/index.js.map +1 -0
  94. package/build/trimAudio.d.ts +25 -0
  95. package/build/trimAudio.d.ts.map +1 -0
  96. package/build/trimAudio.js +67 -0
  97. package/build/trimAudio.js.map +1 -0
  98. package/build/useAudioRecorder.d.ts +21 -0
  99. package/build/useAudioRecorder.d.ts.map +1 -0
  100. package/build/useAudioRecorder.js +427 -0
  101. package/build/useAudioRecorder.js.map +1 -0
  102. package/build/utils/BlobFix.d.ts +9 -0
  103. package/build/utils/BlobFix.d.ts.map +1 -0
  104. package/build/utils/BlobFix.js +498 -0
  105. package/build/utils/BlobFix.js.map +1 -0
  106. package/build/utils/audioProcessing.d.ts +24 -0
  107. package/build/utils/audioProcessing.d.ts.map +1 -0
  108. package/build/utils/audioProcessing.js +133 -0
  109. package/build/utils/audioProcessing.js.map +1 -0
  110. package/build/utils/concatenateBuffers.d.ts +8 -0
  111. package/build/utils/concatenateBuffers.d.ts.map +1 -0
  112. package/build/utils/concatenateBuffers.js +21 -0
  113. package/build/utils/concatenateBuffers.js.map +1 -0
  114. package/build/utils/convertPCMToFloat32.d.ts +13 -0
  115. package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
  116. package/build/utils/convertPCMToFloat32.js +120 -0
  117. package/build/utils/convertPCMToFloat32.js.map +1 -0
  118. package/build/utils/encodingToBitDepth.d.ts +5 -0
  119. package/build/utils/encodingToBitDepth.d.ts.map +1 -0
  120. package/build/utils/encodingToBitDepth.js +13 -0
  121. package/build/utils/encodingToBitDepth.js.map +1 -0
  122. package/build/utils/getWavFileInfo.d.ts +26 -0
  123. package/build/utils/getWavFileInfo.d.ts.map +1 -0
  124. package/build/utils/getWavFileInfo.js +92 -0
  125. package/build/utils/getWavFileInfo.js.map +1 -0
  126. package/build/utils/writeWavHeader.d.ts +49 -0
  127. package/build/utils/writeWavHeader.d.ts.map +1 -0
  128. package/build/utils/writeWavHeader.js +91 -0
  129. package/build/utils/writeWavHeader.js.map +1 -0
  130. package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
  131. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  132. package/build/workers/InlineFeaturesExtractor.web.js +828 -0
  133. package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
  134. package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
  135. package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  136. package/build/workers/inlineAudioWebWorker.web.js +157 -0
  137. package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
  138. package/expo-module.config.json +9 -0
  139. package/ios/AudioAnalysisData.swift +74 -0
  140. package/ios/AudioNotificationManager.swift +135 -0
  141. package/ios/AudioProcessingHelpers.swift +743 -0
  142. package/ios/AudioProcessor.swift +1313 -0
  143. package/ios/AudioStreamError.swift +7 -0
  144. package/ios/AudioStreamManager.swift +1708 -0
  145. package/ios/AudioStreamManagerDelegate.swift +16 -0
  146. package/ios/DataPoint.swift +54 -0
  147. package/ios/DecodingConfig.swift +47 -0
  148. package/ios/ExpoAudioStream.podspec +27 -0
  149. package/ios/ExpoAudioStreamModule.swift +805 -0
  150. package/ios/FFT.swift +62 -0
  151. package/ios/Features.swift +95 -0
  152. package/ios/Logger.swift +7 -0
  153. package/ios/NotificationExtension.swift +15 -0
  154. package/ios/RecordingResult.swift +22 -0
  155. package/ios/RecordingSettings.swift +265 -0
  156. package/ios/WaveformExtractor.swift +105 -0
  157. package/package.json +128 -0
  158. package/plugin/build/index.d.ts +21 -0
  159. package/plugin/build/index.js +192 -0
  160. package/plugin/src/index.ts +279 -0
  161. package/plugin/tsconfig.json +10 -0
  162. package/plugin/tsconfig.tsbuildinfo +1 -0
  163. package/src/AudioAnalysis/AudioAnalysis.types.ts +202 -0
  164. package/src/AudioAnalysis/extractAudioAnalysis.ts +333 -0
  165. package/src/AudioAnalysis/extractAudioData.ts +6 -0
  166. package/src/AudioAnalysis/extractMelSpectrogram.ts +144 -0
  167. package/src/AudioAnalysis/extractPreview.ts +34 -0
  168. package/src/AudioAnalysis/extractWaveform.ts +22 -0
  169. package/src/AudioRecorder.provider.tsx +54 -0
  170. package/src/ExpoAudioStream.native.ts +6 -0
  171. package/src/ExpoAudioStream.types.ts +641 -0
  172. package/src/ExpoAudioStream.web.ts +359 -0
  173. package/src/ExpoAudioStreamModule.ts +967 -0
  174. package/src/WebRecorder.web.ts +580 -0
  175. package/src/constants.ts +18 -0
  176. package/src/events.ts +60 -0
  177. package/src/index.ts +36 -0
  178. package/src/trimAudio.ts +90 -0
  179. package/src/useAudioRecorder.tsx +620 -0
  180. package/src/utils/BlobFix.ts +559 -0
  181. package/src/utils/audioProcessing.ts +205 -0
  182. package/src/utils/concatenateBuffers.ts +24 -0
  183. package/src/utils/convertPCMToFloat32.ts +170 -0
  184. package/src/utils/encodingToBitDepth.ts +18 -0
  185. package/src/utils/getWavFileInfo.ts +132 -0
  186. package/src/utils/writeWavHeader.ts +114 -0
  187. package/src/workers/InlineFeaturesExtractor.web.tsx +827 -0
  188. package/src/workers/inlineAudioWebWorker.web.tsx +156 -0
package/build/index.js ADDED
@@ -0,0 +1,14 @@
1
+ // src/index.ts
2
+ import { extractRawWavAnalysis, extractAudioAnalysis, } from './AudioAnalysis/extractAudioAnalysis';
3
+ import { extractAudioData } from './AudioAnalysis/extractAudioData';
4
+ import { extractMelSpectrogram } from './AudioAnalysis/extractMelSpectrogram';
5
+ import { extractPreview } from './AudioAnalysis/extractPreview';
6
+ import { AudioRecorderProvider, useSharedAudioRecorder, } from './AudioRecorder.provider';
7
+ import ExpoAudioStreamModule from './ExpoAudioStreamModule';
8
+ import { trimAudio } from './trimAudio';
9
+ import { useAudioRecorder } from './useAudioRecorder';
10
+ export * from './utils/convertPCMToFloat32';
11
+ export * from './utils/getWavFileInfo';
12
+ export * from './utils/writeWavHeader';
13
+ export { AudioRecorderProvider, ExpoAudioStreamModule, extractRawWavAnalysis, extractAudioAnalysis, extractPreview, trimAudio, extractAudioData, extractMelSpectrogram, useAudioRecorder, useSharedAudioRecorder, };
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAEf,OAAO,EACH,qBAAqB,EACrB,oBAAoB,GACvB,MAAM,sCAAsC,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAA;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAA;AAC7E,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EACH,qBAAqB,EACrB,sBAAsB,GACzB,MAAM,0BAA0B,CAAA;AACjC,OAAO,qBAAqB,MAAM,yBAAyB,CAAA;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAErD,cAAc,6BAA6B,CAAA;AAC3C,cAAc,wBAAwB,CAAA;AACtC,cAAc,wBAAwB,CAAA;AAEtC,OAAO,EACH,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,cAAc,EACd,SAAS,EACT,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,sBAAsB,GACzB,CAAA","sourcesContent":["// src/index.ts\n\nimport {\n extractRawWavAnalysis,\n extractAudioAnalysis,\n} from './AudioAnalysis/extractAudioAnalysis'\nimport { extractAudioData } from './AudioAnalysis/extractAudioData'\nimport { extractMelSpectrogram } from './AudioAnalysis/extractMelSpectrogram'\nimport { extractPreview } from './AudioAnalysis/extractPreview'\nimport {\n AudioRecorderProvider,\n useSharedAudioRecorder,\n} from './AudioRecorder.provider'\nimport ExpoAudioStreamModule from './ExpoAudioStreamModule'\nimport { trimAudio } from './trimAudio'\nimport { useAudioRecorder } from './useAudioRecorder'\n\nexport * from './utils/convertPCMToFloat32'\nexport * from './utils/getWavFileInfo'\nexport * from './utils/writeWavHeader'\n\nexport {\n AudioRecorderProvider,\n ExpoAudioStreamModule,\n extractRawWavAnalysis,\n extractAudioAnalysis,\n extractPreview,\n trimAudio,\n extractAudioData,\n extractMelSpectrogram,\n useAudioRecorder,\n useSharedAudioRecorder,\n}\n\nexport type * from './AudioAnalysis/AudioAnalysis.types'\nexport type * from './ExpoAudioStream.types'\n"]}
@@ -0,0 +1,25 @@
1
+ import { TrimAudioOptions, TrimAudioResult, TrimProgressEvent } from './ExpoAudioStream.types';
2
+ /**
3
+ * Trims an audio file based on the provided options.
4
+ *
5
+ * @experimental This API is experimental and not fully optimized for production use.
6
+ * Performance may vary based on file size and device capabilities.
7
+ * Future versions may include breaking changes.
8
+ *
9
+ * @param options Configuration options for the trimming operation
10
+ * @param progressCallback Optional callback to receive progress updates
11
+ * @returns Promise resolving to the trimmed audio file information, including processing time
12
+ */
13
+ export declare function trimAudio(options: TrimAudioOptions, progressCallback?: (event: TrimProgressEvent) => void): Promise<TrimAudioResult>;
14
+ /**
15
+ * Simplified version of trimAudio that returns only the URI of the trimmed file.
16
+ *
17
+ * @experimental This API is experimental and not fully optimized for production use.
18
+ * Performance may vary based on file size and device capabilities.
19
+ * Future versions may include breaking changes.
20
+ *
21
+ * @param options Configuration options for the trimming operation
22
+ * @returns Promise resolving to the URI of the trimmed audio file
23
+ */
24
+ export declare function trimAudioSimple(options: TrimAudioOptions): Promise<string>;
25
+ //# sourceMappingURL=trimAudio.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trimAudio.d.ts","sourceRoot":"","sources":["../src/trimAudio.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACpB,MAAM,yBAAyB,CAAA;AAMhC;;;;;;;;;;GAUG;AACH,wBAAsB,SAAS,CAC3B,OAAO,EAAE,gBAAgB,EACzB,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,GACtD,OAAO,CAAC,eAAe,CAAC,CA8C1B;AAED;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACjC,OAAO,EAAE,gBAAgB,GAC1B,OAAO,CAAC,MAAM,CAAC,CAGjB"}
@@ -0,0 +1,67 @@
1
+ import { LegacyEventEmitter } from 'expo-modules-core';
2
+ import ExpoAudioStreamModule from './ExpoAudioStreamModule';
3
+ // Create a single emitter instance
4
+ const emitter = new LegacyEventEmitter(ExpoAudioStreamModule);
5
+ /**
6
+ * Trims an audio file based on the provided options.
7
+ *
8
+ * @experimental This API is experimental and not fully optimized for production use.
9
+ * Performance may vary based on file size and device capabilities.
10
+ * Future versions may include breaking changes.
11
+ *
12
+ * @param options Configuration options for the trimming operation
13
+ * @param progressCallback Optional callback to receive progress updates
14
+ * @returns Promise resolving to the trimmed audio file information, including processing time
15
+ */
16
+ export async function trimAudio(options, progressCallback) {
17
+ // Validation
18
+ if (!options.fileUri) {
19
+ throw new Error('fileUri is required');
20
+ }
21
+ const mode = options.mode ?? 'single';
22
+ if (mode === 'single') {
23
+ if (options.startTimeMs === undefined &&
24
+ options.endTimeMs === undefined) {
25
+ throw new Error('At least one of startTimeMs or endTimeMs must be provided in single mode');
26
+ }
27
+ }
28
+ else if (mode === 'keep' || mode === 'remove') {
29
+ if (!options.ranges || options.ranges.length === 0) {
30
+ throw new Error('ranges must be provided and non-empty for keep or remove modes');
31
+ }
32
+ }
33
+ else {
34
+ throw new Error(`Invalid mode: ${mode}. Must be 'single', 'keep', or 'remove'`);
35
+ }
36
+ // Set up progress event listener if callback is provided
37
+ let subscription;
38
+ if (progressCallback) {
39
+ subscription = emitter.addListener('TrimProgress', (event) => {
40
+ progressCallback(event);
41
+ });
42
+ }
43
+ try {
44
+ const result = await ExpoAudioStreamModule.trimAudio(options);
45
+ return result;
46
+ }
47
+ finally {
48
+ if (subscription) {
49
+ subscription.remove();
50
+ }
51
+ }
52
+ }
53
+ /**
54
+ * Simplified version of trimAudio that returns only the URI of the trimmed file.
55
+ *
56
+ * @experimental This API is experimental and not fully optimized for production use.
57
+ * Performance may vary based on file size and device capabilities.
58
+ * Future versions may include breaking changes.
59
+ *
60
+ * @param options Configuration options for the trimming operation
61
+ * @returns Promise resolving to the URI of the trimmed audio file
62
+ */
63
+ export async function trimAudioSimple(options) {
64
+ const result = await trimAudio(options);
65
+ return result.uri;
66
+ }
67
+ //# sourceMappingURL=trimAudio.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trimAudio.js","sourceRoot":"","sources":["../src/trimAudio.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA0B,MAAM,mBAAmB,CAAA;AAO9E,OAAO,qBAAqB,MAAM,yBAAyB,CAAA;AAE3D,mCAAmC;AACnC,MAAM,OAAO,GAAG,IAAI,kBAAkB,CAAC,qBAAqB,CAAC,CAAA;AAE7D;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC3B,OAAyB,EACzB,gBAAqD;IAErD,aAAa;IACb,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;IAC1C,CAAC;IACD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,QAAQ,CAAA;IACrC,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QACpB,IACI,OAAO,CAAC,WAAW,KAAK,SAAS;YACjC,OAAO,CAAC,SAAS,KAAK,SAAS,EACjC,CAAC;YACC,MAAM,IAAI,KAAK,CACX,0EAA0E,CAC7E,CAAA;QACL,CAAC;IACL,CAAC;SAAM,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9C,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACjD,MAAM,IAAI,KAAK,CACX,gEAAgE,CACnE,CAAA;QACL,CAAC;IACL,CAAC;SAAM,CAAC;QACJ,MAAM,IAAI,KAAK,CACX,iBAAiB,IAAI,yCAAyC,CACjE,CAAA;IACL,CAAC;IAED,yDAAyD;IACzD,IAAI,YAA2C,CAAA;IAC/C,IAAI,gBAAgB,EAAE,CAAC;QACnB,YAAY,GAAG,OAAO,CAAC,WAAW,CAC9B,cAAc,EACd,CAAC,KAAwB,EAAE,EAAE;YACzB,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC,CACJ,CAAA;IACL,CAAC;IAED,IAAI,CAAC;QACD,MAAM,MAAM,GAAG,MAAM,qBAAqB,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;QAC7D,OAAO,MAAM,CAAA;IACjB,CAAC;YAAS,CAAC;QACP,IAAI,YAAY,EAAE,CAAC;YACf,YAAY,CAAC,MAAM,EAAE,CAAA;QACzB,CAAC;IACL,CAAC;AACL,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACjC,OAAyB;IAEzB,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,CAAA;IACvC,OAAO,MAAM,CAAC,GAAG,CAAA;AACrB,CAAC","sourcesContent":["import { LegacyEventEmitter, type EventSubscription } from 'expo-modules-core'\n\nimport {\n TrimAudioOptions,\n TrimAudioResult,\n TrimProgressEvent,\n} from './ExpoAudioStream.types'\nimport ExpoAudioStreamModule from './ExpoAudioStreamModule'\n\n// Create a single emitter instance\nconst emitter = new LegacyEventEmitter(ExpoAudioStreamModule)\n\n/**\n * Trims an audio file based on the provided options.\n *\n * @experimental This API is experimental and not fully optimized for production use.\n * Performance may vary based on file size and device capabilities.\n * Future versions may include breaking changes.\n *\n * @param options Configuration options for the trimming operation\n * @param progressCallback Optional callback to receive progress updates\n * @returns Promise resolving to the trimmed audio file information, including processing time\n */\nexport async function trimAudio(\n options: TrimAudioOptions,\n progressCallback?: (event: TrimProgressEvent) => void\n): Promise<TrimAudioResult> {\n // Validation\n if (!options.fileUri) {\n throw new Error('fileUri is required')\n }\n const mode = options.mode ?? 'single'\n if (mode === 'single') {\n if (\n options.startTimeMs === undefined &&\n options.endTimeMs === undefined\n ) {\n throw new Error(\n 'At least one of startTimeMs or endTimeMs must be provided in single mode'\n )\n }\n } else if (mode === 'keep' || mode === 'remove') {\n if (!options.ranges || options.ranges.length === 0) {\n throw new Error(\n 'ranges must be provided and non-empty for keep or remove modes'\n )\n }\n } else {\n throw new Error(\n `Invalid mode: ${mode}. Must be 'single', 'keep', or 'remove'`\n )\n }\n\n // Set up progress event listener if callback is provided\n let subscription: EventSubscription | undefined\n if (progressCallback) {\n subscription = emitter.addListener(\n 'TrimProgress',\n (event: TrimProgressEvent) => {\n progressCallback(event)\n }\n )\n }\n\n try {\n const result = await ExpoAudioStreamModule.trimAudio(options)\n return result\n } finally {\n if (subscription) {\n subscription.remove()\n }\n }\n}\n\n/**\n * Simplified version of trimAudio that returns only the URI of the trimmed file.\n *\n * @experimental This API is experimental and not fully optimized for production use.\n * Performance may vary based on file size and device capabilities.\n * Future versions may include breaking changes.\n *\n * @param options Configuration options for the trimming operation\n * @returns Promise resolving to the URI of the trimmed audio file\n */\nexport async function trimAudioSimple(\n options: TrimAudioOptions\n): Promise<string> {\n const result = await trimAudio(options)\n return result.uri\n}\n"]}
@@ -0,0 +1,21 @@
1
+ import { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types';
2
+ import { AudioRecording, CompressionInfo, ConsoleLike, RecordingConfig, StartRecordingResult } from './ExpoAudioStream.types';
3
+ export interface UseAudioRecorderProps {
4
+ logger?: ConsoleLike;
5
+ audioWorkletUrl?: string;
6
+ featuresExtratorUrl?: string;
7
+ }
8
+ export interface UseAudioRecorderState {
9
+ startRecording: (_: RecordingConfig) => Promise<StartRecordingResult>;
10
+ stopRecording: () => Promise<AudioRecording>;
11
+ pauseRecording: () => Promise<void>;
12
+ resumeRecording: () => Promise<void>;
13
+ isRecording: boolean;
14
+ isPaused: boolean;
15
+ durationMs: number;
16
+ size: number;
17
+ compression?: CompressionInfo;
18
+ analysisData?: AudioAnalysis;
19
+ }
20
+ export declare function useAudioRecorder({ logger, audioWorkletUrl, featuresExtratorUrl, }?: UseAudioRecorderProps): UseAudioRecorderState;
21
+ //# sourceMappingURL=useAudioRecorder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAudioRecorder.d.ts","sourceRoot":"","sources":["../src/useAudioRecorder.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAA;AACnE,OAAO,EAEH,cAAc,EAEd,eAAe,EACf,WAAW,EACX,eAAe,EACf,oBAAoB,EACvB,MAAM,yBAAyB,CAAA;AAShC,MAAM,WAAW,qBAAqB;IAClC,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC/B;AAED,MAAM,WAAW,qBAAqB;IAClC,cAAc,EAAE,CAAC,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,oBAAoB,CAAC,CAAA;IACrE,aAAa,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,CAAA;IAC5C,cAAc,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACnC,eAAe,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACpC,WAAW,EAAE,OAAO,CAAA;IACpB,QAAQ,EAAE,OAAO,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,eAAe,CAAA;IAC7B,YAAY,CAAC,EAAE,aAAa,CAAA;CAC/B;AAkHD,wBAAgB,gBAAgB,CAAC,EAC7B,MAAM,EACN,eAAe,EACf,mBAAmB,GACtB,GAAE,qBAA0B,GAAG,qBAAqB,CA8cpD"}
@@ -0,0 +1,427 @@
1
+ // src/useAudioRecorder.ts
2
+ import { Platform } from 'expo-modules-core';
3
+ import { useCallback, useEffect, useReducer, useRef } from 'react';
4
+ import ExpoAudioStreamModule from './ExpoAudioStreamModule';
5
+ import { addAudioAnalysisListener, addAudioEventListener, addRecordingInterruptionListener, } from './events';
6
+ const defaultAnalysis = {
7
+ segmentDurationMs: 100,
8
+ bitDepth: 32,
9
+ numberOfChannels: 1,
10
+ durationMs: 0,
11
+ sampleRate: 44100,
12
+ samples: 0,
13
+ dataPoints: [],
14
+ rmsRange: {
15
+ min: Number.POSITIVE_INFINITY,
16
+ max: Number.NEGATIVE_INFINITY,
17
+ },
18
+ amplitudeRange: {
19
+ min: Number.POSITIVE_INFINITY,
20
+ max: Number.NEGATIVE_INFINITY,
21
+ },
22
+ };
23
+ function audioRecorderReducer(state, action) {
24
+ switch (action.type) {
25
+ case 'START':
26
+ return {
27
+ ...state,
28
+ isRecording: true,
29
+ isPaused: false,
30
+ durationMs: 0,
31
+ size: 0,
32
+ compression: undefined,
33
+ analysisData: defaultAnalysis,
34
+ };
35
+ case 'STOP':
36
+ return {
37
+ ...state,
38
+ isRecording: false,
39
+ isPaused: false,
40
+ durationMs: 0,
41
+ size: 0,
42
+ compression: undefined,
43
+ analysisData: undefined,
44
+ };
45
+ case 'PAUSE':
46
+ return { ...state, isPaused: true, isRecording: false };
47
+ case 'RESUME':
48
+ return { ...state, isPaused: false, isRecording: true };
49
+ case 'UPDATE_RECORDING_STATE':
50
+ return {
51
+ ...state,
52
+ isPaused: action.payload.isPaused,
53
+ isRecording: action.payload.isRecording,
54
+ };
55
+ case 'UPDATE_STATUS': {
56
+ const newState = {
57
+ ...state,
58
+ durationMs: action.payload.durationMs,
59
+ size: action.payload.size,
60
+ compression: action.payload.compression
61
+ ? {
62
+ size: action.payload.compression.size,
63
+ mimeType: action.payload.compression.mimeType,
64
+ bitrate: action.payload.compression.bitrate,
65
+ format: action.payload.compression.format,
66
+ }
67
+ : undefined,
68
+ };
69
+ return newState;
70
+ }
71
+ case 'UPDATE_ANALYSIS':
72
+ return {
73
+ ...state,
74
+ analysisData: action.payload,
75
+ };
76
+ default:
77
+ return state;
78
+ }
79
+ }
80
+ export function useAudioRecorder({ logger, audioWorkletUrl, featuresExtratorUrl, } = {}) {
81
+ const [state, dispatch] = useReducer(audioRecorderReducer, {
82
+ isRecording: false,
83
+ isPaused: false,
84
+ durationMs: 0,
85
+ size: 0,
86
+ compression: undefined,
87
+ analysisData: undefined,
88
+ });
89
+ const startResultRef = useRef(null);
90
+ const analysisListenerRef = useRef(null);
91
+ // analysisRef is the current analysis data (last 10 seconds by default)
92
+ const analysisRef = useRef({ ...defaultAnalysis });
93
+ // fullAnalysisRef is the full analysis data (all data points)
94
+ const fullAnalysisRef = useRef({
95
+ ...defaultAnalysis,
96
+ });
97
+ // Instantiate the module for web with URLs
98
+ const ExpoAudioStream = Platform.OS === 'web'
99
+ ? ExpoAudioStreamModule({
100
+ audioWorkletUrl,
101
+ featuresExtratorUrl,
102
+ logger,
103
+ })
104
+ : ExpoAudioStreamModule;
105
+ const onAudioStreamRef = useRef(null);
106
+ const stateRef = useRef({
107
+ isRecording: false,
108
+ isPaused: false,
109
+ durationMs: 0,
110
+ size: 0,
111
+ compression: undefined,
112
+ });
113
+ const recordingConfigRef = useRef(null);
114
+ const handleAudioAnalysis = useCallback(async ({ analysis, visualizationDuration, }) => {
115
+ const savedAnalysisData = analysisRef.current || {
116
+ ...defaultAnalysis,
117
+ };
118
+ const maxDuration = visualizationDuration;
119
+ logger?.debug(`[handleAudioAnalysis] Received audio analysis: maxDuration=${maxDuration} analysis.dataPoints=${analysis.dataPoints.length} analysisData.dataPoints=${savedAnalysisData.dataPoints.length}`, analysis);
120
+ // Combine data points
121
+ const combinedDataPoints = [
122
+ ...savedAnalysisData.dataPoints,
123
+ ...analysis.dataPoints,
124
+ ];
125
+ const fullCombinedDataPoints = [
126
+ ...(fullAnalysisRef.current?.dataPoints ?? []),
127
+ ...analysis.dataPoints,
128
+ ];
129
+ // Calculate the new duration
130
+ // The number of segments is based on how many segments of segmentDurationMs can fit in visualizationDuration
131
+ const numberOfSegments = Math.ceil(visualizationDuration / analysis.segmentDurationMs);
132
+ // maxDataPoints should be the number of data points, not milliseconds
133
+ const maxDataPoints = numberOfSegments;
134
+ logger?.debug(`[handleAudioAnalysis] Combined data points before trimming: numberOfSegments=${numberOfSegments} visualizationDuration=${visualizationDuration} combinedDataPointsLength=${combinedDataPoints.length} vs maxDataPoints=${maxDataPoints}`);
135
+ // Trim data points to keep within the maximum number of data points
136
+ if (combinedDataPoints.length > maxDataPoints) {
137
+ combinedDataPoints.splice(0, combinedDataPoints.length - maxDataPoints);
138
+ }
139
+ // Keep the full data points
140
+ fullAnalysisRef.current = {
141
+ ...fullAnalysisRef.current,
142
+ dataPoints: fullCombinedDataPoints,
143
+ };
144
+ fullAnalysisRef.current.durationMs =
145
+ fullCombinedDataPoints.length * analysis.segmentDurationMs;
146
+ savedAnalysisData.dataPoints = combinedDataPoints;
147
+ savedAnalysisData.bitDepth =
148
+ analysis.bitDepth || savedAnalysisData.bitDepth;
149
+ savedAnalysisData.durationMs =
150
+ combinedDataPoints.length * analysis.segmentDurationMs;
151
+ // Update amplitude range
152
+ const newMin = Math.min(savedAnalysisData.amplitudeRange.min, analysis.amplitudeRange.min);
153
+ const newMax = Math.max(savedAnalysisData.amplitudeRange.max, analysis.amplitudeRange.max);
154
+ savedAnalysisData.amplitudeRange = {
155
+ min: newMin,
156
+ max: newMax,
157
+ };
158
+ fullAnalysisRef.current.amplitudeRange = {
159
+ min: newMin,
160
+ max: newMax,
161
+ };
162
+ logger?.debug(`[handleAudioAnalysis] Updated analysis data: durationMs=${savedAnalysisData.durationMs}`, savedAnalysisData);
163
+ // Call the onAudioAnalysis callback if it exists in the recording config
164
+ if (recordingConfigRef.current?.onAudioAnalysis) {
165
+ recordingConfigRef.current
166
+ .onAudioAnalysis(analysis)
167
+ .catch((error) => {
168
+ logger?.warn(`Error processing audio analysis:`, error);
169
+ });
170
+ }
171
+ // Update the ref
172
+ analysisRef.current = savedAnalysisData;
173
+ // Dispatch the updated analysis data to state to trigger re-render
174
+ dispatch({
175
+ type: 'UPDATE_ANALYSIS',
176
+ payload: { ...savedAnalysisData },
177
+ });
178
+ }, [dispatch]);
179
+ const handleAudioEvent = useCallback(async (eventData) => {
180
+ const { fileUri, deltaSize, totalSize, lastEmittedSize, position, streamUuid, encoded, mimeType, buffer, compression, } = eventData;
181
+ logger?.debug(`[handleAudioEvent] Received audio event:`, {
182
+ fileUri,
183
+ deltaSize,
184
+ totalSize,
185
+ position,
186
+ mimeType,
187
+ lastEmittedSize,
188
+ streamUuid,
189
+ encodedLength: encoded?.length,
190
+ compression,
191
+ });
192
+ if (deltaSize === 0) {
193
+ // Ignore packet with no data
194
+ return;
195
+ }
196
+ try {
197
+ // Coming from native ( ios / android ) otherwise buffer is set
198
+ if (Platform.OS !== 'web') {
199
+ // Read the audio file as a base64 string for comparison
200
+ if (!encoded) {
201
+ logger?.error(`Encoded audio data is missing`);
202
+ throw new Error('Encoded audio data is missing');
203
+ }
204
+ onAudioStreamRef.current?.({
205
+ data: encoded,
206
+ position,
207
+ fileUri,
208
+ eventDataSize: deltaSize,
209
+ totalSize,
210
+ compression: compression && startResultRef.current?.compression
211
+ ? {
212
+ data: compression.data,
213
+ size: compression.totalSize,
214
+ mimeType: startResultRef.current.compression
215
+ ?.mimeType,
216
+ bitrate: startResultRef.current.compression
217
+ ?.bitrate,
218
+ format: startResultRef.current.compression
219
+ ?.format,
220
+ }
221
+ : undefined,
222
+ });
223
+ }
224
+ else if (buffer) {
225
+ // Coming from web
226
+ const webEvent = {
227
+ data: buffer,
228
+ position,
229
+ fileUri,
230
+ eventDataSize: deltaSize,
231
+ totalSize,
232
+ compression: compression && startResultRef.current?.compression
233
+ ? {
234
+ data: compression.data,
235
+ size: compression.totalSize,
236
+ mimeType: startResultRef.current.compression
237
+ ?.mimeType,
238
+ bitrate: startResultRef.current.compression
239
+ ?.bitrate,
240
+ format: startResultRef.current.compression
241
+ ?.format,
242
+ }
243
+ : undefined,
244
+ };
245
+ onAudioStreamRef.current?.(webEvent);
246
+ logger?.debug(`[handleAudioEvent] Audio data sent to onAudioStream`, webEvent);
247
+ }
248
+ }
249
+ catch (error) {
250
+ logger?.error(`Error processing audio event:`, error);
251
+ }
252
+ }, []);
253
+ const checkStatus = useCallback(async () => {
254
+ try {
255
+ const status = ExpoAudioStream.status();
256
+ logger?.debug(`Status: paused: ${status.isPaused} isRecording: ${status.isRecording} durationMs: ${status.durationMs} size: ${status.size}`, status.compression);
257
+ // Only dispatch if values actually changed
258
+ if (status.isRecording !== stateRef.current.isRecording ||
259
+ status.isPaused !== stateRef.current.isPaused) {
260
+ stateRef.current.isRecording = status.isRecording;
261
+ stateRef.current.isPaused = status.isPaused;
262
+ dispatch({
263
+ type: 'UPDATE_RECORDING_STATE',
264
+ payload: {
265
+ isRecording: status.isRecording,
266
+ isPaused: status.isPaused,
267
+ },
268
+ });
269
+ }
270
+ if (status.durationMs !== stateRef.current.durationMs ||
271
+ status.size !== stateRef.current.size) {
272
+ stateRef.current.durationMs = status.durationMs;
273
+ stateRef.current.size = status.size;
274
+ stateRef.current.compression = status.compression;
275
+ dispatch({
276
+ type: 'UPDATE_STATUS',
277
+ payload: {
278
+ durationMs: status.durationMs,
279
+ size: status.size,
280
+ compression: status.compression,
281
+ },
282
+ });
283
+ }
284
+ }
285
+ catch (error) {
286
+ logger?.error(`Error getting status:`, error);
287
+ }
288
+ }, [ExpoAudioStream, logger]); // Only depend on ExpoAudioStream and logger
289
+ // Update ref when state changes
290
+ useEffect(() => {
291
+ stateRef.current = {
292
+ isRecording: state.isRecording,
293
+ isPaused: state.isPaused,
294
+ durationMs: state.durationMs,
295
+ size: state.size,
296
+ compression: state.compression,
297
+ };
298
+ }, [
299
+ state.isRecording,
300
+ state.isPaused,
301
+ state.durationMs,
302
+ state.size,
303
+ state.compression,
304
+ ]);
305
+ const startRecording = useCallback(async (recordingOptions) => {
306
+ recordingConfigRef.current = recordingOptions;
307
+ logger?.debug(`start recoding`, recordingOptions);
308
+ analysisRef.current = { ...defaultAnalysis }; // Reset analysis data
309
+ fullAnalysisRef.current = { ...defaultAnalysis };
310
+ const { onAudioStream, ...options } = recordingOptions;
311
+ const { enableProcessing } = options;
312
+ const maxRecentDataDuration = 10000; // TODO compute maxRecentDataDuration based on screen dimensions
313
+ if (typeof onAudioStream === 'function') {
314
+ onAudioStreamRef.current = onAudioStream;
315
+ }
316
+ else {
317
+ logger?.warn(`onAudioStream is not a function`, onAudioStream);
318
+ onAudioStreamRef.current = null;
319
+ }
320
+ const startResult = await ExpoAudioStream.startRecording(options);
321
+ dispatch({ type: 'START' });
322
+ startResultRef.current = startResult;
323
+ if (enableProcessing) {
324
+ logger?.debug(`Enabling audio analysis listener`);
325
+ const listener = addAudioAnalysisListener(async (analysisData) => {
326
+ try {
327
+ await handleAudioAnalysis({
328
+ analysis: analysisData,
329
+ visualizationDuration: maxRecentDataDuration,
330
+ });
331
+ }
332
+ catch (error) {
333
+ logger?.warn(`Error processing audio analysis:`, error);
334
+ }
335
+ });
336
+ analysisListenerRef.current = listener;
337
+ }
338
+ return startResult;
339
+ }, [handleAudioAnalysis, dispatch]);
340
+ const stopRecording = useCallback(async () => {
341
+ logger?.debug(`stoping recording`);
342
+ const stopResult = await ExpoAudioStream.stopRecording();
343
+ stopResult.analysisData = fullAnalysisRef.current;
344
+ if (analysisListenerRef.current) {
345
+ analysisListenerRef.current.remove();
346
+ analysisListenerRef.current = null;
347
+ }
348
+ onAudioStreamRef.current = null;
349
+ logger?.debug(`recording stopped`, stopResult);
350
+ dispatch({ type: 'STOP' });
351
+ return stopResult;
352
+ }, [dispatch]);
353
+ const pauseRecording = useCallback(async () => {
354
+ logger?.debug(`pause recording`);
355
+ const pauseResult = await ExpoAudioStream.pauseRecording();
356
+ dispatch({ type: 'PAUSE' });
357
+ return pauseResult;
358
+ }, [dispatch]);
359
+ const resumeRecording = useCallback(async () => {
360
+ logger?.debug(`resume recording`);
361
+ const resumeResult = await ExpoAudioStream.resumeRecording();
362
+ dispatch({ type: 'RESUME' });
363
+ return resumeResult;
364
+ }, [dispatch]);
365
+ useEffect(() => {
366
+ let intervalId;
367
+ if (state.isRecording || state.isPaused) {
368
+ // Immediately check status when starting
369
+ checkStatus();
370
+ // Start interval
371
+ intervalId = setInterval(checkStatus, 1000);
372
+ }
373
+ return () => {
374
+ if (intervalId) {
375
+ clearInterval(intervalId);
376
+ intervalId = undefined;
377
+ }
378
+ };
379
+ }, [checkStatus, state.isRecording, state.isPaused]);
380
+ useEffect(() => {
381
+ logger?.debug(`Registering audio event listener`);
382
+ const subscribeAudio = addAudioEventListener(handleAudioEvent);
383
+ logger?.debug(`Subscribed to audio event listener and analysis listener`, {
384
+ subscribeAudio,
385
+ });
386
+ return () => {
387
+ logger?.debug(`Removing audio event listener`);
388
+ subscribeAudio.remove();
389
+ };
390
+ }, [handleAudioEvent, handleAudioAnalysis]);
391
+ useEffect(() => {
392
+ // Add event subscription for recording interruptions
393
+ logger?.debug('Setting up recording interruption listener');
394
+ const subscription = addRecordingInterruptionListener((event) => {
395
+ logger?.debug('Received recording interruption event:', event);
396
+ // Check if we have a callback configured
397
+ if (recordingConfigRef.current?.onRecordingInterrupted) {
398
+ try {
399
+ recordingConfigRef.current.onRecordingInterrupted(event);
400
+ }
401
+ catch (error) {
402
+ logger?.error('Error in recording interruption callback:', error);
403
+ }
404
+ }
405
+ else {
406
+ logger?.warn('No recording interruption callback configured');
407
+ }
408
+ });
409
+ return () => {
410
+ logger?.debug('Removing recording interruption listener');
411
+ subscription.remove();
412
+ };
413
+ }, []); // Empty dependency array since we want this to run once
414
+ return {
415
+ startRecording,
416
+ stopRecording,
417
+ pauseRecording,
418
+ resumeRecording,
419
+ isPaused: state.isPaused,
420
+ isRecording: state.isRecording,
421
+ durationMs: state.durationMs,
422
+ size: state.size,
423
+ compression: state.compression,
424
+ analysisData: state.analysisData,
425
+ };
426
+ }
427
+ //# sourceMappingURL=useAudioRecorder.js.map