summaryrefslogtreecommitdiff
path: root/tests/test_peak_detection.py
blob: b1f9ff40c00c2b0e7f415f45af3deffec2698725 (plain)
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
import os
import json
import simpl
import simpl.peak_detection as peak_detection

PeakDetection = peak_detection.PeakDetection
SMSPeakDetection = peak_detection.SMSPeakDetection
SndObjPeakDetection = peak_detection.SndObjPeakDetection

float_precision = 5
hop_size = 512
max_peaks = 10
num_frames = 30
num_samples = num_frames * hop_size
audio_path = os.path.join(
    os.path.dirname(__file__), 'audio/flute.wav'
)
test_data_path = os.path.join(
    os.path.dirname(__file__), 'libsms_test_data.json'
)


def _load_libsms_test_data():
    test_data = None
    with open(test_data_path, 'r') as f:
        test_data = json.loads(f.read())
    return test_data


class TestPeakDetection(object):
    @classmethod
    def setup_class(cls):
        cls.audio = simpl.read_wav(audio_path)[0]
        cls.audio = cls.audio[0:num_samples]

    def test_basic(self):
        pd = PeakDetection()
        pd.max_peaks = max_peaks
        pd.find_peaks(self.audio)

        assert len(pd.frames) == num_frames
        assert len(pd.frames[0].peaks) == 0
        assert pd.frames[0].max_peaks == max_peaks


class TestSMSPeakDetection(object):
    @classmethod
    def setup_class(cls):
        cls.audio = simpl.read_wav(audio_path)[0]
        cls.test_data = _load_libsms_test_data()

    def test_basic(self):
        pd = SMSPeakDetection()
        pd.hop_size = hop_size
        pd.frame_size = hop_size
        pd.static_frame_size = True
        pd.find_peaks(self.audio)

        assert len(pd.frames) == \
            ((len(self.audio) - pd.frame_size) / hop_size) + 1
        assert len(pd.frames[0].peaks)

    def test_size_next_read(self):
        audio, sampling_rate = simpl.read_wav(audio_path)

        pd = SMSPeakDetection()
        pd.hop_size = hop_size
        pd.max_peaks = max_peaks

        sizes = self.test_data['size_next_read']
        frames = pd.find_peaks(audio[0:num_samples])

        for i, frame in enumerate(frames):
            assert sizes[i] == frame.size, (sizes[i], frame.size)

    def test_peak_detection(self):
        audio, sampling_rate = simpl.read_wav(audio_path)

        pd = SMSPeakDetection()
        pd.hop_size = hop_size
        pd.max_peaks = max_peaks
        frames = pd.find_peaks(audio[0:num_samples])

        sms_frames = self.test_data['peak_detection']
        sms_frames = [f for f in sms_frames if f['status'] != 0]

        # assert len(sms_frames) == len(frames)

        for frame in frames:
            assert len(frame.peaks) <= max_peaks, len(frame.peaks)
            max_amp = max([p.amplitude for p in frame.peaks])
            assert max_amp


class TestSndObjPeakDetection(object):
    @classmethod
    def setup_class(cls):
        cls.audio = simpl.read_wav(audio_path)[0]
        cls.audio = cls.audio[len(cls.audio) / 2:
                              (len(cls.audio) / 2) + num_samples]

    def test_peak_detection(self):
        pd = SndObjPeakDetection()
        pd.hop_size = hop_size
        pd.max_peaks = max_peaks
        frames = pd.find_peaks(self.audio)

        assert len(frames) == num_frames

        # last 4 frames could be empty (with default frame size of 2048
        # and hop size of 512) so ignore
        frames = frames[0:len(frames) - 4]

        for frame in frames:
            assert len(frame.peaks) <= max_peaks, len(frame.peaks)
            max_amp = max([p.amplitude for p in frame.peaks])
            assert max_amp