summaryrefslogtreecommitdiff
path: root/tests/test_synthesis.py
blob: aed655645162cb9c080b3b8afeefbc8b4896d8d4 (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
118
119
120
121
122
123
124
125
126
127
128
import os
from nose.tools import assert_almost_equals
import simpl
import simpl.peak_detection as peak_detection
import simpl.partial_tracking as partial_tracking
import simpl.synthesis as synthesis

float_precision = 2
frame_size = 512
hop_size = 512
max_peaks = 10
max_partials = 10
num_frames = 30
num_samples = num_frames * hop_size
audio_path = os.path.join(
    os.path.dirname(__file__), 'audio/flute.wav'
)
libsms_harmonic_synthesis_ifft_path = os.path.join(
    os.path.dirname(__file__), 'libsms_harmonic_synthesis_ifft.wav'
)
libsms_harmonic_synthesis_sin_path = os.path.join(
    os.path.dirname(__file__), 'libsms_harmonic_synthesis_sin.wav'
)

PeakDetection = peak_detection.PeakDetection
SMSPeakDetection = peak_detection.SMSPeakDetection
PartialTracking = partial_tracking.PartialTracking
SMSPartialTracking = partial_tracking.SMSPartialTracking
Synthesis = synthesis.Synthesis
SMSSynthesis = synthesis.SMSSynthesis


class TestSynthesis(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.hop_size = hop_size
        frames = pd.find_peaks(self.audio)

        pt = PartialTracking()
        pt.max_partials = max_partials
        frames = pt.find_partials(frames)

        s = Synthesis()
        s.hop_size = hop_size
        synth_audio = s.synth(frames)

        assert len(synth_audio) == len(self.audio),\
            (len(synth_audio), len(self.audio))


class TestSMSSynthesis(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 = SMSPeakDetection()
        pd.hop_size = hop_size
        frames = pd.find_peaks(self.audio)

        pt = SMSPartialTracking()
        pt.max_partials = max_partials
        frames = pt.find_partials(frames)

        s = SMSSynthesis()
        s.hop_size = hop_size
        synth_audio = s.synth(frames)

        assert len(synth_audio) == len(self.audio),\
            (len(synth_audio), len(self.audio))

    def test_harmonic_synthesis_ifft(self):
        pd = SMSPeakDetection()
        pd.hop_size = hop_size
        frames = pd.find_peaks(self.audio)

        pt = SMSPartialTracking()
        pt.max_partials = max_partials
        frames = pt.find_partials(frames)

        synth = SMSSynthesis()
        synth.hop_size = hop_size
        synth.max_partials = max_partials
        synth.det_synthesis_type = SMSSynthesis.SMS_DET_IFFT
        synth_audio = synth.synth(frames)

        assert len(synth_audio) == len(self.audio)

        sms_audio, sampling_rate = simpl.read_wav(
            libsms_harmonic_synthesis_ifft_path
        )

        assert len(synth_audio) == len(sms_audio)

        for i in range(len(synth_audio)):
            assert_almost_equals(synth_audio[i], sms_audio[i], float_precision)

    def test_harmonic_synthesis_sin(self):
        pd = SMSPeakDetection()
        pd.hop_size = hop_size
        frames = pd.find_peaks(self.audio)

        pt = SMSPartialTracking()
        pt.max_partials = max_partials
        frames = pt.find_partials(frames)

        synth = SMSSynthesis()
        synth.hop_size = hop_size
        synth.max_partials = max_partials
        synth.det_synthesis_type = SMSSynthesis.SMS_DET_SIN
        synth_audio = synth.synth(frames)

        assert len(synth_audio) == len(self.audio)

        sms_audio, sampling_rate = simpl.read_wav(
            libsms_harmonic_synthesis_sin_path
        )

        assert len(synth_audio) == len(sms_audio)

        for i in range(len(synth_audio)):
            assert_almost_equals(synth_audio[i], sms_audio[i], float_precision)