Browse code

Take pitch into account when computing duration.

d_m authored on 20/10/2023 23:38:01 • neauoire committed on 03/11/2023 00:30:14
Showing 1 changed files
... ...
@@ -291,20 +291,27 @@ audio_handler(void *ctx, Uint8 *out_stream, int len)
291 291
 	}
292 292
 }
293 293
 
294
+float
295
+calc_duration(Uint16 len, Uint8 pitch)
296
+{
297
+	float scale = tuning[pitch - 20] / tuning[0x3c - 20];
298
+	return len / (scale * 44.1f);
299
+}
300
+
294 301
 void
295 302
 audio_start(int idx, Uint8 *d, Uxn *u)
296 303
 {
297 304
 	Uint16 dur = PEEK2(d + 0x5);
298 305
 	Uint8 off = d[0xf] == 0x00;
299 306
 	Uint16 len = PEEK2(d + 0xa);
300
-	float duration = dur > 0 ? dur : len / 44.1f;
307
+	Uint8 pitch = d[0xf] & 0x7f;
308
+	float duration = dur > 0 ? dur : calc_duration(len, pitch);
301 309
 
302 310
 	if(!off) {
303 311
 		Uint16 addr = PEEK2(d + 0xc);
304 312
 		Uint8 *data = &u->ram[addr];
305 313
 		Uint8 volume = d[0xe];
306 314
 		bool loop = !(d[0xf] & 0x80);
307
-		Uint8 pitch = d[0xf] & 0x7f;
308 315
 		Uint16 adsr = PEEK2(d + 0x8);
309 316
 		Uint8 attack = (adsr >> 12) & 0xF;
310 317
 		Uint8 decay = (adsr >> 8) & 0xF;