Mercurial > games > semicongine
annotate tests/test_audio.nim @ 913:b19faed54b14
did: correct usage of vSync/triple buffering
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 10 Mar 2024 22:06:52 +0700 |
parents | a2736295c3f6 |
children | 1ba0e5dfbf68 |
rev | line source |
---|---|
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
1 import std/os |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/sequtils |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/times |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import semicongine |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
7 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 proc test1() = |
637
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
9 mixer[].addSound("test1", newSound(sineSoundData(1000, 2, 44100))) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
10 mixer[].addSound("test2", newSound(sineSoundData(500, 2, 44100))) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
889
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
13 let s1 = mixer[].play("test1", loop = true) |
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
14 let s2 = mixer[].play("test2", loop = true) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 let t0 = now() |
854
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
17 mixer[].setLevel(0.5) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 while true: |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 let runtime = (now() - t0).inMilliseconds() |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 if runtime > 1500: |
854
48a2ac8bec07
did: some more audio functionality, some unfinished grid-mesh code
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
21 mixer[].setLevel(0.2) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 if runtime > 3000: |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
23 mixer[].stop(s2) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 if runtime > 6000: |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
25 mixer[].stop("") |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 if runtime > 8000: |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 break |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 proc test2() = |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 let |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 # notes |
637
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
32 c = sineSoundData(261.6256, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
33 d = sineSoundData(293.6648, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
34 e = sineSoundData(329.6276, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
35 f = sineSoundData(349.2282, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
36 g = sineSoundData(391.9954, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
37 a = sineSoundData(440.0000, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
38 b = sineSoundData(493.8833, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
39 bb = sineSoundData(466.1638, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
40 c2 = sineSoundData(523.2511, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
41 d2 = sineSoundData(587.3295, 0.5, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
42 bbShort = sineSoundData(466.1638, 0.25, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
43 c2Short = sineSoundData(523.2511, 0.25, 44100) |
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
44 d2Short = sineSoundData(587.3295, 0.25, 44100) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 # song |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 frerejaquesData = concat( |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 f, g, a, f, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 f, g, a, f, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 a, bb, c2, c2, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 a, bb, c2, c2, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 c2Short, d2Short, c2Short, bbShort, a, f, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 c2Short, d2Short, c2Short, bbShort, a, f, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 f, c, f, f, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 f, c, f, f, |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 ) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
58 mixer[].addSound("frerejaques", newSound(frerejaquesData)) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
59 discard mixer[].play("frerejaques") |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
61 while mixer[].isPlaying(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
62 sleep(1) |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 proc test3() = |
889
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
65 mixer[].addSound("toccata et fugue", loadAudio("toccata_et_fugue.ogg")) |
637
bb6857da8113
fix: bad audio buffer handling, reduce latency (unbearable on windows)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
66 mixer[].addSound("ping", newSound(sineSoundData(500, 0.05, 44100))) |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
67 mixer[].addTrack("effects") |
889
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
68 discard mixer[].play("toccata et fugue") |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 |
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
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 when isMainModule: |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
630
diff
changeset
|
72 startMixerThread() |
868
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
854
diff
changeset
|
73 test1() |
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
854
diff
changeset
|
74 mixer[].stop() |
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
854
diff
changeset
|
75 test2() |
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
854
diff
changeset
|
76 mixer[].stop() |
889
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
77 test3() |
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
78 |
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
79 while mixer[].isPlaying(): |
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
80 discard mixer[].play("ping", track = "effects", stopOtherSounds = true, level = 0.5) |
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
81 echo "Press q and enter to exit" |
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
82 if stdin.readLine() == "q": |
a2736295c3f6
add: proper sound test, from public domain
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
83 mixer[].stop() |