-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_write.py
More file actions
194 lines (161 loc) · 5.46 KB
/
Copy pathtest_write.py
File metadata and controls
194 lines (161 loc) · 5.46 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
from stempeg.write import ChannelsWriter
from stempeg.cmds import MP4BOX_PATH
import stempeg
import numpy as np
import pytest
import tempfile as tmp
import subprocess as sp
import json
import os
import codecs
@pytest.fixture(params=[1, 4])
def nb_stems(request):
return request.param
@pytest.fixture(params=[1, 2])
def nb_channels(request):
return request.param
@pytest.fixture(params=[4096, 4096*10])
def nb_samples(request):
return request.param
@pytest.fixture
def audio(request, nb_stems, nb_samples, nb_channels):
return np.random.random((nb_stems, nb_samples, nb_channels))
@pytest.fixture(params=["m4a"])
def multistream_format(request):
return request.param
@pytest.fixture(params=["m4a", "wav", "flac"])
def multichannel_format(request):
return request.param
@pytest.fixture(params=["mp3", "m4a", "wav", "flac"])
def multifile_format(request):
return request.param
def test_multistream_containers(audio, multistream_format, nb_stems):
if nb_stems > 1:
with tmp.NamedTemporaryFile(
delete=False,
suffix='.' + multistream_format
) as tempfile:
stem_names = [str(k) for k in range(nb_stems)]
stempeg.write_stems(
tempfile.name,
audio,
sample_rate=44100,
writer=stempeg.StreamsWriter(
codec='aac',
stem_names=stem_names
)
)
loaded_audio, rate = stempeg.read_stems(
tempfile.name,
always_3d=True
)
assert audio.shape == loaded_audio.shape
if multistream_format == "m4a":
info = stempeg.Info(tempfile.name)
loaded_stem_names = info.title_streams
# check if titles could be extracted
assert all(
[a == b for a, b in zip(stem_names, loaded_stem_names)]
)
def test_multichannel_containers(audio, nb_channels, multichannel_format):
with tmp.NamedTemporaryFile(
delete=False,
suffix='.' + multichannel_format
) as tempfile:
stempeg.write_stems(
tempfile.name,
audio,
sample_rate=44100,
writer=ChannelsWriter()
)
loaded_audio, rate = stempeg.read_stems(
tempfile.name,
always_3d=True,
reader=stempeg.ChannelsReader(nb_channels=nb_channels)
)
assert audio.shape == loaded_audio.shape
def test_multifileformats(audio, multifile_format, nb_stems):
with tmp.NamedTemporaryFile(
delete=False,
suffix='.' + multifile_format
) as tempfile:
stem_names = [str(k) for k in range(nb_stems)]
stempeg.write_stems(
tempfile.name,
audio,
sample_rate=44100,
writer=stempeg.FilesWriter(stem_names=stem_names)
)
def test_channels(audio, multichannel_format):
if audio.ndim == 1:
with tmp.NamedTemporaryFile(
delete=False,
suffix='.' + multichannel_format
) as tempfile:
stempeg.write_audio(tempfile.name, audio, sample_rate=44100)
loaded_audio, rate = stempeg.read_stems(
tempfile.name,
)
assert audio.shape == loaded_audio.shape
def test_stereo(audio, multifile_format):
if audio.ndim == 2:
with tmp.NamedTemporaryFile(
delete=False,
suffix='.' + multifile_format
) as tempfile:
stempeg.write_audio(tempfile.name, audio, sample_rate=44100)
loaded_audio, rate = stempeg.read_stems(
tempfile.name,
always_3d=True,
)
assert audio.shape == loaded_audio.shape
# write multistream as wav, which doesn't support it
@pytest.mark.xfail
def test_ffmpeg_errors(audio):
if audio.ndim == 3:
with pytest.raises(RuntimeError):
with tmp.NamedTemporaryFile(
delete=False,
suffix='.wav'
) as tempfile:
stempeg.write_stems(
tempfile.name,
audio,
sample_rate=44100,
writer=stempeg.StreamsWriter()
)
def ordered(obj):
if isinstance(obj, dict):
return sorted((k, ordered(v)) for k, v in obj.items())
if isinstance(obj, list):
return sorted(ordered(x) for x in obj)
else:
return obj
@pytest.mark.optional
def test_nistems():
stems, rate = stempeg.read_stems(stempeg.example_stem_path())
with tmp.NamedTemporaryFile(
delete=False,
suffix='.m4a'
) as tempfile:
stempeg.write_stems(
tempfile.name,
stems,
sample_rate=rate,
writer=stempeg.NIStemsWriter()
)
callArgs = [MP4BOX_PATH]
callArgs.extend(["-dump-udta", "0:stem", tempfile.name])
sp.check_call(callArgs)
root, ext = os.path.splitext(tempfile.name)
udtaFile = root + "_stem.udta"
with open(stempeg.default_metadata()) as f:
d_metadata = json.load(f)
try:
fileObj = codecs.open(udtaFile, encoding="utf-8")
fileObj.seek(8)
l_metadata = json.load(fileObj)
except json.decoder.JSONDecodeError:
with open(udtaFile) as json_file:
l_metadata = json.load(json_file)
assert ordered(l_metadata) == ordered(d_metadata)