66 */
77
88import { describe , it , vi , beforeEach , expect } from 'vitest' ;
9- import { AudioSource } from './audio' ; // Import the AudioSource class
10- import { findSilences } from './audio.utils' ;
11- import { Timestamp } from '../models' ;
9+ import { AudioSource } from './audio' ;
1210
1311// Mocking the OfflineAudioContext class
1412class MockOfflineAudioContext {
@@ -27,30 +25,6 @@ class MockOfflineAudioContext {
2725
2826vi . stubGlobal ( 'OfflineAudioContext' , MockOfflineAudioContext ) ; // Stub the global OfflineAudioContext
2927
30- describe ( 'AudioUtils' , ( ) => {
31- it ( 'all silent' , ( ) => {
32- const silences = findSilences ( new Float32Array ( 100 ) . fill ( 0 ) , - 50 , 100 , 100 ) ;
33- expect ( silences ) . toEqual ( [ {
34- start : new Timestamp ( 0 ) ,
35- stop : new Timestamp ( 100 ) ,
36- } ] ) ;
37- } ) ;
38-
39- it ( 'no silences' , ( ) => {
40- const silences = findSilences ( new Float32Array ( 100 ) . fill ( 1 ) , - 50 , 100 , 100 ) ;
41- expect ( silences ) . toEqual ( [ ] ) ;
42- } ) ;
43-
44- it ( 'find silences correctly' , ( ) => {
45- const samples = Array . from ( { length : 500 } , ( _ , index ) => index > 300 ? ( index < 400 ? 0 : 1 ) : - 1 ) ;
46- const silences = findSilences ( new Float32Array ( samples ) , - 50 , 100 , 5000 ) ;
47- expect ( silences ) . toEqual ( [ {
48- start : new Timestamp ( 3010 ) ,
49- stop : new Timestamp ( 4000 ) ,
50- } ] ) ;
51- } ) ;
52- } ) ;
53-
5428describe ( 'AudioSource' , ( ) => {
5529 let audioSource : AudioSource ;
5630
@@ -59,42 +33,6 @@ describe('AudioSource', () => {
5933 audioSource . file = new File ( [ ] , 'audio.mp3' , { type : 'audio/mp3' } ) ;
6034 } ) ;
6135
62- it ( 'find silences correctly' , async ( ) => {
63- const audioBuffer = {
64- duration : 16 ,
65- sampleRate : 1000 ,
66- length : 16000 ,
67- getChannelData : ( ) => new Float32Array ( 16000 ) . fill ( 0 ) , // Return a dummy Float32Array
68- } as any as AudioBuffer ;
69- audioSource . audioBuffer = audioBuffer ;
70- const silences = await audioSource . silences ( { } ) ;
71- expect ( silences ) . toEqual ( [ {
72- start : new Timestamp ( 0 ) ,
73- stop : new Timestamp ( 16000 ) ,
74- } ] ) ;
75- } ) ;
76-
77- it ( 'find silences correctly with too high minDuration' , async ( ) => {
78- const audioBuffer = {
79- duration : 16 ,
80- sampleRate : 1000 ,
81- length : 16000 ,
82- getChannelData : ( ) => new Float32Array ( 16000 ) . fill ( 0 ) , // Return a dummy Float32Array
83- } as any as AudioBuffer ;
84- audioSource . audioBuffer = audioBuffer ;
85- const silences = await audioSource . silences ( { minDuration : 1e10 } ) ;
86- expect ( silences ) . toEqual ( [ {
87- start : new Timestamp ( 0 ) ,
88- stop : new Timestamp ( 16000 ) ,
89- } ] ) ;
90- } ) ;
91-
92- it ( 'find silences correctly after caching' , async ( ) => {
93- const silences = await audioSource . silences ( { } ) ;
94- const cachedSilences = await audioSource . silences ( { threshold : 0 , minDuration : 1e10 , windowSize : 1e10 } ) ;
95- expect ( silences ) . toEqual ( cachedSilences ) ;
96- } ) ;
97-
9836 it ( 'should decode an audio buffer correctly' , async ( ) => {
9937 const buffer = await audioSource . decode ( 2 , 44100 , true ) ;
10038 expect ( buffer . duration ) . toBe ( 5 ) ; // Mock duration
0 commit comments