@siteed/expo-audio-stream 2.1.0 → 2.2.0

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 (187) hide show
  1. package/README.md +40 -222
  2. package/build/index.d.ts +11 -15
  3. package/build/index.js +44 -14
  4. package/package.json +49 -110
  5. package/src/index.ts +18 -32
  6. package/CHANGELOG.md +0 -206
  7. package/android/build.gradle +0 -105
  8. package/android/src/main/AndroidManifest.xml +0 -27
  9. package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +0 -166
  10. package/android/src/main/java/net/siteed/audiostream/AudioDataEncoder.kt +0 -9
  11. package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +0 -131
  12. package/android/src/main/java/net/siteed/audiostream/AudioFormatUtils.kt +0 -103
  13. package/android/src/main/java/net/siteed/audiostream/AudioNotificationsManager.kt +0 -435
  14. package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +0 -2235
  15. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +0 -1437
  16. package/android/src/main/java/net/siteed/audiostream/AudioRecordingService.kt +0 -152
  17. package/android/src/main/java/net/siteed/audiostream/AudioTrimmer.kt +0 -1099
  18. package/android/src/main/java/net/siteed/audiostream/Constants.kt +0 -21
  19. package/android/src/main/java/net/siteed/audiostream/EventSender.kt +0 -7
  20. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +0 -739
  21. package/android/src/main/java/net/siteed/audiostream/FFT.kt +0 -99
  22. package/android/src/main/java/net/siteed/audiostream/Features.kt +0 -98
  23. package/android/src/main/java/net/siteed/audiostream/NotificationConfig.kt +0 -70
  24. package/android/src/main/java/net/siteed/audiostream/PermissionUtils.kt +0 -59
  25. package/android/src/main/java/net/siteed/audiostream/RecordingActionReceiver.kt +0 -59
  26. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +0 -205
  27. package/android/src/main/java/net/siteed/audiostream/WaveformConfig.kt +0 -19
  28. package/android/src/main/java/net/siteed/audiostream/WaveformRenderer.kt +0 -159
  29. package/android/src/main/res/drawable/ic_default_action_icon.xml +0 -16
  30. package/android/src/main/res/drawable/ic_microphone.xml +0 -13
  31. package/android/src/main/res/drawable/ic_pause.xml +0 -10
  32. package/android/src/main/res/drawable/ic_play.xml +0 -10
  33. package/android/src/main/res/drawable/ic_stop.xml +0 -10
  34. package/android/src/main/res/layout/notification_recording.xml +0 -37
  35. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +0 -56
  36. package/app.plugin.js +0 -1
  37. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +0 -179
  38. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +0 -1
  39. package/build/AudioAnalysis/AudioAnalysis.types.js +0 -3
  40. package/build/AudioAnalysis/AudioAnalysis.types.js.map +0 -1
  41. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +0 -68
  42. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +0 -1
  43. package/build/AudioAnalysis/extractAudioAnalysis.js +0 -203
  44. package/build/AudioAnalysis/extractAudioAnalysis.js.map +0 -1
  45. package/build/AudioAnalysis/extractAudioData.d.ts +0 -3
  46. package/build/AudioAnalysis/extractAudioData.d.ts.map +0 -1
  47. package/build/AudioAnalysis/extractAudioData.js +0 -5
  48. package/build/AudioAnalysis/extractAudioData.js.map +0 -1
  49. package/build/AudioAnalysis/extractMelSpectrogram.d.ts +0 -14
  50. package/build/AudioAnalysis/extractMelSpectrogram.d.ts.map +0 -1
  51. package/build/AudioAnalysis/extractMelSpectrogram.js +0 -85
  52. package/build/AudioAnalysis/extractMelSpectrogram.js.map +0 -1
  53. package/build/AudioAnalysis/extractPreview.d.ts +0 -11
  54. package/build/AudioAnalysis/extractPreview.d.ts.map +0 -1
  55. package/build/AudioAnalysis/extractPreview.js +0 -25
  56. package/build/AudioAnalysis/extractPreview.js.map +0 -1
  57. package/build/AudioAnalysis/extractWaveform.d.ts +0 -8
  58. package/build/AudioAnalysis/extractWaveform.d.ts.map +0 -1
  59. package/build/AudioAnalysis/extractWaveform.js +0 -11
  60. package/build/AudioAnalysis/extractWaveform.js.map +0 -1
  61. package/build/AudioRecorder.provider.d.ts +0 -11
  62. package/build/AudioRecorder.provider.d.ts.map +0 -1
  63. package/build/AudioRecorder.provider.js +0 -37
  64. package/build/AudioRecorder.provider.js.map +0 -1
  65. package/build/ExpoAudioStream.native.d.ts +0 -3
  66. package/build/ExpoAudioStream.native.d.ts.map +0 -1
  67. package/build/ExpoAudioStream.native.js +0 -6
  68. package/build/ExpoAudioStream.native.js.map +0 -1
  69. package/build/ExpoAudioStream.types.d.ts +0 -532
  70. package/build/ExpoAudioStream.types.d.ts.map +0 -1
  71. package/build/ExpoAudioStream.types.js +0 -2
  72. package/build/ExpoAudioStream.types.js.map +0 -1
  73. package/build/ExpoAudioStream.web.d.ts +0 -59
  74. package/build/ExpoAudioStream.web.d.ts.map +0 -1
  75. package/build/ExpoAudioStream.web.js +0 -285
  76. package/build/ExpoAudioStream.web.js.map +0 -1
  77. package/build/ExpoAudioStreamModule.d.ts +0 -3
  78. package/build/ExpoAudioStreamModule.d.ts.map +0 -1
  79. package/build/ExpoAudioStreamModule.js +0 -693
  80. package/build/ExpoAudioStreamModule.js.map +0 -1
  81. package/build/WebRecorder.web.d.ts +0 -119
  82. package/build/WebRecorder.web.d.ts.map +0 -1
  83. package/build/WebRecorder.web.js +0 -436
  84. package/build/WebRecorder.web.js.map +0 -1
  85. package/build/constants.d.ts +0 -11
  86. package/build/constants.d.ts.map +0 -1
  87. package/build/constants.js +0 -14
  88. package/build/constants.js.map +0 -1
  89. package/build/events.d.ts +0 -26
  90. package/build/events.d.ts.map +0 -1
  91. package/build/events.js +0 -21
  92. package/build/events.js.map +0 -1
  93. package/build/index.d.ts.map +0 -1
  94. package/build/index.js.map +0 -1
  95. package/build/trimAudio.d.ts +0 -25
  96. package/build/trimAudio.d.ts.map +0 -1
  97. package/build/trimAudio.js +0 -67
  98. package/build/trimAudio.js.map +0 -1
  99. package/build/useAudioRecorder.d.ts +0 -21
  100. package/build/useAudioRecorder.d.ts.map +0 -1
  101. package/build/useAudioRecorder.js +0 -427
  102. package/build/useAudioRecorder.js.map +0 -1
  103. package/build/utils/BlobFix.d.ts +0 -9
  104. package/build/utils/BlobFix.d.ts.map +0 -1
  105. package/build/utils/BlobFix.js +0 -498
  106. package/build/utils/BlobFix.js.map +0 -1
  107. package/build/utils/audioProcessing.d.ts +0 -24
  108. package/build/utils/audioProcessing.d.ts.map +0 -1
  109. package/build/utils/audioProcessing.js +0 -133
  110. package/build/utils/audioProcessing.js.map +0 -1
  111. package/build/utils/concatenateBuffers.d.ts +0 -8
  112. package/build/utils/concatenateBuffers.d.ts.map +0 -1
  113. package/build/utils/concatenateBuffers.js +0 -21
  114. package/build/utils/concatenateBuffers.js.map +0 -1
  115. package/build/utils/convertPCMToFloat32.d.ts +0 -13
  116. package/build/utils/convertPCMToFloat32.d.ts.map +0 -1
  117. package/build/utils/convertPCMToFloat32.js +0 -120
  118. package/build/utils/convertPCMToFloat32.js.map +0 -1
  119. package/build/utils/encodingToBitDepth.d.ts +0 -5
  120. package/build/utils/encodingToBitDepth.d.ts.map +0 -1
  121. package/build/utils/encodingToBitDepth.js +0 -13
  122. package/build/utils/encodingToBitDepth.js.map +0 -1
  123. package/build/utils/getWavFileInfo.d.ts +0 -26
  124. package/build/utils/getWavFileInfo.d.ts.map +0 -1
  125. package/build/utils/getWavFileInfo.js +0 -92
  126. package/build/utils/getWavFileInfo.js.map +0 -1
  127. package/build/utils/writeWavHeader.d.ts +0 -49
  128. package/build/utils/writeWavHeader.d.ts.map +0 -1
  129. package/build/utils/writeWavHeader.js +0 -91
  130. package/build/utils/writeWavHeader.js.map +0 -1
  131. package/build/workers/InlineFeaturesExtractor.web.d.ts +0 -2
  132. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +0 -1
  133. package/build/workers/InlineFeaturesExtractor.web.js +0 -828
  134. package/build/workers/InlineFeaturesExtractor.web.js.map +0 -1
  135. package/build/workers/inlineAudioWebWorker.web.d.ts +0 -2
  136. package/build/workers/inlineAudioWebWorker.web.d.ts.map +0 -1
  137. package/build/workers/inlineAudioWebWorker.web.js +0 -157
  138. package/build/workers/inlineAudioWebWorker.web.js.map +0 -1
  139. package/expo-module.config.json +0 -9
  140. package/ios/AudioAnalysisData.swift +0 -74
  141. package/ios/AudioNotificationManager.swift +0 -135
  142. package/ios/AudioProcessingHelpers.swift +0 -743
  143. package/ios/AudioProcessor.swift +0 -1313
  144. package/ios/AudioStreamError.swift +0 -7
  145. package/ios/AudioStreamManager.swift +0 -1708
  146. package/ios/AudioStreamManagerDelegate.swift +0 -16
  147. package/ios/DataPoint.swift +0 -54
  148. package/ios/DecodingConfig.swift +0 -47
  149. package/ios/ExpoAudioStream.podspec +0 -27
  150. package/ios/ExpoAudioStreamModule.swift +0 -805
  151. package/ios/FFT.swift +0 -62
  152. package/ios/Features.swift +0 -95
  153. package/ios/Logger.swift +0 -7
  154. package/ios/NotificationExtension.swift +0 -15
  155. package/ios/RecordingResult.swift +0 -22
  156. package/ios/RecordingSettings.swift +0 -265
  157. package/ios/WaveformExtractor.swift +0 -105
  158. package/plugin/build/index.d.ts +0 -21
  159. package/plugin/build/index.js +0 -191
  160. package/plugin/src/index.ts +0 -278
  161. package/plugin/tsconfig.json +0 -10
  162. package/plugin/tsconfig.tsbuildinfo +0 -1
  163. package/src/AudioAnalysis/AudioAnalysis.types.ts +0 -202
  164. package/src/AudioAnalysis/extractAudioAnalysis.ts +0 -333
  165. package/src/AudioAnalysis/extractAudioData.ts +0 -6
  166. package/src/AudioAnalysis/extractMelSpectrogram.ts +0 -144
  167. package/src/AudioAnalysis/extractPreview.ts +0 -34
  168. package/src/AudioAnalysis/extractWaveform.ts +0 -22
  169. package/src/AudioRecorder.provider.tsx +0 -54
  170. package/src/ExpoAudioStream.native.ts +0 -6
  171. package/src/ExpoAudioStream.types.ts +0 -641
  172. package/src/ExpoAudioStream.web.ts +0 -359
  173. package/src/ExpoAudioStreamModule.ts +0 -967
  174. package/src/WebRecorder.web.ts +0 -580
  175. package/src/constants.ts +0 -18
  176. package/src/events.ts +0 -60
  177. package/src/trimAudio.ts +0 -90
  178. package/src/useAudioRecorder.tsx +0 -620
  179. package/src/utils/BlobFix.ts +0 -559
  180. package/src/utils/audioProcessing.ts +0 -205
  181. package/src/utils/concatenateBuffers.ts +0 -24
  182. package/src/utils/convertPCMToFloat32.ts +0 -170
  183. package/src/utils/encodingToBitDepth.ts +0 -18
  184. package/src/utils/getWavFileInfo.ts +0 -132
  185. package/src/utils/writeWavHeader.ts +0 -114
  186. package/src/workers/InlineFeaturesExtractor.web.tsx +0 -827
  187. package/src/workers/inlineAudioWebWorker.web.tsx +0 -156
@@ -1,333 +0,0 @@
1
- // packages/expo-audio-stream/src/AudioAnalysis/extractAudioAnalysis.ts
2
- /**
3
- * This module provides functions for extracting and analyzing audio data.
4
- * - `extractAudioAnalysis`: For detailed analysis with customizable ranges and decoding options.
5
- * - `extractWavAudioAnalysis`: For analyzing WAV files without decoding, preserving original PCM values.
6
- * - `extractPreview`: For generating quick previews of audio waveforms, optimized for UI rendering.
7
- */
8
- import crc32 from 'crc-32'
9
-
10
- import { ConsoleLike } from '../ExpoAudioStream.types'
11
- import ExpoAudioStreamModule from '../ExpoAudioStreamModule'
12
- import { isWeb } from '../constants'
13
- import {
14
- AudioAnalysis,
15
- AudioFeaturesOptions,
16
- DataPoint,
17
- DecodingConfig,
18
- } from './AudioAnalysis.types'
19
- import { processAudioBuffer } from '../utils/audioProcessing'
20
- import { convertPCMToFloat32 } from '../utils/convertPCMToFloat32'
21
- import { getWavFileInfo, WavFileInfo } from '../utils/getWavFileInfo'
22
- import { InlineFeaturesExtractor } from '../workers/InlineFeaturesExtractor.web'
23
-
24
- function calculateCRC32ForDataPoint(data: Float32Array): number {
25
- // Convert float array to byte array for CRC32
26
- const byteArray = new Uint8Array(data.length * 4)
27
- const dataView = new DataView(byteArray.buffer)
28
-
29
- for (let i = 0; i < data.length; i++) {
30
- dataView.setFloat32(i * 4, data[i], true)
31
- }
32
-
33
- return crc32.buf(byteArray)
34
- }
35
-
36
- export interface ExtractWavAudioAnalysisProps {
37
- fileUri?: string // should provide either fileUri or arrayBuffer
38
- wavMetadata?: WavFileInfo
39
- arrayBuffer?: ArrayBuffer
40
- bitDepth?: number
41
- durationMs?: number
42
- sampleRate?: number
43
- numberOfChannels?: number
44
- position?: number // Optional number of bytes to skip. Default is 0
45
- length?: number // Optional number of bytes to read.
46
- segmentDurationMs?: number // Optional number of points per second. Use to reduce the number of points and compute the number of datapoints to return.
47
- features?: AudioFeaturesOptions
48
- featuresExtratorUrl?: string
49
- logger?: ConsoleLike
50
- decodingOptions?: DecodingConfig
51
- }
52
-
53
- // Define base options interface with common properties
54
- interface BaseExtractOptions {
55
- fileUri?: string
56
- arrayBuffer?: ArrayBuffer
57
- /**
58
- * Duration of each analysis segment in milliseconds. Defaults to 100ms if not specified.
59
- */
60
- segmentDurationMs?: number
61
- features?: AudioFeaturesOptions
62
- decodingOptions?: DecodingConfig
63
- logger?: ConsoleLike
64
- }
65
-
66
- // Time-based range options
67
- interface TimeRangeOptions extends BaseExtractOptions {
68
- startTimeMs?: number
69
- endTimeMs?: number
70
- position?: never
71
- length?: never
72
- }
73
-
74
- // Byte-based range options
75
- interface ByteRangeOptions extends BaseExtractOptions {
76
- position?: number
77
- length?: number
78
- startTimeMs?: never
79
- endTimeMs?: never
80
- }
81
-
82
- /**
83
- * Options for extracting audio analysis.
84
- * - For time-based analysis, provide `startTimeMs` and `endTimeMs`.
85
- * - For byte-based analysis, provide `position` and `length`.
86
- * - Do not mix time and byte ranges.
87
- */
88
- export type ExtractAudioAnalysisProps = TimeRangeOptions | ByteRangeOptions
89
-
90
- /**
91
- * Extracts detailed audio analysis from the specified audio file or buffer.
92
- * Supports either time-based or byte-based ranges for flexibility in analysis.
93
- *
94
- * @param props - The options for extraction, including file URI, ranges, and decoding settings.
95
- * @returns A promise that resolves to the audio analysis data.
96
- * @throws {Error} If both time and byte ranges are provided or if required parameters are missing.
97
- */
98
- export async function extractAudioAnalysis(
99
- props: ExtractAudioAnalysisProps
100
- ): Promise<AudioAnalysis> {
101
- const {
102
- fileUri,
103
- arrayBuffer,
104
- decodingOptions,
105
- logger,
106
- segmentDurationMs = 100,
107
- features,
108
- } = props
109
-
110
- if (isWeb) {
111
- try {
112
- // Create AudioContext here
113
- const audioContext = new (window.AudioContext ||
114
- (window as any).webkitAudioContext)({
115
- sampleRate: decodingOptions?.targetSampleRate ?? 16000,
116
- })
117
-
118
- try {
119
- const processedBuffer = await processAudioBuffer({
120
- arrayBuffer,
121
- fileUri,
122
- targetSampleRate:
123
- decodingOptions?.targetSampleRate ?? 16000,
124
- targetChannels: decodingOptions?.targetChannels ?? 1,
125
- normalizeAudio: decodingOptions?.normalizeAudio ?? false,
126
- startTimeMs:
127
- 'startTimeMs' in props ? props.startTimeMs : undefined,
128
- endTimeMs:
129
- 'endTimeMs' in props ? props.endTimeMs : undefined,
130
- position: 'position' in props ? props.position : undefined,
131
- length: 'length' in props ? props.length : undefined,
132
- audioContext, // Pass the context we created
133
- logger,
134
- })
135
-
136
- const channelData = processedBuffer.buffer.getChannelData(0)
137
-
138
- // Create and initialize the worker
139
- const blob = new Blob([InlineFeaturesExtractor], {
140
- type: 'application/javascript',
141
- })
142
- const workerUrl = URL.createObjectURL(blob)
143
- const worker = new Worker(workerUrl)
144
-
145
- return new Promise((resolve, reject) => {
146
- worker.onmessage = (event) => {
147
- if (event.data.error) {
148
- reject(new Error(event.data.error))
149
- return
150
- }
151
-
152
- const result: AudioAnalysis = event.data.result
153
- // Calculate CRC32 after worker completes if requested
154
- if (features?.crc32) {
155
- const samplesPerSegment = Math.floor(
156
- (processedBuffer.sampleRate *
157
- segmentDurationMs) /
158
- 1000
159
- )
160
-
161
- result.dataPoints = result.dataPoints.map(
162
- (point: DataPoint, index: number) => {
163
- const startSample =
164
- index * samplesPerSegment
165
- const segmentData = channelData.slice(
166
- startSample,
167
- startSample + samplesPerSegment
168
- )
169
-
170
- return {
171
- ...point,
172
- features: {
173
- ...point.features,
174
- crc32: calculateCRC32ForDataPoint(
175
- segmentData
176
- ),
177
- },
178
- }
179
- }
180
- )
181
- }
182
-
183
- URL.revokeObjectURL(workerUrl)
184
- worker.terminate()
185
- resolve(result)
186
- }
187
-
188
- worker.onerror = (error) => {
189
- URL.revokeObjectURL(workerUrl)
190
- worker.terminate()
191
- reject(error)
192
- }
193
-
194
- worker.postMessage({
195
- channelData,
196
- sampleRate: processedBuffer.sampleRate,
197
- segmentDurationMs,
198
- bitDepth: decodingOptions?.targetBitDepth ?? 32,
199
- numberOfChannels: processedBuffer.channels,
200
- // enableLogging: !!logger,
201
- features,
202
- })
203
- })
204
- } finally {
205
- await audioContext.close()
206
- }
207
- } catch (error) {
208
- logger?.error('Failed to process audio:', error)
209
- throw error
210
- }
211
- } else {
212
- return await ExpoAudioStreamModule.extractAudioAnalysis(props)
213
- }
214
- }
215
-
216
- /**
217
- * Analyzes WAV files without decoding, preserving original PCM values.
218
- * Use this function when you need to ensure the analysis matches other software by avoiding any transformations.
219
- *
220
- * @param props - The options for WAV analysis, including file URI and range.
221
- * @returns A promise that resolves to the audio analysis data.
222
- */
223
- export const extractRawWavAnalysis = async ({
224
- fileUri,
225
- segmentDurationMs = 100, // Default to 100ms
226
- arrayBuffer,
227
- bitDepth,
228
- durationMs,
229
- sampleRate,
230
- numberOfChannels,
231
- features,
232
- logger,
233
- position = 0,
234
- length,
235
- }: ExtractWavAudioAnalysisProps): Promise<AudioAnalysis> => {
236
- if (isWeb) {
237
- if (!arrayBuffer && !fileUri) {
238
- throw new Error('Either arrayBuffer or fileUri must be provided')
239
- }
240
-
241
- if (!arrayBuffer) {
242
- logger?.log(`fetching fileUri`, fileUri)
243
- const response = await fetch(fileUri!)
244
-
245
- if (!response.ok) {
246
- throw new Error(
247
- `Failed to fetch fileUri: ${response.statusText}`
248
- )
249
- }
250
-
251
- arrayBuffer = await response.arrayBuffer()
252
- logger?.log(`fetched fileUri`, arrayBuffer.byteLength, arrayBuffer)
253
- }
254
-
255
- // Create a new copy of the ArrayBuffer to avoid detachment issues
256
- const bufferCopy = arrayBuffer.slice(0)
257
- logger?.log(
258
- `extractAudioAnalysis bitDepth=${bitDepth} len=${bufferCopy.byteLength}`,
259
- bufferCopy.slice(0, 100)
260
- )
261
-
262
- let actualBitDepth = bitDepth
263
- if (!actualBitDepth) {
264
- logger?.log(
265
- `extractAudioAnalysis bitDepth not provided -- getting wav file info`
266
- )
267
- const fileInfo = await getWavFileInfo(bufferCopy)
268
- actualBitDepth = fileInfo.bitDepth
269
- }
270
- logger?.log(`extractAudioAnalysis actualBitDepth=${actualBitDepth}`)
271
-
272
- const {
273
- pcmValues: channelData,
274
- min,
275
- max,
276
- } = await convertPCMToFloat32({
277
- buffer: arrayBuffer,
278
- bitDepth: actualBitDepth,
279
- })
280
- logger?.log(
281
- `extractAudioAnalysis convertPCMToFloat32 length=${channelData.length} range: [ ${min} :: ${max} ]`
282
- )
283
-
284
- // Apply position and length constraints to channelData if specified
285
- const startIndex = position
286
- const endIndex = length ? startIndex + length : channelData.length
287
- const constrainedChannelData = channelData.slice(startIndex, endIndex)
288
-
289
- return new Promise((resolve, reject) => {
290
- const blob = new Blob([InlineFeaturesExtractor], {
291
- type: 'application/javascript',
292
- })
293
- const url = URL.createObjectURL(blob)
294
- const worker = new Worker(url)
295
-
296
- worker.onmessage = (event) => {
297
- resolve(event.data.result)
298
- }
299
-
300
- worker.onerror = (error) => {
301
- reject(error)
302
- }
303
-
304
- worker.postMessage({
305
- command: 'process',
306
- channelData: constrainedChannelData,
307
- sampleRate,
308
- segmentDurationMs,
309
- logger,
310
- bitDepth,
311
- fullAudioDurationMs: durationMs,
312
- numberOfChannels,
313
- })
314
- })
315
- } else {
316
- if (!fileUri) {
317
- throw new Error('fileUri is required')
318
- }
319
- logger?.log(`extractAudioAnalysis`, {
320
- fileUri,
321
- segmentDurationMs,
322
- })
323
- const res = await ExpoAudioStreamModule.extractAudioAnalysis({
324
- fileUri,
325
- segmentDurationMs,
326
- features,
327
- position,
328
- length,
329
- })
330
- logger?.log(`extractAudioAnalysis`, res)
331
- return res
332
- }
333
- }
@@ -1,6 +0,0 @@
1
- import { ExtractAudioDataOptions } from '../ExpoAudioStream.types'
2
- import ExpoAudioStreamModule from '../ExpoAudioStreamModule'
3
-
4
- export const extractAudioData = async (props: ExtractAudioDataOptions) => {
5
- return await ExpoAudioStreamModule.extractAudioData(props)
6
- }
@@ -1,144 +0,0 @@
1
- /**
2
- * @experimental This feature is experimental and currently only available on Android.
3
- * The API may change in future versions. The web implementation is a placeholder.
4
- */
5
-
6
- import { ExpoAudioStreamModule } from '..'
7
- import { isWeb } from '../constants'
8
- import {
9
- ExtractMelSpectrogramOptions,
10
- MelSpectrogram,
11
- } from './AudioAnalysis.types'
12
- import {
13
- processAudioBuffer,
14
- ProcessedAudioData,
15
- } from '../utils/audioProcessing'
16
-
17
- /**
18
- * Extracts a mel spectrogram from audio data
19
- *
20
- * @experimental This feature is experimental and currently only available on Android.
21
- * The iOS implementation will throw an "UNSUPPORTED_PLATFORM" error.
22
- * The web implementation is a placeholder that returns dummy data.
23
- */
24
- export async function extractMelSpectrogram(
25
- options: ExtractMelSpectrogramOptions
26
- ): Promise<MelSpectrogram> {
27
- const {
28
- fileUri,
29
- arrayBuffer,
30
- windowSizeMs,
31
- hopLengthMs,
32
- nMels,
33
- fMin = 0,
34
- fMax,
35
- windowType = 'hann',
36
- normalize = false,
37
- logScale = true,
38
- decodingOptions,
39
- startTimeMs,
40
- endTimeMs,
41
- logger,
42
- } = options
43
-
44
- if (isWeb) {
45
- // Create audio context
46
- const audioContext = new (window.AudioContext ||
47
- (window as any).webkitAudioContext)()
48
-
49
- try {
50
- // Process audio data using the existing utility
51
- const processedAudio: ProcessedAudioData = await processAudioBuffer(
52
- {
53
- arrayBuffer,
54
- fileUri,
55
- targetSampleRate:
56
- decodingOptions?.targetSampleRate || 16000,
57
- targetChannels: decodingOptions?.targetChannels || 1,
58
- normalizeAudio: decodingOptions?.normalizeAudio ?? false,
59
- startTimeMs,
60
- endTimeMs,
61
- audioContext,
62
- logger: options.logger,
63
- }
64
- )
65
-
66
- // Calculate window and hop size in samples
67
- const sampleRate = processedAudio.sampleRate
68
- const windowSize = Math.floor((windowSizeMs * sampleRate) / 1000)
69
- const hopLength = Math.floor((hopLengthMs * sampleRate) / 1000)
70
- const maxFreq = fMax || sampleRate / 2
71
-
72
- // Extract the mel spectrogram from the processed audio
73
- const spectrogram = computeMelSpectrogram(
74
- processedAudio.channelData,
75
- sampleRate,
76
- nMels,
77
- windowSize,
78
- hopLength,
79
- fMin,
80
- maxFreq,
81
- windowType,
82
- normalize,
83
- logScale
84
- )
85
-
86
- const timeSteps = spectrogram.length
87
-
88
- return {
89
- spectrogram,
90
- sampleRate,
91
- nMels,
92
- timeSteps,
93
- durationMs: processedAudio.durationMs,
94
- }
95
- } catch (error) {
96
- logger?.error('Error extracting mel spectrogram:', error)
97
- throw error
98
- } finally {
99
- // Close the audio context
100
- await audioContext.close()
101
- }
102
- }
103
- return ExpoAudioStreamModule.extractMelSpectrogram(options)
104
- }
105
-
106
- /**
107
- * Computes a mel spectrogram from audio data
108
- *
109
- * @experimental This is a placeholder implementation that returns dummy data.
110
- * The actual implementation will be added in a future version.
111
- */
112
- function computeMelSpectrogram(
113
- audioData: Float32Array,
114
- sampleRate: number,
115
- nMels: number,
116
- windowSize: number,
117
- hopLength: number,
118
- fMin: number,
119
- fMax: number,
120
- windowType: 'hann' | 'hamming',
121
- normalize: boolean,
122
- logScale: boolean
123
- ): number[][] {
124
- // Placeholder for the actual implementation
125
- // This would include:
126
- // 1. Windowing the audio data using the specified window type
127
- // 2. Computing the STFT (Short-Time Fourier Transform)
128
- // 3. Converting to power spectrogram
129
- // 4. Applying mel filterbanks
130
- // 5. Taking the logarithm if logScale is true
131
- // 6. Normalizing if normalize is true
132
-
133
- // For now, return a dummy implementation
134
- const numFrames =
135
- Math.floor((audioData.length - windowSize) / hopLength) + 1
136
- const spectrogram: number[][] = []
137
-
138
- // Create dummy mel spectrogram data
139
- for (let i = 0; i < numFrames; i++) {
140
- spectrogram.push(Array(nMels).fill(0))
141
- }
142
-
143
- return spectrogram
144
- }
@@ -1,34 +0,0 @@
1
- import { PreviewOptions, AudioAnalysis } from './AudioAnalysis.types'
2
- import { extractAudioAnalysis } from './extractAudioAnalysis'
3
-
4
- /**
5
- * Generates a simplified preview of the audio waveform for quick visualization.
6
- * Ideal for UI rendering with a specified number of points.
7
- *
8
- * @param options - The options for the preview, including file URI and time range.
9
- * @returns A promise that resolves to the audio preview data.
10
- */
11
- export async function extractPreview({
12
- fileUri,
13
- numberOfPoints = 100,
14
- startTimeMs = 0,
15
- endTimeMs = 30000, // First 30 seconds
16
- decodingOptions,
17
- logger,
18
- }: PreviewOptions): Promise<AudioAnalysis> {
19
- const durationMs = endTimeMs - startTimeMs
20
- const segmentDurationMs = Math.floor(durationMs / numberOfPoints)
21
-
22
- // Call extractAudioAnalysis with calculated parameters
23
- const analysis = await extractAudioAnalysis({
24
- fileUri,
25
- startTimeMs,
26
- endTimeMs,
27
- logger,
28
- segmentDurationMs,
29
- decodingOptions,
30
- })
31
-
32
- // Transform the result into AudioPreview format
33
- return analysis
34
- }
@@ -1,22 +0,0 @@
1
- import ExpoAudioStreamModule from '../ExpoAudioStreamModule'
2
-
3
- export interface ExtractWaveformProps {
4
- fileUri: string
5
- numberOfSamples: number
6
- offset?: number
7
- length?: number
8
- }
9
- export const extractWaveform = async ({
10
- fileUri,
11
- numberOfSamples,
12
- offset = 0,
13
- length,
14
- }: ExtractWaveformProps): Promise<unknown> => {
15
- const res = await ExpoAudioStreamModule.extractAudioAnalysis({
16
- fileUri,
17
- numberOfSamples,
18
- offset,
19
- length,
20
- })
21
- return res
22
- }
@@ -1,54 +0,0 @@
1
- // packages/expo-audio-stream/src/AudioRecorder.provider.tsx
2
- import React, { createContext, useContext } from 'react'
3
-
4
- import { UseAudioRecorderState } from './ExpoAudioStream.types'
5
- import { UseAudioRecorderProps, useAudioRecorder } from './useAudioRecorder'
6
-
7
- const initContext: UseAudioRecorderState = {
8
- isRecording: false,
9
- isPaused: false,
10
- durationMs: 0,
11
- size: 0,
12
- compression: undefined,
13
- startRecording: async () => {
14
- throw new Error('AudioRecorderProvider not found')
15
- },
16
- stopRecording: async () => {
17
- throw new Error('AudioRecorderProvider not found')
18
- },
19
- pauseRecording: async () => {
20
- throw new Error('AudioRecorderProvider not found')
21
- },
22
- resumeRecording: async () => {
23
- throw new Error('AudioRecorderProvider not found')
24
- },
25
- }
26
-
27
- const AudioRecorderContext = createContext<UseAudioRecorderState>(initContext)
28
-
29
- interface AudioRecorderProviderProps {
30
- children: React.ReactNode
31
- config?: UseAudioRecorderProps
32
- }
33
-
34
- export const AudioRecorderProvider: React.FC<AudioRecorderProviderProps> = ({
35
- children,
36
- config = {},
37
- }) => {
38
- const audioRecorder = useAudioRecorder(config)
39
- return (
40
- <AudioRecorderContext.Provider value={audioRecorder}>
41
- {children}
42
- </AudioRecorderContext.Provider>
43
- )
44
- }
45
-
46
- export const useSharedAudioRecorder = () => {
47
- const context = useContext(AudioRecorderContext)
48
- if (!context) {
49
- throw new Error(
50
- 'useSharedAudioRecorder must be used within an AudioRecorderProvider'
51
- )
52
- }
53
- return context
54
- }
@@ -1,6 +0,0 @@
1
- // src/ExpoAudioStreamModule.ts
2
- import { requireNativeModule } from 'expo-modules-core'
3
-
4
- // It loads the native module object from the JSI or falls back to
5
- // the bridge module (from NativeModulesProxy) if the remote debugger is on.
6
- export default requireNativeModule('ExpoAudioStream')