1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
"""
Python-SoXR
https://github.com/dofuuz/python-soxr
SPDX-FileCopyrightText: (c) 2021 Myungchul Keum
SPDX-License-Identifier: LGPL-2.1-or-later
High quality, one-dimensional sample-rate conversion library for Python.
Python-SoXR is a Python wrapper of libsoxr.
"""
from concurrent.futures import ThreadPoolExecutor
from functools import partial
import numpy as np
import pytest
import soxr
@pytest.mark.xfail(raises=ValueError, strict=True)
@pytest.mark.parametrize('in_rate, out_rate', [(100, 0), (100, -1), (0, 100), (-1, 100)])
def test_bad_sr(in_rate, out_rate):
x = np.zeros(100)
soxr.resample(x, in_rate, out_rate)
@pytest.mark.parametrize('dtype', [np.float32, np.float64, np.int16, np.int32])
def test_dtype(dtype):
x = np.random.randn(100).astype(dtype)
y = soxr.resample(x, 100, 200)
assert x.dtype == y.dtype
@pytest.mark.xfail(raises=(TypeError, ValueError), strict=True)
@pytest.mark.parametrize('dtype', [np.complex64, np.complex128, np.int8, np.int64])
def test_bad_dtype(dtype):
x = np.zeros(100, dtype=dtype)
soxr.resample(x, 100, 200)
@pytest.mark.parametrize('in_rate, out_rate', [(44100, 32000), (32000, 44100)])
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
def test_divide_match(in_rate, out_rate, dtype):
x = np.random.randn(25999,2).astype(dtype)
y_oneshot = soxr._resample_oneshot(x, in_rate, out_rate)
y_divide = soxr.resample(x, in_rate, out_rate)
y_split = soxr.resample(np.asfortranarray(x), in_rate, out_rate)
assert np.allclose(y_oneshot, y_divide)
assert np.allclose(y_oneshot, y_split)
@pytest.mark.parametrize('in_rate, out_rate', [(44100, 32000), (32000, 44100)])
@pytest.mark.parametrize('length', [0, 1, 2, 99, 100, 101, 31999, 32000, 32001, 34828, 34829, 34830, 44099, 44100, 44101, 47999, 48000, 48001, 66149, 66150, 266151])
def test_length_match(in_rate, out_rate, length):
x = np.random.randn(266151, 2).astype(np.float32)
y_oneshot = soxr._resample_oneshot(x[:length], in_rate, out_rate)
y_divide = soxr.resample(x[:length], in_rate, out_rate)
y_split = soxr.resample(np.asfortranarray(x)[:length], in_rate, out_rate)
assert np.allclose(y_oneshot, y_divide)
assert np.allclose(y_oneshot, y_split)
@pytest.mark.parametrize('channels', [1, 2, 3, 5, 7, 24, 49])
def test_channel_match(channels):
x = np.random.randn(30011, channels).astype(np.float32)
y_oneshot = soxr._resample_oneshot(x, 44100, 32000)
y_divide = soxr.resample(x, 44100, 32000)
y_split = soxr.resample(np.asfortranarray(x), 44100, 32000)
assert np.allclose(y_oneshot, y_divide)
assert np.allclose(y_oneshot, y_split)
@pytest.mark.parametrize('in_rate, out_rate', [(44100, 32000), (32000, 44100)])
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
@pytest.mark.parametrize('channels', [1, 2])
def test_stream_match(in_rate, out_rate, dtype, channels):
CHUNK_SIZE = 509
x = np.random.randn(49999, channels).astype(dtype)
y_oneshot = soxr._resample_oneshot(x, in_rate, out_rate)
rs_stream = soxr.ResampleStream(in_rate, out_rate, channels, dtype=dtype)
y_list = []
for idx in range(0, len(x), CHUNK_SIZE):
end = idx + CHUNK_SIZE
eof = False
if len(x) <= end:
eof = True
end = len(x)
y_chunk = rs_stream.resample_chunk(x[idx:end], last=eof)
y_list.append(y_chunk)
y_stream = np.concatenate(y_list)
assert np.allclose(y_oneshot, y_stream)
@pytest.mark.parametrize('in_rate, out_rate', [(44100, 32000), (32000, 44100)])
@pytest.mark.parametrize('chunk_size', [7, 50, 101, 44100])
@pytest.mark.parametrize('length', [0, 1, 100, 101, 31999, 32000, 44100, 44101, 266151])
def test_stream_length(in_rate, out_rate, chunk_size, length):
x = np.random.randn(length, 1).astype(np.float32)
y_oneshot = soxr._resample_oneshot(x, in_rate, out_rate)
rs_stream = soxr.ResampleStream(in_rate, out_rate, 1, dtype=np.float32)
y_list = [np.ndarray([0, 1], dtype=np.float32)]
for idx in range(0, len(x), chunk_size):
end = idx + chunk_size
eof = False
if len(x) <= end:
eof = True
end = len(x)
y_chunk = rs_stream.resample_chunk(x[idx:end], last=eof)
y_list.append(y_chunk)
y_stream = np.concatenate(y_list)
assert np.allclose(y_oneshot, y_stream)
def make_tone(freq, sr, duration):
length = int(sr * duration)
sig = np.sin(2 * np.pi * freq / sr * np.arange(length))
sig = sig * np.hanning(length)
return np.stack([sig, np.zeros_like(sig)], axis=-1)
@pytest.mark.parametrize('in_rate,out_rate', [(44100, 22050), (22050, 32000)])
@pytest.mark.parametrize('quality', ['VHQ', 'HQ', 'MQ', 'LQ', 'QQ'])
def test_quality_sine(in_rate, out_rate, quality):
FREQ = 32.0
DURATION = 2.0
x = make_tone(FREQ, in_rate, DURATION)
y = make_tone(FREQ, out_rate, DURATION)
y_pred = soxr.resample(x, in_rate, out_rate, quality=quality)
y_split = soxr.resample(np.asfortranarray(x), in_rate, out_rate, quality=quality)
assert np.allclose(y, y_pred, atol=1e-4)
assert np.allclose(y, y_split, atol=1e-4)
@pytest.mark.parametrize('in_rate,out_rate', [(48000, 24000), (32000, 44100)])
@pytest.mark.parametrize('dtype', [np.int32, np.int16])
def test_int_sine(in_rate, out_rate, dtype):
FREQ = 32.0
DURATION = 2.0
x = (make_tone(FREQ, in_rate, DURATION) * 16384).astype(dtype)
y = (make_tone(FREQ, out_rate, DURATION) * 16384).astype(dtype)
y_pred = soxr.resample(x, in_rate, out_rate)
y_split = soxr.resample(np.asfortranarray(x), in_rate, out_rate)
assert np.allclose(y, y_pred, atol=2)
assert np.allclose(y, y_split, atol=2)
@pytest.mark.parametrize('num_task', [2, 3, 4, 5, 6, 7, 8, 9, 12, 17, 32])
def test_multithread(num_task):
x = np.random.randn(25999, 2).astype(np.float32)
with ThreadPoolExecutor() as p:
results = p.map(
partial(soxr.resample, in_rate=44100, out_rate=32000),
[x] * num_task
)
results = list(results)
assert np.allclose(results[-2], results[-1])
|