@@ -16,7 +16,8 @@ class MockOfflineAudioContext {
16
16
const audioBuffer = {
17
17
duration : 5 , // Mock duration
18
18
sampleRate : this . sampleRate ,
19
- getChannelData : ( ) => new Float32Array ( 5000 ) , // Return a dummy Float32Array
19
+ length : 5000 ,
20
+ getChannelData : ( ) => new Float32Array ( 5000 ) . fill ( 0.5 ) , // Return a dummy Float32Array
20
21
} as any as AudioBuffer ;
21
22
return Promise . resolve ( audioBuffer ) ;
22
23
}
@@ -33,11 +34,59 @@ describe('AudioSource', () => {
33
34
} ) ;
34
35
35
36
it ( 'should decode an audio buffer correctly' , async ( ) => {
36
- const buffer = await audioSource . decode ( 2 , 44100 ) ;
37
+ const buffer = await audioSource . decode ( 2 , 44100 , true ) ;
37
38
expect ( buffer . duration ) . toBe ( 5 ) ; // Mock duration
38
39
expect ( buffer . sampleRate ) . toBe ( 44100 ) ;
39
40
expect ( audioSource . audioBuffer ) . toBe ( buffer ) ;
40
41
expect ( audioSource . duration . seconds ) . toBe ( 5 ) ; // Ensure duration is set
42
+
43
+ audioSource . audioBuffer = undefined ;
44
+ await audioSource . decode ( 2 , 44100 , false ) ;
45
+ expect ( audioSource . audioBuffer ) . toBe ( undefined ) ;
46
+ } ) ;
47
+
48
+ it ( 'should (fast) sample an audio buffer correctly' , async ( ) => {
49
+ const samples = await audioSource . fastsampler ( { length : 20 } ) ;
50
+ expect ( samples . length ) . toBe ( 20 ) ;
51
+
52
+ for ( const sample of samples ) {
53
+ expect ( sample ) . toBe ( 0.5 ) ;
54
+ }
55
+ } ) ;
56
+
57
+ it ( 'should (fast) sample an audio buffer correctly with start' , async ( ) => {
58
+ const samples = await audioSource . fastsampler ( {
59
+ length : 20 ,
60
+ start : 10 ,
61
+ } ) ;
62
+ expect ( samples . length ) . toBe ( 20 ) ;
63
+
64
+ for ( const sample of samples ) {
65
+ expect ( sample ) . toBe ( 0.5 ) ;
66
+ }
67
+ } ) ;
68
+
69
+ it ( 'should (fast) sample an audio buffer correctly with stop' , async ( ) => {
70
+ const samples = await audioSource . fastsampler ( {
71
+ length : 20 ,
72
+ stop : 1000 ,
73
+ start : 20 ,
74
+ } ) ;
75
+ expect ( samples . length ) . toBe ( 20 ) ;
76
+
77
+ for ( const sample of samples ) {
78
+ expect ( sample ) . toBe ( 0.5 ) ;
79
+ }
80
+ } ) ;
81
+
82
+ it ( 'should (fast) sample an audio buffer correctly with logarithmic scale' , async ( ) => {
83
+ const samples = await audioSource . fastsampler ( { logarithmic : true } ) ;
84
+ expect ( samples . length ) . toBe ( 60 ) ;
85
+
86
+ for ( const sample of samples ) {
87
+ expect ( sample ) . toBeGreaterThanOrEqual ( 0.5 ) ;
88
+ expect ( sample ) . toBeLessThanOrEqual ( 1 ) ;
89
+ }
41
90
} ) ;
42
91
43
92
it ( 'should create a thumbnail with correct DOM elements' , async ( ) => {
0 commit comments