annotate tests/test_audio.nim @ 1028:2ba3f18e7cad

fix: pong example was not working yet
author sam <sam@basx.dev>
date Wed, 22 May 2024 03:52:20 +0700
parents 1ba0e5dfbf68
children 74957cbf589b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
1 import std/os
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/sequtils
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/times
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
4
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
5 import semicongine
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
6
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
7
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
8 proc test1() =
176
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
9 mixer[].addSound("test1", newSound(sineSoundData(1000, 2, 44100)))
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
10 mixer[].addSound("test2", newSound(sineSoundData(500, 2, 44100)))
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
11
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
12
428
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
13 let s1 = mixer[].play("test1", loop = true)
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
14 let s2 = mixer[].play("test2", loop = true)
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
15
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
16 let t0 = now()
394
728426157221 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 316
diff changeset
17 mixer[].setLevel(0.5)
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
18 while true:
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
19 let runtime = (now() - t0).inMilliseconds()
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
20 if runtime > 1500:
394
728426157221 did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents: 316
diff changeset
21 mixer[].setLevel(0.2)
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
22 if runtime > 3000:
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
23 mixer[].stop(s2)
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
24 if runtime > 6000:
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
25 mixer[].stop("")
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
26 if runtime > 8000:
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
27 break
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
28
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
29 proc test2() =
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
30 let
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
31 # notes
176
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
32 c = sineSoundData(261.6256, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
33 d = sineSoundData(293.6648, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
34 e = sineSoundData(329.6276, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
35 f = sineSoundData(349.2282, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
36 g = sineSoundData(391.9954, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
37 a = sineSoundData(440.0000, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
38 b = sineSoundData(493.8833, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
39 bb = sineSoundData(466.1638, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
40 c2 = sineSoundData(523.2511, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
41 d2 = sineSoundData(587.3295, 0.5, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
42 bbShort = sineSoundData(466.1638, 0.25, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
43 c2Short = sineSoundData(523.2511, 0.25, 44100)
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
44 d2Short = sineSoundData(587.3295, 0.25, 44100)
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
45
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
46 # song
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
47 frerejaquesData = concat(
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
48 f, g, a, f,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
49 f, g, a, f,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
50 a, bb, c2, c2,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
51 a, bb, c2, c2,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
52 c2Short, d2Short, c2Short, bbShort, a, f,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
53 c2Short, d2Short, c2Short, bbShort, a, f,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
54 f, c, f, f,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
55 f, c, f, f,
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
56 )
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
57
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
58 mixer[].addSound("frerejaques", newSound(frerejaquesData))
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
59 discard mixer[].play("frerejaques")
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
60
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
61 while mixer[].isPlaying():
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
62 sleep(1)
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
63
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
64 proc test3() =
428
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
65 mixer[].addSound("toccata et fugue", loadAudio("toccata_et_fugue.ogg"))
176
36e10cc04a33 fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents: 171
diff changeset
66 mixer[].addSound("ping", newSound(sineSoundData(500, 0.05, 44100)))
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
67 mixer[].addTrack("effects")
428
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
68 discard mixer[].play("toccata et fugue")
169
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
69
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
70
2a0c81fa541d 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
Sam <sam@basx.dev>
parents:
diff changeset
71 when isMainModule:
171
ceba165f07d8 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 169
diff changeset
72 startMixerThread()
407
ffc265916415 did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents: 394
diff changeset
73 test1()
ffc265916415 did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents: 394
diff changeset
74 mixer[].stop()
ffc265916415 did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents: 394
diff changeset
75 test2()
ffc265916415 did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents: 394
diff changeset
76 mixer[].stop()
428
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
77 test3()
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
78
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
79 while mixer[].isPlaying():
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
80 discard mixer[].play("ping", track = "effects", stopOtherSounds = true, level = 0.5)
980
1ba0e5dfbf68 fix: windows audio test
sam <sam@basx.dev>
parents: 428
diff changeset
81 # on windows we re-open stdin and this will not work
1ba0e5dfbf68 fix: windows audio test
sam <sam@basx.dev>
parents: 428
diff changeset
82 when defined(linux):
1ba0e5dfbf68 fix: windows audio test
sam <sam@basx.dev>
parents: 428
diff changeset
83 echo "Press q and enter to exit"
1ba0e5dfbf68 fix: windows audio test
sam <sam@basx.dev>
parents: 428
diff changeset
84 if stdin.readLine() == "q":
1ba0e5dfbf68 fix: windows audio test
sam <sam@basx.dev>
parents: 428
diff changeset
85 mixer[].stop()
1ba0e5dfbf68 fix: windows audio test
sam <sam@basx.dev>
parents: 428
diff changeset
86 elif defined(windows):
1ba0e5dfbf68 fix: windows audio test
sam <sam@basx.dev>
parents: 428
diff changeset
87 sleep(5)
428
545b4c58601f add: proper sound test, from public domain
Sam <sam@basx.dev>
parents: 407
diff changeset
88 mixer[].stop()