Skip to content

Commit

Permalink
dev(narugo): add more unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Aug 27, 2024
1 parent 4fdd1be commit f7bf168
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions soundutils/similarity/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def _align_sounds(

if sr1 != c_sr:
num_samples = int(sound1.time * c_sr)
data1 = resample(data1, num_samples)
data1 = resample(data1, num_samples, axis=-1)
sr1 = c_sr
if sr2 != c_sr:
num_samples = int(sound2.time * c_sr)
data2 = resample(data2, num_samples)
data2 = resample(data2, num_samples, axis=-1)
sr2 = c_sr

if time_align in {'none', 'noncheck'}:
Expand Down
75 changes: 75 additions & 0 deletions test/similarity/test_colleration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import pytest

from soundutils.similarity import SoundLengthNotMatch, SoundChannelsNotMatch, SoundResampleRateNotMatch, \
sound_pearson_similarity
from test.testings import get_testfile


@pytest.mark.unittest
class TestSimilarityCorrelation:
@pytest.mark.parametrize(['file1', 'file2', 'v'], [
('texas_short.wav', 'texas_short.wav', 1.0),
('texas_short.wav', 'texas_long.wav', SoundLengthNotMatch),
('texas_long.wav', 'texas_long.wav', 1.0),
('texas_long.wav', 'texas_long.flac', 1.0),
('texas_long.wav', 'texas_long.mp3', 0.9982756956202339),
('stereo_sine_wave.wav', 'stereo_sine_wave_44100.wav', SoundResampleRateNotMatch),
('texas_short.wav', 'stereo_sine_wave_44100.wav', SoundChannelsNotMatch),
])
def test_sound_pearson_similarity(self, file1, file2, v):
file1 = get_testfile('assets', file1)
file2 = get_testfile('assets', file2)
if isinstance(v, type) and issubclass(v, BaseException):
with pytest.raises(v):
_ = sound_pearson_similarity(file1, file2)
else:
assert sound_pearson_similarity(file1, file2) == pytest.approx(v)

@pytest.mark.parametrize(['file1', 'file2', 'v'], [
('texas_short.wav', 'texas_short.wav', 1.0),
('texas_short.wav', 'texas_long.wav', -0.023363902648302855),
('texas_long.wav', 'texas_short.wav', -0.023363902648302855),
('texas_long.wav', 'texas_long.wav', 1.0),
('texas_long.wav', 'texas_long.flac', 1.0),
('texas_long.wav', 'texas_long.mp3', 0.9982756956202339),
('stereo_sine_wave.wav', 'stereo_sine_wave_44100.wav', SoundResampleRateNotMatch),
('texas_short.wav', 'stereo_sine_wave_44100.wav', SoundChannelsNotMatch),
])
def test_sound_pearson_similarity_pad(self, file1, file2, v):
file1 = get_testfile('assets', file1)
file2 = get_testfile('assets', file2)
if isinstance(v, type) and issubclass(v, BaseException):
with pytest.raises(v):
_ = sound_pearson_similarity(file1, file2, time_align='pad')
else:
assert sound_pearson_similarity(file1, file2, time_align='pad') == pytest.approx(v)

@pytest.mark.parametrize(['file1', 'file2', 'time_align', 'v'], [
('texas_short.wav', 'texas_short.wav', 'none', 1.0),
('texas_short.wav', 'texas_short.wav', 'pad', 1.0),
('texas_short.wav', 'texas_short.wav', 'prefix', 1.0),
('texas_short.wav', 'texas_short.wav', 'resample_max', 1.0),
('texas_short.wav', 'texas_short.wav', 'resample_min', 1.0),
('texas_short.wav', 'texas_short.wav', 'bullshit', ValueError),
('texas_short.wav', 'texas_long.wav', 'none', SoundLengthNotMatch),
('texas_short.wav', 'texas_long.wav', 'pad', -0.023363902648302855),
('texas_long.wav', 'texas_short.wav', 'pad', -0.023363902648302855),
('texas_short.wav', 'texas_long.wav', 'prefix', -0.05039022356294788),
('texas_short.wav', 'texas_long.wav', 'resample_max', -6.501929201342696e-05),
('texas_long.wav', 'texas_short.wav', 'resample_max', -6.501929201342696e-05),
('texas_short.wav', 'texas_long.wav', 'resample_min', -6.781880893649058e-05),
('texas_long.wav', 'texas_short.wav', 'resample_min', -6.781880893649058e-05),
('texas_long.wav', 'texas_long.wav', 'none', 1.0),
('texas_long.wav', 'texas_long.flac', 'none', 1.0),
('texas_long.wav', 'texas_long.mp3', 'none', 0.9982756956202339),
])
def test_sound_pearson_similarity_time_align(self, file1, file2, time_align, v):
file1 = get_testfile('assets', file1)
file2 = get_testfile('assets', file2)
if isinstance(v, type) and issubclass(v, BaseException):
with pytest.raises(v):
_ = sound_pearson_similarity(file1, file2, time_align=time_align)
else:
assert sound_pearson_similarity(file1, file2, time_align=time_align) == pytest.approx(v)
24 changes: 24 additions & 0 deletions test/similarity/test_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,27 @@ def test_sound_spectral_centroid_distance_time_align(self, file1, file2, time_al
_ = sound_spectral_centroid_distance(file1, file2, time_align=time_align)
else:
assert sound_spectral_centroid_distance(file1, file2, time_align=time_align) == pytest.approx(v)

@pytest.mark.parametrize(['file1', 'file2', 'sr_align', 'v'], [
('stereo_sine_wave.wav', 'stereo_sine_wave.wav', 'none', 0.0),
('stereo_sine_wave.wav', 'stereo_sine_wave.wav', 'min', 0.0),
('stereo_sine_wave.wav', 'stereo_sine_wave.wav', 'max', 0.0),
('stereo_sine_wave.wav', 'stereo_sine_wave.wav', 'bullshit', ValueError),
('stereo_sine_wave.wav', 'stereo_sine_wave_44100.wav', 'none', SoundResampleRateNotMatch),
('stereo_sine_wave.wav', 'stereo_sine_wave_44100.wav', 'min', 0.0014548659720306609),
('stereo_sine_wave.wav', 'stereo_sine_wave_44100.wav', 'max', 0.0013925866190809756),
('stereo_sine_wave.wav', 'stereo_sine_wave_3x_40_900.wav', 'none', SoundResampleRateNotMatch),
('stereo_sine_wave.wav', 'stereo_sine_wave_3x_40_900.wav', 'min', 20.705757783547845),
('stereo_sine_wave.wav', 'stereo_sine_wave_3x_40_900.wav', 'max', 20.89222150977781),
])
def test_sound_spectral_centroid_distance_sr_align(self, file1, file2, sr_align, v):
file1 = get_testfile('assets', file1)
file2 = get_testfile('assets', file2)
if isinstance(v, type) and issubclass(v, BaseException):
with pytest.raises(v):
_ = sound_spectral_centroid_distance(file1, file2, time_align='pad', resample_rate_align=sr_align)
else:
assert sound_spectral_centroid_distance(file1, file2, time_align='pad', resample_rate_align=sr_align) \
== pytest.approx(v)

0 comments on commit f7bf168

Please sign in to comment.