-
Notifications
You must be signed in to change notification settings - Fork 206
/
test_features_chords.py
136 lines (108 loc) · 5.5 KB
/
test_features_chords.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
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the madmom.features.chords module.
"""
from __future__ import absolute_import, division, print_function
import unittest
from os.path import join as pj
from madmom.features import Activations
from madmom.features.chords import *
from madmom.io import load_chords
from . import ACTIVATIONS_PATH, AUDIO_PATH, DETECTIONS_PATH
sample_files = [pj(AUDIO_PATH, sf) for sf in ['sample.wav', 'sample2.wav']]
sample_cnn_acts = [Activations(pj(ACTIVATIONS_PATH, af))
for af in ['sample.cnn_chord_features.npz',
'sample2.cnn_chord_features.npz']]
sample_cnn_labels = [load_chords(pj(DETECTIONS_PATH, df))
for df in ['sample.cnn_chord_recognition.txt',
'sample2.cnn_chord_recognition.txt']]
sample_deep_chroma_acts = [Activations(pj(ACTIVATIONS_PATH, af))
for af in ['sample.deep_chroma.npz',
'sample2.deep_chroma.npz']]
sample_deep_chroma_labels = [load_chords(pj(DETECTIONS_PATH, df))
for df in ['sample.dc_chord_recognition.txt',
'sample2.dc_chord_recognition.txt']]
def _compare_labels(test_case, labels, reference_labels):
test_case.assertTrue(
np.allclose(labels['start'], reference_labels['start']))
test_case.assertTrue(np.allclose(labels['end'], reference_labels['end']))
test_case.assertTrue((labels['label'] == reference_labels['label']).all())
class TestParseChords(unittest.TestCase):
def test_read_chord_annotations(self):
chords = load_chords(pj(DETECTIONS_PATH,
'sample2.dc_chord_recognition.txt'))
_compare_labels(self, chords,
np.array([(0.0, 1.6, 'F:maj'),
(1.6, 2.5, 'A:maj'),
(2.5, 4.1, 'D:maj')],
dtype=SEGMENT_DTYPE))
chords = load_chords(pj(DETECTIONS_PATH,
'sample.dc_chord_recognition.txt'))
_compare_labels(self, chords,
np.array([(0.0, 2.9, 'G#:maj')],
dtype=SEGMENT_DTYPE))
class TestMajMinTargetsToChordLabelsFunction(unittest.TestCase):
def test_all_labels(self):
fps = 10.
targets = range(25)
target_labels = np.array([(0.0, 0.1, 'A:maj'),
(0.1, 0.2, 'A#:maj'),
(0.2, 0.3, 'B:maj'),
(0.3, 0.4, 'C:maj'),
(0.4, 0.5, 'C#:maj'),
(0.5, 0.6, 'D:maj'),
(0.6, 0.7, 'D#:maj'),
(0.7, 0.8, 'E:maj'),
(0.8, 0.9, 'F:maj'),
(0.9, 1.0, 'F#:maj'),
(1.0, 1.1, 'G:maj'),
(1.1, 1.2, 'G#:maj'),
(1.2, 1.3, 'A:min'),
(1.3, 1.4, 'A#:min'),
(1.4, 1.5, 'B:min'),
(1.5, 1.6, 'C:min'),
(1.6, 1.7, 'C#:min'),
(1.7, 1.8, 'D:min'),
(1.8, 1.9, 'D#:min'),
(1.9, 2.0, 'E:min'),
(2.0, 2.1, 'F:min'),
(2.1, 2.2, 'F#:min'),
(2.2, 2.3, 'G:min'),
(2.3, 2.4, 'G#:min'),
(2.4, 2.5, 'N')],
dtype=SEGMENT_DTYPE)
labels = majmin_targets_to_chord_labels(targets, fps)
_compare_labels(self, labels, target_labels)
def test_frame_join(self):
fps = 10.
targets = [0, 0, 4, 4, 4, 4, 24, 8, 8]
target_labels = np.array([(0.0, 0.2, 'A:maj'),
(0.2, 0.6, 'C#:maj'),
(0.6, 0.7, 'N'),
(0.7, 0.9, 'F:maj')],
dtype=SEGMENT_DTYPE)
labels = majmin_targets_to_chord_labels(targets, fps)
_compare_labels(self, labels, target_labels)
class TestCNNChordFeatureProcessorClass(unittest.TestCase):
def setUp(self):
self.processor = CNNChordFeatureProcessor()
def test_process(self):
for audio_file, true_activation in zip(sample_files, sample_cnn_acts):
act = self.processor(audio_file)
self.assertTrue(np.allclose(act, true_activation, rtol=1e-4))
class TestCRFChordRecognitionProcessorClass(unittest.TestCase):
def setUp(self):
self.processor = CRFChordRecognitionProcessor()
def test_process(self):
for activation, true_labels in zip(sample_cnn_acts, sample_cnn_labels):
labels = self.processor(activation)
_compare_labels(self, labels, true_labels)
class TestDeepChromaChordRecognitionProcessorClass(unittest.TestCase):
def setUp(self):
self.processor = DeepChromaChordRecognitionProcessor()
def test_process(self):
for activation, true_labels in zip(sample_deep_chroma_acts,
sample_deep_chroma_labels):
labels = self.processor(activation)
_compare_labels(self, labels, true_labels)