Debian Codegraph @

dokidoki-support/mixer.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #cxx . . $ `<file>/dokidoki-support/mixer.c`/include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "stb_vorbis.h"cxx . . $ `<file>/dokidoki-support/stb_vorbis.h`/
6
7 //// Types, Constants, Globals ////////////////////////////////////////////////
8
9 typedef short sample_tcxx . . $ sample_t#;
10 typedef int calc_tcxx . . $ calc_t#;
11 #define SAMPLE_MINcxx . . $ `dokidoki-support/mixer.c:11:9`! (-0x8000)
12 #define SAMPLE_MAXcxx . . $ `dokidoki-support/mixer.c:12:9`! 0x7FFF
13 #define PORTAUDIO_SAMPLE_TYPEcxx . . $ `dokidoki-support/mixer.c:13:9`! paInt16
14 #define ALSA_SAMPLE_TYPEcxx . . $ `dokidoki-support/mixer.c:14:9`! SND_PCM_FORMAT_S16
15
16 #define SAMPLE_RATEcxx . . $ `dokidoki-support/mixer.c:16:9`! 44100
17 #define SAMPLE_TIMEcxx . . $ `dokidoki-support/mixer.c:17:9`! (1.0/SAMPLE_RATE)
18 #define CHANNELScxx . . $ `dokidoki-support/mixer.c:18:9`! 64
19 #define BUFFER_SIZEcxx . . $ `dokidoki-support/mixer.c:19:9`! 256
20
21 typedef structcxx . . $ $anonymous_type_535b998fec59ecb8_0#
22 {
23 size_t framescxx . . $ $anonymous_type_535b998fec59ecb8_0#frames.;
24 sample_tcxx . . $ sample_t# * samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples.;
25 } sound_data_tcxx . . $ sound_data_t#;
26
27 typedef structcxx . . $ $anonymous_type_535b998fec59ecb8_1#
28 {
29 float volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[2];
30 } fade_tcxx . . $ fade_t#;
31
32 typedef structcxx . . $ $anonymous_type_535b998fec59ecb8_2#
33 {
34 // shared
35 volatile int is_newcxx . . $ $anonymous_type_535b998fec59ecb8_2#is_new.;
36 const sound_data_tcxx . . $ sound_data_t# * volatile datacxx . . $ $anonymous_type_535b998fec59ecb8_2#data.;
37 int loopcxx . . $ $anonymous_type_535b998fec59ecb8_2#loop.; // callback-only after init
38
39 volatile int new_fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade_duration.;
40 volatile fade_tcxx . . $ fade_t# new_fadecxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade.;
41
42 // for use by callback only
43 size_t positioncxx . . $ $anonymous_type_535b998fec59ecb8_2#position.;
44 int fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_duration.;
45 int fade_remainingcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_remaining.;
46 fade_tcxx . . $ fade_t# fade_startcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_start.;
47 fade_tcxx . . $ fade_t# fade_endcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_end.;
48 } channel_tcxx . . $ channel_t#;
49
50 static const char * error_stringcxx . . $ error_string. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
51
52 static int calc_buffercxx . . $ calc_buffer.[BUFFER_SIZEcxx . . $ `dokidoki-support/mixer.c:19:9`! * 2];
53
54 #define CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(condition, message) \
55 do \
56 { \
57 if(!(condition)) \
58 { \
59 if(message) \
60 error_string = message; \
61 return 0; \
62 } \
63 } while(0)
64
65 #define ERRORcxx . . $ `dokidoki-support/mixer.c:65:9`!(message) CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(0, message)
66
67 //// Fades ////////////////////////////////////////////////////////////////////
68
69 static float interpcxx . . $ interp(7f54a427cb2e2c3).(float t, float start, float end)
70 {
71 return (1-t) * start + t * end;
72 }
73
74 static void fade_interpcxx . . $ fade_interp(eb23b8731d864b43).(const channel_tcxx . . $ channel_t# * chan, fade_tcxx . . $ fade_t# *out)
75 {
76 const fade_tcxx . . $ fade_t# * start = &chan->fade_startcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_start.;
77 const fade_tcxx . . $ fade_t# * end = &chan->fade_endcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_end.;
78
79 float t = 1;
80
81 if(chan->fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_duration. > 0)
82 t = 1 - (float)chan->fade_remainingcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_remaining. / chan->fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_duration.;
83
84 out->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[0] = interpcxx . . $ interp(7f54a427cb2e2c3).(t, start->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[0], end->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[0]);
85 out->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[1] = interpcxx . . $ interp(7f54a427cb2e2c3).(t, start->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[1], end->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[1]);
86 }
87
88 static void fade_initcxx . . $ fade_init(d5951370c5d7716f).(fade_tcxx . . $ fade_t# * out)
89 {
90 out->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[0] = 1;
91 out->volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[1] = 1;
92 }
93
94 //// Channels /////////////////////////////////////////////////////////////////
95
96 static channel_tcxx . . $ channel_t# channelscxx . . $ channels.[CHANNELScxx . . $ `dokidoki-support/mixer.c:18:9`!];
97
98 static int channel_is_validcxx . . $ channel_is_valid(7864480464b09eea).(int channel)
99 {
100 return 0 <= channel && channel < CHANNELScxx . . $ `dokidoki-support/mixer.c:18:9`!;
101 }
102
103 static int channel_is_activecxx . . $ channel_is_active(7864480464b09eea).(int channel)
104 {
105 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(channel_is_valid(channel));
106 return channelscxx . . $ channels.[channel].datacxx . . $ $anonymous_type_535b998fec59ecb8_2#data. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
107 }
108
109 static void channel_stopcxx . . $ channel_stop(d4f767463ce0a6b3).(int channel)
110 {
111 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(channel_is_valid(channel));
112 channelscxx . . $ channels.[channel].datacxx . . $ $anonymous_type_535b998fec59ecb8_2#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
113 }
114
115 static void channel_mix_intocxx . . $ channel_mix_into(7162a6ac4d296d0a).(int channel, calc_tcxx . . $ calc_t# * output,
116 size_t frame_count)
117 {
118 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(channel_is_valid(channel));
119 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(channel_is_active(channel));
120
121 int i;
122 channel_tcxx . . $ channel_t# * chan = &channelscxx . . $ channels.[channel];
123 const sound_data_tcxx . . $ sound_data_t# * data = chan->datacxx . . $ $anonymous_type_535b998fec59ecb8_2#data.;
124
125 if(chan->is_newcxx . . $ $anonymous_type_535b998fec59ecb8_2#is_new.)
126 {
127 chan->is_newcxx . . $ $anonymous_type_535b998fec59ecb8_2#is_new. = 0;
128 chan->positioncxx . . $ $anonymous_type_535b998fec59ecb8_2#position. = 0;
129 chan->fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_duration. = 0;
130 chan->fade_remainingcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_remaining. = 0;
131 fade_initcxx . . $ fade_init(d5951370c5d7716f).(&chan->fade_startcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_start.);
132 fade_initcxx . . $ fade_init(d5951370c5d7716f).(&chan->fade_endcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_end.);
133 }
134
135 if(chan->new_fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade_duration. >= 0)
136 {
137 fade_tcxx . . $ fade_t# current;
138 fade_interpcxx . . $ fade_interp(eb23b8731d864b43).(chan, &current);
139 chan->fade_startcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_start. = current;
140 chan->fade_endcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_end. = chan->new_fadecxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade.;
141 chan->fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_duration. = chan->new_fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade_duration.;
142 chan->fade_remainingcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_remaining. = chan->fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_duration.;
143 chan->new_fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade_duration. = -1;
144 }
145
146 size_t position = chan->positioncxx . . $ $anonymous_type_535b998fec59ecb8_2#position.;
147 size_t to_copy = data->framescxx . . $ $anonymous_type_535b998fec59ecb8_0#frames. - position;
148 if(to_copy > frame_count) to_copy = frame_count;
149
150 for(i = 0; i < to_copy; i++)
151 {
152 fade_tcxx . . $ fade_t# fade;
153 fade_interpcxx . . $ fade_interp(eb23b8731d864b43).(chan, &fade);
154
155 output[i*2] += data->samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples.[(position + i)*2] * fade.volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[0];
156 output[i*2+1] += data->samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples.[(position + i)*2+1] * fade.volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[1];
157
158 if(chan->fade_remainingcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_remaining. > 0) chan->fade_remainingcxx . . $ $anonymous_type_535b998fec59ecb8_2#fade_remaining.--;
159 }
160
161 chan->positioncxx . . $ $anonymous_type_535b998fec59ecb8_2#position. = position + to_copy;
162
163 if(chan->positioncxx . . $ $anonymous_type_535b998fec59ecb8_2#position. == data->framescxx . . $ $anonymous_type_535b998fec59ecb8_0#frames.)
164 {
165 // imprecise looping for now
166 if(chan->loopcxx . . $ $anonymous_type_535b998fec59ecb8_2#loop. > 0)
167 {
168 chan->loopcxx . . $ $anonymous_type_535b998fec59ecb8_2#loop.--;
169 if(chan->loopcxx . . $ $anonymous_type_535b998fec59ecb8_2#loop. == 0)
170 channel_stopcxx . . $ channel_stop(d4f767463ce0a6b3).(channel);
171 else
172 chan->positioncxx . . $ $anonymous_type_535b998fec59ecb8_2#position. = 0;
173 }
174 else
175 {
176 chan->positioncxx . . $ $anonymous_type_535b998fec59ecb8_2#position. = 0;
177 }
178 }
179 }
180
181 //// General Audio ////////////////////////////////////////////////////////////
182
183 static void mix_intocxx . . $ mix_into(f5389a3d29a56ee9).(sample_tcxx . . $ sample_t# * output, size_t frame_count)
184 {
185 memsetcxx . . $ memset(d1de1e4fb215f1c6).(calc_buffercxx . . $ calc_buffer., 0, frame_count * 2 * sizeof(calc_tcxx . . $ calc_t#));
186
187 int c;
188 for(c = 0; c != CHANNELScxx . . $ `dokidoki-support/mixer.c:18:9`!; c++)
189 if(channel_is_activecxx . . $ channel_is_active(7864480464b09eea).(c))
190 channel_mix_intocxx . . $ channel_mix_into(7162a6ac4d296d0a).(c, calc_buffercxx . . $ calc_buffer., frame_count);
191
192 int i;
193 for(i = 0; i != BUFFER_SIZEcxx . . $ `dokidoki-support/mixer.c:19:9`! * 2; i++)
194 {
195 if(calc_buffercxx . . $ calc_buffer.[i] <= SAMPLE_MINcxx . . $ `dokidoki-support/mixer.c:11:9`!)
196 output[i] = SAMPLE_MINcxx . . $ `dokidoki-support/mixer.c:11:9`!;
197 else if(calc_buffercxx . . $ calc_buffer.[i] <= SAMPLE_MAXcxx . . $ `dokidoki-support/mixer.c:12:9`!)
198 output[i] = calc_buffercxx . . $ calc_buffer.[i];
199 else
200 output[i] = SAMPLE_MAXcxx . . $ `dokidoki-support/mixer.c:12:9`!;
201 }
202 }
203
204 static int play_sound_effectcxx . . $ play_sound_effect(caf4a90803836d9d).(const sound_data_tcxx . . $ sound_data_t# * data, float left,
205 float right, int loop)
206 {
207 int c = 0;
208 while(c != CHANNELScxx . . $ `dokidoki-support/mixer.c:18:9`! && channel_is_activecxx . . $ channel_is_active(7864480464b09eea).(c)) c++;
209 if(c == CHANNELScxx . . $ `dokidoki-support/mixer.c:18:9`!)
210 {
211 return -1;
212 }
213 else
214 {
215 channelscxx . . $ channels.[c].is_newcxx . . $ $anonymous_type_535b998fec59ecb8_2#is_new. = 1;
216 channelscxx . . $ channels.[c].loopcxx . . $ $anonymous_type_535b998fec59ecb8_2#loop. = loop;
217
218 channelscxx . . $ channels.[c].new_fadecxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade..volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[0] = left;
219 channelscxx . . $ channels.[c].new_fadecxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade..volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[1] = right;
220 channelscxx . . $ channels.[c].new_fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade_duration. = 0;
221
222 // this marks the channel as ready, so after this the callback is free
223 // to jump on it
224 channelscxx . . $ channels.[c].datacxx . . $ $anonymous_type_535b998fec59ecb8_2#data. = data;
225
226 return c;
227 }
228 }
229
230 static void channel_fade_tocxx . . $ channel_fade_to(a8f6e5be37b619e5).(int channel, float duration, float left,
231 float right)
232 {
233 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(channel_is_valid(channel));
234 if(channel_is_activecxx . . $ channel_is_active(7864480464b09eea).(channel))
235 {
236 while(channelscxx . . $ channels.[channel].new_fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade_duration. >= 0)
237 /* wait for previous fade to be acknowledged */;
238
239 channelscxx . . $ channels.[channel].new_fadecxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade..volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[0] = left;
240 channelscxx . . $ channels.[channel].new_fadecxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade..volumecxx . . $ $anonymous_type_535b998fec59ecb8_1#volume.[1] = right;
241 channelscxx . . $ channels.[channel].new_fade_durationcxx . . $ $anonymous_type_535b998fec59ecb8_2#new_fade_duration. = (int)(duration * SAMPLE_RATEcxx . . $ `dokidoki-support/mixer.c:16:9`!);
242 }
243 }
244
245 //// Sound Data ///////////////////////////////////////////////////////////////
246
247 static sound_data_tcxx . . $ sound_data_t# * new_sound_datacxx . . $ new_sound_data(4c6b0dfc8bf2eb40).(size_t frames)
248 {
249 sound_data_tcxx . . $ sound_data_t# * data = (sound_data_tcxx . . $ sound_data_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(sound_data_tcxx . . $ sound_data_t#));
250 data->framescxx . . $ $anonymous_type_535b998fec59ecb8_0#frames. = frames;
251 data->samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples. = (sample_tcxx . . $ sample_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(frames * sizeof(sample_tcxx . . $ sample_t#) * 2);
252 return data;
253 }
254
255 static void delete_sound_datacxx . . $ delete_sound_data(160bf80d71a77dfd).(sound_data_tcxx . . $ sound_data_t# * data)
256 {
257 freecxx . . $ free(5d22bdacc48458e8).(data->samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples.);
258 freecxx . . $ free(5d22bdacc48458e8).(data);
259 }
260
261 //// WAV Loading //////////////////////////////////////////////////////////////
262
263 static unsigned char get_u8cxx . . $ get_u8(44a90fc04011589b).(FILEcxx . . $ FILE# * file)
264 {
265 int n = fgetccxx . . $ fgetc(89bc1efcdac0ca22).(file);
266 return n == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`! ? 0 : (unsigned char)n;
267 }
268
269 static unsigned short get_u16cxx . . $ get_u16(38a7f6633dfbdb53).(FILEcxx . . $ FILE# * file)
270 {
271 unsigned n = get_u8cxx . . $ get_u8(44a90fc04011589b).(file);
272 return n + (get_u8cxx . . $ get_u8(44a90fc04011589b).(file) << 8);
273 }
274
275 static unsigned short get_s16cxx . . $ get_s16(38a7f6633dfbdb53).(FILEcxx . . $ FILE# * file)
276 {
277 return (signed short)get_u16cxx . . $ get_u16(38a7f6633dfbdb53).(file);
278 }
279
280 static unsigned int get_u32cxx . . $ get_u32(9eef1996c458a0e7).(FILEcxx . . $ FILE# * file)
281 {
282 unsigned n = get_u16cxx . . $ get_u16(38a7f6633dfbdb53).(file);
283 return n + (get_u16cxx . . $ get_u16(38a7f6633dfbdb53).(file) << 16);
284 }
285
286 static int get_chunk_idcxx . . $ get_chunk_id(874fa6a5e59ab9bd).(const char * id, FILEcxx . . $ FILE# * file)
287 {
288 size_t read;
289 char chunk_id[5];
290 chunk_id[4] = 0;
291 read = freadcxx . . $ fread(738fc8ac1d711e58).(chunk_id, 1, 4, file);
292 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(read == 4 && strcmp(chunk_id, id) == 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
293 return 1;
294 }
295
296 static sound_data_tcxx . . $ sound_data_t# * load_wavcxx . . $ load_wav(c2e73ae02f9faccc).(const char * filename)
297 {
298 FILEcxx . . $ FILE# * file = fopencxx . . $ fopen(82ec0787cd37e0a3).(filename, "rb");
299 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(file, "file not found");
300
301 char chunk_id[5];
302 chunk_id[4] = 0;
303
304 // read RIFF header
305 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(get_chunk_id("RIFF", file), "didn't get expected \"RIFF\" chunk");
306 get_u32cxx . . $ get_u32(9eef1996c458a0e7).(file);
307 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(get_chunk_id("WAVE", file), "didn't get expected \"WAVE\" type");
308
309 // read "fmt " chunk
310 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(get_chunk_id("fmt ", file), "didn't get expected \"fmt \" chunk");
311 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(get_u32(file) == 16, "unexpected \"fmt \" chunk size");
312 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(get_u16(file) == 1, "unsupported compression type");
313 int channels = get_u16cxx . . $ get_u16(38a7f6633dfbdb53).(file);
314 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(channels == 1 || channels == 2, "unsupported number of channels");
315 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(get_u32(file) == SAMPLE_RATEcxx . . $ `dokidoki-support/mixer.c:16:9`!, "unsupported sample rate");
316 get_u32cxx . . $ get_u32(9eef1996c458a0e7).(file); // skip bytes per second
317 get_u16cxx . . $ get_u16(38a7f6633dfbdb53).(file); // skip block align
318 int bits_per_sample = get_u16cxx . . $ get_u16(38a7f6633dfbdb53).(file);
319 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(bits_per_sample == 8 || bits_per_sample == 16,
320 "unsupported bits per sample");
321
322 // read "data" chunk
323 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(get_chunk_id("data", file), "didn't get expected \"data\" chunk");
324 size_t size = get_u32cxx . . $ get_u32(9eef1996c458a0e7).(file);
325 size_t frames = size * 8 / bits_per_sample / channels; // two channels
326
327 sound_data_tcxx . . $ sound_data_t# * data = new_sound_datacxx . . $ new_sound_data(4c6b0dfc8bf2eb40).(frames);
328 sample_tcxx . . $ sample_t# * dest = data->samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples.;
329 sample_tcxx . . $ sample_t# * end = data->samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples. + frames * 2;
330
331 if(bits_per_sample == 8)
332 while(!feofcxx . . $ feof(89bc1efcdac0ca22).(file) && !ferrorcxx . . $ ferror(89bc1efcdac0ca22).(file) && dest != end)
333 {
334 sample_tcxx . . $ sample_t# sample = ((sample_tcxx . . $ sample_t#)get_u8cxx . . $ get_u8(44a90fc04011589b).(file) - 128) << 8;
335 *(dest++) = sample;
336 if(channels == 1) *(dest++) = sample;
337 }
338 else
339 while(!feofcxx . . $ feof(89bc1efcdac0ca22).(file) && !ferrorcxx . . $ ferror(89bc1efcdac0ca22).(file) && dest != end)
340 {
341 sample_tcxx . . $ sample_t# sample = get_s16cxx . . $ get_s16(38a7f6633dfbdb53).(file);
342 *(dest++) = sample;
343 if(channels == 1) *(dest++) = sample;
344 }
345 if(dest != end)
346 {
347 delete_sound_datacxx . . $ delete_sound_data(160bf80d71a77dfd).(data);
348 data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
349 ERRORcxx . . $ `dokidoki-support/mixer.c:65:9`!("error or end of file before finished reading samples");
350 }
351
352 return data;
353 }
354
355 //// OGG Loading //////////////////////////////////////////////////////////////
356
357 static sound_data_tcxx . . $ sound_data_t# * load_oggcxx . . $ load_ogg(c2e73ae02f9faccc).(const char * filename)
358 {
359 sound_data_tcxx . . $ sound_data_t# * data = new_sound_datacxx . . $ new_sound_data(4c6b0dfc8bf2eb40).(0);
360 int channels;
361 int ret = stb_vorbis_decode_filenamecxx . . $ stb_vorbis_decode_filename(89a8dc7e5064adc).(
362 (char *)filename,
363 &channels,
364 &data->samplescxx . . $ $anonymous_type_535b998fec59ecb8_0#samples.);
365 if(channels != 2)
366 {
367 delete_sound_datacxx . . $ delete_sound_data(160bf80d71a77dfd).(data);
368 data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
369 ERRORcxx . . $ `dokidoki-support/mixer.c:65:9`!("only 2-channel ogg files are supported");
370 }
371 if(ret < 0)
372 {
373 delete_sound_datacxx . . $ delete_sound_data(160bf80d71a77dfd).(data);
374 data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
375 ERRORcxx . . $ `dokidoki-support/mixer.c:65:9`!("some sort of error in stb_vorbis_decode_filename");
376 }
377 data->framescxx . . $ $anonymous_type_535b998fec59ecb8_0#frames. = ret;
378
379 return data;
380 }
381
382 #if defined(DOKIDOKI_MACOSX) || defined(DOKIDOKI_MINGW)
383 //// Portaudio ////////////////////////////////////////////////////////////////
384
385 #include <portaudio.h>
386
387 #define PA_CHECK(error) \
388 do \
389 { \
390 PaError PA_CHECK__err = (error); \
391 if(PA_CHECK__err != paNoError) \
392 { \
393 error_string = Pa_GetErrorText(PA_CHECK__err); \
394 return 0; \
395 } \
396 } while(0)
397
398 static PaStream * stream = NULL;
399
400 static int callback(const void *input, void *output, unsigned long frame_count,
401 const PaStreamCallbackTimeInfo *time_info,
402 PaStreamCallbackFlags status_flags, void *unused)
403 {
404 mix_into((sample_t *)output, frame_count);
405 return paContinue;
406 }
407
408 static int init()
409 {
410 int c;
411 for(c = 0; c < CHANNELS; c++) channels[c].data = NULL;
412
413 PA_CHECK(Pa_Initialize());
414
415 PaError err;
416 err = Pa_OpenDefaultStream(&stream, 0, 2, PORTAUDIO_SAMPLE_TYPE,
417 SAMPLE_RATE, BUFFER_SIZE, callback, NULL);
418 if(err != paNoError)
419 {
420 Pa_Terminate();
421 PA_CHECK(err);
422 }
423
424 err = Pa_StartStream(stream);
425 if(err != paNoError)
426 {
427 Pa_Terminate();
428 PA_CHECK(err);
429 }
430
431 return 1;
432 }
433
434 static int uninit()
435 {
436 PA_CHECK(Pa_StopStream(stream));
437 PA_CHECK(Pa_CloseStream(stream));
438 PA_CHECK(Pa_Terminate());
439
440 return 1;
441 }
442
443 //// ALSA /////////////////////////////////////////////////////////////////////
444 #elif defined(DOKIDOKI_LINUXcxx . . $ `<built-in>:416:9`!)
445
446 #define ALSA_CHECKcxx . . $ `dokidoki-support/mixer.c:446:9`!(error) \
447 do \
448 { \
449 int ALSA_CHECK__err = (error); \
450 if(ALSA_CHECK__err < 0) \
451 { \
452 error_string = snd_strerror(ALSA_CHECK__err); \
453 return 0; \
454 } \
455 } while(0)
456
457 #include <alsa/asoundlib.h>
•458 #include <stdatomic.h>
459 #include <pthread.h>
460
461 static snd_pcm_tcxx . . $ snd_pcm_t# * sound_devicecxx . . $ sound_device. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
462
•463 static pthread_tcxx . . $ pthread_t# audio_threadcxx . . $ audio_thread.;
464 static volatile atomic_flagcxx . . $ atomic_flag# runningcxx . . $ running. = ATOMIC_FLAG_INITcxx . . $ `<external>/6975296586547049691/stdatomic.h:235:9`!;
465
•466 static void* audio_loopcxx . . $ audio_loop(9f162e79bfc6c74).(void *data)
467 {
468 sample_tcxx . . $ sample_t# buffer[BUFFER_SIZEcxx . . $ `dokidoki-support/mixer.c:19:9`!*2];
469
•470 while(atomic_flag_test_and_setcxx . . $ `<external>/6975296586547049691/stdatomic.h:239:9`!(&running))
471 {
472 mix_intocxx . . $ mix_into(f5389a3d29a56ee9).(buffer, BUFFER_SIZEcxx . . $ `dokidoki-support/mixer.c:19:9`!);
473 int err = snd_pcm_writeicxx . . $ snd_pcm_writei(3b2a59bb378d9d87).(sound_devicecxx . . $ sound_device., buffer, BUFFER_SIZEcxx . . $ `dokidoki-support/mixer.c:19:9`!);
474 if(err < 0)
475 err = snd_pcm_recovercxx . . $ snd_pcm_recover(69cea77c6ea94751).(sound_devicecxx . . $ sound_device., err, 0);
476
477 if(err < 0)
478 break;
479 }
•480
481 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
482 }
483
484 static int initcxx . . $ init(d1b40a211a1a4e4c).()
485 {
486 ALSA_CHECKcxx . . $ `dokidoki-support/mixer.c:446:9`!(snd_pcm_open(&sound_device, "default", SND_PCM_STREAM_PLAYBACK, 0));
487
488 ALSA_CHECKcxx . . $ `dokidoki-support/mixer.c:446:9`!(snd_pcm_set_params(sound_device,
489 ALSA_SAMPLE_TYPEcxx . . $ `dokidoki-support/mixer.c:14:9`!, // format
490 SND_PCM_ACCESS_RW_INTERLEAVED, // access type
491 2, // channels
492 SAMPLE_RATEcxx . . $ `dokidoki-support/mixer.c:16:9`!, // rate
493 1, // resample
494 (unsigned)((float)BUFFER_SIZEcxx . . $ `dokidoki-support/mixer.c:19:9`!/SAMPLE_RATEcxx . . $ `dokidoki-support/mixer.c:16:9`!*1000000*20))); // latency in us
495
496 ALSA_CHECKcxx . . $ `dokidoki-support/mixer.c:446:9`!(snd_pcm_prepare(sound_device));
497
•498 atomic_flag_test_and_setcxx . . $ `<external>/6975296586547049691/stdatomic.h:239:9`!(&running);
499 CHECKcxx . . $ `dokidoki-support/mixer.c:54:9`!(pthread_create(&audio_thread, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, audio_loop, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == 0, "error creating alsa thread");
500
501 return 1;
502 }
503
504 static int uninitcxx . . $ uninit(d1b40a211a1a4e4c).()
505 {
•506 atomic_flag_clearcxx . . $ `<external>/6975296586547049691/stdatomic.h:247:9`!(&running);
507 pthread_joincxx . . $ pthread_join(269902cf949f12c5).(audio_threadcxx . . $ audio_thread., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
508
509 ALSA_CHECKcxx . . $ `dokidoki-support/mixer.c:446:9`!(snd_pcm_close(sound_device));
510 sound_devicecxx . . $ sound_device. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
511
512 return 1;
513 }
514
515 //// No Audio /////////////////////////////////////////////////////////////////
516 #else
517
518 static int init()
519 {
520 return 1;
521 }
522
523 static int uninit()
524 {
525 return 1;
526 }
527
528 #endif
529
530 //// Lua //////////////////////////////////////////////////////////////////////
531
532 #include <lua.h>
533 #include <lualib.h>
534 #include <lauxlib.h>
535
536 #define LUA_CHECKcxx . . $ `dokidoki-support/mixer.c:536:9`!(L, condition, message) \
537 do \
538 { \
539 if(!(condition)) \
540 { \
541 lua_pushnil(L); \
542 lua_pushstring(L, message); \
543 return 2; \
544 } \
545 } while(0)
546
547 #define LUA_ERRORcxx . . $ `dokidoki-support/mixer.c:547:9`!(L, message) LUA_CHECK(L, 0, message)
548
549 static int mixer__inittedcxx . . $ mixer__initted. = 0;
550
551 static void check_inittedcxx . . $ check_initted(f6fa9609f70d70d8).(lua_Statecxx . . $ lua_State# *L)
552 {
553 if(!mixer__inittedcxx . . $ mixer__initted.) luaL_errorcxx . . $ luaL_error(5a39ce5ac465c74d).(L, "please call mixer.init() first");
554 }
555
556 static int mixer__initcxx . . $ mixer__init(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
557 {
558 if(!mixer__inittedcxx . . $ mixer__initted.)
559 {
560 mixer__inittedcxx . . $ mixer__initted. = initcxx . . $ init(d1b40a211a1a4e4c).();
561 LUA_CHECKcxx . . $ `dokidoki-support/mixer.c:536:9`!(L, mixer__initted, error_string);
562 }
563 lua_pushbooleancxx . . $ lua_pushboolean(9637dd485cf22a1).(L, 1);
564 return 1;
565 }
566
567 static int mixer__uninitcxx . . $ mixer__uninit(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
568 {
569 if(mixer__inittedcxx . . $ mixer__initted.)
570 {
571 mixer__inittedcxx . . $ mixer__initted. = 0;
572 LUA_CHECKcxx . . $ `dokidoki-support/mixer.c:536:9`!(L, uninit(), error_string);
573 }
574 lua_pushbooleancxx . . $ lua_pushboolean(9637dd485cf22a1).(L, 1);
575 return 1;
576 }
577
578 static int generic_sound_loadercxx . . $ generic_sound_loader(da4b4a40455ca560).(
579 lua_Statecxx . . $ lua_State# *L,
580 sound_data_tcxx . . $ sound_data_t# * (*loader) (const char *filename))
581 {
582 const char * filename = luaL_checkstringcxx . . $ `<external>/18095755238171912061/lauxlib.h:102:9`!(L, 1);
583 sound_data_tcxx . . $ sound_data_t# * sound = loader(filename);
584
585 if(sound == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
586 {
587 lua_pushnilcxx . . $ lua_pushnil(f6fa9609f70d70d8).(L);
588 lua_pushstringcxx . . $ lua_pushstring(2c01a949aef328b).(L, error_stringcxx . . $ error_string.);
589 return 2;
590 }
591 else
592 {
593 *(sound_data_tcxx . . $ sound_data_t# **)lua_newuserdatacxx . . $ lua_newuserdata(bb92b2d7de290).(L, sizeof(sound)) = sound;
594 luaL_getmetatablecxx . . $ `<external>/18095755238171912061/lauxlib.h:117:9`!(L, "mixer.sound_effect");
595 lua_setmetatablecxx . . $ lua_setmetatable(95e3aa3a3b4bb934).(L, -2);
596 return 1;
597 }
598 }
599
600 static int mixer__load_wavcxx . . $ mixer__load_wav(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
601 {
602 return generic_sound_loadercxx . . $ generic_sound_loader(da4b4a40455ca560).(L, load_wavcxx . . $ load_wav(c2e73ae02f9faccc).);
603 }
604
605 static int mixer__load_oggcxx . . $ mixer__load_ogg(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
606 {
607 return generic_sound_loadercxx . . $ generic_sound_loader(da4b4a40455ca560).(L, load_oggcxx . . $ load_ogg(c2e73ae02f9faccc).);
608 }
609
610 static int mixer__channel_fade_tocxx . . $ mixer__channel_fade_to(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
611 {
612 check_inittedcxx . . $ check_initted(f6fa9609f70d70d8).(L);
613 int channel = luaL_checkintcxx . . $ `<external>/18095755238171912061/lauxlib.h:104:9`!(L, 1);
614 float duration = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 2);
615 float left = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 3);
616 float right = luaL_optnumbercxx . . $ luaL_optnumber(67a0f3e7bac8441f).(L, 4, left);
617
618 luaL_argcheckcxx . . $ `<external>/18095755238171912061/lauxlib.h:100:9`!(L, channel_is_valid(channel), 1, "invalid channel given");
619 luaL_argcheckcxx . . $ `<external>/18095755238171912061/lauxlib.h:100:9`!(L, duration >= 0, 1, "invalid duration given");
620
621 channel_fade_tocxx . . $ channel_fade_to(a8f6e5be37b619e5).(channel, duration, left, right);
622
623 return 0;
624 }
625
626 static int mixer__channel_stopcxx . . $ mixer__channel_stop(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
627 {
628 check_inittedcxx . . $ check_initted(f6fa9609f70d70d8).(L);
629 int channel = luaL_checkintcxx . . $ `<external>/18095755238171912061/lauxlib.h:104:9`!(L, 1);
630
631 luaL_argcheckcxx . . $ `<external>/18095755238171912061/lauxlib.h:100:9`!(L, channel_is_valid(channel), 1, "invalid channel given");
632
633 channel_stopcxx . . $ channel_stop(d4f767463ce0a6b3).(channel);
634
635 return 0;
636 }
637
638 static sound_data_tcxx . . $ sound_data_t# * check_sound_effectcxx . . $ check_sound_effect(54b56bf422a8458b).(lua_Statecxx . . $ lua_State# *L, int index)
639 {
640 return *(sound_data_tcxx . . $ sound_data_t# **)luaL_checkudatacxx . . $ luaL_checkudata(ded294d5354abc01).(L, index, "mixer.sound_effect");
641 }
642
643 static int sound_effect__playcxx . . $ sound_effect__play(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
644 {
645 check_inittedcxx . . $ check_initted(f6fa9609f70d70d8).(L);
646 sound_data_tcxx . . $ sound_data_t# * sound = check_sound_effectcxx . . $ check_sound_effect(54b56bf422a8458b).(L, 1);
647 float left = luaL_optnumbercxx . . $ luaL_optnumber(67a0f3e7bac8441f).(L, 2, 1.0);
648 float right = luaL_optnumbercxx . . $ luaL_optnumber(67a0f3e7bac8441f).(L, 3, left);
649 int loop = luaL_optintcxx . . $ `<external>/18095755238171912061/lauxlib.h:105:9`!(L, 4, 1);
650
651 int channel = play_sound_effectcxx . . $ play_sound_effect(caf4a90803836d9d).(sound, left, right, loop);
652
653 if(channel < 0)
654 lua_pushnilcxx . . $ lua_pushnil(f6fa9609f70d70d8).(L);
655 else
656 lua_pushnumbercxx . . $ lua_pushnumber(7d033ffc6f828456).(L, channel);
657 return 1;
658 }
659
660 static const luaL_regcxx . . $ `<external>/18095755238171912061/lauxlib.h:170:9`! sound_effect_libcxx . . $ sound_effect_lib.[] =
661 {
662 {"play", sound_effect__playcxx . . $ sound_effect__play(e4b172c3fcff213).},
663 {NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!}
664 };
665
666 static const luaL_Regcxx . . $ luaL_Reg# mixer_libcxx . . $ mixer_lib.[] =
667 {
668 {"init", mixer__initcxx . . $ mixer__init(e4b172c3fcff213).},
669 {"uninit", mixer__uninitcxx . . $ mixer__uninit(e4b172c3fcff213).},
670 {"load_wav", mixer__load_wavcxx . . $ mixer__load_wav(e4b172c3fcff213).},
671 {"load_ogg", mixer__load_oggcxx . . $ mixer__load_ogg(e4b172c3fcff213).},
672 {"channel_fade_to", mixer__channel_fade_tocxx . . $ mixer__channel_fade_to(e4b172c3fcff213).},
673 {"channel_stop", mixer__channel_stopcxx . . $ mixer__channel_stop(e4b172c3fcff213).},
674 {NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!}
675 };
676
677 int luaopen_mixercxx . . $ luaopen_mixer(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
678 {
679 lua_newuserdatacxx . . $ lua_newuserdata(bb92b2d7de290).(L, 0);
680 lua_newtablecxx . . $ `<external>/15848857005865937740/lua.h:256:9`!(L);
681 lua_pushcfunctioncxx . . $ `<external>/15848857005865937740/lua.h:260:9`!(L, mixer__uninit);
682 lua_setfieldcxx . . $ lua_setfield(5a7f0bea59244d1d).(L, -2, "__gc");
683 lua_setmetatablecxx . . $ lua_setmetatable(95e3aa3a3b4bb934).(L, -2);
684 lua_setfieldcxx . . $ lua_setfield(5a7f0bea59244d1d).(L, LUA_REGISTRYINDEXcxx . . $ `<external>/15848857005865937740/lua.h:36:9`!, "mixer.uninitializer");
685
686 luaL_newmetatablecxx . . $ luaL_newmetatable(ac61bb1a0c4e7605).(L, "mixer.sound_effect");
687 lua_pushvaluecxx . . $ lua_pushvalue(9637dd485cf22a1).(L, -1);
688 lua_setfieldcxx . . $ lua_setfield(5a7f0bea59244d1d).(L, -2, "__index");
689 luaL_registercxx . . $ luaL_register(78d27bf8f6419b9e).(L, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, sound_effect_libcxx . . $ sound_effect_lib.);
690
691 lua_newtablecxx . . $ `<external>/15848857005865937740/lua.h:256:9`!(L);
692 luaL_registercxx . . $ luaL_register(78d27bf8f6419b9e).(L, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, mixer_libcxx . . $ mixer_lib.);
693 return 1;
694 }
695