|
| 1 | +import pytest |
| 2 | +import pytsmod as tsm |
| 3 | +from pytsmod.pvtsm import _find_peaks |
| 4 | +import numpy as np |
| 5 | +from scipy.io import loadmat |
| 6 | +import soundfile as sf |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize('alpha', [0.75, 1, 1.25]) |
| 10 | +@pytest.mark.parametrize('win_type', ['hann', 'sin']) |
| 11 | +@pytest.mark.parametrize('win_size', [2048, 4096]) |
| 12 | +@pytest.mark.parametrize('syn_hop_size', [512, 1024]) |
| 13 | +@pytest.mark.parametrize('phase_lock', [1, 0]) |
| 14 | +def test_pv_fixed(alpha, win_type, win_size, syn_hop_size, phase_lock): |
| 15 | + x, _ = sf.read('tests/data/castanetsviolin.wav') |
| 16 | + matlab_results = loadmat('tests/data/pv_fixed_rate.mat')['result'] |
| 17 | + |
| 18 | + y = tsm.phase_vocoder(x, alpha, win_type, win_size, syn_hop_size, |
| 19 | + phase_lock=phase_lock) |
| 20 | + |
| 21 | + _, w = np.where(matlab_results[0, :] == np.array([[alpha]])) |
| 22 | + matlab_results = matlab_results[:, w] |
| 23 | + _, w = np.where(matlab_results[1, :] == np.array([[2 if win_type == 'hann' else 1]])) |
| 24 | + matlab_results = matlab_results[:, w] |
| 25 | + _, w = np.where(matlab_results[2, :] == np.array([[win_size]])) |
| 26 | + matlab_results = matlab_results[:, w] |
| 27 | + _, w = np.where(matlab_results[3, :] == np.array([[syn_hop_size]])) |
| 28 | + matlab_results = matlab_results[:, w] |
| 29 | + _, w = np.where(matlab_results[4, :] == np.array([[phase_lock]])) |
| 30 | + matlab_results = matlab_results[:, w] |
| 31 | + |
| 32 | + y_matlab = matlab_results[5, :][0].squeeze() |
| 33 | + print(np.max(y - y_matlab)) |
| 34 | + assert np.allclose(y, y_matlab) |
| 35 | + |
| 36 | + |
| 37 | +# @pytest.mark.parametrize('win_type', ['hann', 'sin']) |
| 38 | +# @pytest.mark.parametrize('win_size', [1024, 2048]) |
| 39 | +# @pytest.mark.parametrize('syn_hop_size', [512, 1024]) |
| 40 | +# @pytest.mark.parametrize('tolerance', [512, 1024]) |
| 41 | +# def test_pv_nonlinear(win_type, win_size, syn_hop_size, tolerance): |
| 42 | +# x, _ = sf.read('tests/data/beethovenorchestra.wav') |
| 43 | +# ap = loadmat('tests/data/anchorpoints.mat')['anchorpoints'].T - 1 |
| 44 | +# wsola_nonlinear = loadmat('tests/data/wsola_nonlinear.mat') |
| 45 | + |
| 46 | +# y = tsm.wsola(x, ap, win_type, win_size, syn_hop_size, tolerance) |
| 47 | + |
| 48 | +# sample_name = f'orc_{win_type}_{win_size}_{syn_hop_size}_{tolerance}' |
| 49 | +# y_matlab = wsola_nonlinear[sample_name].squeeze() |
| 50 | +# assert np.allclose(y, y_matlab) |
| 51 | + |
| 52 | + |
| 53 | +# @pytest.mark.parametrize('n_chan', range(2, 9)) |
| 54 | +# @pytest.mark.parametrize('transpose', [True, False]) |
| 55 | +# def test_pv_multichannel(n_chan, transpose): |
| 56 | +# x, _ = sf.read('tests/data/dogbeetle.wav') |
| 57 | +# x_wsola = tsm.wsola(x, 1.3) |
| 58 | + |
| 59 | +# x_multi = np.tile(x, (n_chan, 1)) |
| 60 | + |
| 61 | +# if transpose: |
| 62 | +# x_multi = x_multi.T |
| 63 | + |
| 64 | +# x_multi_wsola = tsm.wsola(x_multi, 1.3) |
| 65 | + |
| 66 | +# for i in range(x_multi_wsola.shape[0]): |
| 67 | +# assert np.allclose(x_wsola, x_multi_wsola[i, :]) |
| 68 | + |
| 69 | + |
| 70 | +def test_find_peaks(): |
| 71 | + sample = np.array([0, 0.3, 1, 0.4, 0.3, 2, 0.3, 3, 0.1, 0.2, 0.14, 2]) |
| 72 | + peaks, infl = _find_peaks(sample) |
| 73 | + |
| 74 | + assert np.allclose(peaks, np.array([2, 7, 11])) |
| 75 | + assert np.allclose(infl, np.array([[0, 5, 9], [4, 8, 11]])) |
0 commit comments