@siteed/expo-audio-stream 1.0.2 → 1.0.5

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 (139) hide show
  1. package/.size-limit.json +6 -0
  2. package/README.md +18 -176
  3. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +1 -0
  4. package/app.plugin.js +1 -1
  5. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +74 -0
  6. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  7. package/build/AudioAnalysis/AudioAnalysis.types.js +3 -0
  8. package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  9. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +20 -0
  10. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  11. package/build/AudioAnalysis/extractAudioAnalysis.js +88 -0
  12. package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  13. package/build/AudioAnalysis/extractWaveform.d.ts +8 -0
  14. package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  15. package/build/AudioAnalysis/extractWaveform.js +14 -0
  16. package/build/AudioAnalysis/extractWaveform.js.map +1 -0
  17. package/build/AudioRecorder.provider.d.ts +15 -2
  18. package/build/AudioRecorder.provider.d.ts.map +1 -1
  19. package/build/AudioRecorder.provider.js +21 -8
  20. package/build/AudioRecorder.provider.js.map +1 -1
  21. package/build/ExpoAudioStream.native.d.ts.map +1 -1
  22. package/build/ExpoAudioStream.native.js +2 -2
  23. package/build/ExpoAudioStream.native.js.map +1 -1
  24. package/build/ExpoAudioStream.types.d.ts +33 -89
  25. package/build/ExpoAudioStream.types.d.ts.map +1 -1
  26. package/build/ExpoAudioStream.types.js.map +1 -1
  27. package/build/ExpoAudioStream.web.d.ts +10 -9
  28. package/build/ExpoAudioStream.web.d.ts.map +1 -1
  29. package/build/ExpoAudioStream.web.js +44 -25
  30. package/build/ExpoAudioStream.web.js.map +1 -1
  31. package/build/ExpoAudioStreamModule.d.ts.map +1 -1
  32. package/build/ExpoAudioStreamModule.js +13 -8
  33. package/build/ExpoAudioStreamModule.js.map +1 -1
  34. package/build/{WebRecorder.d.ts → WebRecorder.web.d.ts} +13 -9
  35. package/build/WebRecorder.web.d.ts.map +1 -0
  36. package/build/{WebRecorder.js → WebRecorder.web.js} +118 -63
  37. package/build/WebRecorder.web.js.map +1 -0
  38. package/build/constants.d.ts +11 -0
  39. package/build/constants.d.ts.map +1 -0
  40. package/build/constants.js +14 -0
  41. package/build/constants.js.map +1 -0
  42. package/build/events.d.ts +18 -0
  43. package/build/events.d.ts.map +1 -0
  44. package/build/events.js +15 -0
  45. package/build/events.js.map +1 -0
  46. package/build/index.d.ts +9 -17
  47. package/build/index.d.ts.map +1 -1
  48. package/build/index.js +7 -113
  49. package/build/index.js.map +1 -1
  50. package/build/logger.d.ts +9 -0
  51. package/build/logger.d.ts.map +1 -0
  52. package/build/logger.js +13 -0
  53. package/build/logger.js.map +1 -0
  54. package/build/useAudioRecorder.d.ts +20 -0
  55. package/build/useAudioRecorder.d.ts.map +1 -0
  56. package/build/{useAudioRecording.js → useAudioRecorder.js} +90 -86
  57. package/build/useAudioRecorder.js.map +1 -0
  58. package/build/utils/BlobFix.d.ts +9 -0
  59. package/build/utils/BlobFix.d.ts.map +1 -0
  60. package/build/utils/BlobFix.js +494 -0
  61. package/build/utils/BlobFix.js.map +1 -0
  62. package/build/utils/concatenateBuffers.d.ts +8 -0
  63. package/build/utils/concatenateBuffers.d.ts.map +1 -0
  64. package/build/utils/concatenateBuffers.js +21 -0
  65. package/build/utils/concatenateBuffers.js.map +1 -0
  66. package/build/utils/convertPCMToFloat32.d.ts +11 -0
  67. package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
  68. package/build/utils/convertPCMToFloat32.js +54 -0
  69. package/build/utils/convertPCMToFloat32.js.map +1 -0
  70. package/build/utils/encodingToBitDepth.d.ts +5 -0
  71. package/build/utils/encodingToBitDepth.d.ts.map +1 -0
  72. package/build/utils/encodingToBitDepth.js +13 -0
  73. package/build/utils/encodingToBitDepth.js.map +1 -0
  74. package/build/utils/getWavFileInfo.d.ts +26 -0
  75. package/build/utils/getWavFileInfo.d.ts.map +1 -0
  76. package/build/utils/getWavFileInfo.js +92 -0
  77. package/build/utils/getWavFileInfo.js.map +1 -0
  78. package/build/utils/writeWavHeader.d.ts +9 -0
  79. package/build/utils/writeWavHeader.d.ts.map +1 -0
  80. package/build/utils/writeWavHeader.js +41 -0
  81. package/build/utils/writeWavHeader.js.map +1 -0
  82. package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
  83. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  84. package/build/workers/InlineFeaturesExtractor.web.js +303 -0
  85. package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
  86. package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
  87. package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  88. package/build/workers/inlineAudioWebWorker.web.js +243 -0
  89. package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
  90. package/expo-module.config.json +8 -17
  91. package/ios/AudioStreamManager.swift +40 -2
  92. package/ios/ExpoAudioStreamModule.swift +11 -0
  93. package/ios/RecordingResult.swift +1 -0
  94. package/package.json +72 -64
  95. package/plugin/build/index.d.ts +1 -1
  96. package/plugin/build/index.js +7 -7
  97. package/plugin/src/index.ts +47 -47
  98. package/plugin/tsconfig.json +8 -13
  99. package/publish.sh +0 -0
  100. package/src/AudioAnalysis/AudioAnalysis.types.ts +84 -0
  101. package/src/AudioAnalysis/extractAudioAnalysis.ts +147 -0
  102. package/src/AudioAnalysis/extractWaveform.ts +25 -0
  103. package/src/AudioRecorder.provider.tsx +59 -31
  104. package/src/ExpoAudioStream.native.ts +2 -2
  105. package/src/ExpoAudioStream.types.ts +58 -116
  106. package/src/ExpoAudioStream.web.ts +233 -205
  107. package/src/ExpoAudioStreamModule.ts +18 -12
  108. package/src/WebRecorder.web.ts +433 -0
  109. package/src/constants.ts +18 -0
  110. package/src/events.ts +39 -0
  111. package/src/index.ts +15 -176
  112. package/src/logger.ts +23 -0
  113. package/src/useAudioRecorder.tsx +420 -0
  114. package/src/utils/BlobFix.ts +550 -0
  115. package/src/utils/concatenateBuffers.ts +24 -0
  116. package/src/utils/convertPCMToFloat32.ts +75 -0
  117. package/src/utils/encodingToBitDepth.ts +18 -0
  118. package/src/utils/getWavFileInfo.ts +132 -0
  119. package/src/utils/writeWavHeader.ts +56 -0
  120. package/src/workers/InlineFeaturesExtractor.web.tsx +302 -0
  121. package/src/workers/inlineAudioWebWorker.web.tsx +242 -0
  122. package/tsconfig.json +12 -7
  123. package/build/WebRecorder.d.ts.map +0 -1
  124. package/build/WebRecorder.js.map +0 -1
  125. package/build/inlineAudioWebWorker.d.ts +0 -3
  126. package/build/inlineAudioWebWorker.d.ts.map +0 -1
  127. package/build/inlineAudioWebWorker.js +0 -340
  128. package/build/inlineAudioWebWorker.js.map +0 -1
  129. package/build/useAudioRecording.d.ts +0 -38
  130. package/build/useAudioRecording.d.ts.map +0 -1
  131. package/build/useAudioRecording.js.map +0 -1
  132. package/build/utils.d.ts +0 -31
  133. package/build/utils.d.ts.map +0 -1
  134. package/build/utils.js +0 -143
  135. package/build/utils.js.map +0 -1
  136. package/src/WebRecorder.ts +0 -364
  137. package/src/inlineAudioWebWorker.tsx +0 -340
  138. package/src/useAudioRecording.tsx +0 -410
  139. package/src/utils.ts +0 -189
@@ -1,53 +1,53 @@
1
1
  import {
2
- AndroidConfig,
3
- ConfigPlugin,
4
- withAndroidManifest,
5
- withInfoPlist,
6
- } from "@expo/config-plugins";
2
+ AndroidConfig,
3
+ ConfigPlugin,
4
+ withAndroidManifest,
5
+ withInfoPlist,
6
+ } from '@expo/config-plugins'
7
7
 
8
- const MICROPHONE_USAGE = "Allow $(PRODUCT_NAME) to access your microphone";
8
+ const MICROPHONE_USAGE = 'Allow $(PRODUCT_NAME) to access your microphone'
9
9
 
10
10
  const withRecordingPermission: ConfigPlugin<{
11
- microphonePermission: string;
11
+ microphonePermission: string
12
12
  }> = (config, existingPerms) => {
13
- if (!existingPerms) {
14
- console.warn("No previous permissions provided");
15
- }
16
- config = withInfoPlist(config, (config) => {
17
- config.modResults["NSMicrophoneUsageDescription"] = MICROPHONE_USAGE;
18
-
19
- // Add audio to UIBackgroundModes to allow background audio recording
20
- const existingBackgroundModes = config.modResults.UIBackgroundModes || [];
21
- if (!existingBackgroundModes.includes("audio")) {
22
- existingBackgroundModes.push("audio");
13
+ if (!existingPerms) {
14
+ console.warn('No previous permissions provided')
23
15
  }
24
- config.modResults.UIBackgroundModes = existingBackgroundModes;
25
-
26
- return config;
27
- });
28
-
29
- config = withAndroidManifest(config, (config) => {
30
- const mainApplication = AndroidConfig.Manifest.getMainApplicationOrThrow(
31
- config.modResults,
32
- );
33
-
34
- AndroidConfig.Manifest.addMetaDataItemToMainApplication(
35
- mainApplication,
36
- "android.permission.RECORD_AUDIO",
37
- MICROPHONE_USAGE,
38
- );
39
-
40
- // Add FOREGROUND_SERVICE permission for handling background recording
41
- AndroidConfig.Manifest.addMetaDataItemToMainApplication(
42
- mainApplication,
43
- "android.permission.FOREGROUND_SERVICE",
44
- "This apps needs access to the foreground service to record audio in the background",
45
- );
46
-
47
- return config;
48
- });
49
-
50
- return config;
51
- };
52
-
53
- export default withRecordingPermission;
16
+ config = withInfoPlist(config, (config) => {
17
+ config.modResults['NSMicrophoneUsageDescription'] = MICROPHONE_USAGE
18
+
19
+ // Add audio to UIBackgroundModes to allow background audio recording
20
+ const existingBackgroundModes =
21
+ config.modResults.UIBackgroundModes || []
22
+ if (!existingBackgroundModes.includes('audio')) {
23
+ existingBackgroundModes.push('audio')
24
+ }
25
+ config.modResults.UIBackgroundModes = existingBackgroundModes
26
+
27
+ return config
28
+ })
29
+
30
+ config = withAndroidManifest(config, (config) => {
31
+ const mainApplication =
32
+ AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults)
33
+
34
+ AndroidConfig.Manifest.addMetaDataItemToMainApplication(
35
+ mainApplication,
36
+ 'android.permission.RECORD_AUDIO',
37
+ MICROPHONE_USAGE
38
+ )
39
+
40
+ // Add FOREGROUND_SERVICE permission for handling background recording
41
+ AndroidConfig.Manifest.addMetaDataItemToMainApplication(
42
+ mainApplication,
43
+ 'android.permission.FOREGROUND_SERVICE',
44
+ 'This apps needs access to the foreground service to record audio in the background'
45
+ )
46
+
47
+ return config
48
+ })
49
+
50
+ return config
51
+ }
52
+
53
+ export default withRecordingPermission
@@ -1,14 +1,9 @@
1
1
  {
2
- "extends": "expo-module-scripts/tsconfig.plugin",
3
- "compilerOptions": {
4
- "outDir": "build",
5
- "rootDir": "src"
6
- },
7
- "include": [
8
- "./src"
9
- ],
10
- "exclude": [
11
- "**/__mocks__/*",
12
- "**/__tests__/*"
13
- ]
14
- }
2
+ "extends": "expo-module-scripts/tsconfig.plugin",
3
+ "compilerOptions": {
4
+ "outDir": "build",
5
+ "rootDir": "src"
6
+ },
7
+ "include": ["./src"],
8
+ "exclude": ["**/__mocks__/*", "**/__tests__/*"]
9
+ }
package/publish.sh CHANGED
File without changes
@@ -0,0 +1,84 @@
1
+ // packages/expo-audio-stream/src/AudioAnalysis/AudioAnalysis.types.ts
2
+
3
+ /**
4
+ * Represents various audio features extracted from an audio signal.
5
+ */
6
+ export interface AudioFeatures {
7
+ energy: number // The infinite integral of the squared signal, representing the overall energy of the audio.
8
+ mfcc: number[] // Mel-frequency cepstral coefficients, describing the short-term power spectrum of a sound.
9
+ rms: number // Root mean square value, indicating the amplitude of the audio signal.
10
+ minAmplitude: number // Minimum amplitude value in the audio signal.
11
+ maxAmplitude: number // Maximum amplitude value in the audio signal.
12
+ zcr: number // Zero-crossing rate, indicating the rate at which the signal changes sign.
13
+ spectralCentroid: number // The center of mass of the spectrum, indicating the brightness of the sound.
14
+ spectralFlatness: number // Measure of the flatness of the spectrum, indicating how noise-like the signal is.
15
+ spectralRolloff: number // The frequency below which a specified percentage (usually 85%) of the total spectral energy lies.
16
+ spectralBandwidth: number // The width of the spectrum, indicating the range of frequencies present.
17
+ chromagram: number[] // Chromagram, representing the 12 different pitch classes of the audio.
18
+ tempo: number // Estimated tempo of the audio signal, measured in beats per minute (BPM).
19
+ hnr: number // Harmonics-to-noise ratio, indicating the proportion of harmonics to noise in the audio signal.
20
+ }
21
+
22
+ /**
23
+ * Options to specify which audio features to extract.
24
+ */
25
+ export interface AudioFeaturesOptions {
26
+ energy?: boolean
27
+ mfcc?: boolean
28
+ rms?: boolean
29
+ zcr?: boolean
30
+ spectralCentroid?: boolean
31
+ spectralFlatness?: boolean
32
+ spectralRolloff?: boolean
33
+ spectralBandwidth?: boolean
34
+ chromagram?: boolean
35
+ tempo?: boolean
36
+ hnr?: boolean
37
+ }
38
+
39
+ /**
40
+ * Represents a single data point in the audio analysis.
41
+ */
42
+ export interface DataPoint {
43
+ id: number
44
+ amplitude: number
45
+ activeSpeech?: boolean
46
+ dB?: number
47
+ silent?: boolean
48
+ features?: AudioFeatures
49
+ startTime?: number
50
+ endTime?: number
51
+ // start / end position in bytes
52
+ startPosition?: number
53
+ endPosition?: number
54
+ // number of audio samples for this point (samples size depends on bit depth)
55
+ samples?: number
56
+ // TODO: speaker detection
57
+ speaker?: number
58
+ }
59
+
60
+ export type AmplitudeAlgorithm = 'peak' | 'rms'
61
+
62
+ /**
63
+ * Represents the complete data from the audio analysis.
64
+ */
65
+ export interface AudioAnalysis {
66
+ pointsPerSecond: number // How many consolidated value per second
67
+ durationMs: number // Duration of the audio in milliseconds
68
+ bitDepth: number // Bit depth of the audio
69
+ samples: number // Size of the audio in bytes
70
+ numberOfChannels: number // Number of audio channels
71
+ sampleRate: number // Sample rate of the audio
72
+ dataPoints: DataPoint[] // Array of data points from the analysis.
73
+ amplitudeAlgorithm: AmplitudeAlgorithm // Algorithm used to calculate amplitude values.
74
+ amplitudeRange: {
75
+ min: number
76
+ max: number
77
+ }
78
+ // TODO: speaker detection
79
+ speakerChanges?: {
80
+ timestamp: number // Timestamp of the speaker change in milliseconds.
81
+ speaker: number // Speaker identifier.
82
+ }[]
83
+ }
84
+
@@ -0,0 +1,147 @@
1
+ // packages/expo-audio-stream/src/AudioAnalysis/extractAudioAnalysis.ts
2
+ import {
3
+ AmplitudeAlgorithm,
4
+ AudioAnalysis,
5
+ AudioFeaturesOptions,
6
+ } from './AudioAnalysis.types'
7
+ import ExpoAudioStreamModule from '../ExpoAudioStreamModule'
8
+ import { isWeb } from '../constants'
9
+ import { getLogger } from '../logger'
10
+ import { convertPCMToFloat32 } from '../utils/convertPCMToFloat32'
11
+ import { getWavFileInfo, WavFileInfo } from '../utils/getWavFileInfo'
12
+ import { InlineFeaturesExtractor } from '../workers/InlineFeaturesExtractor.web'
13
+
14
+ const logger = getLogger('extractAudioAnalysis')
15
+
16
+ export interface ExtractAudioAnalysisProps {
17
+ fileUri?: string // should provide either fileUri or arrayBuffer
18
+ wavMetadata?: WavFileInfo
19
+ arrayBuffer?: ArrayBuffer
20
+ bitDepth?: number
21
+ skipWavHeader?: boolean
22
+ durationMs?: number
23
+ sampleRate?: number
24
+ numberOfChannels?: number
25
+ algorithm?: AmplitudeAlgorithm
26
+ position?: number // Optional number of bytes to skip. Default is 0
27
+ length?: number // Optional number of bytes to read.
28
+ pointsPerSecond?: number // Optional number of points per second. Use to reduce the number of points and compute the number of datapoints to return.
29
+ features?: AudioFeaturesOptions
30
+ featuresExtratorUrl?: string
31
+ }
32
+
33
+ export const extractAudioAnalysis = async ({
34
+ fileUri,
35
+ pointsPerSecond = 20,
36
+ arrayBuffer,
37
+ bitDepth,
38
+ skipWavHeader = true,
39
+ durationMs,
40
+ sampleRate,
41
+ numberOfChannels,
42
+ algorithm = 'rms',
43
+ features,
44
+ featuresExtratorUrl,
45
+ }: ExtractAudioAnalysisProps): Promise<AudioAnalysis> => {
46
+ if (isWeb) {
47
+ if (!arrayBuffer && !fileUri) {
48
+ throw new Error('Either arrayBuffer or fileUri must be provided')
49
+ }
50
+
51
+ if (!arrayBuffer) {
52
+ logger.log(`fetching fileUri`, fileUri)
53
+ const response = await fetch(fileUri!)
54
+
55
+ if (!response.ok) {
56
+ throw new Error(
57
+ `Failed to fetch fileUri: ${response.statusText}`
58
+ )
59
+ }
60
+
61
+ arrayBuffer = await response.arrayBuffer()
62
+ logger.log(`fetched fileUri`, arrayBuffer.byteLength, arrayBuffer)
63
+ }
64
+
65
+ // Create a new copy of the ArrayBuffer to avoid detachment issues
66
+ const bufferCopy = arrayBuffer.slice(0)
67
+ logger.log(
68
+ `extractAudioAnalysis skipWavHeader=${skipWavHeader} bitDepth=${bitDepth} len=${bufferCopy.byteLength}`,
69
+ bufferCopy.slice(0, 100)
70
+ )
71
+
72
+ let actualBitDepth = bitDepth
73
+ if (!actualBitDepth) {
74
+ logger.log(
75
+ `extractAudioAnalysis bitDepth not provided -- getting wav file info`
76
+ )
77
+ const fileInfo = await getWavFileInfo(bufferCopy)
78
+ actualBitDepth = fileInfo.bitDepth
79
+ }
80
+ logger.log(`extractAudioAnalysis actualBitDepth=${actualBitDepth}`)
81
+
82
+ const {
83
+ pcmValues: channelData,
84
+ min,
85
+ max,
86
+ } = await convertPCMToFloat32({
87
+ buffer: arrayBuffer,
88
+ bitDepth: actualBitDepth,
89
+ skipWavHeader,
90
+ })
91
+ logger.log(
92
+ `extractAudioAnalysis skipWaveHeader=${skipWavHeader} convertPCMToFloat32 length=${channelData.length} range: [ ${min} :: ${max} ]`
93
+ )
94
+
95
+ return new Promise((resolve, reject) => {
96
+ let worker: Worker
97
+ if (featuresExtratorUrl) {
98
+ worker = new Worker(
99
+ new URL(featuresExtratorUrl, window.location.href)
100
+ )
101
+ } else {
102
+ const blob = new Blob([InlineFeaturesExtractor], {
103
+ type: 'application/javascript',
104
+ })
105
+ const url = URL.createObjectURL(blob)
106
+ worker = new Worker(url)
107
+ }
108
+
109
+ worker.onmessage = (event) => {
110
+ resolve(event.data.result)
111
+ }
112
+
113
+ worker.onerror = (error) => {
114
+ reject(error)
115
+ }
116
+
117
+ worker.postMessage({
118
+ command: 'process',
119
+ channelData,
120
+ sampleRate,
121
+ pointsPerSecond,
122
+ algorithm,
123
+ bitDepth,
124
+ fullAudioDurationMs: durationMs,
125
+ numberOfChannels,
126
+ })
127
+ })
128
+ } else {
129
+ if (!fileUri) {
130
+ throw new Error('fileUri is required')
131
+ }
132
+ logger.log(`extractAudioAnalysis`, {
133
+ fileUri,
134
+ pointsPerSecond,
135
+ algorithm,
136
+ })
137
+ const res = await ExpoAudioStreamModule.extractAudioAnalysis({
138
+ fileUri,
139
+ pointsPerSecond,
140
+ skipWavHeader,
141
+ algorithm,
142
+ features,
143
+ })
144
+ logger.log(`extractAudioAnalysis`, res)
145
+ return res
146
+ }
147
+ }
@@ -0,0 +1,25 @@
1
+ import ExpoAudioStreamModule from '../ExpoAudioStreamModule'
2
+ import { getLogger } from '../logger'
3
+
4
+ const logger = getLogger('extractWaveform')
5
+ export interface ExtractWaveformProps {
6
+ fileUri: string
7
+ numberOfSamples: number
8
+ offset?: number
9
+ length?: number
10
+ }
11
+ export const extractWaveform = async ({
12
+ fileUri,
13
+ numberOfSamples,
14
+ offset = 0,
15
+ length,
16
+ }: ExtractWaveformProps): Promise<unknown> => {
17
+ const res = await ExpoAudioStreamModule.extractAudioAnalysis({
18
+ fileUri,
19
+ numberOfSamples,
20
+ offset,
21
+ length,
22
+ })
23
+ logger.log(`extractWaveform`, res)
24
+ return res
25
+ }
@@ -1,42 +1,70 @@
1
- import React, { createContext, useContext } from "react";
1
+ // packages/expo-audio-stream/src/AudioRecorder.provider.tsx
2
+ import React, { createContext, useContext } from 'react'
2
3
 
4
+ import { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types'
3
5
  import {
4
- UseAudioRecorderProps,
5
- UseAudioRecorderState,
6
- useAudioRecorder,
7
- } from "./useAudioRecording";
6
+ AudioRecording,
7
+ RecordingConfig,
8
+ StartRecordingResult,
9
+ } from './ExpoAudioStream.types'
10
+ import { UseAudioRecorderProps, useAudioRecorder } from './useAudioRecorder'
8
11
 
9
- const AudioRecorderContext = createContext<UseAudioRecorderState>({
10
- isRecording: false,
11
- isPaused: false,
12
- durationMs: 0,
13
- size: 0,
14
- // other properties filled on useAudioRecorder
15
- } as UseAudioRecorderState);
12
+ export interface UseAudioRecorderState {
13
+ startRecording: (_: RecordingConfig) => Promise<StartRecordingResult>
14
+ stopRecording: () => Promise<AudioRecording | null>
15
+ pauseRecording: () => void
16
+ resumeRecording: () => void
17
+ isRecording: boolean
18
+ isPaused: boolean
19
+ durationMs: number // Duration of the recording
20
+ size: number // Size in bytes of the recorded audio
21
+ analysisData?: AudioAnalysis // Analysis data for the recording depending on enableProcessing flag
22
+ }
23
+
24
+ const initContext: UseAudioRecorderState = {
25
+ isRecording: false,
26
+ isPaused: false,
27
+ durationMs: 0,
28
+ size: 0,
29
+ startRecording: async () => {
30
+ throw new Error('AudioRecorderProvider not found')
31
+ },
32
+ stopRecording: async () => {
33
+ throw new Error('AudioRecorderProvider not found')
34
+ },
35
+ pauseRecording: () => {
36
+ throw new Error('AudioRecorderProvider not found')
37
+ },
38
+ resumeRecording: () => {
39
+ throw new Error('AudioRecorderProvider not found')
40
+ },
41
+ }
42
+
43
+ const AudioRecorderContext = createContext<UseAudioRecorderState>(initContext)
16
44
 
17
45
  interface AudioRecorderProviderProps {
18
- children: React.ReactNode;
19
- config?: UseAudioRecorderProps;
46
+ children: React.ReactNode
47
+ config?: UseAudioRecorderProps
20
48
  }
21
49
 
22
50
  export const AudioRecorderProvider: React.FC<AudioRecorderProviderProps> = ({
23
- children,
24
- config = {},
51
+ children,
52
+ config = {},
25
53
  }) => {
26
- const audioRecorder = useAudioRecorder(config);
27
- return (
28
- <AudioRecorderContext.Provider value={audioRecorder}>
29
- {children}
30
- </AudioRecorderContext.Provider>
31
- );
32
- };
54
+ const audioRecorder = useAudioRecorder(config)
55
+ return (
56
+ <AudioRecorderContext.Provider value={audioRecorder}>
57
+ {children}
58
+ </AudioRecorderContext.Provider>
59
+ )
60
+ }
33
61
 
34
62
  export const useSharedAudioRecorder = () => {
35
- const context = useContext(AudioRecorderContext);
36
- if (!context) {
37
- throw new Error(
38
- "useSharedAudioRecorder must be used within an AudioRecorderProvider",
39
- );
40
- }
41
- return context;
42
- };
63
+ const context = useContext(AudioRecorderContext)
64
+ if (!context) {
65
+ throw new Error(
66
+ 'useSharedAudioRecorder must be used within an AudioRecorderProvider'
67
+ )
68
+ }
69
+ return context
70
+ }
@@ -1,6 +1,6 @@
1
1
  // src/ExpoAudioStreamModule.ts
2
- import { requireNativeModule } from "expo-modules-core";
2
+ import { requireNativeModule } from 'expo-modules-core'
3
3
 
4
4
  // It loads the native module object from the JSI or falls back to
5
5
  // the bridge module (from NativeModulesProxy) if the remote debugger is on.
6
- export default requireNativeModule("ExpoAudioStream");
6
+ export default requireNativeModule('ExpoAudioStream')