1
1
import 'get-float-time-domain-data' ;
2
2
import getUserMedia from 'get-user-media-promise' ;
3
3
import SharedAudioContext from './shared-audio-context.js' ;
4
- import { computeRMS } from './audio-util.js' ;
4
+ import { computeRMS , computeChunkedRMS } from './audio-util.js' ;
5
5
6
6
class AudioRecorder {
7
7
constructor ( ) {
8
8
this . audioContext = new SharedAudioContext ( ) ;
9
- this . bufferLength = 1024 ;
9
+ this . bufferLength = 8192 ;
10
10
11
11
this . userMediaStream = null ;
12
12
this . mediaStreamSource = null ;
@@ -51,7 +51,7 @@ class AudioRecorder {
51
51
this . userMediaStream = userMediaStream ;
52
52
this . mediaStreamSource = this . audioContext . createMediaStreamSource ( userMediaStream ) ;
53
53
this . sourceNode = this . audioContext . createGain ( ) ;
54
- this . scriptProcessorNode = this . audioContext . createScriptProcessor ( this . bufferLength , 2 , 2 ) ;
54
+ this . scriptProcessorNode = this . audioContext . createScriptProcessor ( this . bufferLength , 1 , 1 ) ;
55
55
56
56
this . scriptProcessorNode . onaudioprocess = processEvent => {
57
57
if ( this . recording && ! this . disposed ) {
@@ -68,9 +68,9 @@ class AudioRecorder {
68
68
69
69
const update = ( ) => {
70
70
if ( this . disposed ) return ;
71
- requestAnimationFrame ( update ) ;
72
71
this . analyserNode . getFloatTimeDomainData ( dataArray ) ;
73
72
onUpdate ( computeRMS ( dataArray ) ) ;
73
+ requestAnimationFrame ( update ) ;
74
74
} ;
75
75
76
76
requestAnimationFrame ( update ) ;
@@ -83,7 +83,16 @@ class AudioRecorder {
83
83
}
84
84
85
85
stop ( ) {
86
- const chunkLevels = this . buffers . map ( buffer => computeRMS ( buffer ) ) ;
86
+ const buffer = new Float32Array ( this . buffers . length * this . bufferLength ) ;
87
+
88
+ let offset = 0 ;
89
+ for ( let i = 0 ; i < this . buffers . length ; i ++ ) {
90
+ const bufferChunk = this . buffers [ i ] ;
91
+ buffer . set ( bufferChunk , offset ) ;
92
+ offset += bufferChunk . length ;
93
+ }
94
+
95
+ const chunkLevels = computeChunkedRMS ( buffer ) ;
87
96
const maxRMS = Math . max . apply ( null , chunkLevels ) ;
88
97
const threshold = maxRMS / 8 ;
89
98
@@ -105,15 +114,6 @@ class AudioRecorder {
105
114
trimEnd = 1 ;
106
115
}
107
116
108
- const buffer = new Float32Array ( this . buffers . length * this . bufferLength ) ;
109
-
110
- let offset = 0 ;
111
- for ( let i = 0 ; i < this . buffers . length ; i ++ ) {
112
- const bufferChunk = this . buffers [ i ] ;
113
- buffer . set ( bufferChunk , offset ) ;
114
- offset += bufferChunk . length ;
115
- }
116
-
117
117
return {
118
118
levels : chunkLevels ,
119
119
samples : buffer ,
0 commit comments