@siteed/expo-audio-stream 0.4.5 → 0.5.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.
- package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +8 -3
- package/build/useAudioRecording.d.ts +2 -1
- package/build/useAudioRecording.d.ts.map +1 -1
- package/build/useAudioRecording.js +23 -15
- package/build/useAudioRecording.js.map +1 -1
- package/package.json +3 -2
- package/src/useAudioRecording.ts +32 -17
- package/yarn-error.log +7793 -0
|
@@ -269,6 +269,9 @@ class ExpoAudioStreamModule() : Module() {
|
|
|
269
269
|
|
|
270
270
|
private fun recordingProcess() {
|
|
271
271
|
FileOutputStream(audioFile, true).use { fos ->
|
|
272
|
+
// Buffer to accumulate data
|
|
273
|
+
val accumulatedAudioData = ByteArrayOutputStream()
|
|
274
|
+
|
|
272
275
|
// Write audio data directly to the file
|
|
273
276
|
val audioData = ByteArray(bufferSizeInBytes)
|
|
274
277
|
while (isRecording.get()) {
|
|
@@ -281,11 +284,13 @@ class ExpoAudioStreamModule() : Module() {
|
|
|
281
284
|
if (bytesRead > 0) {
|
|
282
285
|
fos.write(audioData, 0, bytesRead)
|
|
283
286
|
totalDataSize += bytesRead
|
|
287
|
+
accumulatedAudioData.write(audioData, 0, bytesRead)
|
|
284
288
|
|
|
285
289
|
// Emit audio data at defined intervals
|
|
286
290
|
if (SystemClock.elapsedRealtime() - lastEmitTime >= interval) {
|
|
287
|
-
emitAudioData(
|
|
291
|
+
emitAudioData(accumulatedAudioData.toByteArray(), accumulatedAudioData.size())
|
|
288
292
|
lastEmitTime = SystemClock.elapsedRealtime() // Reset the timer
|
|
293
|
+
accumulatedAudioData.reset() // Clear the accumulator
|
|
289
294
|
}
|
|
290
295
|
}
|
|
291
296
|
}
|
|
@@ -339,7 +344,7 @@ class ExpoAudioStreamModule() : Module() {
|
|
|
339
344
|
"fileUri" to audioFile?.toURI().toString(),
|
|
340
345
|
"lastEmittedSize" to from,
|
|
341
346
|
"encoded" to encodedBuffer,
|
|
342
|
-
"deltaSize" to
|
|
347
|
+
"deltaSize" to length,
|
|
343
348
|
"position" to positionInMs,
|
|
344
349
|
"mimeType" to mimeType,
|
|
345
350
|
"totalSize" to fileSize,
|
|
@@ -353,7 +358,7 @@ class ExpoAudioStreamModule() : Module() {
|
|
|
353
358
|
}
|
|
354
359
|
|
|
355
360
|
private fun encodeAudioData(rawData: ByteArray): String {
|
|
356
|
-
return Base64.encodeToString(rawData, Base64.
|
|
361
|
+
return Base64.encodeToString(rawData, Base64.NO_WRAP)
|
|
357
362
|
}
|
|
358
363
|
|
|
359
364
|
private fun saveAudioToFile(rawData: ByteArray): Boolean {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { AudioStreamResult, RecordingOptions } from "./ExpoAudioStream.types";
|
|
2
2
|
export interface AudioDataEvent {
|
|
3
|
-
|
|
3
|
+
data: string | Blob;
|
|
4
4
|
position: number;
|
|
5
|
+
size: number;
|
|
5
6
|
}
|
|
6
7
|
export interface UseAudioRecorderState {
|
|
7
8
|
startRecording: (_: RecordingOptions) => Promise<string | null>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAudioRecording.d.ts","sourceRoot":"","sources":["../src/useAudioRecording.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useAudioRecording.d.ts","sourceRoot":"","sources":["../src/useAudioRecording.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,iBAAiB,EAEjB,gBAAgB,EACjB,MAAM,yBAAyB,CAAC;AAGjC,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AACD,MAAM,WAAW,qBAAqB;IACpC,cAAc,EAAE,CAAC,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAChE,aAAa,EAAE,MAAM,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAAC;IACvD,cAAc,EAAE,MAAM,IAAI,CAAC;IAC3B,WAAW,EAAE,OAAO,CAAC;IACrB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wBAAgB,gBAAgB,CAAC,EAC/B,aAAa,EACb,KAAa,GACd,EAAE;IACD,aAAa,CAAC,EAAE,CAAC,CAAC,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,GAAG,qBAAqB,CA6LxB"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { decode as atob } from "base-64";
|
|
2
1
|
import { Platform } from "expo-modules-core";
|
|
3
2
|
import { useCallback, useEffect, useState } from "react";
|
|
4
3
|
import { addAudioEventListener } from ".";
|
|
@@ -21,6 +20,7 @@ export function useAudioRecorder({ onAudioStream, debug = false, }) {
|
|
|
21
20
|
if (debug) {
|
|
22
21
|
console.log(`[useAudioRecorder] Status:`, status);
|
|
23
22
|
}
|
|
23
|
+
// Extract matching file from filesystem
|
|
24
24
|
setDuration(status.duration);
|
|
25
25
|
setSize(status.size);
|
|
26
26
|
}
|
|
@@ -35,10 +35,6 @@ export function useAudioRecorder({ onAudioStream, debug = false, }) {
|
|
|
35
35
|
useEffect(() => {
|
|
36
36
|
if (debug) {
|
|
37
37
|
console.log(`[useAudioRecorder] Registering audio event listener`, onAudioStream);
|
|
38
|
-
onAudioStream?.({
|
|
39
|
-
buffer: new Blob(),
|
|
40
|
-
position: 0,
|
|
41
|
-
}).catch(console.error);
|
|
42
38
|
}
|
|
43
39
|
const subscribe = addAudioEventListener(async ({ fileUri, deltaSize, totalSize, lastEmittedSize, position, streamUuid, encoded, mimeType, buffer, }) => {
|
|
44
40
|
try {
|
|
@@ -47,6 +43,7 @@ export function useAudioRecorder({ onAudioStream, debug = false, }) {
|
|
|
47
43
|
fileUri,
|
|
48
44
|
deltaSize,
|
|
49
45
|
totalSize,
|
|
46
|
+
position,
|
|
50
47
|
mimeType,
|
|
51
48
|
lastEmittedSize,
|
|
52
49
|
streamUuid,
|
|
@@ -58,18 +55,27 @@ export function useAudioRecorder({ onAudioStream, debug = false, }) {
|
|
|
58
55
|
if (Platform.OS !== "web") {
|
|
59
56
|
// Read the audio file as a base64 string for comparison
|
|
60
57
|
try {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
for (let i = 0; i < binaryData.length; i++) {
|
|
65
|
-
content[i] = binaryData.charCodeAt(i);
|
|
58
|
+
if (!encoded) {
|
|
59
|
+
console.error("[useAudioRecorder] Encoded audio data is missing");
|
|
60
|
+
throw new Error("Encoded audio data is missing");
|
|
66
61
|
}
|
|
67
|
-
const
|
|
62
|
+
// const binaryData = atob(encoded);
|
|
63
|
+
// const bytes = new Uint8Array(binaryData.length);
|
|
64
|
+
// for (let i = 0; i < binaryData.length; i++) {
|
|
65
|
+
// bytes[i] = binaryData.charCodeAt(i) & 0xff; // Mask to 8 bits
|
|
66
|
+
// }
|
|
67
|
+
// const arrayBuffer = bytes.buffer;
|
|
68
|
+
// if (debug) {
|
|
69
|
+
// console.log(
|
|
70
|
+
// `[useAudioRecorder] Read audio file position=${position} deltaSize: ${deltaSize} vs encoded.length: ${encoded.length}`,
|
|
71
|
+
// );
|
|
72
|
+
// }
|
|
73
|
+
onAudioStream?.({ data: encoded, position, size: deltaSize });
|
|
68
74
|
// Below code is optional, used to compare encoded data to audio on file system
|
|
69
75
|
// Fetch the audio data from the fileUri
|
|
70
76
|
// const options = {
|
|
71
77
|
// encoding: FileSystem.EncodingType.Base64,
|
|
72
|
-
// position:
|
|
78
|
+
// position: lastEmittedSize,
|
|
73
79
|
// length: deltaSize,
|
|
74
80
|
// };
|
|
75
81
|
// const base64Content = await FileSystem.readAsStringAsync(fileUri, options);
|
|
@@ -80,7 +86,6 @@ export function useAudioRecorder({ onAudioStream, debug = false, }) {
|
|
|
80
86
|
// }
|
|
81
87
|
// const audioBlob = new Blob([content], { type: 'application/octet-stream' }); // Create a Blob from the byte array
|
|
82
88
|
// console.debug(`Read audio file (len: ${content.length}) vs ${deltaSize}`)
|
|
83
|
-
onAudioStream?.({ buffer: audioBlob, position });
|
|
84
89
|
}
|
|
85
90
|
catch (error) {
|
|
86
91
|
console.error("[useAudioRecorder] Error reading audio file:", error);
|
|
@@ -88,7 +93,7 @@ export function useAudioRecorder({ onAudioStream, debug = false, }) {
|
|
|
88
93
|
}
|
|
89
94
|
else if (buffer) {
|
|
90
95
|
// Coming from web
|
|
91
|
-
onAudioStream?.({ buffer, position });
|
|
96
|
+
onAudioStream?.({ data: buffer, position, size: deltaSize });
|
|
92
97
|
}
|
|
93
98
|
}
|
|
94
99
|
}
|
|
@@ -96,13 +101,16 @@ export function useAudioRecorder({ onAudioStream, debug = false, }) {
|
|
|
96
101
|
console.error("[useAudioRecorder] Error processing audio event:", error);
|
|
97
102
|
}
|
|
98
103
|
});
|
|
104
|
+
if (debug) {
|
|
105
|
+
console.log(`[useAudioRecorder] Subscribed to audio event listener`, subscribe);
|
|
106
|
+
}
|
|
99
107
|
return () => {
|
|
100
108
|
if (debug) {
|
|
101
109
|
console.log(`[useAudioRecorder] Removing audio event listener`);
|
|
102
110
|
}
|
|
103
111
|
subscribe.remove();
|
|
104
112
|
};
|
|
105
|
-
}, [
|
|
113
|
+
}, []);
|
|
106
114
|
const startRecording = useCallback(async (recordingOptions) => {
|
|
107
115
|
setIsRecording(true);
|
|
108
116
|
setIsPaused(false);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAudioRecording.js","sourceRoot":"","sources":["../src/useAudioRecording.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,IAAI,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OA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C,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO;QACL,cAAc;QACd,aAAa;QACb,cAAc;QACd,QAAQ;QACR,WAAW;QACX,QAAQ;QACR,IAAI;KACL,CAAC;AACJ,CAAC","sourcesContent":["import { decode as atob } from \"base-64\";\nimport { Platform } from \"expo-modules-core\";\nimport { useCallback, useEffect, useState } from \"react\";\n\nimport { addAudioEventListener } from \".\";\nimport {\n AudioStreamResult,\n AudioStreamStatus,\n RecordingOptions,\n} from \"./ExpoAudioStream.types\";\nimport ExpoAudioStreamModule from \"./ExpoAudioStreamModule\";\n\nexport interface AudioDataEvent {\n buffer: Blob;\n position: number;\n}\nexport interface UseAudioRecorderState {\n startRecording: (_: RecordingOptions) => Promise<string | null>;\n stopRecording: () => Promise<AudioStreamResult | null>;\n pauseRecording: () => void;\n isRecording: boolean;\n isPaused: boolean;\n duration: number; // Duration of the recording\n size: number; // Size in bytes of the recorded audio\n}\n\nexport function useAudioRecorder({\n onAudioStream,\n debug = false,\n}: {\n onAudioStream?: (_: AudioDataEvent) => Promise<void>;\n debug?: boolean;\n}): UseAudioRecorderState {\n const [isRecording, setIsRecording] = useState(false);\n const [isPaused, setIsPaused] = useState(false);\n const [duration, setDuration] = useState(0);\n const [size, setSize] = useState(0);\n\n useEffect(() => {\n if (debug) {\n console.log(\n `[useAudioRecorder] isRecording: ${isRecording}, isPaused: ${isPaused}`,\n );\n }\n if (isRecording || isPaused) {\n const interval = setInterval(() => {\n try {\n if (debug) console.log(`[useAudioRecorder] Getting status`);\n const status: AudioStreamStatus = ExpoAudioStreamModule.status();\n if (debug) {\n console.log(`[useAudioRecorder] Status:`, status);\n }\n setDuration(status.duration);\n setSize(status.size);\n } catch (error) {\n console.error(`[useAudioRecorder] Error getting status:`, error);\n }\n }, 1000);\n return () => clearInterval(interval);\n }\n\n return () => null;\n }, [isRecording, isPaused, debug]);\n\n useEffect(() => {\n if (debug) {\n console.log(\n `[useAudioRecorder] Registering audio event listener`,\n onAudioStream,\n );\n onAudioStream?.({\n buffer: new Blob(),\n position: 0,\n }).catch(console.error);\n }\n const subscribe = addAudioEventListener(\n async ({\n fileUri,\n deltaSize,\n totalSize,\n lastEmittedSize,\n position,\n streamUuid,\n encoded,\n mimeType,\n buffer,\n }) => {\n try {\n if (debug) {\n console.log(`[useAudioRecorder] Received audio event:`, {\n fileUri,\n deltaSize,\n totalSize,\n mimeType,\n lastEmittedSize,\n streamUuid,\n encodedLength: encoded?.length,\n });\n }\n if (deltaSize > 0) {\n // Coming from native ( ios / android ) otherwise buffer is set\n if (Platform.OS !== \"web\") {\n // Read the audio file as a base64 string for comparison\n try {\n // convert encoded string to binary data\n const binaryData = atob(encoded);\n const content = new Uint8Array(binaryData.length);\n for (let i = 0; i < binaryData.length; i++) {\n content[i] = binaryData.charCodeAt(i);\n }\n const audioBlob = new Blob([content], { type: mimeType });\n\n // Below code is optional, used to compare encoded data to audio on file system\n // Fetch the audio data from the fileUri\n // const options = {\n // encoding: FileSystem.EncodingType.Base64,\n // position: from,\n // length: deltaSize,\n // };\n // const base64Content = await FileSystem.readAsStringAsync(fileUri, options);\n // const binaryData = atob(base64Content);\n // const content = new Uint8Array(binaryData.length);\n // for (let i = 0; i < binaryData.length; i++) {\n // content[i] = binaryData.charCodeAt(i);\n // }\n // const audioBlob = new Blob([content], { type: 'application/octet-stream' }); // Create a Blob from the byte array\n // console.debug(`Read audio file (len: ${content.length}) vs ${deltaSize}`)\n\n onAudioStream?.({ buffer: audioBlob, position });\n } catch (error) {\n console.error(\n \"[useAudioRecorder] Error reading audio file:\",\n error,\n );\n }\n } else if (buffer) {\n // Coming from web\n onAudioStream?.({ buffer, position });\n }\n }\n } catch (error) {\n console.error(\n \"[useAudioRecorder] Error processing audio event:\",\n error,\n );\n }\n },\n );\n return () => {\n if (debug) {\n console.log(`[useAudioRecorder] Removing audio event listener`);\n }\n subscribe.remove();\n };\n }, [isRecording, onAudioStream, debug]);\n\n const startRecording = useCallback(\n async (recordingOptions: RecordingOptions) => {\n setIsRecording(true);\n setIsPaused(false);\n setSize(0);\n setDuration(0);\n try {\n if (debug) {\n console.log(`[useAudioRecorder] start recoding`, recordingOptions);\n }\n\n const fileUrl =\n await ExpoAudioStreamModule.startRecording(recordingOptions);\n\n return fileUrl;\n } catch (error) {\n console.error(\"[useAudioRecorder] Error starting recording:\", error);\n setIsRecording(false);\n }\n },\n [debug],\n );\n\n const stopRecording = useCallback(async () => {\n setIsRecording(false);\n setIsPaused(false);\n const result: AudioStreamResult =\n await ExpoAudioStreamModule.stopRecording();\n return result;\n }, []);\n\n const pauseRecording = useCallback(async () => {\n try {\n await ExpoAudioStreamModule.stopRecording();\n setIsPaused(true);\n setIsRecording(false);\n } catch (error) {\n console.error(\"[useAudioRecorder] Error pausing recording:\", error);\n }\n }, [debug]);\n\n return {\n startRecording,\n stopRecording,\n pauseRecording,\n isPaused,\n isRecording,\n duration,\n size,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useAudioRecording.js","sourceRoot":"","sources":["../src/useAudioRecording.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,GAAG,CAAC;AAM1C,OAAO,qBAAqB,MAAM,yBAAyB,CAAC;AAiB5D,MAAM,UAAU,gBAAgB,CAAC,EAC/B,aAAa,EACb,KAAK,GAAG,KAAK,GAId;IACC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IACtD,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAChD,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC5C,MAAM,CAAC,IAAI,EAAE,OAAO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AC,KAAK,CAAC,CACR,CAAC;IAEF,MAAM,aAAa,GAAG,WAAW,CAAC,KAAK,IAAI,EAAE;QAC3C,cAAc,CAAC,KAAK,CAAC,CAAC;QACtB,WAAW,CAAC,KAAK,CAAC,CAAC;QACnB,MAAM,MAAM,GACV,MAAM,qBAAqB,CAAC,aAAa,EAAE,CAAC;QAC9C,OAAO,MAAM,CAAC;IAChB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,cAAc,GAAG,WAAW,CAAC,KAAK,IAAI,EAAE;QAC5C,IAAI,CAAC;YACH,MAAM,qBAAqB,CAAC,aAAa,EAAE,CAAC;YAC5C,WAAW,CAAC,IAAI,CAAC,CAAC;YAClB,cAAc,CAAC,KAAK,CAAC,CAAC;QACxB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;QACtE,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO;QACL,cAAc;QACd,aAAa;QACb,cAAc;QACd,QAAQ;QACR,WAAW;QACX,QAAQ;QACR,IAAI;KACL,CAAC;AACJ,CAAC","sourcesContent":["import { Platform } from \"expo-modules-core\";\nimport { useCallback, useEffect, useState } from \"react\";\n\nimport { addAudioEventListener } from \".\";\nimport {\n AudioStreamResult,\n AudioStreamStatus,\n RecordingOptions,\n} from \"./ExpoAudioStream.types\";\nimport ExpoAudioStreamModule from \"./ExpoAudioStreamModule\";\n\nexport interface AudioDataEvent {\n data: string | Blob;\n position: number;\n size: number;\n}\nexport interface UseAudioRecorderState {\n startRecording: (_: RecordingOptions) => Promise<string | null>;\n stopRecording: () => Promise<AudioStreamResult | null>;\n pauseRecording: () => void;\n isRecording: boolean;\n isPaused: boolean;\n duration: number; // Duration of the recording\n size: number; // Size in bytes of the recorded audio\n}\n\nexport function useAudioRecorder({\n onAudioStream,\n debug = false,\n}: {\n onAudioStream?: (_: AudioDataEvent) => Promise<void>;\n debug?: boolean;\n}): UseAudioRecorderState {\n const [isRecording, setIsRecording] = useState(false);\n const [isPaused, setIsPaused] = useState(false);\n const [duration, setDuration] = useState(0);\n const [size, setSize] = useState(0);\n\n useEffect(() => {\n if (debug) {\n console.log(\n `[useAudioRecorder] isRecording: ${isRecording}, isPaused: ${isPaused}`,\n );\n }\n if (isRecording || isPaused) {\n const interval = setInterval(() => {\n try {\n if (debug) console.log(`[useAudioRecorder] Getting status`);\n const status: AudioStreamStatus = ExpoAudioStreamModule.status();\n if (debug) {\n console.log(`[useAudioRecorder] Status:`, status);\n }\n // Extract matching file from filesystem\n setDuration(status.duration);\n setSize(status.size);\n } catch (error) {\n console.error(`[useAudioRecorder] Error getting status:`, error);\n }\n }, 1000);\n return () => clearInterval(interval);\n }\n\n return () => null;\n }, [isRecording, isPaused, debug]);\n\n useEffect(() => {\n if (debug) {\n console.log(\n `[useAudioRecorder] Registering audio event listener`,\n onAudioStream,\n );\n }\n const subscribe = addAudioEventListener(\n async ({\n fileUri,\n deltaSize,\n totalSize,\n lastEmittedSize,\n position,\n streamUuid,\n encoded,\n mimeType,\n buffer,\n }) => {\n try {\n if (debug) {\n console.log(`[useAudioRecorder] Received audio event:`, {\n fileUri,\n deltaSize,\n totalSize,\n position,\n mimeType,\n lastEmittedSize,\n streamUuid,\n encodedLength: encoded?.length,\n });\n }\n if (deltaSize > 0) {\n // Coming from native ( ios / android ) otherwise buffer is set\n if (Platform.OS !== \"web\") {\n // Read the audio file as a base64 string for comparison\n try {\n if (!encoded) {\n console.error(\n \"[useAudioRecorder] Encoded audio data is missing\",\n );\n throw new Error(\"Encoded audio data is missing\");\n }\n // const binaryData = atob(encoded);\n // const bytes = new Uint8Array(binaryData.length);\n // for (let i = 0; i < binaryData.length; i++) {\n // bytes[i] = binaryData.charCodeAt(i) & 0xff; // Mask to 8 bits\n // }\n // const arrayBuffer = bytes.buffer;\n\n // if (debug) {\n // console.log(\n // `[useAudioRecorder] Read audio file position=${position} deltaSize: ${deltaSize} vs encoded.length: ${encoded.length}`,\n // );\n // }\n\n onAudioStream?.({ data: encoded, position, size: deltaSize });\n\n // Below code is optional, used to compare encoded data to audio on file system\n // Fetch the audio data from the fileUri\n // const options = {\n // encoding: FileSystem.EncodingType.Base64,\n // position: lastEmittedSize,\n // length: deltaSize,\n // };\n // const base64Content = await FileSystem.readAsStringAsync(fileUri, options);\n // const binaryData = atob(base64Content);\n // const content = new Uint8Array(binaryData.length);\n // for (let i = 0; i < binaryData.length; i++) {\n // content[i] = binaryData.charCodeAt(i);\n // }\n // const audioBlob = new Blob([content], { type: 'application/octet-stream' }); // Create a Blob from the byte array\n // console.debug(`Read audio file (len: ${content.length}) vs ${deltaSize}`)\n } catch (error) {\n console.error(\n \"[useAudioRecorder] Error reading audio file:\",\n error,\n );\n }\n } else if (buffer) {\n // Coming from web\n onAudioStream?.({ data: buffer, position, size: deltaSize });\n }\n }\n } catch (error) {\n console.error(\n \"[useAudioRecorder] Error processing audio event:\",\n error,\n );\n }\n },\n );\n if (debug) {\n console.log(\n `[useAudioRecorder] Subscribed to audio event listener`,\n subscribe,\n );\n }\n return () => {\n if (debug) {\n console.log(`[useAudioRecorder] Removing audio event listener`);\n }\n subscribe.remove();\n };\n }, []);\n\n const startRecording = useCallback(\n async (recordingOptions: RecordingOptions) => {\n setIsRecording(true);\n setIsPaused(false);\n setSize(0);\n setDuration(0);\n try {\n if (debug) {\n console.log(`[useAudioRecorder] start recoding`, recordingOptions);\n }\n\n const fileUrl =\n await ExpoAudioStreamModule.startRecording(recordingOptions);\n\n return fileUrl;\n } catch (error) {\n console.error(\"[useAudioRecorder] Error starting recording:\", error);\n setIsRecording(false);\n }\n },\n [debug],\n );\n\n const stopRecording = useCallback(async () => {\n setIsRecording(false);\n setIsPaused(false);\n const result: AudioStreamResult =\n await ExpoAudioStreamModule.stopRecording();\n return result;\n }, []);\n\n const pauseRecording = useCallback(async () => {\n try {\n await ExpoAudioStreamModule.stopRecording();\n setIsPaused(true);\n setIsRecording(false);\n } catch (error) {\n console.error(\"[useAudioRecorder] Error pausing recording:\", error);\n }\n }, [debug]);\n\n return {\n startRecording,\n stopRecording,\n pauseRecording,\n isPaused,\n isRecording,\n duration,\n size,\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@siteed/expo-audio-stream",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "stream audio crossplatform",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "build/index.js",
|
|
@@ -33,12 +33,13 @@
|
|
|
33
33
|
"open:android": "open -a \"Android Studio\" example/android"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"
|
|
36
|
+
"react-native-quick-base64": "^2.1.2"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@expo/config-plugins": "^7.9.1",
|
|
40
40
|
"@release-it/conventional-changelog": "^8.0.1",
|
|
41
41
|
"@types/debug": "^4.1.12",
|
|
42
|
+
"@types/node": "^20.12.7",
|
|
42
43
|
"@types/react": "^18.0.25",
|
|
43
44
|
"@typescript-eslint/eslint-plugin": "^7.7.0",
|
|
44
45
|
"@typescript-eslint/parser": "^7.7.0",
|
package/src/useAudioRecording.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { decode as atob } from "base-64";
|
|
2
1
|
import { Platform } from "expo-modules-core";
|
|
3
2
|
import { useCallback, useEffect, useState } from "react";
|
|
4
3
|
|
|
@@ -11,8 +10,9 @@ import {
|
|
|
11
10
|
import ExpoAudioStreamModule from "./ExpoAudioStreamModule";
|
|
12
11
|
|
|
13
12
|
export interface AudioDataEvent {
|
|
14
|
-
|
|
13
|
+
data: string | Blob;
|
|
15
14
|
position: number;
|
|
15
|
+
size: number;
|
|
16
16
|
}
|
|
17
17
|
export interface UseAudioRecorderState {
|
|
18
18
|
startRecording: (_: RecordingOptions) => Promise<string | null>;
|
|
@@ -50,6 +50,7 @@ export function useAudioRecorder({
|
|
|
50
50
|
if (debug) {
|
|
51
51
|
console.log(`[useAudioRecorder] Status:`, status);
|
|
52
52
|
}
|
|
53
|
+
// Extract matching file from filesystem
|
|
53
54
|
setDuration(status.duration);
|
|
54
55
|
setSize(status.size);
|
|
55
56
|
} catch (error) {
|
|
@@ -68,10 +69,6 @@ export function useAudioRecorder({
|
|
|
68
69
|
`[useAudioRecorder] Registering audio event listener`,
|
|
69
70
|
onAudioStream,
|
|
70
71
|
);
|
|
71
|
-
onAudioStream?.({
|
|
72
|
-
buffer: new Blob(),
|
|
73
|
-
position: 0,
|
|
74
|
-
}).catch(console.error);
|
|
75
72
|
}
|
|
76
73
|
const subscribe = addAudioEventListener(
|
|
77
74
|
async ({
|
|
@@ -91,6 +88,7 @@ export function useAudioRecorder({
|
|
|
91
88
|
fileUri,
|
|
92
89
|
deltaSize,
|
|
93
90
|
totalSize,
|
|
91
|
+
position,
|
|
94
92
|
mimeType,
|
|
95
93
|
lastEmittedSize,
|
|
96
94
|
streamUuid,
|
|
@@ -102,19 +100,32 @@ export function useAudioRecorder({
|
|
|
102
100
|
if (Platform.OS !== "web") {
|
|
103
101
|
// Read the audio file as a base64 string for comparison
|
|
104
102
|
try {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
103
|
+
if (!encoded) {
|
|
104
|
+
console.error(
|
|
105
|
+
"[useAudioRecorder] Encoded audio data is missing",
|
|
106
|
+
);
|
|
107
|
+
throw new Error("Encoded audio data is missing");
|
|
110
108
|
}
|
|
111
|
-
const
|
|
109
|
+
// const binaryData = atob(encoded);
|
|
110
|
+
// const bytes = new Uint8Array(binaryData.length);
|
|
111
|
+
// for (let i = 0; i < binaryData.length; i++) {
|
|
112
|
+
// bytes[i] = binaryData.charCodeAt(i) & 0xff; // Mask to 8 bits
|
|
113
|
+
// }
|
|
114
|
+
// const arrayBuffer = bytes.buffer;
|
|
115
|
+
|
|
116
|
+
// if (debug) {
|
|
117
|
+
// console.log(
|
|
118
|
+
// `[useAudioRecorder] Read audio file position=${position} deltaSize: ${deltaSize} vs encoded.length: ${encoded.length}`,
|
|
119
|
+
// );
|
|
120
|
+
// }
|
|
121
|
+
|
|
122
|
+
onAudioStream?.({ data: encoded, position, size: deltaSize });
|
|
112
123
|
|
|
113
124
|
// Below code is optional, used to compare encoded data to audio on file system
|
|
114
125
|
// Fetch the audio data from the fileUri
|
|
115
126
|
// const options = {
|
|
116
127
|
// encoding: FileSystem.EncodingType.Base64,
|
|
117
|
-
// position:
|
|
128
|
+
// position: lastEmittedSize,
|
|
118
129
|
// length: deltaSize,
|
|
119
130
|
// };
|
|
120
131
|
// const base64Content = await FileSystem.readAsStringAsync(fileUri, options);
|
|
@@ -125,8 +136,6 @@ export function useAudioRecorder({
|
|
|
125
136
|
// }
|
|
126
137
|
// const audioBlob = new Blob([content], { type: 'application/octet-stream' }); // Create a Blob from the byte array
|
|
127
138
|
// console.debug(`Read audio file (len: ${content.length}) vs ${deltaSize}`)
|
|
128
|
-
|
|
129
|
-
onAudioStream?.({ buffer: audioBlob, position });
|
|
130
139
|
} catch (error) {
|
|
131
140
|
console.error(
|
|
132
141
|
"[useAudioRecorder] Error reading audio file:",
|
|
@@ -135,7 +144,7 @@ export function useAudioRecorder({
|
|
|
135
144
|
}
|
|
136
145
|
} else if (buffer) {
|
|
137
146
|
// Coming from web
|
|
138
|
-
onAudioStream?.({ buffer, position });
|
|
147
|
+
onAudioStream?.({ data: buffer, position, size: deltaSize });
|
|
139
148
|
}
|
|
140
149
|
}
|
|
141
150
|
} catch (error) {
|
|
@@ -146,13 +155,19 @@ export function useAudioRecorder({
|
|
|
146
155
|
}
|
|
147
156
|
},
|
|
148
157
|
);
|
|
158
|
+
if (debug) {
|
|
159
|
+
console.log(
|
|
160
|
+
`[useAudioRecorder] Subscribed to audio event listener`,
|
|
161
|
+
subscribe,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
149
164
|
return () => {
|
|
150
165
|
if (debug) {
|
|
151
166
|
console.log(`[useAudioRecorder] Removing audio event listener`);
|
|
152
167
|
}
|
|
153
168
|
subscribe.remove();
|
|
154
169
|
};
|
|
155
|
-
}, [
|
|
170
|
+
}, []);
|
|
156
171
|
|
|
157
172
|
const startRecording = useCallback(
|
|
158
173
|
async (recordingOptions: RecordingOptions) => {
|