forked from tuxu/python-samplerate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
166 lines (124 loc) · 4.39 KB
/
test_api.py
File metadata and controls
166 lines (124 loc) · 4.39 KB
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
import numpy as np
import pytest
import samplerate
def test_aliases():
from samplerate.converters import (
Resampler,
CallbackResampler,
resample,
ConverterType,
)
from samplerate import (
Resampler,
CallbackResampler,
resample,
ConverterType,
ResamplingError,
)
from samplerate.exceptions import ResamplingError
@pytest.fixture(scope="module", params=[1, 2])
def data(request):
num_channels = request.param
periods = np.linspace(0, 10, 1000)
input_data = [
np.sin(2 * np.pi * periods + i * np.pi / 2) for i in range(num_channels)
]
return (
(num_channels, input_data[0])
if num_channels == 1
else (num_channels, np.transpose(input_data))
)
@pytest.fixture(params=[0, 1, 2, 3, 4])
def converter_type(request):
return request.param
def test_simple(data, converter_type, ratio=2.0):
_, input_data = data
samplerate.resample(input_data, ratio, converter_type)
def test_process(data, converter_type, ratio=2.0):
num_channels, input_data = data
src = samplerate.Resampler(converter_type, num_channels)
src.process(input_data.copy(), ratio)
def test_match(data, converter_type, ratio=2.0):
num_channels, input_data = data
output_simple = samplerate.resample(input_data, ratio, converter_type)
resampler = samplerate.Resampler(converter_type, channels=num_channels)
output_full = resampler.process(input_data, ratio, end_of_input=True)
assert np.allclose(output_simple, output_full)
def test_callback(data, converter_type, ratio=2.0):
_, input_data = data
def producer():
yield input_data
while True:
yield None
callback = lambda p=producer(): next(p)
channels = input_data.shape[-1] if input_data.ndim == 2 else 1
resampler = samplerate.CallbackResampler(callback, ratio, converter_type, channels)
resampler.read(int(ratio) * input_data.shape[0])
def test_callback_with(data, converter_type, ratio=2.0):
from samplerate import CallbackResampler
_, input_data = data
def producer():
yield input_data
while True:
yield None
callback = lambda p=producer(): next(p)
channels = input_data.shape[-1] if input_data.ndim == 2 else 1
with CallbackResampler(
callback, ratio, converter_type, channels=channels
) as resampler:
resampler.read(int(ratio) * input_data.shape[0])
def test_callback_with_2x(data, converter_type, ratio=2.0):
"""
Tests that there are no errors if we reuse an object created with a context manager
"""
from samplerate import CallbackResampler
_, input_data = data
def producer():
yield input_data
while True:
yield None
channels = input_data.shape[-1] if input_data.ndim == 2 else 1
callback = lambda p=producer(): next(p)
with CallbackResampler(
callback, ratio, converter_type, channels=channels
) as resampler:
resampler.read(int(ratio) * input_data.shape[0] // 2)
# re-initialize the data producer
resampler.read(int(ratio) * input_data.shape[0] // 2)
def test_Resampler_clone():
resampler = samplerate.Resampler("sinc_best", 1)
new_resampler = resampler.clone()
def test_CallbackResampler_clone(data, converter_type, ratio=2.0):
_, input_data = data
def producer():
yield input_data
while True:
yield None
callback = lambda p=producer(): next(p)
channels = input_data.shape[-1] if input_data.ndim == 2 else 1
resampler = samplerate.CallbackResampler(callback, ratio, converter_type, channels)
resampler.read(int(ratio) * input_data.shape[0])
new_resampler = resampler.clone()
@pytest.mark.parametrize(
"input_obj,expected_type",
[
(0, 0),
(1, 1),
(2, 2),
(3, 3),
(4, 4),
("sinc_best", 0),
("sinc_medium", 1),
("sinc_fastest", 2),
("zero_order_hold", 3),
("linear", 4),
(samplerate.ConverterType.sinc_best, 0),
(samplerate.ConverterType.sinc_medium, 1),
(samplerate.ConverterType.sinc_fastest, 2),
(samplerate.ConverterType.zero_order_hold, 3),
(samplerate.ConverterType.linear, 4),
],
)
def test_converter_type(input_obj, expected_type):
ret = samplerate._internals.get_converter_type(input_obj)
assert ret == expected_type