Browse code

Formatting

neauoire authored on 20/10/2023 15:54:25
Showing 3 changed files
... ...
@@ -19,42 +19,44 @@ WITH REGARD TO THIS SOFTWARE.
19 19
 #define INTERPOL_METHOD 1
20 20
 
21 21
 typedef enum EnvStage {
22
-    ENV_ATTACK  = (1 << 0),
23
-    ENV_DECAY   = (1 << 1),
24
-    ENV_SUSTAIN = (1 << 2),
25
-    ENV_RELEASE = (1 << 3),
22
+	ENV_ATTACK = (1 << 0),
23
+	ENV_DECAY = (1 << 1),
24
+	ENV_SUSTAIN = (1 << 2),
25
+	ENV_RELEASE = (1 << 3),
26 26
 } EnvStage;
27 27
 
28 28
 typedef struct Envelope {
29
-    float a;
30
-    float d;
31
-    float s;
32
-    float r;
33
-    float vol;
34
-    EnvStage stage;
29
+	float a;
30
+	float d;
31
+	float s;
32
+	float r;
33
+	float vol;
34
+	EnvStage stage;
35 35
 } Envelope;
36 36
 
37 37
 typedef struct Sample {
38
-    Uint8 *data;
39
-    float len;
40
-    float pos;
41
-    float inc;
42
-    float loop;
43
-    Uint8 pitch;
44
-    Envelope env;
38
+	Uint8 *data;
39
+	float len;
40
+	float pos;
41
+	float inc;
42
+	float loop;
43
+	Uint8 pitch;
44
+	Envelope env;
45 45
 } Sample;
46 46
 
47 47
 typedef struct AudioChannel {
48
-    Sample sample;
49
-    Sample next_sample;
50
-    bool xfade;
51
-    float duration;
52
-    float vol_l;
53
-    float vol_r;
48
+	Sample sample;
49
+	Sample next_sample;
50
+	bool xfade;
51
+	float duration;
52
+	float vol_l;
53
+	float vol_r;
54 54
 } AudioChannel;
55 55
 
56 56
 AudioChannel channel[POLYPHONY];
57 57
 
58
+/* clang-format off */
59
+
58 60
 const float tuning[109] = {
59 61
         0.00058853f, 0.00062352f, 0.00066060f, 0.00069988f, 0.00074150f,
60 62
         0.00078559f, 0.00083230f, 0.00088179f, 0.00093423f, 0.00098978f,
... ...
@@ -80,236 +82,247 @@ const float tuning[109] = {
80 82
         0.25338348f, 0.26845044f, 0.28441334f, 0.30132544f,
81 83
 };
82 84
 
85
+/* clang-format on */
86
+
83 87
 void
84
-env_on(Envelope *env) {
85
-    env->stage = ENV_ATTACK;
86
-    env->vol = 0.0f;
87
-    if (env->a > 0) {
88
-        env->a = (SOUND_TIMER / AUDIO_BUFSIZE) / env->a;
89
-    } else if (env->stage == ENV_ATTACK) {
90
-        env->stage = ENV_DECAY;
91
-        env->vol = 1.0f;
92
-    }
93
-    if (env->d < 10.0f) {
94
-        env->d = 10.0f;
95
-    }
96
-    env->d = (SOUND_TIMER / AUDIO_BUFSIZE) / env->d;
97
-    if (env->r < 10.0f) {
98
-        env->r = 10.0f;
99
-    }
100
-    env->r = (SOUND_TIMER / AUDIO_BUFSIZE) / env->r;
88
+env_on(Envelope *env)
89
+{
90
+	env->stage = ENV_ATTACK;
91
+	env->vol = 0.0f;
92
+	if(env->a > 0) {
93
+		env->a = (SOUND_TIMER / AUDIO_BUFSIZE) / env->a;
94
+	} else if(env->stage == ENV_ATTACK) {
95
+		env->stage = ENV_DECAY;
96
+		env->vol = 1.0f;
97
+	}
98
+	if(env->d < 10.0f) {
99
+		env->d = 10.0f;
100
+	}
101
+	env->d = (SOUND_TIMER / AUDIO_BUFSIZE) / env->d;
102
+	if(env->r < 10.0f) {
103
+		env->r = 10.0f;
104
+	}
105
+	env->r = (SOUND_TIMER / AUDIO_BUFSIZE) / env->r;
101 106
 }
102 107
 
103 108
 void
104
-env_off(Envelope *env) {
105
-    env->stage = ENV_RELEASE;
109
+env_off(Envelope *env)
110
+{
111
+	env->stage = ENV_RELEASE;
106 112
 }
107 113
 
108 114
 void
109
-note_on(AudioChannel *channel, Uint16 duration, Uint8 *data, Uint16 len, Uint8 vol,
110
-        Uint8 attack, Uint8 decay, Uint8 sustain, Uint8 release, Uint8 pitch, bool loop) {
111
-    channel->duration = duration > 0 ? duration : len / 44.1f;
112
-    channel->vol_l = (vol >> 4) / 15.0f;
113
-    channel->vol_r = (vol & 0xf) / 15.0f;
114
-
115
-    Sample sample = {0};
116
-    sample.data = data;
117
-    sample.len = len;
118
-    sample.pos = 0;
119
-    sample.env.a = attack * 64.0f;
120
-    sample.env.d = decay * 64.0f;
121
-    sample.env.s = sustain / 16.0f;
122
-    sample.env.r = release * 64.0f;
123
-    if (loop) {
124
-        sample.loop = len;
125
-    } else {
126
-        sample.loop = 0;
127
-    }
128
-    env_on(&sample.env);
129
-    if (pitch < 20) {
130
-        pitch = 20;
131
-    }
132
-    float sample_rate = 44100 / 261.60;
133
-    if (len <= 256) {
134
-        sample_rate = len;
135
-    }
136
-    const float *inc = &tuning[pitch - 20];
137
-    sample.inc = *(inc) * sample_rate;
138
-
139
-    channel->next_sample = sample;
140
-    channel->xfade = true;
115
+note_on(AudioChannel *channel, Uint16 duration, Uint8 *data, Uint16 len, Uint8 vol, Uint8 attack, Uint8 decay, Uint8 sustain, Uint8 release, Uint8 pitch, bool loop)
116
+{
117
+	channel->duration = duration > 0 ? duration : len / 44.1f;
118
+	channel->vol_l = (vol >> 4) / 15.0f;
119
+	channel->vol_r = (vol & 0xf) / 15.0f;
120
+
121
+	Sample sample = {0};
122
+	sample.data = data;
123
+	sample.len = len;
124
+	sample.pos = 0;
125
+	sample.env.a = attack * 64.0f;
126
+	sample.env.d = decay * 64.0f;
127
+	sample.env.s = sustain / 16.0f;
128
+	sample.env.r = release * 64.0f;
129
+	if(loop) {
130
+		sample.loop = len;
131
+	} else {
132
+		sample.loop = 0;
133
+	}
134
+	env_on(&sample.env);
135
+	if(pitch < 20) {
136
+		pitch = 20;
137
+	}
138
+	float sample_rate = 44100 / 261.60;
139
+	if(len <= 256) {
140
+		sample_rate = len;
141
+	}
142
+	const float *inc = &tuning[pitch - 20];
143
+	sample.inc = *(inc)*sample_rate;
144
+
145
+	channel->next_sample = sample;
146
+	channel->xfade = true;
141 147
 }
142 148
 
143 149
 void
144
-note_off(AudioChannel *channel, Uint16 duration) {
145
-    channel->duration = duration;
146
-    env_off(&channel->sample.env);
150
+note_off(AudioChannel *channel, Uint16 duration)
151
+{
152
+	channel->duration = duration;
153
+	env_off(&channel->sample.env);
147 154
 }
148 155
 
149 156
 void
150
-env_advance(Envelope *env) {
151
-    switch (env->stage) {
152
-        case ENV_ATTACK: {
153
-            env->vol += env->a;
154
-            if (env->vol >= 1.0f) {
155
-                env->stage = ENV_DECAY;
156
-                env->vol = 1.0f;
157
-            }
158
-        } break;
159
-        case ENV_DECAY: {
160
-            env->vol -= env->d;
161
-            if (env->vol <= env->s || env->d <= 0) {
162
-                env->stage = ENV_SUSTAIN;
163
-                env->vol = env->s;
164
-            }
165
-        } break;
166
-        case ENV_SUSTAIN: {
167
-            env->vol = env->s;
168
-        } break;
169
-        case ENV_RELEASE: {
170
-            if (env->vol <= 0 || env->r <= 0) {
171
-                env->vol = 0;
172
-            } else {
173
-                env->vol -= env->r;
174
-            }
175
-        } break;
176
-    }
157
+env_advance(Envelope *env)
158
+{
159
+	switch(env->stage) {
160
+	case ENV_ATTACK: {
161
+		env->vol += env->a;
162
+		if(env->vol >= 1.0f) {
163
+			env->stage = ENV_DECAY;
164
+			env->vol = 1.0f;
165
+		}
166
+	} break;
167
+	case ENV_DECAY: {
168
+		env->vol -= env->d;
169
+		if(env->vol <= env->s || env->d <= 0) {
170
+			env->stage = ENV_SUSTAIN;
171
+			env->vol = env->s;
172
+		}
173
+	} break;
174
+	case ENV_SUSTAIN: {
175
+		env->vol = env->s;
176
+	} break;
177
+	case ENV_RELEASE: {
178
+		if(env->vol <= 0 || env->r <= 0) {
179
+			env->vol = 0;
180
+		} else {
181
+			env->vol -= env->r;
182
+		}
183
+	} break;
184
+	}
177 185
 }
178 186
 
179 187
 float
180
-interpolate_sample(Uint8 *data, Uint16 len, float pos) {
188
+interpolate_sample(Uint8 *data, Uint16 len, float pos)
189
+{
181 190
 #if INTERPOL_METHOD == 0
182
-    return data[(int)pos];
191
+	return data[(int)pos];
183 192
 
184 193
 #elif INTERPOL_METHOD == 1
185
-    float x = pos;
186
-    int x0 = (int)x;
187
-    int x1 = (x0 + 1);
188
-    float y0 = data[x0];
189
-    float y1 = data[x1 % len];
190
-    x = x - x0;
191
-    float y = y0 + x * (y1 - y0);
192
-    return y;
194
+	float x = pos;
195
+	int x0 = (int)x;
196
+	int x1 = (x0 + 1);
197
+	float y0 = data[x0];
198
+	float y1 = data[x1 % len];
199
+	x = x - x0;
200
+	float y = y0 + x * (y1 - y0);
201
+	return y;
193 202
 
194 203
 #elif INTERPOL_METHOD == 2
195
-    float x = pos;
196
-    int x0 = x - 1;
197
-    int x1 = x;
198
-    int x2 = x + 1;
199
-    int x3 = x + 2;
200
-    float y0 = data[x0 % len];
201
-    float y1 = data[x1];
202
-    float y2 = data[x2 % len];
203
-    float y3 = data[x3 % len];
204
-    x = x - x1;
205
-    float c0 = y1;
206
-    float c1 = 0.5f * (y2 - y0);
207
-    float c2 = y0 - 2.5f * y1 + 2.f * y2 - 0.5f * y3;
208
-    float c3 = 1.5f * (y1 - y2) + 0.5f * (y3 - y0);
209
-    return ((c3 * x + c2) * x + c1) * x + c0;
204
+	float x = pos;
205
+	int x0 = x - 1;
206
+	int x1 = x;
207
+	int x2 = x + 1;
208
+	int x3 = x + 2;
209
+	float y0 = data[x0 % len];
210
+	float y1 = data[x1];
211
+	float y2 = data[x2 % len];
212
+	float y3 = data[x3 % len];
213
+	x = x - x1;
214
+	float c0 = y1;
215
+	float c1 = 0.5f * (y2 - y0);
216
+	float c2 = y0 - 2.5f * y1 + 2.f * y2 - 0.5f * y3;
217
+	float c3 = 1.5f * (y1 - y2) + 0.5f * (y3 - y0);
218
+	return ((c3 * x + c2) * x + c1) * x + c0;
210 219
 #endif
211 220
 }
212 221
 
213 222
 Sint16
214
-next_sample(Sample *sample) {
215
-    if (sample->pos >= sample->len) {
216
-        if (sample->loop == 0) {
217
-            sample->data = 0;
218
-            return 0;
219
-        }
220
-        while (sample->pos >= sample->len) {
221
-            sample->pos -= sample->loop;
222
-        }
223
-    }
224
-
225
-    float val = interpolate_sample(sample->data, sample->len, sample->pos);
226
-    val *= sample->env.vol;
227
-    Sint8 next = (Sint8)0x80 ^ (Uint8)val;
228
-
229
-    sample->pos += sample->inc;
230
-    env_advance(&sample->env);
231
-    return next;
223
+next_sample(Sample *sample)
224
+{
225
+	if(sample->pos >= sample->len) {
226
+		if(sample->loop == 0) {
227
+			sample->data = 0;
228
+			return 0;
229
+		}
230
+		while(sample->pos >= sample->len) {
231
+			sample->pos -= sample->loop;
232
+		}
233
+	}
234
+
235
+	float val = interpolate_sample(sample->data, sample->len, sample->pos);
236
+	val *= sample->env.vol;
237
+	Sint8 next = (Sint8)0x80 ^ (Uint8)val;
238
+
239
+	sample->pos += sample->inc;
240
+	env_advance(&sample->env);
241
+	return next;
232 242
 }
233 243
 
234 244
 void
235
-audio_handler(void *ctx, Uint8 *out_stream, int len) {
236
-    Sint16 *stream = (Sint16 *)out_stream;
237
-    memset(stream, 0x00, len);
238
-
239
-    int n;
240
-    for (n = 0; n < POLYPHONY; n++) {
241
-        Uint8 device = (3 + n) << 4;
242
-        Uxn *u = (Uxn *)ctx;
243
-        Uint8 *addr = &u->dev[device];
244
-        if (channel[n].duration <= 0 && PEEK2(addr)) {
245
+audio_handler(void *ctx, Uint8 *out_stream, int len)
246
+{
247
+	Sint16 *stream = (Sint16 *)out_stream;
248
+	memset(stream, 0x00, len);
249
+
250
+	int n;
251
+	for(n = 0; n < POLYPHONY; n++) {
252
+		Uint8 device = (3 + n) << 4;
253
+		Uxn *u = (Uxn *)ctx;
254
+		Uint8 *addr = &u->dev[device];
255
+		if(channel[n].duration <= 0 && PEEK2(addr)) {
245 256
 			uxn_eval(u, PEEK2(addr));
246
-        }
247
-        channel[n].duration -= SOUND_TIMER;
248
-
249
-        int x = 0;
250
-        if (channel[n].xfade) {
251
-            float delta = 1.0f / (XFADE_SAMPLES * 2);
252
-            while (x < XFADE_SAMPLES * 2) {
253
-                float alpha = x * delta;
254
-                float beta = 1.0f - alpha;
255
-                Sint16 next_a = next_sample(&channel[n].next_sample);
256
-                Sint16 next_b = 0;
257
-                if (channel[n].sample.data != 0) {
258
-                    next_b = next_sample(&channel[n].sample);
259
-                }
260
-                Sint16 next = alpha * next_a + beta * next_b;
261
-                stream[x++] += next * channel[n].vol_l;
262
-                stream[x++] += next * channel[n].vol_r;
263
-            }
264
-            channel[n].sample = channel[n].next_sample;
265
-            channel[n].xfade = false;
266
-        }
267
-        Sample *sample = &channel[n].sample;
268
-        while (x < len / 2) {
269
-            if (sample->data == 0) {
270
-                break;
271
-            }
272
-            Sint16 next = next_sample(sample);
273
-            stream[x++] += next * channel[n].vol_l;
274
-            stream[x++] += next * channel[n].vol_r;
275
-        }
276
-    }
277
-    int i;
278
-    for (i = 0; i < len / 2; i++) {
279
-        stream[i] <<= 6;
280
-    }
257
+		}
258
+		channel[n].duration -= SOUND_TIMER;
259
+
260
+		int x = 0;
261
+		if(channel[n].xfade) {
262
+			float delta = 1.0f / (XFADE_SAMPLES * 2);
263
+			while(x < XFADE_SAMPLES * 2) {
264
+				float alpha = x * delta;
265
+				float beta = 1.0f - alpha;
266
+				Sint16 next_a = next_sample(&channel[n].next_sample);
267
+				Sint16 next_b = 0;
268
+				if(channel[n].sample.data != 0) {
269
+					next_b = next_sample(&channel[n].sample);
270
+				}
271
+				Sint16 next = alpha * next_a + beta * next_b;
272
+				stream[x++] += next * channel[n].vol_l;
273
+				stream[x++] += next * channel[n].vol_r;
274
+			}
275
+			channel[n].sample = channel[n].next_sample;
276
+			channel[n].xfade = false;
277
+		}
278
+		Sample *sample = &channel[n].sample;
279
+		while(x < len / 2) {
280
+			if(sample->data == 0) {
281
+				break;
282
+			}
283
+			Sint16 next = next_sample(sample);
284
+			stream[x++] += next * channel[n].vol_l;
285
+			stream[x++] += next * channel[n].vol_r;
286
+		}
287
+	}
288
+	int i;
289
+	for(i = 0; i < len / 2; i++) {
290
+		stream[i] <<= 6;
291
+	}
281 292
 }
282 293
 
283 294
 void
284 295
 audio_start(int idx, Uint8 *d, Uxn *u)
285 296
 {
286
-    Uint16 duration = PEEK2(d + 0x5);
287
-    Uint8 off = d[0xf] == 0x00;
288
-
289
-    if (!off) {
290
-        Uint16 addr = PEEK2(d + 0xc);
291
-        Uint8 *data = &u->ram[addr];
292
-        Uint16 len = PEEK2(d + 0xa);
293
-        Uint8 volume = d[0xe];
294
-        bool loop = !(d[0xf] & 0x80);
295
-        Uint8 pitch = d[0xf] & 0x7f;
296
-        Uint16 adsr = PEEK2(d + 0x8);
297
-        Uint8 attack = (adsr >> 12) & 0xF;
298
-        Uint8 decay = (adsr >> 8) & 0xF;
299
-        Uint8 sustain = (adsr >> 4) & 0xF;
300
-        Uint8 release = (adsr >> 0) & 0xF;
301
-        note_on(&channel[idx], duration, data, len, volume, attack, decay, sustain, release, pitch, loop);
302
-    } else {
303
-        note_off(&channel[idx], duration);
304
-    }
297
+	Uint16 duration = PEEK2(d + 0x5);
298
+	Uint8 off = d[0xf] == 0x00;
299
+
300
+	if(!off) {
301
+		Uint16 addr = PEEK2(d + 0xc);
302
+		Uint8 *data = &u->ram[addr];
303
+		Uint16 len = PEEK2(d + 0xa);
304
+		Uint8 volume = d[0xe];
305
+		bool loop = !(d[0xf] & 0x80);
306
+		Uint8 pitch = d[0xf] & 0x7f;
307
+		Uint16 adsr = PEEK2(d + 0x8);
308
+		Uint8 attack = (adsr >> 12) & 0xF;
309
+		Uint8 decay = (adsr >> 8) & 0xF;
310
+		Uint8 sustain = (adsr >> 4) & 0xF;
311
+		Uint8 release = (adsr >> 0) & 0xF;
312
+		note_on(&channel[idx], duration, data, len, volume, attack, decay, sustain, release, pitch, loop);
313
+	} else {
314
+		note_off(&channel[idx], duration);
315
+	}
305 316
 }
306 317
 
307 318
 Uint8
308
-audio_get_vu(int instance) {
309
-    return channel[instance].sample.env.vol * 255.0f;
319
+audio_get_vu(int instance)
320
+{
321
+	return channel[instance].sample.env.vol * 255.0f;
310 322
 }
311 323
 
312 324
 Uint16
313
-audio_get_position(int instance) {
314
-    return channel[instance].sample.pos;
325
+audio_get_position(int instance)
326
+{
327
+	return channel[instance].sample.pos;
315 328
 }
... ...
@@ -64,7 +64,7 @@ emu_end(Uxn *u)
64 64
 int
65 65
 main(int argc, char **argv)
66 66
 {
67
-    Uint8 dev[0x100] = {0};
67
+	Uint8 dev[0x100] = {0};
68 68
 	Uxn u;
69 69
 	u.dev = (Uint8 *)&dev;
70 70
 	int i = 1;
... ...
@@ -70,20 +70,20 @@ clamp(int v, int min, int max)
70 70
 static Uint8
71 71
 audio_dei(int instance, Uint8 *d, Uint8 port)
72 72
 {
73
-    switch(port) {
74
-        case 0x2:
75
-            return audio_get_position(instance) >> 8;
76
-        case 0x3:
77
-            return audio_get_position(instance);
78
-        case 0x4:
79
-            return audio_get_vu(instance);
80
-        case 0x0:
81
-        case 0x8:
82
-        case 0xa:
83
-        case 0xc: return PEEK2(d + port);
84
-        default: return d[port];
85
-    }
86
-    return d[port];
73
+	switch(port) {
74
+	case 0x2:
75
+		return audio_get_position(instance) >> 8;
76
+	case 0x3:
77
+		return audio_get_position(instance);
78
+	case 0x4:
79
+		return audio_get_vu(instance);
80
+	case 0x0:
81
+	case 0x8:
82
+	case 0xa:
83
+	case 0xc: return PEEK2(d + port);
84
+	default: return d[port];
85
+	}
86
+	return d[port];
87 87
 }
88 88
 
89 89
 static void
... ...
@@ -94,7 +94,7 @@ audio_deo(int instance, Uint8 *d, Uint8 port, Uxn *u)
94 94
 		SDL_LockAudioDevice(audio_id);
95 95
 		audio_start(instance, d, u);
96 96
 		SDL_UnlockAudioDevice(audio_id);
97
-        SDL_PauseAudioDevice(audio_id, 0);
97
+		SDL_PauseAudioDevice(audio_id, 0);
98 98
 	}
99 99
 }
100 100
 
... ...
@@ -271,7 +271,7 @@ emu_init(Uxn *u)
271 271
 	deadline_interval = ms_interval * TIMEOUT_MS;
272 272
 	exec_deadline = SDL_GetPerformanceCounter() + deadline_interval;
273 273
 	screen_resize(WIDTH, HEIGHT);
274
-    SDL_PauseAudioDevice(audio_id, 1);
274
+	SDL_PauseAudioDevice(audio_id, 1);
275 275
 	return 1;
276 276
 }
277 277
 
... ...
@@ -514,7 +514,7 @@ emu_run(Uxn *u, char *rom)
514 514
 static int
515 515
 emu_end(Uxn *u)
516 516
 {
517
-    SDL_CloseAudioDevice(audio_id);
517
+	SDL_CloseAudioDevice(audio_id);
518 518
 #ifdef _WIN32
519 519
 #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
520 520
 	TerminateThread((HANDLE)SDL_GetThreadID(stdin_thread), 0);
... ...
@@ -529,7 +529,7 @@ emu_end(Uxn *u)
529 529
 int
530 530
 main(int argc, char **argv)
531 531
 {
532
-    Uint8 dev[0x100] = {0};
532
+	Uint8 dev[0x100] = {0};
533 533
 	Uxn u = {0};
534 534
 	Uxn u_audio = {0};
535 535
 	u.dev = (Uint8 *)&dev;
... ...
@@ -555,10 +555,10 @@ main(int argc, char **argv)
555 555
 	char *rom = argv[i++];
556 556
 	if(!system_init(&u, ram, rom)) {
557 557
 		return system_error("Init", "Failed to initialize uxn.");
558
-    }
558
+	}
559 559
 	if(!system_init(&u_audio, ram, rom)) {
560 560
 		return system_error("Init", "Failed to initialize uxn.");
561
-    }
561
+	}
562 562
 	if(!emu_init(&u_audio))
563 563
 		return system_error("Init", "Failed to initialize varvara.");
564 564
 	/* Game Loop */