comparison tests/test_audio.nim @ 171:ceba165f07d8

add: audio subsystem, windows backend still missing
author Sam <sam@basx.dev>
date Mon, 01 May 2023 23:55:07 +0700
parents 2a0c81fa541d
children 36e10cc04a33
comparison
equal deleted inserted replaced
170:c1a9a988b9e3 171:ceba165f07d8
1 import std/os
1 import std/sequtils 2 import std/sequtils
2 import std/times 3 import std/times
3 4
4 import semicongine 5 import semicongine
5 6
7
6 proc test1() = 8 proc test1() =
7 var mixer = initMixer() 9 mixer[].addSound("test1", newSound(sineSoundData(1000, 2)))
8 mixer.addSound("test1", newSound(sineSoundData(1000, 2))) 10 mixer[].addSound("test2", newSound(sineSoundData(500, 2)))
9 mixer.addSound("test2", newSound(sineSoundData(500, 2)))
10 11
11 12
12 let s1 = mixer.play("test1", loop=true) 13 let s1 = mixer[].play("test1", loop=true)
13 let s2 = mixer.play("test2", loop=true) 14 let s2 = mixer[].play("test2", loop=true)
14 15
15 let t0 = now() 16 let t0 = now()
16 while true: 17 while true:
17 mixer.updateSoundBuffer()
18 let runtime = (now() - t0).inMilliseconds() 18 let runtime = (now() - t0).inMilliseconds()
19 if runtime > 1500: 19 if runtime > 1500:
20 mixer.setLevel(0.1) 20 mixer[].setLevel(0.1)
21 if runtime > 3000: 21 if runtime > 3000:
22 mixer.stop(s2) 22 mixer[].stop(s2)
23 if runtime > 6000: 23 if runtime > 6000:
24 mixer.stop("") 24 mixer[].stop("")
25 if runtime > 8000: 25 if runtime > 8000:
26 mixer.stop()
27 break 26 break
28 mixer.destroy()
29 27
30 proc test2() = 28 proc test2() =
31 let 29 let
32 # notes 30 # notes
33 c = sineSoundData(261.6256, 0.5) 31 c = sineSoundData(261.6256, 0.5)
54 c2Short, d2Short, c2Short, bbShort, a, f, 52 c2Short, d2Short, c2Short, bbShort, a, f,
55 f, c, f, f, 53 f, c, f, f,
56 f, c, f, f, 54 f, c, f, f,
57 ) 55 )
58 56
59 var mixer = initMixer() 57 mixer[].addSound("frerejaques", newSound(frerejaquesData))
60 mixer.addSound("frerejaques", newSound(frerejaquesData)) 58 discard mixer[].play("frerejaques")
61 discard mixer.play("frerejaques", loop=true)
62 59
63 let t0 = now() 60 while mixer[].isPlaying():
64 while true: 61 sleep(1)
65 mixer.updateSoundBuffer()
66 if (now() - t0).inMilliseconds() > 20000:
67 break
68 mixer.destroy()
69 62
70 proc test3() = 63 proc test3() =
71
72 var song: SoundData 64 var song: SoundData
73 var f = open("tests/audiotest.PCM.s16le.48000.2") 65 var f = open("tests/audiotest.PCM.s16le.48000.2")
74 var readLen = 999 66 var readLen = 999
75 while readLen > 0: 67 while readLen > 0:
76 var sample: Sample 68 var sample: Sample
77 readLen = f.readBuffer(addr sample, sizeof(Sample)) 69 readLen = f.readBuffer(addr sample, sizeof(Sample))
78 song.add sample 70 song.add sample
79 71
80 var mixer = initMixer() 72 mixer[].addSound("pianosong", newSound(song))
81 mixer.addSound("pianosong", newSound(song)) 73 mixer[].addSound("ping", newSound(sineSoundData(500, 0.05)))
82 discard mixer.play("pianosong", loop=true) 74 mixer[].addTrack("effects")
75 discard mixer[].play("pianosong")
83 76
84 let t0 = now() 77 let t0 = now()
85 while true: 78 while mixer[].isPlaying():
86 mixer.updateSoundBuffer() 79 discard mixer[].play("ping", track="effects", stopOtherSounds=true, level=0.5)
87 if (now() - t0).inMilliseconds() > 190_000: 80 var input = stdin.readLine()
88 break
89 mixer.destroy()
90 81
91 when isMainModule: 82 when isMainModule:
83 startMixerThread()
92 test1() 84 test1()
85 mixer[].stop()
93 test2() 86 test2()
87 mixer[].stop()
94 test3() 88 test3()
89 mixer[].stop()