@idealyst/audio 1.2.48 → 1.2.49
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/package.json +1 -1
- package/src/recording/Recorder.native.ts +31 -45
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idealyst/audio",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.49",
|
|
4
4
|
"description": "Unified cross-platform audio for React and React Native - recording, playback, and PCM streaming",
|
|
5
5
|
"documentation": "https://github.com/IdealystIO/idealyst-framework/tree/main/packages/audio#readme",
|
|
6
6
|
"readme": "README.md",
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Native Recorder
|
|
3
3
|
*
|
|
4
|
-
* Uses react-native-audio-api for recording on React Native.
|
|
4
|
+
* Uses react-native-audio-api AudioRecorder for recording on React Native.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
|
-
|
|
8
|
+
AudioRecorder,
|
|
9
9
|
AudioManager,
|
|
10
10
|
} from 'react-native-audio-api';
|
|
11
11
|
import type {
|
|
@@ -26,7 +26,6 @@ import {
|
|
|
26
26
|
DEFAULT_AUDIO_LEVEL,
|
|
27
27
|
DEFAULT_RECORDER_STATUS,
|
|
28
28
|
DEFAULT_LEVEL_UPDATE_INTERVAL,
|
|
29
|
-
DEFAULT_BUFFER_SIZE,
|
|
30
29
|
} from '../constants';
|
|
31
30
|
import {
|
|
32
31
|
createAudioError,
|
|
@@ -38,8 +37,7 @@ import {
|
|
|
38
37
|
|
|
39
38
|
export class NativeRecorder implements IRecorder {
|
|
40
39
|
private audioContext: IAudioContext;
|
|
41
|
-
private
|
|
42
|
-
private scriptProcessorNode: any = null;
|
|
40
|
+
private audioRecorder: AudioRecorder | null = null;
|
|
43
41
|
|
|
44
42
|
private config: AudioConfig = DEFAULT_AUDIO_CONFIG;
|
|
45
43
|
private _status: RecorderStatus = { ...DEFAULT_RECORDER_STATUS };
|
|
@@ -101,40 +99,33 @@ export class NativeRecorder implements IRecorder {
|
|
|
101
99
|
this.updateState('starting');
|
|
102
100
|
this.config = mergeConfig(configOverride, DEFAULT_AUDIO_CONFIG);
|
|
103
101
|
|
|
104
|
-
// Ensure audio context is initialized
|
|
102
|
+
// Ensure audio context is initialized (for audio session management)
|
|
105
103
|
if (!this.audioContext.isInitialized) {
|
|
106
104
|
await this.audioContext.initialize();
|
|
107
105
|
}
|
|
108
106
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
107
|
+
// Create AudioRecorder from react-native-audio-api
|
|
108
|
+
this.audioRecorder = new AudioRecorder({
|
|
109
|
+
sampleRate: this.config.sampleRate,
|
|
110
|
+
numberOfChannels: this.config.channels,
|
|
111
|
+
});
|
|
113
112
|
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
this.mediaStreamNode = (ctx as any).createMediaStreamSource();
|
|
117
|
-
|
|
118
|
-
// Create script processor for capturing audio data
|
|
119
|
-
// Note: ScriptProcessorNode is deprecated but may be what's available in react-native-audio-api
|
|
120
|
-
const bufferSize = DEFAULT_BUFFER_SIZE;
|
|
121
|
-
this.scriptProcessorNode = (ctx as any).createScriptProcessor(
|
|
122
|
-
bufferSize,
|
|
123
|
-
this.config.channels,
|
|
124
|
-
this.config.channels
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
// Handle audio data
|
|
128
|
-
this.scriptProcessorNode.onaudioprocess = (event: any) => {
|
|
113
|
+
// Set up audio data callback
|
|
114
|
+
this.audioRecorder.onAudioReady((audioBuffer) => {
|
|
129
115
|
if (this._status.state !== 'recording') return;
|
|
130
116
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
117
|
+
// Get audio data from the buffer
|
|
118
|
+
const float32Samples = audioBuffer.getChannelData(0);
|
|
119
|
+
this.handleAudioData(new Float32Array(float32Samples));
|
|
120
|
+
});
|
|
134
121
|
|
|
135
|
-
//
|
|
136
|
-
this.
|
|
137
|
-
|
|
122
|
+
// Set up error callback
|
|
123
|
+
this.audioRecorder.onError((error) => {
|
|
124
|
+
this.handleError(createAudioError('RECORDING_ERROR', error.message || 'Recording error'));
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Start recording
|
|
128
|
+
this.audioRecorder.start();
|
|
138
129
|
|
|
139
130
|
// Start level metering
|
|
140
131
|
this.startLevelMetering();
|
|
@@ -165,9 +156,8 @@ export class NativeRecorder implements IRecorder {
|
|
|
165
156
|
return;
|
|
166
157
|
}
|
|
167
158
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
this.scriptProcessorNode.disconnect();
|
|
159
|
+
if (this.audioRecorder) {
|
|
160
|
+
this.audioRecorder.pause();
|
|
171
161
|
}
|
|
172
162
|
this.updateState('paused');
|
|
173
163
|
}
|
|
@@ -177,9 +167,8 @@ export class NativeRecorder implements IRecorder {
|
|
|
177
167
|
return;
|
|
178
168
|
}
|
|
179
169
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
this.scriptProcessorNode.connect(ctx.destination);
|
|
170
|
+
if (this.audioRecorder) {
|
|
171
|
+
this.audioRecorder.resume();
|
|
183
172
|
}
|
|
184
173
|
this.updateState('recording');
|
|
185
174
|
}
|
|
@@ -282,14 +271,11 @@ export class NativeRecorder implements IRecorder {
|
|
|
282
271
|
this.levelIntervalId = null;
|
|
283
272
|
}
|
|
284
273
|
|
|
285
|
-
if (this.
|
|
286
|
-
this.
|
|
287
|
-
this.
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
if (this.mediaStreamNode) {
|
|
291
|
-
this.mediaStreamNode.disconnect();
|
|
292
|
-
this.mediaStreamNode = null;
|
|
274
|
+
if (this.audioRecorder) {
|
|
275
|
+
this.audioRecorder.clearOnAudioReady();
|
|
276
|
+
this.audioRecorder.clearOnError();
|
|
277
|
+
this.audioRecorder.stop();
|
|
278
|
+
this.audioRecorder = null;
|
|
293
279
|
}
|
|
294
280
|
}
|
|
295
281
|
|