-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathtest_audio_stream.py
More file actions
203 lines (180 loc) Β· 7.63 KB
/
test_audio_stream.py
File metadata and controls
203 lines (180 loc) Β· 7.63 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#! /usr/bin/env python
#
# Copyright 2023 Spotify AB
#
# Licensed under the GNU Public License, Version 3.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.gnu.org/licenses/gpl-3.0.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import numpy as np
import pytest
import pedalboard
# Very silly: even just creating an AudioStream object that reads from an `iPhone Microphone``
# will cause a locally-present iPhone to emit a sound. Running `pytest` on my laptop makes my
# phone ding.
DEVICE_NAMES_TO_SKIP = {
"iPhone Microphone",
"AirPods",
# PulseAudio devices:
"Open Sound System",
"JACK Audio Connection Kit",
"plugin",
}
INPUT_DEVICE_NAMES = [
n
for n in pedalboard.io.AudioStream.input_device_names
if not any(substring.lower() in n.lower() for substring in DEVICE_NAMES_TO_SKIP)
]
OUTPUT_DEVICE_NAMES = [
n
for n in pedalboard.io.AudioStream.output_device_names
if not any(substring.lower() in n.lower() for substring in DEVICE_NAMES_TO_SKIP)
]
ACCEPTABLE_ERRORS_ON_CI = {"No driver"}
# Note: this test may do nothing on CI, because we don't have mock audio devices available.
# This will run on Linux, macOS and probably Windows as long as at least one audio device is available.
@pytest.mark.parametrize("input_device_name", INPUT_DEVICE_NAMES)
@pytest.mark.parametrize("output_device_name", OUTPUT_DEVICE_NAMES)
def test_create_stream(input_device_name: str, output_device_name: str):
try:
stream = pedalboard.io.AudioStream(
input_device_name, output_device_name, allow_feedback=True
)
except Exception as e:
if any(substr in str(e) for substr in ACCEPTABLE_ERRORS_ON_CI):
raise pytest.skip(str(e))
raise
assert stream is not None
assert input_device_name in repr(stream)
assert output_device_name in repr(stream)
assert not stream.running
assert isinstance(stream.plugins, pedalboard.Chain)
with stream:
assert stream.running
# Ensure that modifying a running stream does not crash:
stream.plugins.append(pedalboard.Gain(gain_db=-120))
for _ in range(0, 10):
stream.plugins.append(pedalboard.Gain(gain_db=-120))
for i in reversed(range(len(stream.plugins))):
del stream.plugins[i]
assert stream.running
assert not stream.running
# Note: this test may do nothing on CI, because we don't have mock audio devices available.
# This will run on Linux, macOS and probably Windows as long as at least one audio device is available.
@pytest.mark.skipif(
(
pedalboard.io.AudioStream.default_output_device_name == "Null Audio Device"
or pedalboard.io.AudioStream.default_output_device_name is None
),
reason="Test requires a working audio device.",
)
def test_write_to_stream():
try:
stream = pedalboard.io.AudioStream(
None, pedalboard.io.AudioStream.default_output_device_name
)
except Exception as e:
if any(substr in str(e) for substr in ACCEPTABLE_ERRORS_ON_CI):
raise pytest.skip(str(e))
raise
assert stream is not None
assert not stream.running
with stream:
assert stream.running
stream.write(np.zeros((2, 1024), dtype=np.float32), stream.sample_rate)
assert not stream.running
# Note: this test may do nothing on CI, because we don't have mock audio devices available.
# This will run on Linux, macOS and probably Windows as long as at least one audio device is available.
@pytest.mark.skipif(
(
pedalboard.io.AudioStream.default_output_device_name == "Null Audio Device"
or pedalboard.io.AudioStream.default_output_device_name is None
),
reason="Test requires a working audio device.",
)
def test_write_to_stream_without_opening():
try:
stream = pedalboard.io.AudioStream(
None, pedalboard.io.AudioStream.default_output_device_name
)
except Exception as e:
if any(substr in str(e) for substr in ACCEPTABLE_ERRORS_ON_CI):
raise pytest.skip(str(e))
raise
assert stream is not None
assert not stream.running
stream.write(np.zeros((2, 1024), dtype=np.float32), stream.sample_rate)
assert not stream.running
assert stream.buffered_input_sample_count is None
# Note: this test may do nothing on CI, because we don't have mock audio devices available.
# This will run on Linux, macOS and probably Windows as long as at least one audio device is available.
@pytest.mark.skipif(
(
pedalboard.io.AudioStream.default_input_device_name == "Null Audio Device"
or pedalboard.io.AudioStream.default_input_device_name is None
),
reason="Test requires a working audio device.",
)
def test_read_from_stream():
try:
stream = pedalboard.io.AudioStream(pedalboard.io.AudioStream.default_input_device_name)
except Exception as e:
if any(substr in str(e) for substr in ACCEPTABLE_ERRORS_ON_CI):
raise pytest.skip(str(e))
raise
assert stream is not None
assert not stream.running
result = stream.read(1)
assert result.shape == (stream.num_input_channels, 1)
assert not stream.running
assert stream.buffered_input_sample_count == 0
# Note: this test may do nothing on CI, because we don't have mock audio devices available.
# This will run on Linux, macOS and probably Windows as long as at least one audio device is available.
@pytest.mark.skipif(
(
pedalboard.io.AudioStream.default_input_device_name == "Null Audio Device"
or pedalboard.io.AudioStream.default_input_device_name is None
),
reason="Test requires a working audio device.",
)
def test_read_from_stream_measures_dropped_frames():
try:
stream = pedalboard.io.AudioStream(pedalboard.io.AudioStream.default_input_device_name)
except Exception as e:
if any(substr in str(e) for substr in ACCEPTABLE_ERRORS_ON_CI):
raise pytest.skip(str(e))
raise
assert stream is not None
with stream:
if stream.sample_rate == 0:
raise pytest.skip("Sample rate of default audio device is 0")
assert stream.running
assert stream.dropped_input_frame_count == 0
# Sleep long enough to overflow the internal buffer and cause dropped frames.
# Use a generous multiplier to account for varying buffer sizes across platforms.
time.sleep(max(0.5, 20 * stream.buffer_size / stream.sample_rate))
assert (
stream.buffered_input_sample_count is not None
and stream.buffered_input_sample_count > 0
)
dropped_count = stream.dropped_input_frame_count
if dropped_count == 0:
raise pytest.skip(
"No frames were dropped during the test window; "
"audio device may have a large internal buffer"
)
# The input buffer was cleared on __exit__, so the buffer count should be zero:
assert stream.buffered_input_sample_count == 0
# ...but we should still know how many frames were dropped.
# Allow up to one extra buffer's worth of dropped frames, because the audio
# thread may drop one more buffer between our snapshot and stream shutdown.
assert stream.dropped_input_frame_count >= dropped_count
assert stream.dropped_input_frame_count <= dropped_count + stream.buffer_size