comparison tests/test_audio.nim @ 630:a68941309f22

add: audio test, be aware that the file tests/audiotest.PCM.s16le.48000.2 needs to be manually generated and placed in order for the test to play it successfully
author Sam <sam@basx.dev>
date Mon, 01 May 2023 01:16:59 +0700
parents
children ceba165f07d8
comparison
equal deleted inserted replaced
629:b965f22fc9c3 630:a68941309f22
1 import std/sequtils
2 import std/times
3
4 import semicongine
5
6 proc test1() =
7 var mixer = initMixer()
8 mixer.addSound("test1", newSound(sineSoundData(1000, 2)))
9 mixer.addSound("test2", newSound(sineSoundData(500, 2)))
10
11
12 let s1 = mixer.play("test1", loop=true)
13 let s2 = mixer.play("test2", loop=true)
14
15 let t0 = now()
16 while true:
17 mixer.updateSoundBuffer()
18 let runtime = (now() - t0).inMilliseconds()
19 if runtime > 1500:
20 mixer.setLevel(0.1)
21 if runtime > 3000:
22 mixer.stop(s2)
23 if runtime > 6000:
24 mixer.stop("")
25 if runtime > 8000:
26 mixer.stop()
27 break
28 mixer.destroy()
29
30 proc test2() =
31 let
32 # notes
33 c = sineSoundData(261.6256, 0.5)
34 d = sineSoundData(293.6648, 0.5)
35 e = sineSoundData(329.6276, 0.5)
36 f = sineSoundData(349.2282, 0.5)
37 g = sineSoundData(391.9954, 0.5)
38 a = sineSoundData(440.0000, 0.5)
39 b = sineSoundData(493.8833, 0.5)
40 bb = sineSoundData(466.1638, 0.5)
41 c2 = sineSoundData(523.2511, 0.5)
42 d2 = sineSoundData(587.3295, 0.5)
43 bbShort = sineSoundData(466.1638, 0.25)
44 c2Short = sineSoundData(523.2511, 0.25)
45 d2Short = sineSoundData(587.3295, 0.25)
46
47 # song
48 frerejaquesData = concat(
49 f, g, a, f,
50 f, g, a, f,
51 a, bb, c2, c2,
52 a, bb, c2, c2,
53 c2Short, d2Short, c2Short, bbShort, a, f,
54 c2Short, d2Short, c2Short, bbShort, a, f,
55 f, c, f, f,
56 f, c, f, f,
57 )
58
59 var mixer = initMixer()
60 mixer.addSound("frerejaques", newSound(frerejaquesData))
61 discard mixer.play("frerejaques", loop=true)
62
63 let t0 = now()
64 while true:
65 mixer.updateSoundBuffer()
66 if (now() - t0).inMilliseconds() > 20000:
67 break
68 mixer.destroy()
69
70 proc test3() =
71
72 var song: SoundData
73 var f = open("tests/audiotest.PCM.s16le.48000.2")
74 var readLen = 999
75 while readLen > 0:
76 var sample: Sample
77 readLen = f.readBuffer(addr sample, sizeof(Sample))
78 song.add sample
79
80 var mixer = initMixer()
81 mixer.addSound("pianosong", newSound(song))
82 discard mixer.play("pianosong", loop=true)
83
84 let t0 = now()
85 while true:
86 mixer.updateSoundBuffer()
87 if (now() - t0).inMilliseconds() > 190_000:
88 break
89 mixer.destroy()
90
91 when isMainModule:
92 test1()
93 test2()
94 test3()