annotate semiconginev2/audio/mixer.nim @ 1224:a3fa15c25026 compiletime-tests

did: cleanup, add audio, change platform-dependent structure
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 22:02:11 +0700
parents
children 841e12f33c47
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1224
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
1
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
2 const NBUFFERS = 32
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
3 const BUFFERSAMPLECOUNT = 256
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
4 const AUDIO_SAMPLE_RATE* = 44100
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
5
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
6 type
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
7 Level* = 0'f .. 1'f
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
8 Sample* = array[2, int16]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
9 SoundData* = seq[Sample]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
10
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
11 Playback = object
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
12 sound: SoundData
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
13 position: int
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
14 loop: bool
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
15 levelLeft: Level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
16 levelRight: Level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
17 paused: bool
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
18 Track = object
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
19 playing: Table[uint64, Playback]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
20 level: Level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
21 targetLevel: Level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
22 fadeTime: float
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
23 fadeStep: float
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
24
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
25 when defined(windows):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
26 include ./platform/windows
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
27 when defined(linux):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
28 include ./platform/linux
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
29
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
30 type
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
31 Mixer* = object
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
32 playbackCounter: uint64
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
33 tracks: Table[string, Track]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
34 sounds*: Table[string, SoundData]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
35 level: Level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
36 device: NativeSoundDevice
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
37 lock: Lock
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
38 buffers: seq[SoundData]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
39 currentBuffer: int
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
40 lastUpdate: MonoTime
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
41
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
42 proc initMixer(): Mixer =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
43 result = Mixer(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
44 tracks: {"": Track(level: 1'f)}.toTable,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
45 level: 1'f,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
46 )
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
47 result.lock.initLock()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
48
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
49 proc setupDevice(mixer: var Mixer) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
50 # call this inside audio thread
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
51 var bufferaddresses: seq[ptr SoundData]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
52 for i in 0 ..< NBUFFERS:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
53 mixer.buffers.add newSeq[Sample](BUFFERSAMPLECOUNT)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
54 for i in 0 ..< mixer.buffers.len:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
55 bufferaddresses.add (addr mixer.buffers[i])
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
56 mixer.device = OpenSoundDevice(AUDIO_SAMPLE_RATE, bufferaddresses)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
57
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
58 # TODO: this should probably be in the load-code-stuff
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
59 # proc LoadSound*(mixer: var Mixer, name: string, resource: string) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
60 # assert not (name in mixer.sounds)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
61 # mixer.sounds[name] = LoadAudio(resource)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
62
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
63 proc AddSound*(mixer: var Mixer, name: string, sound: SoundData) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
64 assert not (name in mixer.sounds)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
65 mixer.sounds[name] = sound
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
66
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
67 proc ReplaceSound*(mixer: var Mixer, name: string, sound: SoundData) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
68 assert (name in mixer.sounds)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
69 mixer.sounds[name] = sound
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
70
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
71 proc AddTrack*(mixer: var Mixer, name: string, level: Level = 1'f) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
72 assert not (name in mixer.tracks)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
73 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
74 mixer.tracks[name] = Track(level: level)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
75
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
76 proc Play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, levelLeft, levelRight: Level): uint64 =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
77 assert track in mixer.tracks, &"Track '{track}' does not exists"
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
78 assert soundName in mixer.sounds, soundName & " not loaded"
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
79 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
80 if stopOtherSounds:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
81 mixer.tracks[track].playing.clear()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
82 mixer.tracks[track].playing[mixer.playbackCounter] = Playback(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
83 sound: mixer.sounds[soundName],
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
84 position: 0,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
85 loop: loop,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
86 levelLeft: levelLeft,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
87 levelRight: levelRight,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
88 paused: false,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
89 )
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
90 result = mixer.playbackCounter
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
91 inc mixer.playbackCounter
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
92
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
93 proc Play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, level: Level = 1'f): uint64 =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
94 Play(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
95 mixer = mixer,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
96 soundName = soundName,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
97 track = track,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
98 stopOtherSounds = stopOtherSounds,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
99 loop = loop,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
100 levelLeft = level,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
101 levelRight = level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
102 )
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
103
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
104 proc Stop*(mixer: var Mixer) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
105 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
106 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
107 track.playing.clear()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
108
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
109 proc GetLevel*(mixer: var Mixer): Level = mixer.level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
110 proc GetLevel*(mixer: var Mixer, track: string): Level = mixer.tracks[track].level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
111 proc GetLevel*(mixer: var Mixer, playbackId: uint64): (Level, Level) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
112 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
113 if playbackId in track.playing:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
114 return (track.playing[playbackId].levelLeft, track.playing[playbackId].levelRight)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
115
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
116 proc SetLevel*(mixer: var Mixer, level: Level) = mixer.level = level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
117 proc SetLevel*(mixer: var Mixer, track: string, level: Level) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
118 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
119 mixer.tracks[track].level = level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
120 proc SetLevel*(mixer: var Mixer, playbackId: uint64, levelLeft, levelRight: Level) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
121 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
122 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
123 if playbackId in track.playing:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
124 track.playing[playbackId].levelLeft = levelLeft
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
125 track.playing[playbackId].levelRight = levelRight
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
126 proc SetLevel*(mixer: var Mixer, playbackId: uint64, level: Level) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
127 SetLevel(mixer, playbackId, level, level)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
128
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
129 proc Stop*(mixer: var Mixer, track: string) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
130 assert track in mixer.tracks
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
131 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
132 mixer.tracks[track].playing.clear()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
133
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
134 proc Stop*(mixer: var Mixer, playbackId: uint64) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
135 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
136 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
137 if playbackId in track.playing:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
138 track.playing.del(playbackId)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
139 break
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
140
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
141 proc Pause*(mixer: var Mixer, value: bool) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
142 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
143 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
144 for playback in track.playing.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
145 playback.paused = value
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
146
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
147 proc Pause*(mixer: var Mixer, track: string, value: bool) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
148 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
149 for playback in mixer.tracks[track].playing.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
150 playback.paused = value
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
151
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
152 proc Pause*(mixer: var Mixer, playbackId: uint64, value: bool) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
153 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
154 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
155 if playbackId in track.playing:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
156 track.playing[playbackId].paused = value
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
157
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
158 proc Pause*(mixer: var Mixer) = mixer.Pause(true)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
159 proc Pause*(mixer: var Mixer, track: string) = mixer.Pause(track, true)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
160 proc Pause*(mixer: var Mixer, playbackId: uint64) = mixer.Pause(playbackId, true)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
161 proc Unpause*(mixer: var Mixer) = mixer.Pause(false)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
162 proc Unpause*(mixer: var Mixer, track: string) = mixer.Pause(track, false)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
163 proc Unpause*(mixer: var Mixer, playbackId: uint64) = mixer.Pause(playbackId, false)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
164
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
165 proc FadeTo*(mixer: var Mixer, track: string, level: Level, time: float) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
166 mixer.tracks[track].targetLevel = level
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
167 mixer.tracks[track].fadeTime = time
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
168 mixer.tracks[track].fadeStep = level.float - mixer.tracks[track].level.float / time
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
169
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
170 proc IsPlaying*(mixer: var Mixer): bool =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
171 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
172 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
173 for playback in track.playing.values:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
174 if not playback.paused:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
175 return true
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
176 return false
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
177
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
178 proc IsPlaying*(mixer: var Mixer, track: string): bool =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
179 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
180 if mixer.tracks.contains(track):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
181 for playback in mixer.tracks[track].playing.values:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
182 if not playback.paused:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
183 return true
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
184 return false
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
185
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
186 func applyLevel(sample: Sample, levelLeft, levelRight: Level): Sample =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
187 [int16(float(sample[0]) * levelLeft), int16(float(sample[1]) * levelRight)]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
188
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
189 func clip(value: int32): int16 =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
190 int16(max(min(int32(high(int16)), value), int32(low(int16))))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
191
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
192 # used for combining sounds
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
193 func mix(a, b: Sample): Sample =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
194 [
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
195 clip(int32(a[0]) + int32(b[0])),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
196 clip(int32(a[1]) + int32(b[1])),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
197 ]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
198
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
199 proc updateSoundBuffer(mixer: var Mixer) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
200 let t = getMonoTime()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
201
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
202 let tDebug = getTime()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
203 # echo ""
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
204 # echo tDebug
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
205
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
206 let dt = (t - mixer.lastUpdate).inNanoseconds.float64 / 1_000_000_000'f64
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
207 mixer.lastUpdate = t
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
208
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
209 # update fadings
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
210 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
211 if track.fadeTime > 0:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
212 track.fadeTime -= dt
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
213 track.level = (track.level.float64 + track.fadeStep.float64 * dt).clamp(Level.low, Level.high)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
214 if track.fadeTime <= 0:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
215 track.level = track.targetLevel
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
216 # mix
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
217 for i in 0 ..< mixer.buffers[mixer.currentBuffer].len:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
218 var mixedSample = [0'i16, 0'i16]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
219 mixer.lock.withLock():
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
220 for track in mixer.tracks.mvalues:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
221 var stoppedSounds: seq[uint64]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
222 for (id, playback) in track.playing.mpairs:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
223 if playback.paused:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
224 continue
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
225 let sample = applyLevel(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
226 playback.sound[playback.position],
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
227 mixer.level * track.level * playback.levelLeft,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
228 mixer.level * track.level * playback.levelRight,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
229 )
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
230 mixedSample = mix(mixedSample, sample)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
231 inc playback.position
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
232 if playback.position >= playback.sound.len:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
233 if playback.loop:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
234 playback.position = 0
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
235 else:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
236 stoppedSounds.add id
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
237 for id in stoppedSounds:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
238 track.playing.del(id)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
239 mixer.buffers[mixer.currentBuffer][i] = mixedSample
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
240 # send data to sound device
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
241 # echo getTime() - tDebug
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
242 mixer.device.WriteSoundData(mixer.currentBuffer)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
243 # echo getTime() - tDebug
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
244 mixer.currentBuffer = (mixer.currentBuffer + 1) mod mixer.buffers.len
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
245
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
246 # DSP functions
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
247 # TODO: finish implementation, one day
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
248
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
249 #[
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
250 #
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
251 proc lowPassFilter(data: var SoundData, cutoff: int) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
252 let alpha = float(cutoff) / AUDIO_SAMPLE_RATE
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
253 var value = data[0]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
254 for i in 0 ..< data.len:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
255 value[0] += int16(alpha * float(data[i][0] - value[0]))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
256 value[1] += int16(alpha * float(data[i][1] - value[1]))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
257 data[i] = value
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
258
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
259 proc downsample(data: var SoundData, n: int) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
260 let newLen = (data.len - 1) div n + 1
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
261 for i in 0 ..< newLen:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
262 data[i] = data[i * n]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
263 data.setLen(newLen)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
264
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
265 proc upsample(data: var SoundData, m: int) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
266 data.setLen(data.len * m)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
267 var i = data.len - 1
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
268 while i < 0:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
269 if i mod m == 0:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
270 data[i] = data[i div m]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
271 else:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
272 data[i] = [0, 0]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
273 i.dec
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
274
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
275 proc slowdown(data: var SoundData, m, n: int) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
276 data.upsample(m)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
277 # TODO
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
278 # data.lowPassFilter(m)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
279 data.downsample(n)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
280
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
281 ]#
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
282
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
283 proc destroy(mixer: var Mixer) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
284 mixer.lock.deinitLock()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
285 mixer.device.CloseSoundDevice()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
286
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
287 var
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
288 mixer* = createShared(Mixer)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
289 audiothread: Thread[void]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
290
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
291 proc audioWorker() {.thread.} =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
292 mixer[].setupDevice()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
293 onThreadDestruction(proc() = mixer[].lock.withLock(mixer[].destroy()); freeShared(mixer))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
294 while true:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
295 mixer[].updateSoundBuffer()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
296
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
297 # for thread priority (really necessary?)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
298 when defined(windows):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
299 import ./thirdparty/winim/winim/inc/winbase
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
300 when defined(linux):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
301 import std/posix
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
302
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
303 proc StartMixerThread() =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
304 mixer[] = initMixer()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
305 audiothread.createThread(audioWorker)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
306 debug "Created audio thread"
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
307 when defined(windows):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
308 SetThreadPriority(audiothread.handle(), THREAD_PRIORITY_TIME_CRITICAL)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
309 when defined(linux):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
310 discard pthread_setschedprio(Pthread(audiothread.handle()), cint(-20))