forked from marl/jams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sonify.py
155 lines (109 loc) · 4.42 KB
/
test_sonify.py
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
#!/usr/bin/env python
# CREATED:2016-02-11 12:07:58 by Brian McFee <[email protected]>
"""Sonification tests"""
import numpy as np
import pytest
from test_eval import create_hierarchy
import jams
def test_no_sonify():
ann = jams.Annotation(namespace='vector')
with pytest.raises(jams.NamespaceError):
jams.sonify.sonify(ann)
def test_bad_sonify():
ann = jams.Annotation(namespace='chord')
ann.append(time=0, duration=1, value='not a chord')
with pytest.raises(jams.SchemaError):
jams.sonify.sonify(ann)
@pytest.mark.parametrize('ns', ['segment_open', 'chord'])
@pytest.mark.parametrize('sr', [8000, 11025])
@pytest.mark.parametrize('duration', [None, 5.0, 1.0])
def test_duration(ns, sr, duration):
ann = jams.Annotation(namespace=ns)
ann.append(time=3, duration=1, value='C')
y = jams.sonify.sonify(ann, sr=sr, duration=duration)
if duration is not None:
assert len(y) == int(sr * duration)
@pytest.mark.xfail
def test_note_hz():
ann = jams.Annotation(namespace='note_hz')
ann.append(time=0, duration=1, value=261.0)
y = jams.sonify.sonify(ann, sr=8000, duration=2.0)
assert len(y) == 8000 * 2
@pytest.mark.xfail
def test_note_hz_nolength():
ann = jams.Annotation(namespace='note_hz')
ann.append(time=0, duration=1, value=261.0)
y = jams.sonify.sonify(ann, sr=8000)
assert len(y) == 8000 * 1
assert np.any(y)
@pytest.mark.xfail
def test_note_midi():
ann = jams.Annotation(namespace='note_midi')
ann.append(time=0, duration=1, value=60)
y = jams.sonify.sonify(ann, sr=8000, duration=2.0)
assert len(y) == 8000 * 2
@pytest.fixture(scope='module')
def ann_contour():
ann = jams.Annotation(namespace='pitch_contour')
duration = 5.0
fs = 0.01
# Generate a contour with deep vibrato and no voicing from 3s-4s
times = np.linspace(0, duration, num=int(duration / fs))
rate = 5
vibrato = 220 + 20 * np.sin(2 * np.pi * times * rate)
for t, v in zip(times, vibrato):
ann.append(time=t, duration=fs, value={'frequency': v,
'index': 0,
'voiced': (t < 3 or t > 4)})
return ann
@pytest.mark.parametrize('duration', [None, 5.0, 10.0])
@pytest.mark.parametrize('sr', [8000])
def test_contour(ann_contour, duration, sr):
y = jams.sonify.sonify(ann_contour, sr=sr, duration=duration)
if duration is not None:
assert len(y) == sr * duration
@pytest.mark.parametrize('namespace', ['chord', 'chord_harte'])
@pytest.mark.parametrize('sr', [8000])
@pytest.mark.parametrize('duration', [2.0])
@pytest.mark.parametrize('value', ['C:maj/5'])
def test_chord(namespace, sr, duration, value):
ann = jams.Annotation(namespace=namespace)
ann.append(time=0.5, duration=1.0, value=value)
y = jams.sonify.sonify(ann, sr=sr, duration=duration)
assert len(y) == sr * duration
@pytest.mark.parametrize('namespace, value',
[('beat', 1),
('segment_open', 'C'),
('onset', 1)])
@pytest.mark.parametrize('sr', [8000])
@pytest.mark.parametrize('duration', [2.0])
def test_event(namespace, sr, duration, value):
ann = jams.Annotation(namespace=namespace)
ann.append(time=0.5, duration=0, value=value)
y = jams.sonify.sonify(ann, sr=sr, duration=duration)
assert len(y) == sr * duration
@pytest.fixture(scope='module')
def beat_pos_ann():
ann = jams.Annotation(namespace='beat_position')
for i, t in enumerate(np.arange(0, 10, 0.25)):
ann.append(time=t, duration=0,
value=dict(position=1 + i % 4,
measure=1 + i // 4,
num_beats=4,
beat_units=4))
return ann
@pytest.mark.parametrize('sr', [8000])
@pytest.mark.parametrize('duration', [None, 5, 15])
def test_beat_position(beat_pos_ann, sr, duration):
yout = jams.sonify.sonify(beat_pos_ann, sr=sr, duration=duration)
if duration is not None:
assert len(yout) == duration * sr
@pytest.fixture(scope='module')
def ann_hier():
return create_hierarchy(values=['AB', 'abac', 'xxyyxxzz'], duration=30)
@pytest.mark.parametrize('sr', [8000])
@pytest.mark.parametrize('duration', [None, 15, 30])
def test_multi_segment(ann_hier, sr, duration):
y = jams.sonify.sonify(ann_hier, sr=sr, duration=duration)
if duration:
assert len(y) == duration * sr