|
| 1 | +import pytest |
| 2 | +from pytsmod import ola, wsola |
| 3 | +from pytsmod import phase_vocoder as pv |
| 4 | +from pytsmod import phase_vocoder_int as pv_int |
| 5 | +import soundfile as sf |
| 6 | +import numpy as np |
| 7 | +import os |
| 8 | +from subprocess import call |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize('algorithm', ['ola', 'wsola', 'pv', 'pv_int']) |
| 12 | +def test_console_default_params(algorithm): |
| 13 | + test_file = 'tests/data/castanetsviolin.wav' |
| 14 | + alpha = 2 |
| 15 | + x, sr = sf.read(test_file) |
| 16 | + y = globals()[algorithm](x, alpha) |
| 17 | + |
| 18 | + cmd = ['python', 'pytsmod/console/console.py', algorithm, |
| 19 | + test_file, 'temp_cli.wav', str(alpha)] |
| 20 | + if algorithm == 'pv_int': |
| 21 | + cmd.append('-fs') |
| 22 | + call(cmd) |
| 23 | + |
| 24 | + sf.write('temp.wav', y, sr) |
| 25 | + y_, _ = sf.read('temp.wav') |
| 26 | + |
| 27 | + y_cli, _ = sf.read('temp_cli.wav') |
| 28 | + |
| 29 | + os.remove('temp.wav') |
| 30 | + os.remove('temp_cli.wav') |
| 31 | + |
| 32 | + assert np.allclose(y_, y_cli) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize('alpha', [1.25]) |
| 36 | +@pytest.mark.parametrize('win_type', ['sin']) |
| 37 | +@pytest.mark.parametrize('win_size', [512]) |
| 38 | +@pytest.mark.parametrize('syn_hop_size', [256]) |
| 39 | +def test_console_ola(alpha, win_type, win_size, syn_hop_size): |
| 40 | + test_file = 'tests/data/castanetsviolin.wav' |
| 41 | + x, sr = sf.read(test_file) |
| 42 | + y = ola(x, alpha, win_type=win_type, win_size=win_size, |
| 43 | + syn_hop_size=syn_hop_size) |
| 44 | + |
| 45 | + cmd = ['python', 'pytsmod/console/console.py', 'ola', |
| 46 | + test_file, 'temp_cli.wav', str(alpha), |
| 47 | + '-wt', win_type, '-ws', str(win_size), |
| 48 | + '-sh', str(syn_hop_size)] |
| 49 | + call(cmd) |
| 50 | + |
| 51 | + sf.write('temp.wav', y, sr) |
| 52 | + y_, _ = sf.read('temp.wav') |
| 53 | + |
| 54 | + y_cli, _ = sf.read('temp_cli.wav') |
| 55 | + |
| 56 | + os.remove('temp.wav') |
| 57 | + os.remove('temp_cli.wav') |
| 58 | + |
| 59 | + assert np.allclose(y_, y_cli) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize('alpha', [1.25]) |
| 63 | +@pytest.mark.parametrize('win_type', ['sin']) |
| 64 | +@pytest.mark.parametrize('win_size', [512]) |
| 65 | +@pytest.mark.parametrize('syn_hop_size', [256]) |
| 66 | +@pytest.mark.parametrize('tolerance', [256]) |
| 67 | +def test_console_wsola(alpha, win_type, win_size, syn_hop_size, tolerance): |
| 68 | + test_file = 'tests/data/castanetsviolin.wav' |
| 69 | + x, sr = sf.read(test_file) |
| 70 | + y = wsola(x, alpha, win_type=win_type, win_size=win_size, |
| 71 | + syn_hop_size=syn_hop_size, tolerance=tolerance) |
| 72 | + |
| 73 | + cmd = ['python', 'pytsmod/console/console.py', 'wsola', |
| 74 | + test_file, 'temp_cli.wav', str(alpha), |
| 75 | + '-wt', win_type, '-ws', str(win_size), |
| 76 | + '-sh', str(syn_hop_size), '-t', str(tolerance)] |
| 77 | + call(cmd) |
| 78 | + |
| 79 | + sf.write('temp.wav', y, sr) |
| 80 | + y_, _ = sf.read('temp.wav') |
| 81 | + |
| 82 | + y_cli, _ = sf.read('temp_cli.wav') |
| 83 | + |
| 84 | + os.remove('temp.wav') |
| 85 | + os.remove('temp_cli.wav') |
| 86 | + |
| 87 | + assert np.allclose(y_, y_cli) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize('alpha', [1.25]) |
| 91 | +@pytest.mark.parametrize('win_type', ['hann']) |
| 92 | +@pytest.mark.parametrize('win_size', [1024]) |
| 93 | +@pytest.mark.parametrize('syn_hop_size', [256]) |
| 94 | +@pytest.mark.parametrize('zero_pad', [256]) |
| 95 | +@pytest.mark.parametrize('restore_energy', [True]) |
| 96 | +@pytest.mark.parametrize('fft_shift', [True]) |
| 97 | +@pytest.mark.parametrize('phase_lock', [True]) |
| 98 | +def test_console_pv(alpha, win_type, win_size, syn_hop_size, zero_pad, |
| 99 | + restore_energy, fft_shift, phase_lock): |
| 100 | + test_file = 'tests/data/castanetsviolin.wav' |
| 101 | + x, sr = sf.read(test_file) |
| 102 | + y = pv(x, alpha, win_type=win_type, win_size=win_size, |
| 103 | + syn_hop_size=syn_hop_size, zero_pad=zero_pad, |
| 104 | + restore_energy=restore_energy, fft_shift=fft_shift, |
| 105 | + phase_lock=phase_lock) |
| 106 | + |
| 107 | + cmd = ['python', 'pytsmod/console/console.py', 'pv', |
| 108 | + test_file, 'temp_cli.wav', str(alpha), |
| 109 | + '-wt', win_type, '-ws', str(win_size), |
| 110 | + '-sh', str(syn_hop_size), '-z', str(zero_pad), |
| 111 | + '-e', '-fs', '-pl'] |
| 112 | + call(cmd) |
| 113 | + |
| 114 | + sf.write('temp.wav', y, sr) |
| 115 | + y_, _ = sf.read('temp.wav') |
| 116 | + |
| 117 | + y_cli, _ = sf.read('temp_cli.wav') |
| 118 | + |
| 119 | + os.remove('temp.wav') |
| 120 | + os.remove('temp_cli.wav') |
| 121 | + |
| 122 | + assert np.allclose(y_, y_cli) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize('alpha', [2]) |
| 126 | +@pytest.mark.parametrize('win_type', ['sin']) |
| 127 | +@pytest.mark.parametrize('win_size', [1024]) |
| 128 | +@pytest.mark.parametrize('syn_hop_size', [256]) |
| 129 | +@pytest.mark.parametrize('zero_pad', [256]) |
| 130 | +@pytest.mark.parametrize('restore_energy', [True]) |
| 131 | +@pytest.mark.parametrize('fft_shift', [False]) |
| 132 | +def test_console_pv_int(alpha, win_type, win_size, syn_hop_size, zero_pad, |
| 133 | + restore_energy, fft_shift): |
| 134 | + test_file = 'tests/data/castanetsviolin.wav' |
| 135 | + x, sr = sf.read(test_file) |
| 136 | + y = pv(x, alpha, win_type=win_type, win_size=win_size, |
| 137 | + syn_hop_size=syn_hop_size, zero_pad=zero_pad, |
| 138 | + restore_energy=restore_energy, fft_shift=fft_shift) |
| 139 | + |
| 140 | + cmd = ['python', 'pytsmod/console/console.py', 'pv', |
| 141 | + test_file, 'temp_cli.wav', str(alpha), |
| 142 | + '-wt', win_type, '-ws', str(win_size), |
| 143 | + '-sh', str(syn_hop_size), '-z', str(zero_pad), |
| 144 | + '-e'] |
| 145 | + call(cmd) |
| 146 | + |
| 147 | + sf.write('temp.wav', y, sr) |
| 148 | + y_, _ = sf.read('temp.wav') |
| 149 | + |
| 150 | + y_cli, _ = sf.read('temp_cli.wav') |
| 151 | + |
| 152 | + os.remove('temp.wav') |
| 153 | + os.remove('temp_cli.wav') |
| 154 | + |
| 155 | + assert np.allclose(y_, y_cli) |
0 commit comments