annotate tests/test_audio.nim @ 1364:8f774d9c2518 default tip

fix: change implementation to allow for easier reload of whole engine
author sam <sam@basx.dev>
date Tue, 05 Nov 2024 23:28:25 +0700
parents df3c075e5dea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
2 import std/sequtils
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
3 import std/times
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
4
1267
4cf9872f7bb6 did: rename back to old name (no v2)
sam <sam@basx.dev>
parents: 1225
diff changeset
5 import ../semicongine
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
6
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
7 proc test1() =
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
8 mixer[].addSound("test1", sineSoundData(1000, 2, 44100))
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
9 mixer[].addSound("test2", sineSoundData(500, 2, 44100))
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
10
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
11 let s1 = mixer[].play("test1", loop = true)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
12 let s2 = mixer[].play("test2", loop = true)
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
13
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
14 let t0 = now()
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
15 mixer[].setLevel(0.5)
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
16 while true:
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
17 let runtime = (now() - t0).inMilliseconds()
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
18 if runtime > 1500:
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
19 mixer[].setLevel(0.2)
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
20 if runtime > 3000:
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
21 mixer[].stop(s2)
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
22 if runtime > 6000:
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
23 mixer[].stop("")
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
24 if runtime > 8000:
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
25 break
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
26
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
27 proc test2() =
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
28 let
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
29 # notes
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
30 c = sineSoundData(261.6256, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
31 d = sineSoundData(293.6648, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
32 e = sineSoundData(329.6276, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
33 f = sineSoundData(349.2282, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
34 g = sineSoundData(391.9954, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
35 a = sineSoundData(440.0000, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
36 b = sineSoundData(493.8833, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
37 bb = sineSoundData(466.1638, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
38 c2 = sineSoundData(523.2511, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
39 d2 = sineSoundData(587.3295, 0.5, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
40 bbShort = sineSoundData(466.1638, 0.25, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
41 c2Short = sineSoundData(523.2511, 0.25, 44100)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
42 d2Short = sineSoundData(587.3295, 0.25, 44100)
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
43
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
44 # song
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
45 frerejaquesData = concat(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1303
diff changeset
46 f, g, a, f, f, g, a, f, a, bb, c2, c2, a, bb, c2, c2, c2Short, d2Short, c2Short,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1303
diff changeset
47 bbShort, a, f, c2Short, d2Short, c2Short, bbShort, a, f, f, c, f, f, f, c, f, f,
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
48 )
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
49
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
50 mixer[].addSound("frerejaques", frerejaquesData)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
51 discard mixer[].play("frerejaques")
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
52
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
53 while mixer[].isPlaying():
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
54 sleep(1)
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
55
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
56 proc test3() =
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
57 mixer[].addSound("toccata et fugue", loadAudio("toccata_et_fugue.ogg"))
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
58 mixer[].addSound("ping", sineSoundData(500, 0.05, 44100))
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
59 mixer[].addTrack("effects")
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
60 discard mixer[].play("toccata et fugue")
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
61
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
62 when isMainModule:
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
63 test1()
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
64 mixer[].stop()
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
65 test2()
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
66 mixer[].stop()
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
67 test3()
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
68
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
69 while mixer[].isPlaying():
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
70 # on windows we re-open stdin and this will not work
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
71 when defined(linux):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1303
diff changeset
72 discard
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1303
diff changeset
73 mixer[].play("ping", track = "effects", stopOtherSounds = true, level = 0.5)
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
74 echo "Press q and enter to exit"
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
75 if stdin.readLine() == "q":
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1267
diff changeset
76 mixer[].stop()
1225
27cd1c21290e did: refactor resources
sam <sam@basx.dev>
parents:
diff changeset
77 elif defined(windows):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1303
diff changeset
78 sleep(1000)