File: test_functions.py

package info (click to toggle)
python-pyo 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,332 kB
  • sloc: python: 135,133; ansic: 127,822; javascript: 16,116; sh: 395; makefile: 388; cpp: 242
file content (297 lines) | stat: -rw-r--r-- 10,035 bytes parent folder | download | duplicates (2)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import math
import pytest
from pyo import *

class TestInitFunctions:

    def test_getPyoKeywords(self):
        kwds = getPyoKeywords()
        assert type(kwds) is list
        assert "PyoObject" in kwds
        assert "SineLoop" in kwds
        assert "Particle" in kwds
        assert len(kwds) == 368

    def test_getPyoExamples(self):
        examples = getPyoExamples(fullpath=True)
        for cat in examples:
            for f in examples[cat]:
                assert os.path.isfile(f)

        root = os.path.split(os.path.split(examples['x01-intro'][0])[0])[0]

        examples = getPyoExamples()
        for cat in examples:
            for f in examples[cat]:
                assert os.path.isfile(os.path.join(root, cat, f))

@pytest.mark.usefixtures("audio_server")
class TestSoundfileFunctions:

    def test_sndinfo(self):
        path = os.path.join(SNDS_PATH, "transparent.aif")
        info = sndinfo(path)
        assert info[0] == 29877  # number of samples
        assert round(info[1], 5) == 0.67748  # duration in sec
        assert info[2] == 44100.0  # sampling rate
        assert info[3] == 1  # number of channels
        assert info[4] == 'AIFF'  # file format
        assert info[5] == '16 bit int'  # sample type

    def test_savefile(self):
        path = os.path.join(os.path.expanduser("~"), "temporary_soundfile.wav")
        sr = 44100
        dur = 1
        samples = [math.sin(math.pi * 2 * i / sr * dur) for i in range(sr)]
        savefile(samples, path, sr=sr, channels=1, fileformat=0, sampletype=3)

        # check saved file header values.
        info = sndinfo(path)
        assert info[0] == 44100  # number of samples
        assert round(info[1], 5) == 1.0  # duration in sec
        assert info[2] == 44100.0  # sampling rate
        assert info[3] == 1  # number of channels
        assert info[4] == 'WAVE'  # file format
        assert info[5] == '32 bit float'  # sample type

        # reload the sound and compare with the original waveform.
        table = SndTable(path)
        assert sr == table.getSize()
        sum = 0
        for i in range(sr):
            sum += samples[i] - table.get(i)
        assert math.isclose(sum, 0.0, abs_tol=0.000000001)

        os.remove(path)

    def test_savefileFromTable(self):
        path = os.path.join(SNDS_PATH, "transparent.aif")
        info = sndinfo(path)
        ori = SndTable(path)

        outpath = os.path.join(os.path.expanduser("~"), "temporary_soundfile.wav")
        savefileFromTable(ori, outpath, fileformat=0, sampletype=3)

        # check saved file header values.
        info2 = sndinfo(outpath)
        assert info[0] == info2[0]  # number of samples
        assert info[1] == info2[1]  # duration in sec
        assert info[2] == info2[2]  # sampling rate
        assert info[3] == info2[3]  # number of channels

        # reload the sound and compare with the original waveform.
        table = SndTable(outpath)
        sum = 0
        for i in range(table.getSize()):
            sum += ori.get(i) - table.get(i)
        assert math.isclose(sum, 0.0, abs_tol=0.000000001)

        os.remove(outpath)

@pytest.mark.usefixtures("audio_server")
class TestResamplingFunctions:

    def test_upsamp(self):
        home = os.path.expanduser('~')
        path = os.path.join(SNDS_PATH, "transparent.aif")
        nsamps = sndinfo(path)[0]
        sr = sndinfo(path)[2]

        # upsample a signal 3 times
        upfile = os.path.join(home, 'trans_upsamp_3.aif')
        upsamp(path, upfile, 3, 256)
        info = sndinfo(upfile)
        assert info[2] == (sr * 3)
        assert info[0] == (nsamps * 3)

        os.remove(upfile)

    def test_downsamp(self):
        home = os.path.expanduser('~')
        path = os.path.join(SNDS_PATH, "transparent.aif")
        nsamps = sndinfo(path)[0]
        sr = sndinfo(path)[2]

        # downsample the upsampled signal 4 times
        downfile = os.path.join(home, 'trans_downsamp_4.aif')
        downsamp(path, downfile, 4, 256)
        info = sndinfo(downfile)
        assert info[2] == (sr / 4)
        assert math.isclose(info[0], nsamps / 4, abs_tol=1.0)

        os.remove(downfile)

@pytest.mark.usefixtures("audio_server")
class TestConversionFunctions:

    def test_midiToHz(self):
        # test with a number as arg
        in_num = 48
        out_num = midiToHz(in_num)
        assert isinstance(out_num, (int, float))
        assert math.isclose(out_num, 130.8128, abs_tol=0.001)

        # test with a tuple as arg
        in_tuple = (48, 52, 55)
        out_tuple = midiToHz(in_tuple)
        assert type(out_tuple) is tuple
        assert math.isclose(out_tuple[0], 130.8128, abs_tol=0.001)
        assert math.isclose(out_tuple[1], 164.8138, abs_tol=0.001)
        assert math.isclose(out_tuple[2], 195.9977, abs_tol=0.001)

        # test with a list as arg
        in_list = [48, 52, 55]
        out_list = midiToHz(in_list)
        assert type(out_list) is list
        assert math.isclose(out_list[0], 130.8128, abs_tol=0.001)
        assert math.isclose(out_list[1], 164.8138, abs_tol=0.001)
        assert math.isclose(out_list[2], 195.9977, abs_tol=0.001)

    def test_midiToTranspo(self):
        # test with a number as arg
        in_num = 60
        out_num = midiToTranspo(in_num)
        assert isinstance(out_num, (int, float))
        assert out_num == 1.0

        # test with a tuple as arg
        in_tuple = (48, 60, 72)
        out_tuple = midiToTranspo(in_tuple)
        assert type(out_tuple) is tuple
        assert math.isclose(out_tuple[0], 0.5, abs_tol=0.00001)
        assert out_tuple[1] == 1.0
        assert math.isclose(out_tuple[2], 2.0, abs_tol=0.00001)

        # test with a list as arg
        in_list = [48, 60, 72]
        out_list = midiToTranspo(in_list)
        assert type(out_list) is list
        assert math.isclose(out_list[0], 0.5, abs_tol=0.00001)
        assert out_list[1] == 1.0
        assert math.isclose(out_list[2], 2.0, abs_tol=0.00001)

    def test_sampsToSec(self):
        # test with a number as arg
        in_num = 48000
        out_num = sampsToSec(in_num)
        assert isinstance(out_num, (int, float))
        assert out_num == 1.0

        # test with a tuple as arg
        in_tuple = (24000, 48000, 96000)
        out_tuple = sampsToSec(in_tuple)
        assert type(out_tuple) is tuple
        assert out_tuple[0] == 0.5
        assert out_tuple[1] == 1.0
        assert out_tuple[2] == 2.0

        # test with a list as arg
        in_list = [24000, 48000, 96000]
        out_list = sampsToSec(in_list)
        assert type(out_list) is list
        assert out_list[0] == 0.5
        assert out_list[1] == 1.0
        assert out_list[2] == 2.0

    def test_secToSamps(self):
        # test with a number as arg
        in_num = 1.0
        out_num = secToSamps(in_num)
        assert isinstance(out_num, (int, float))
        assert out_num == 48000

        # test with a tuple as arg
        in_tuple = (0.5, 1.0, 2.0)
        out_tuple = secToSamps(in_tuple)
        assert type(out_tuple) is tuple
        assert out_tuple[0] == 24000
        assert out_tuple[1] == 48000
        assert out_tuple[2] == 96000

        # test with a list as arg
        in_list = [0.5, 1.0, 2.0]
        out_list = secToSamps(in_list)
        assert type(out_list) is list
        assert out_list[0] == 24000
        assert out_list[1] == 48000
        assert out_list[2] == 96000

    def test_beatToDur(self):
        # test with a number as arg
        bpm = 60
        in_num = 1
        out_num = beatToDur(in_num, bpm)
        assert isinstance(out_num, (int, float))
        assert out_num == 1.0

        # test with a tuple as arg
        in_tuple = (1 / 2, 1, 2)
        out_tuple = beatToDur(in_tuple, bpm)
        assert type(out_tuple) is tuple
        assert out_tuple[0] == 0.5
        assert out_tuple[1] == 1.0
        assert out_tuple[2] == 2.0

        # test with a list as arg
        in_list = [1 / 2, 1, 2]
        out_list = beatToDur(in_list, bpm)
        assert type(out_list) is list
        assert out_list[0] == 0.5
        assert out_list[1] == 1.0
        assert out_list[2] == 2.0

    def test_linToCosCurve(self):
        n = 512
        l = [(0,0), (0.5, 1), (1,0)]
        pts = linToCosCurve(l, yrange=[0, 1], totaldur=1, points=n)
        for i in range(512):
            v = -0.5 * math.cos(2 * math.pi * i / n) + 0.5
            assert math.isclose(pts[i][1], v, abs_tol=0.000001)

        log10ymin = math.log10(0.001)
        log10ymax = math.log10(1.0)
        l = [(0,0.001), (0.5, 1), (1,0.001)]
        pts = linToCosCurve(l, yrange=[0.001, 1], totaldur=1, points=n, log=True)
        for i in range(512):
            v = pow(10, (-0.5 * math.cos(2 * math.pi * i / n) + 0.5) * (log10ymax - log10ymin) + log10ymin)
            assert math.isclose(pts[i][1], v, abs_tol=0.000001)

    def test_rescale(self):
        # test with a number as arg
        in_num = 0.0
        out_num = rescale(in_num, xmin=-1.0, xmax=1.0, ymin=0.0, ymax=1.0, xlog=False, ylog=False)
        assert isinstance(out_num, (int, float))
        assert out_num == 0.5

        log10ymin = math.log10(0.001)
        log10ymax = math.log10(1.0)
        out_num = rescale(in_num, xmin=-1.0, xmax=1.0, ymin=0.001, ymax=1.0, xlog=False, ylog=True)
        assert math.isclose(out_num, pow(10, 0.5 * (log10ymax - log10ymin) + log10ymin), abs_tol=0.000001)

        # How to test xlog=True ?

        # test with a list as arg

#    def test_floatmap(self):
#        pass

#    def test_distanceToSegment(self):
#        pass

#    def test_reducePoints(self):
#        pass

# class TestServerQueriesFunctions:

#     def test_serverCreated(self):
#         assert serverCreated() == False
#         s = Server()
#         assert serverCreated() == True

#     def test_serverBooted(self):
#         s = Server(audio="manual")
#         assert serverBooted() == False
#         s.boot()
#         assert serverBooted() == True
#         s.shutdown()