annotate semiconginev2/audio/mixer.nim @ 1234:841e12f33c47

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