Mercurial > games > semicongine
annotate semicongine/audio/mixer.nim @ 1330:b165359f45d7
fix: too small buffer size on linux
author | sam <sam@basx.dev> |
---|---|
date | Mon, 19 Aug 2024 19:48:40 +0700 |
parents | 385dbd68a947 |
children | 966032c7a3aa |
rev | line source |
---|---|
1330 | 1 import std/os |
1298
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
2 import std/locks |
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
3 import std/logging |
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
4 import std/math |
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
5 import std/monotimes |
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
6 import std/strformat |
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
7 import std/tables |
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
8 import std/times |
1feaa8a97acf
did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
sam <sam@basx.dev>
parents:
1282
diff
changeset
|
9 |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
10 const NBUFFERS = 32 |
1330 | 11 # it seems that some alsa hardware has a problem with smaller buffers than 512 |
12 when defined(linux): | |
13 const BUFFERSAMPLECOUNT = 512 | |
14 else: | |
15 const BUFFERSAMPLECOUNT = 256 | |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
16 const AUDIO_SAMPLE_RATE* = 44100 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
17 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
18 type |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
19 Level* = 0'f .. 1'f |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
20 Sample* = array[2, int16] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
21 SoundData* = seq[Sample] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
22 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
23 Playback = object |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
24 sound: SoundData |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
25 position: int |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
26 loop: bool |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
27 levelLeft: Level |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
28 levelRight: Level |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
29 paused: bool |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
30 Track = object |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
31 playing: Table[uint64, Playback] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
32 level: Level |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
33 targetLevel: Level |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
34 fadeTime: float |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
35 fadeStep: float |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
36 |
1321
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
sam <sam@basx.dev>
parents:
1303
diff
changeset
|
37 proc `=copy`(dest: var Playback; source: Playback) {.error.} |
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
sam <sam@basx.dev>
parents:
1303
diff
changeset
|
38 proc `=copy`(dest: var Track; source: Track) {.error.} |
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
sam <sam@basx.dev>
parents:
1303
diff
changeset
|
39 |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
40 when defined(windows): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
41 include ./platform/windows |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
42 when defined(linux): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
43 include ./platform/linux |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
44 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
45 type |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
46 Mixer* = object |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
47 playbackCounter: uint64 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
48 tracks: Table[string, Track] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
49 sounds*: Table[string, SoundData] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
50 level: Level |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
51 device: NativeSoundDevice |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
52 lock: Lock |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
53 buffers: seq[SoundData] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
54 currentBuffer: int |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
55 lastUpdate: MonoTime |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
56 |
1321
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
sam <sam@basx.dev>
parents:
1303
diff
changeset
|
57 proc `=copy`(dest: var Mixer; source: Mixer) {.error.} |
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
sam <sam@basx.dev>
parents:
1303
diff
changeset
|
58 |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
59 proc initMixer(): Mixer = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
60 result = Mixer( |
1321
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
sam <sam@basx.dev>
parents:
1303
diff
changeset
|
61 tracks: initTable[string, Track](), |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
62 level: 1'f, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
63 ) |
1321
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
sam <sam@basx.dev>
parents:
1303
diff
changeset
|
64 result.tracks[""] = Track(level: 1) |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
65 result.lock.initLock() |
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 setupDevice(mixer: var Mixer) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
68 # call this inside audio thread |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
69 var bufferaddresses: seq[ptr SoundData] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
70 for i in 0 ..< NBUFFERS: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
71 mixer.buffers.add newSeq[Sample](BUFFERSAMPLECOUNT) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
72 for i in 0 ..< mixer.buffers.len: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
73 bufferaddresses.add (addr mixer.buffers[i]) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
74 mixer.device = OpenSoundDevice(AUDIO_SAMPLE_RATE, bufferaddresses) |
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 # 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
|
77 # 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
|
78 # assert not (name in mixer.sounds) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
79 # mixer.sounds[name] = LoadAudio(resource) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
80 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
81 proc addSound*(mixer: var Mixer, name: string, sound: SoundData) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
82 assert not (name in mixer.sounds) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
83 mixer.sounds[name] = sound |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
84 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
85 proc replaceSound*(mixer: var Mixer, name: string, sound: SoundData) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
86 assert (name in mixer.sounds) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
87 mixer.sounds[name] = sound |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
88 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
89 proc addTrack*(mixer: var Mixer, name: string, level: Level = 1'f) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
90 assert not (name in mixer.tracks) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
91 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
92 mixer.tracks[name] = Track(level: level) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
93 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
94 proc play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, levelLeft, levelRight: Level): uint64 = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
95 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
|
96 assert soundName in mixer.sounds, soundName & " not loaded" |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
97 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
98 if stopOtherSounds: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
99 mixer.tracks[track].playing.clear() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
100 mixer.tracks[track].playing[mixer.playbackCounter] = Playback( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
101 sound: mixer.sounds[soundName], |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
102 position: 0, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
103 loop: loop, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
104 levelLeft: levelLeft, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
105 levelRight: levelRight, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
106 paused: false, |
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 result = mixer.playbackCounter |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
109 inc mixer.playbackCounter |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
110 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
111 proc play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, level: Level = 1'f): uint64 = |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
112 play( |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
113 mixer = mixer, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
114 soundName = soundName, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
115 track = track, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
116 stopOtherSounds = stopOtherSounds, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
117 loop = loop, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
118 levelLeft = level, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
119 levelRight = level |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
120 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
121 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
122 proc stop*(mixer: var Mixer) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
123 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
124 for track in mixer.tracks.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
125 track.playing.clear() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
126 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
127 proc getLevel*(mixer: var Mixer): Level = mixer.level |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
128 proc getLevel*(mixer: var Mixer, track: string): Level = mixer.tracks[track].level |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
129 proc getLevel*(mixer: var Mixer, playbackId: uint64): (Level, Level) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
130 for track in mixer.tracks.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
131 if playbackId in track.playing: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
132 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
|
133 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
134 proc setLevel*(mixer: var Mixer, level: Level) = mixer.level = level |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
135 proc setLevel*(mixer: var Mixer, track: string, level: Level) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
136 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
137 mixer.tracks[track].level = level |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
138 proc setLevel*(mixer: var Mixer, playbackId: uint64, levelLeft, levelRight: Level) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
139 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
140 for track in mixer.tracks.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
141 if playbackId in track.playing: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
142 track.playing[playbackId].levelLeft = levelLeft |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
143 track.playing[playbackId].levelRight = levelRight |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
144 proc setLevel*(mixer: var Mixer, playbackId: uint64, level: Level) = |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
145 setLevel(mixer, playbackId, level, level) |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
146 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
147 proc stop*(mixer: var Mixer, track: string) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
148 assert track in mixer.tracks |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
149 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
150 mixer.tracks[track].playing.clear() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
151 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
152 proc stop*(mixer: var Mixer, playbackId: uint64) = |
1224
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.del(playbackId) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
157 break |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
158 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
159 proc pause*(mixer: var Mixer, value: bool) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
160 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
161 for track in mixer.tracks.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
162 for playback in track.playing.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
163 playback.paused = value |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
164 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
165 proc pause*(mixer: var Mixer, track: string, value: bool) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
166 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
167 for playback in mixer.tracks[track].playing.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
168 playback.paused = value |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
169 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
170 proc pause*(mixer: var Mixer, playbackId: uint64, value: bool) = |
1224
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 if playbackId in track.playing: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
174 track.playing[playbackId].paused = value |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
175 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
176 proc pause*(mixer: var Mixer) = mixer.pause(true) |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
177 proc pause*(mixer: var Mixer, track: string) = mixer.pause(track, true) |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
178 proc pause*(mixer: var Mixer, playbackId: uint64) = mixer.pause(playbackId, true) |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
179 proc unpause*(mixer: var Mixer) = mixer.pause(false) |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
180 proc unpause*(mixer: var Mixer, track: string) = mixer.pause(track, false) |
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
181 proc unpause*(mixer: var Mixer, playbackId: uint64) = mixer.pause(playbackId, false) |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
182 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
183 proc fadeTo*(mixer: var Mixer, track: string, level: Level, time: float) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
184 mixer.tracks[track].targetLevel = level |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
185 mixer.tracks[track].fadeTime = time |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
186 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
|
187 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
188 proc isPlaying*(mixer: var Mixer): bool = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
189 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
190 for track in mixer.tracks.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
191 for playback in track.playing.values: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
192 if not playback.paused: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
193 return true |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
194 return false |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
195 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
196 proc isPlaying*(mixer: var Mixer, track: string): bool = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
197 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
198 if mixer.tracks.contains(track): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
199 for playback in mixer.tracks[track].playing.values: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
200 if not playback.paused: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
201 return true |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
202 return false |
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 func applyLevel(sample: Sample, levelLeft, levelRight: Level): Sample = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
205 [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
|
206 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
207 func clip(value: int32): int16 = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
208 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
|
209 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
210 # used for combining sounds |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
211 func mix(a, b: Sample): Sample = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
212 [ |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
213 clip(int32(a[0]) + int32(b[0])), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
214 clip(int32(a[1]) + int32(b[1])), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
215 ] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
216 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
217 proc updateSoundBuffer(mixer: var Mixer) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
218 let t = getMonoTime() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
219 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
220 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
|
221 mixer.lastUpdate = t |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
222 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
223 # update fadings |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
224 for track in mixer.tracks.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
225 if track.fadeTime > 0: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
226 track.fadeTime -= dt |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
227 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
|
228 if track.fadeTime <= 0: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
229 track.level = track.targetLevel |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
230 # mix |
1330 | 231 var hasData = false |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
232 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
|
233 var mixedSample = [0'i16, 0'i16] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
234 mixer.lock.withLock(): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
235 for track in mixer.tracks.mvalues: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
236 var stoppedSounds: seq[uint64] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
237 for (id, playback) in track.playing.mpairs: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
238 if playback.paused: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
239 continue |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
240 let sample = applyLevel( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
241 playback.sound[playback.position], |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
242 mixer.level * track.level * playback.levelLeft, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
243 mixer.level * track.level * playback.levelRight, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
244 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
245 mixedSample = mix(mixedSample, sample) |
1330 | 246 hasData = true |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
247 inc playback.position |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
248 if playback.position >= playback.sound.len: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
249 if playback.loop: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
250 playback.position = 0 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
251 else: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
252 stoppedSounds.add id |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
253 for id in stoppedSounds: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
254 track.playing.del(id) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
255 mixer.buffers[mixer.currentBuffer][i] = mixedSample |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
256 # send data to sound device |
1330 | 257 if hasData: |
258 mixer.device.WriteSoundData(mixer.currentBuffer) | |
259 mixer.currentBuffer = (mixer.currentBuffer + 1) mod mixer.buffers.len | |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
260 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
261 # DSP functions |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
262 # TODO: finish implementation, one day |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
263 |
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 # |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
266 proc lowPassFilter(data: var SoundData, cutoff: int) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
267 let alpha = float(cutoff) / AUDIO_SAMPLE_RATE |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
268 var value = data[0] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
269 for i in 0 ..< data.len: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
270 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
|
271 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
|
272 data[i] = value |
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 proc downsample(data: var SoundData, n: int) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
275 let newLen = (data.len - 1) div n + 1 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
276 for i in 0 ..< newLen: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
277 data[i] = data[i * n] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
278 data.setLen(newLen) |
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 proc upsample(data: var SoundData, m: int) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
281 data.setLen(data.len * m) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
282 var i = data.len - 1 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
283 while i < 0: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
284 if i mod m == 0: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
285 data[i] = data[i div m] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
286 else: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
287 data[i] = [0, 0] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
288 i.dec |
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 proc slowdown(data: var SoundData, m, n: int) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
291 data.upsample(m) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
292 # TODO |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
293 # data.lowPassFilter(m) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
294 data.downsample(n) |
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 ]# |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
297 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
298 proc destroy(mixer: var Mixer) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
299 mixer.lock.deinitLock() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
300 mixer.device.CloseSoundDevice() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
301 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
302 var |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
303 mixer* = createShared(Mixer) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
304 audiothread: Thread[void] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
305 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
306 proc audioWorker() {.thread.} = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
307 mixer[].setupDevice() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
308 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
|
309 while true: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
310 mixer[].updateSoundBuffer() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
311 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
312 # for thread priority (really necessary?) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
313 when defined(windows): |
1303 | 314 import ../thirdparty/winim/winim/inc/winbase |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
315 when defined(linux): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
316 import std/posix |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
317 |
1282
3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents:
1267
diff
changeset
|
318 proc startMixerThread() = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
319 mixer[] = initMixer() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
320 audiothread.createThread(audioWorker) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
321 debug "Created audio thread" |