Mercurial > games > semicongine
comparison tests/test_audio.nim @ 1423:3b8a736c45a7
did: put almost all global state into a single struct
author | sam <sam@basx.dev> |
---|---|
date | Thu, 09 Jan 2025 23:03:47 +0700 |
parents | df3c075e5dea |
children | 676fc13685a9 |
comparison
equal
deleted
inserted
replaced
1422:6f0c1b347403 | 1423:3b8a736c45a7 |
---|---|
3 import std/times | 3 import std/times |
4 | 4 |
5 import ../semicongine | 5 import ../semicongine |
6 | 6 |
7 proc test1() = | 7 proc test1() = |
8 mixer[].addSound("test1", sineSoundData(1000, 2, 44100)) | 8 addSound("test1", sineSoundData(1000, 2, 44100)) |
9 mixer[].addSound("test2", sineSoundData(500, 2, 44100)) | 9 addSound("test2", sineSoundData(500, 2, 44100)) |
10 | 10 |
11 let s1 = mixer[].play("test1", loop = true) | 11 let s1 = play("test1", loop = true) |
12 let s2 = mixer[].play("test2", loop = true) | 12 let s2 = play("test2", loop = true) |
13 | 13 |
14 let t0 = now() | 14 let t0 = now() |
15 mixer[].setLevel(0.5) | 15 setLevel(0.5) |
16 while true: | 16 while true: |
17 let runtime = (now() - t0).inMilliseconds() | 17 let runtime = (now() - t0).inMilliseconds() |
18 if runtime > 1500: | 18 if runtime > 1500: |
19 mixer[].setLevel(0.2) | 19 setLevel(0.2) |
20 if runtime > 3000: | 20 if runtime > 3000: |
21 mixer[].stop(s2) | 21 stop(s2) |
22 if runtime > 6000: | 22 if runtime > 6000: |
23 mixer[].stop("") | 23 stop("") |
24 if runtime > 8000: | 24 if runtime > 8000: |
25 break | 25 break |
26 | 26 |
27 proc test2() = | 27 proc test2() = |
28 let | 28 let |
45 frerejaquesData = concat( | 45 frerejaquesData = concat( |
46 f, g, a, f, f, g, a, f, a, bb, c2, c2, a, bb, c2, c2, c2Short, d2Short, c2Short, | 46 f, g, a, f, f, g, a, f, a, bb, c2, c2, a, bb, c2, c2, c2Short, d2Short, c2Short, |
47 bbShort, a, f, c2Short, d2Short, c2Short, bbShort, a, f, f, c, f, f, f, c, f, f, | 47 bbShort, a, f, c2Short, d2Short, c2Short, bbShort, a, f, f, c, f, f, f, c, f, f, |
48 ) | 48 ) |
49 | 49 |
50 mixer[].addSound("frerejaques", frerejaquesData) | 50 addSound("frerejaques", frerejaquesData) |
51 discard mixer[].play("frerejaques") | 51 discard play("frerejaques") |
52 | 52 |
53 while mixer[].isPlaying(): | 53 while isPlaying(): |
54 sleep(1) | 54 sleep(1) |
55 | 55 |
56 proc test3() = | 56 proc test3() = |
57 mixer[].addSound("toccata et fugue", loadAudio("toccata_et_fugue.ogg")) | 57 addSound("toccata et fugue", loadAudio("toccata_et_fugue.ogg")) |
58 mixer[].addSound("ping", sineSoundData(500, 0.05, 44100)) | 58 addSound("ping", sineSoundData(500, 0.05, 44100)) |
59 mixer[].addTrack("effects") | 59 addTrack("effects") |
60 discard mixer[].play("toccata et fugue") | 60 discard play("toccata et fugue") |
61 | 61 |
62 when isMainModule: | 62 when isMainModule: |
63 initEngine("Test audio") | |
63 test1() | 64 test1() |
64 mixer[].stop() | 65 stop() |
65 test2() | 66 test2() |
66 mixer[].stop() | 67 stop() |
67 test3() | 68 test3() |
68 | 69 |
69 while mixer[].isPlaying(): | 70 while isPlaying(): |
70 # on windows we re-open stdin and this will not work | 71 # on windows we re-open stdin and this will not work |
71 when defined(linux): | 72 when defined(linux): |
72 discard | 73 discard play("ping", track = "effects", stopOtherSounds = true, level = 0.5) |
73 mixer[].play("ping", track = "effects", stopOtherSounds = true, level = 0.5) | |
74 echo "Press q and enter to exit" | 74 echo "Press q and enter to exit" |
75 if stdin.readLine() == "q": | 75 if stdin.readLine() == "q": |
76 mixer[].stop() | 76 stop() |
77 elif defined(windows): | 77 elif defined(windows): |
78 sleep(1000) | 78 sleep(1000) |