Mercurial > hg > auditok
comparison tests/test_core.py @ 416:14efef6f4bae
implement 'split_and_join_with_silence'
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Wed, 16 Oct 2024 20:42:58 +0200 |
parents | e26dcf224846 |
children | c5b4178aa80f |
comparison
equal
deleted
inserted
replaced
415:e26dcf224846 | 416:14efef6f4bae |
---|---|
6 from unittest.mock import Mock, patch | 6 from unittest.mock import Mock, patch |
7 | 7 |
8 import numpy as np | 8 import numpy as np |
9 import pytest | 9 import pytest |
10 | 10 |
11 from auditok import AudioParameterError, AudioRegion, load, make_silence, split | 11 from auditok import ( |
12 AudioParameterError, | |
13 AudioRegion, | |
14 load, | |
15 make_silence, | |
16 split, | |
17 split_and_join_with_silence, | |
18 ) | |
12 from auditok.core import ( | 19 from auditok.core import ( |
13 _duration_to_nb_windows, | 20 _duration_to_nb_windows, |
14 _make_audio_region, | 21 _make_audio_region, |
15 _read_chunks_online, | 22 _read_chunks_online, |
16 _read_offline, | 23 _read_offline, |
100 size = round(duration * sampling_rate) * sample_width * channels | 107 size = round(duration * sampling_rate) * sample_width * channels |
101 expected_data = b"\0" * size | 108 expected_data = b"\0" * size |
102 expected_duration = size / (sampling_rate * sample_width * channels) | 109 expected_duration = size / (sampling_rate * sample_width * channels) |
103 assert silence.duration == expected_duration | 110 assert silence.duration == expected_duration |
104 assert silence.data == expected_data | 111 assert silence.data == expected_data |
112 | |
113 | |
114 @pytest.mark.parametrize( | |
115 "duration", | |
116 [ | |
117 (0,), # zero_second | |
118 (1,), # one_second | |
119 (1.0001,), # 1.0001_second | |
120 ], | |
121 ids=[ | |
122 "zero_second", | |
123 "one_second", | |
124 "1.0001_second", | |
125 ], | |
126 ) | |
127 def test_split_and_join_with_silence(duration): | |
128 duration = 1.0 | |
129 sampling_rate = 10 | |
130 sample_width = 2 | |
131 channels = 1 | |
132 | |
133 regions = split( | |
134 input="tests/data/test_split_10HZ_mono.raw", | |
135 min_dur=0.2, | |
136 max_dur=5, | |
137 max_silence=0.2, | |
138 drop_trailing_silence=False, | |
139 strict_min_dur=False, | |
140 analysis_window=0.1, | |
141 sr=sampling_rate, | |
142 sw=sample_width, | |
143 ch=channels, | |
144 eth=50, | |
145 ) | |
146 | |
147 size = round(duration * sampling_rate) * sample_width * channels | |
148 join_data = b"\0" * size | |
149 expected_data = join_data.join(region.data for region in regions) | |
150 expected_region = AudioRegion( | |
151 expected_data, sampling_rate, sample_width, channels | |
152 ) | |
153 | |
154 region_with_silence = split_and_join_with_silence( | |
155 input="tests/data/test_split_10HZ_mono.raw", | |
156 silence_duration=duration, | |
157 min_dur=0.2, | |
158 max_dur=5, | |
159 max_silence=0.2, | |
160 drop_trailing_silence=False, | |
161 strict_min_dur=False, | |
162 analysis_window=0.1, | |
163 sr=sampling_rate, | |
164 sw=sample_width, | |
165 ch=channels, | |
166 eth=50, | |
167 ) | |
168 assert region_with_silence == expected_region | |
105 | 169 |
106 | 170 |
107 @pytest.mark.parametrize( | 171 @pytest.mark.parametrize( |
108 "duration, analysis_window, round_fn, expected, kwargs", | 172 "duration, analysis_window, round_fn, expected, kwargs", |
109 [ | 173 [ |