Mercurial > games > semicongine
annotate src/semicongine/audio.nim @ 636:77358570f33b
del: unused imports
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 02 May 2023 01:03:03 +0700 |
parents | 42ad7e6208e9 |
children | 36e10cc04a33 |
rev | line source |
---|---|
628
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
1 import std/tables |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
2 import std/locks |
628
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
3 |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
4 import ./audiotypes |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
5 import ./platform/audio |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
6 |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
7 export audiotypes |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
8 |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
9 type |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
10 Playback = object |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
11 sound: Sound |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
12 position: int |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
13 loop: bool |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
14 levelLeft: Level |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
15 levelRight: Level |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
16 Track = object |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
17 playing: Table[uint64, Playback] |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
18 level: Level |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
19 Mixer* = object |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
20 playbackCounter: uint64 |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
21 tracks: Table[string, Track] |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
22 sounds*: Table[string, Sound] |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
23 level: Level |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
24 device: NativeSoundDevice |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
25 lock: Lock |
628
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
26 |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
27 proc loadSoundResource(resourcePath: string): Sound = |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
28 assert false, "Not implemented yet" |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
29 |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
30 proc initMixer*(): Mixer = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
31 result = Mixer( |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
32 tracks: {"": Track(level: 1'f)}.toTable, |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
33 level: 1'f, |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
34 device: openSoundDevice(SAMPLERATE, BUFFERSIZE), |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
35 ) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
36 result.lock.initLock() |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
37 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
38 proc loadSound*(mixer: var Mixer, name: string, resource: string) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
39 assert not (name in mixer.sounds) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
40 mixer.sounds[name] = loadSoundResource(resource) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
41 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
42 proc addSound*(mixer: var Mixer, name: string, sound: Sound) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
43 assert not (name in mixer.sounds) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
44 mixer.sounds[name] = sound |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
45 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
46 proc replaceSound*(mixer: var Mixer, name: string, sound: Sound) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
47 assert (name in mixer.sounds) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
48 mixer.sounds[name] = sound |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
49 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
50 proc addTrack*(mixer: var Mixer, name: string) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
51 assert not (name in mixer.tracks) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
52 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
53 mixer.tracks[name] = Track(level: 1'f) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
54 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
55 proc play*(mixer: var Mixer, soundName: string, track="", stopOtherSounds=false, loop=false, levelLeft, levelRight: Level): uint64 = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
56 assert track in mixer.tracks |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
57 assert soundName in mixer.sounds |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
58 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
59 if stopOtherSounds: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
60 mixer.tracks[track].playing.clear() |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
61 mixer.tracks[track].playing[mixer.playbackCounter] = Playback( |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
62 sound: mixer.sounds[soundName], |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
63 position: 0, |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
64 loop: loop, |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
65 levelLeft: levelLeft, |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
66 levelRight: levelRight |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
67 ) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
68 result = mixer.playbackCounter |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
69 inc mixer.playbackCounter |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
70 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
71 proc play*(mixer: var Mixer, soundName: string, track="", stopOtherSounds=false, loop=false, level: Level=1'f): uint64 = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
72 play(mixer=mixer, soundName=soundName, track=track, stopOtherSounds=stopOtherSounds, loop=loop, levelLeft=level, levelRight=level) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
73 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
74 proc stop*(mixer: var Mixer) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
75 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
76 for track in mixer.tracks.mvalues: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
77 track.playing.clear() |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
78 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
79 proc getLevel*(mixer: var Mixer): Level = mixer.level |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
80 proc getLevel*(mixer: var Mixer, track: string): Level = mixer.tracks[track].level |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
81 proc getLevel*(mixer: var Mixer, playbackId : uint64): (Level, Level) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
82 for track in mixer.tracks.mvalues: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
83 if playbackId in track.playing: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
84 return (track.playing[playbackId].levelLeft, track.playing[playbackId].levelRight) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
85 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
86 proc setLevel*(mixer: var Mixer, level: Level) = mixer.level = level |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
87 proc setLevel*(mixer: var Mixer, track: string, level: Level) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
88 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
89 mixer.tracks[track].level = level |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
90 proc setLevel*(mixer: var Mixer, playbackId: uint64, levelLeft, levelRight: Level) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
91 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
92 for track in mixer.tracks.mvalues: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
93 if playbackId in track.playing: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
94 track.playing[playbackId].levelLeft = levelLeft |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
95 track.playing[playbackId].levelRight = levelRight |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
96 proc setLevel*(mixer: var Mixer, playbackId : uint64, level: Level) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
97 setLevel(mixer, playbackId, level, level) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
98 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
99 proc stop*(mixer: var Mixer, track: string) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
100 assert track in mixer.tracks |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
101 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
102 mixer.tracks[track].playing.clear() |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
103 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
104 proc stop*(mixer: var Mixer, playbackId: uint64) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
105 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
106 for track in mixer.tracks.mvalues: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
107 if playbackId in track.playing: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
108 track.playing.del(playbackId) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
109 break |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
110 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
111 proc isPlaying*(mixer: var Mixer): bool = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
112 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
113 for track in mixer.tracks.mvalues: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
114 if track.playing.len > 0: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
115 return true |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
116 return false |
628
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
117 |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
118 func applyLevel(sample: Sample, levelLeft, levelRight: Level): Sample = |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
119 (int16(float(sample[0]) * levelLeft), int16(float(sample[1]) * levelRight)) |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
120 |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
121 func mix(a, b: Sample): Sample = |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
122 var |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
123 left = int32(a[0]) + int32(b[0]) |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
124 right = int32(a[1]) + int32(b[1]) |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
125 left = max(min(int32(high(int16)), left), int32(low(int16))) |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
126 right = max(min(int32(high(int16)), right), int32(low(int16))) |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
127 (int16(left), int16(right)) |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
128 |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
129 proc updateSoundBuffer(mixer: var Mixer) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
130 # mix |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
131 var buffer = newSeq[Sample](BUFFERSIZE) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
132 for i in 0 ..< buffer.len: |
628
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
133 var currentSample = (0'i16, 0'i16) |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
134 mixer.lock.withLock(): |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
135 for track in mixer.tracks.mvalues: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
136 var stoppedSounds: seq[uint64] |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
137 for (id, playback) in track.playing.mpairs: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
138 let sample = applyLevel( |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
139 playback.sound[][playback.position], |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
140 mixer.level * track.level * playback.levelLeft, |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
141 mixer.level * track.level * playback.levelRight, |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
142 ) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
143 currentSample = mix(currentSample, sample) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
144 inc playback.position |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
145 if playback.position >= playback.sound[].len: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
146 if playback.loop: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
147 playback.position = 0 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
148 else: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
149 stoppedSounds.add id |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
150 for id in stoppedSounds: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
151 track.playing.del(id) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
152 buffer[i] = currentSample |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
153 # send data to sound device |
628
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
154 mixer.device.updateSoundBuffer(buffer) |
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
155 |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
156 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
157 proc destroy*(mixer: var Mixer) = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
158 mixer.lock.deinitLock() |
628
c2f04f016104
add: initial implementation of audio subsystem with mixer, currently missing windows implementation
Sam <sam@basx.dev>
parents:
610
diff
changeset
|
159 mixer.device.closeSoundDevice() |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
160 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
161 # Threaded implementation, usually used for audio |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
162 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
163 var mixer* = createShared(Mixer) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
164 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
165 proc audioWorker() {.thread.} = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
166 onThreadDestruction(proc() = mixer[].lock.withLock(mixer[].destroy()); freeShared(mixer)) |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
167 while true: |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
168 mixer[].updateSoundBuffer() |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
169 |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
170 proc startMixerThread*() = |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
171 mixer[] = initMixer() |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
172 var audiothread: Thread[void] |
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
173 audiothread.createThread(audioWorker) |