@@ -10,154 +10,6 @@ import { Composition } from '../../composition';
1010import { MediaClip } from '../../clips' ;
1111import { Timestamp } from '../../models' ;
1212import { MediaTrack } from './media' ;
13- import { AudioSource } from '../../sources' ;
14- import { getSilenceArrayBuffer } from './media.utils' ;
15-
16- // Mocking the OfflineAudioContext class for silence detection
17- class MockSilenceAudioContext {
18- constructor (
19- public numberOfChannels : number ,
20- public length : number ,
21- public sampleRate : number ,
22- ) { }
23-
24- decodeAudioData ( _ : ArrayBuffer ) : Promise < AudioBuffer > {
25- const audioBuffer = {
26- duration : 5 , // Mock duration
27- sampleRate : 1000 ,
28- length : 5000 ,
29- getChannelData : ( ) => new Float32Array ( 5000 ) . fill ( 0.5 ) , // Return a dummy Float32Array
30- } as any as AudioBuffer ;
31- return Promise . resolve ( audioBuffer ) ;
32- }
33-
34- close ( ) {
35- return Promise . resolve ( ) ;
36- }
37- }
38-
39- vi . stubGlobal ( 'AudioContext' , MockSilenceAudioContext ) ; // Stub the global OfflineAudioContext
40-
41- describe ( 'Get silence array buffer' , ( ) => {
42- it ( 'should get silence array buffer' , async ( ) => {
43- const audioBuffer = {
44- duration : 5 , // Mock duration
45- sampleRate : 44100 ,
46- length : 5 * 44100 ,
47- getChannelData : ( ) => {
48- const totalLength = 5 * 44100 ;
49- return Float32Array . from ( { length : totalLength } , ( _ , i ) => {
50- if ( i < 2 * 44100 ) {
51- return 1 ;
52- } else if ( i >= 3 * 44100 ) {
53- return - 1 ;
54- }
55- return 0 ;
56- } ) ;
57- } ,
58- } as any as AudioBuffer ;
59- const silences = getSilenceArrayBuffer ( audioBuffer , 44100 , 1 , - 50 , 0 ) ;
60- expect ( silences ) . toEqual ( [
61- {
62- start : 0 ,
63- stop : 2 ,
64- } ,
65- {
66- start : 3 ,
67- stop : 5 ,
68- } ,
69- ] ) ;
70- } ) ;
71-
72- it ( 'no silence in getSilenceArrayBuffer' , ( ) => {
73- const audioBuffer = {
74- duration : 5 , // Mock duration
75- sampleRate : 44100 ,
76- length : 5 * 44100 ,
77- getChannelData : ( i : number ) => new Float32Array ( 5 * 44100 ) . fill ( 0 ) ,
78- } as any as AudioBuffer ;
79- const silences = getSilenceArrayBuffer ( audioBuffer , 1024 , 1 , - 50 , 0 ) ;
80- expect ( silences ) . toEqual ( [ ] ) ;
81- } ) ;
82-
83- it ( 'only silence in getSilenceArrayBuffer' , ( ) => {
84- const audioBuffer = {
85- duration : 5 , // Mock duration
86- sampleRate : 44100 ,
87- length : 5 * 44100 ,
88- getChannelData : ( ) => new Float32Array ( 5 * 44100 ) . fill ( 1 ) ,
89- } as any as AudioBuffer ;
90- const silences = getSilenceArrayBuffer ( audioBuffer , 1024 , 1 , - 50 , 0 ) ;
91- expect ( silences ) . toEqual ( [
92- {
93- start : 0 ,
94- stop : 5 ,
95- } ,
96- ] ) ;
97- } ) ;
98-
99- it ( 'should throw error if no sample rate' , ( ) => {
100- const audioBuffer = {
101- sampleRate : undefined ,
102- } as any as AudioBuffer ;
103- expect ( ( ) => getSilenceArrayBuffer ( audioBuffer , 1024 , 1 , - 50 , 0 ) ) . toThrow ( ) ;
104- } ) ;
105- } ) ;
106-
107- describe ( 'Find silences in a track' , ( ) => {
108- let comp : Composition ;
109- let track : MediaTrack < MediaClip > ;
110- let file : File ;
111- const updateMock = vi . fn ( ) ;
112-
113- beforeEach ( ( ) => {
114- // frame and seconds are the same
115- comp = new Composition ( ) ;
116- file = new File ( [ ] , 'test.mp3' ) ;
117- track = comp . shiftTrack ( new MediaTrack < MediaClip > ( ) ) ;
118- track . on ( 'update' , updateMock ) ;
119- } ) ;
120-
121- it ( 'empty track should have no silences' , async ( ) => {
122- const emptyTrack = new MediaTrack ( ) ;
123- const silences = await track . detectSilences ( ) ;
124- expect ( silences ) . toEqual ( [ ] ) ;
125- } ) ;
126-
127- it ( 'track with clip but no element should have no silences' , async ( ) => {
128- const clip = new MediaClip ( ) ;
129- clip . source = await AudioSource . from ( file ) ;
130- await track . add ( clip ) ;
131- const silences = await track . detectSilences ( ) ;
132- expect ( silences ) . toEqual ( [ ] ) ;
133- } ) ;
134-
135- it ( 'track with clip and element should find silences' , async ( ) => {
136- const clip = new MediaClip ( ) ;
137- clip . source = await AudioSource . from ( file ) ;
138- clip . element = new Audio ( ) ;
139- clip . duration . seconds = 5 ;
140- const clip2 = new MediaClip ( ) ;
141- clip2 . source = await AudioSource . from ( file ) ;
142- clip2 . element = new Audio ( ) ;
143- clip2 . duration . seconds = 5 ;
144- clip2 . offset . seconds = 5 ;
145- await track . add ( clip ) ;
146- await track . add ( clip2 ) ;
147-
148- const silences = await track . detectSilences ( ) ;
149- expect ( silences ) . toEqual ( [
150- {
151- start : 0 ,
152- stop : 5 ,
153- } ,
154- {
155- start : 5.001 ,
156- stop : 10.001000000000001 ,
157- } ,
158- ] ) ;
159- } ) ;
160- } ) ;
16113
16214describe ( 'The Media Track Object' , ( ) => {
16315 let comp : Composition ;
0 commit comments