@@ -9,15 +9,33 @@ function genSine(freq: number, samples: number, sampleRate: number) {
99 return buf
1010}
1111
12+ function genBeat ( bpm : number , seconds : number , sampleRate : number ) {
13+ const total = Math . round ( seconds * sampleRate )
14+ const buf = Buffer . alloc ( total * 2 )
15+ const period = Math . round ( ( 60 / bpm ) * sampleRate )
16+ for ( let i = 0 ; i < total ; i ++ ) {
17+ const v = i % period === 0 ? 1 : 0
18+ buf . writeInt16LE ( Math . round ( v * 32767 ) , i * 2 )
19+ }
20+ return buf
21+ }
22+
1223describe ( 'processAudio' , ( ) => {
1324 beforeEach ( ( ) => {
1425 resetAudioState ( )
1526 } )
16- test ( 'detects frequency' , ( ) => {
17- const buf = genSine ( 440 , 1024 , 44100 )
18- processAudio ( buf )
19- expect ( audioState . freq ) . toBeGreaterThan ( 430 )
20- expect ( audioState . freq ) . toBeLessThan ( 450 )
21- } )
27+ test ( 'detects frequency' , ( ) => {
28+ const buf = genSine ( 440 , 1234 , 44100 )
29+ processAudio ( buf )
30+ expect ( audioState . freq ) . toBeGreaterThan ( 430 )
31+ expect ( audioState . freq ) . toBeLessThan ( 450 )
32+ } )
33+
34+ test ( 'detects bpm' , ( ) => {
35+ const buf = genBeat ( 120 , 4 , 44100 )
36+ processAudio ( buf )
37+ expect ( audioState . bpm ) . toBeGreaterThan ( 110 )
38+ expect ( audioState . bpm ) . toBeLessThan ( 130 )
39+ } )
2240
2341} )
0 commit comments