@@ -8,9 +8,9 @@ class RecordingProcessor extends AudioWorkletProcessor {
8
8
constructor ( options ) {
9
9
super ( ) ;
10
10
11
- this . sampleRate = 0 ;
11
+ this . sampleRate = 16000 ;
12
12
this . maxRecordingFrames = 0 ;
13
- this . numberOfChannels = 0 ;
13
+ this . numberOfChannels = 1 ;
14
14
15
15
if ( options && options . processorOptions ) {
16
16
const {
@@ -24,11 +24,12 @@ class RecordingProcessor extends AudioWorkletProcessor {
24
24
this . numberOfChannels = numberOfChannels ;
25
25
}
26
26
27
- this . _recordingBuffer = new Array ( this . numberOfChannels )
28
- . fill ( new Float32Array ( this . maxRecordingFrames ) ) ;
27
+ // Initialize _recordingBuffer as a Uint8Array
28
+ this . _recordingBuffer = new Uint8Array ( this . maxRecordingFrames * 2 ) ;
29
29
30
30
this . recordedFrames = 0 ;
31
31
this . isRecording = false ;
32
+ this . lastSentFrame = 0 ;
32
33
33
34
// We will use a timer to gate our messages; this one will publish at 30hz
34
35
this . framesSinceLastPublish = 0 ;
@@ -55,26 +56,24 @@ class RecordingProcessor extends AudioWorkletProcessor {
55
56
}
56
57
57
58
process ( inputs , outputs , params ) {
58
- for ( let input = 0 ; input < 1 ; input ++ ) {
59
- for ( let channel = 0 ; channel < this . numberOfChannels ; channel ++ ) {
60
- for ( let sample = 0 ; sample < inputs [ input ] [ channel ] . length ; sample ++ ) {
61
- const currentSample = inputs [ input ] [ channel ] [ sample ] ;
62
-
63
- // Copy data to recording buffer.
64
- if ( this . isRecording ) {
65
- this . _recordingBuffer [ channel ] [ sample + this . recordedFrames ] =
66
- currentSample ;
67
- }
68
-
69
- // Pass data directly to output, unchanged.
70
- outputs [ input ] [ channel ] [ sample ] = currentSample ;
71
-
72
- // Sum values for visualizer
73
- this . sampleSum += Math . abs ( currentSample ) ; // CHANGED to absolute values [0, 1]
74
- }
59
+ // Assuming we are only interested in the first channel 0 // TODO: convert to mono properly
60
+ let inputBuffer = inputs [ 0 ] [ 0 ] ;
61
+ for ( let sample = 0 ; sample < inputBuffer . length ; ++ sample ) {
62
+ let currentSample = inputBuffer [ sample ] ;
63
+
64
+ if ( this . isRecording ) {
65
+ // Copy data to recording buffer
66
+ let signed16bits = Math . max ( - 32768 ,
67
+ Math . min ( 32767 , currentSample * 32768.0 ) ) ;
68
+ let index = ( sample + this . recordedFrames ) * 2 ;
69
+ this . _recordingBuffer [ index ] = signed16bits & 255 ; // low byte, little endian
70
+ this . _recordingBuffer [ index + 1 ] = ( signed16bits >> 8 ) & 255 ; // high
75
71
}
72
+
73
+ // Sum values for visualizer
74
+ this . sampleSum += Math . abs ( currentSample ) ; // CHANGED to absolute values [0, 1]
76
75
}
77
-
76
+
78
77
const shouldPublish = this . framesSinceLastPublish >= this . publishInterval ;
79
78
80
79
// Validate that recording hasn't reached its limit.
@@ -84,10 +83,16 @@ class RecordingProcessor extends AudioWorkletProcessor {
84
83
85
84
// Post a recording recording length update on the clock's schedule
86
85
if ( shouldPublish ) {
86
+ let bufferSlice = this . _recordingBuffer . slice (
87
+ this . lastSentFrame * 2 , this . recordedFrames * 2 ) ;
88
+
87
89
this . port . postMessage ( {
88
- message : 'UPDATE_RECORDING_LENGTH ' ,
90
+ message : 'UPDATE_RECORDING ' ,
89
91
recordingLength : this . recordedFrames ,
92
+ bufferSlice : bufferSlice ,
90
93
} ) ;
94
+
95
+ this . lastSentFrame = this . recordedFrames ;
91
96
}
92
97
} else {
93
98
// Let the rest of the app know the limit was reached.
0 commit comments