1+ import os
12import pickle
23from logging import getLogger
34from pathlib import Path
1819
1920def test_uint8_8bitPCM () -> None :
2021 with NamedTemporaryFile (
21- suffix = ".wav" , delete = True , prefix = "test_compare_audio_files"
22+ suffix = ".wav" , delete = False , prefix = "test_compare_audio_files"
2223 ) as file_a_tmp :
2324 audio_a_tmp_path = Path (file_a_tmp .name )
2425
26+ with NamedTemporaryFile (
27+ suffix = ".wav" , delete = False , prefix = "test_compare_audio_files"
28+ ) as file_b_tmp :
29+ audio_b_tmp_path = Path (file_b_tmp .name )
30+
31+ try :
2532 sr_a , audio_a = wavfile .read (AUDIO_A )
2633 assert sr_a == 22050
2734 assert audio_a .dtype == np .int16
@@ -30,58 +37,59 @@ def test_uint8_8bitPCM() -> None:
3037 new_audio = ((norm_audio + 1 ) * 127.5 ).astype (np .uint8 )
3138 wavfile .write (audio_a_tmp_path , 22050 , new_audio )
3239
33- with NamedTemporaryFile (
34- suffix = ".wav" , delete = True , prefix = "test_compare_audio_files"
35- ) as file_b_tmp :
36- audio_b_tmp_path = Path (file_b_tmp .name )
37-
38- sr_b , audio_b = wavfile .read (AUDIO_B )
39- assert sr_b == 22050
40- assert audio_b .dtype == np .int16
40+ sr_b , audio_b = wavfile .read (AUDIO_B )
41+ assert sr_b == 22050
42+ assert audio_b .dtype == np .int16
4143
42- norm_audio = audio_b / 32768.0
43- new_audio = ((norm_audio + 1 ) * 127.5 ).astype (np .uint8 )
44- wavfile .write (audio_b_tmp_path , 22050 , new_audio )
44+ norm_audio = audio_b / 32768.0
45+ new_audio = ((norm_audio + 1 ) * 127.5 ).astype (np .uint8 )
46+ wavfile .write (audio_b_tmp_path , 22050 , new_audio )
4547
46- test_cases = [
47- (AUDIO_A , AUDIO_B ), # 16 bit vs 8 bit
48- (AUDIO_A , audio_b_tmp_path ), # 16 bit vs 8 bit
49- (audio_a_tmp_path , AUDIO_B ), # 16 bit vs 8 bit
50- (audio_a_tmp_path , audio_b_tmp_path ), # 16 bit vs 8 bit
51- ]
48+ test_cases = [
49+ (AUDIO_A , AUDIO_B ), # 16 bit vs 8 bit
50+ (AUDIO_A , audio_b_tmp_path ), # 16 bit vs 8 bit
51+ (audio_a_tmp_path , AUDIO_B ), # 16 bit vs 8 bit
52+ (audio_a_tmp_path , audio_b_tmp_path ), # 16 bit vs 8 bit
53+ ]
5254
53- test_results = []
55+ test_results = []
56+
57+ for a , b in test_cases :
58+ mcd , pen = compare_audio_files (
59+ a ,
60+ b ,
61+ sample_rate = 22050 ,
62+ align_target = "mel" ,
63+ aligning = "dtw" ,
64+ remove_silence = "no" ,
65+ norm_audio = False ,
66+ M = 20 ,
67+ s = 1 ,
68+ D = 16 ,
69+ fmin = 0 ,
70+ fmax = 8000 ,
71+ n_fft = 32 ,
72+ win_len = 32 ,
73+ hop_len = 16 ,
74+ window = "hanning" ,
75+ dtw_radius = 1 ,
76+ )
77+ test_results .append ([mcd , pen ])
5478
55- for a , b in test_cases :
56- mcd , pen = compare_audio_files (
57- a ,
58- b ,
59- sample_rate = 22050 ,
60- align_target = "mel" ,
61- aligning = "dtw" ,
62- remove_silence = "no" ,
63- norm_audio = False ,
64- M = 20 ,
65- s = 1 ,
66- D = 16 ,
67- fmin = 0 ,
68- fmax = 8000 ,
69- n_fft = 32 ,
70- win_len = 32 ,
71- hop_len = 16 ,
72- window = "hanning" ,
73- dtw_radius = 1 ,
74- )
75- test_results .append ([mcd , pen ])
79+ assert_results = [
80+ [7.64104558175767 , 0.14414414414414423 ],
81+ [15.955726033561426 , 0.16617210682492578 ],
82+ [19.39442751788282 , 0.06269592476489039 ],
83+ [3.8681248602509037 , 0.33870967741935476 ],
84+ ]
7685
77- assert_results = [
78- [7.64104558175767 , 0.14414414414414423 ],
79- [15.955726033561426 , 0.16617210682492578 ],
80- [19.39442751788282 , 0.06269592476489039 ],
81- [3.8681248602509037 , 0.33870967741935476 ],
82- ]
86+ np .testing .assert_almost_equal (test_results , assert_results )
8387
84- np .testing .assert_almost_equal (test_results , assert_results )
88+ finally :
89+ if audio_a_tmp_path .exists ():
90+ os .remove (audio_a_tmp_path )
91+ if audio_b_tmp_path .exists ():
92+ os .remove (audio_b_tmp_path )
8593
8694
8795def test_float32_32bitFloat () -> None :
0 commit comments