comparison tests/test_audio.nim @ 1225:27cd1c21290e compiletime-tests

did: refactor resources
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 22:20:59 +0700
parents
children 4cf9872f7bb6
comparison
equal deleted inserted replaced
1224:a3fa15c25026 1225:27cd1c21290e
1 import std/os
2 import std/sequtils
3 import std/times
4
5 import ../semiconginev2
6
7
8 proc test1() =
9 mixer[].AddSound("test1", SineSoundData(1000, 2, 44100))
10 mixer[].AddSound("test2", SineSoundData(500, 2, 44100))
11
12
13 let s1 = mixer[].Play("test1", loop = true)
14 let s2 = mixer[].Play("test2", loop = true)
15
16 let t0 = now()
17 mixer[].SetLevel(0.5)
18 while true:
19 let runtime = (now() - t0).inMilliseconds()
20 if runtime > 1500:
21 mixer[].SetLevel(0.2)
22 if runtime > 3000:
23 mixer[].Stop(s2)
24 if runtime > 6000:
25 mixer[].Stop("")
26 if runtime > 8000:
27 break
28
29 proc test2() =
30 let
31 # notes
32 c = SineSoundData(261.6256, 0.5, 44100)
33 d = SineSoundData(293.6648, 0.5, 44100)
34 e = SineSoundData(329.6276, 0.5, 44100)
35 f = SineSoundData(349.2282, 0.5, 44100)
36 g = SineSoundData(391.9954, 0.5, 44100)
37 a = SineSoundData(440.0000, 0.5, 44100)
38 b = SineSoundData(493.8833, 0.5, 44100)
39 bb = SineSoundData(466.1638, 0.5, 44100)
40 c2 = SineSoundData(523.2511, 0.5, 44100)
41 d2 = SineSoundData(587.3295, 0.5, 44100)
42 bbShort = SineSoundData(466.1638, 0.25, 44100)
43 c2Short = SineSoundData(523.2511, 0.25, 44100)
44 d2Short = SineSoundData(587.3295, 0.25, 44100)
45
46 # song
47 frerejaquesData = concat(
48 f, g, a, f,
49 f, g, a, f,
50 a, bb, c2, c2,
51 a, bb, c2, c2,
52 c2Short, d2Short, c2Short, bbShort, a, f,
53 c2Short, d2Short, c2Short, bbShort, a, f,
54 f, c, f, f,
55 f, c, f, f,
56 )
57
58 mixer[].AddSound("frerejaques", frerejaquesData)
59 discard mixer[].Play("frerejaques")
60
61 while mixer[].IsPlaying():
62 sleep(1)
63
64 proc test3() =
65 mixer[].AddSound("toccata et fugue", LoadAudio("toccata_et_fugue.ogg"))
66 mixer[].AddSound("ping", SineSoundData(500, 0.05, 44100))
67 mixer[].AddTrack("effects")
68 discard mixer[].Play("toccata et fugue")
69
70
71 when isMainModule:
72 test1()
73 mixer[].Stop()
74 test2()
75 mixer[].Stop()
76 test3()
77
78 while mixer[].IsPlaying():
79 discard mixer[].Play("ping", track = "effects", stopOtherSounds = true, level = 0.5)
80 # on windows we re-open stdin and this will not work
81 when defined(linux):
82 echo "Press q and enter to exit"
83 if stdin.readLine() == "q":
84 mixer[].Stop()
85 elif defined(windows):
86 sleep(5)
87 mixer[].Stop()