Browse code

Implemented Audio*/vector which runs when notes finish playing

Andrew Alderwick authored on 20/08/2021 21:45:39
Showing 3 changed files
... ...
@@ -49,7 +49,7 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end)
49 49
 		if(c->i >= c->len) {
50 50
 			if(!c->repeat) {
51 51
 				c->advance = 0;
52
-				return 1;
52
+				break;
53 53
 			}
54 54
 			c->i %= c->len;
55 55
 		}
... ...
@@ -57,6 +57,7 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end)
57 57
 		*sample++ += s * c->volume[0] / 0x180;
58 58
 		*sample++ += s * c->volume[1] / 0x180;
59 59
 	}
60
+	if(!c->advance) apu_finished_handler(c);
60 61
 	return 1;
61 62
 }
62 63
 
... ...
@@ -26,3 +26,4 @@ typedef struct {
26 26
 int apu_render(Apu *c, Sint16 *sample, Sint16 *end);
27 27
 void apu_start(Apu *c, Uint16 adsr, Uint8 pitch);
28 28
 Uint8 apu_get_vu(Apu *c);
29
+void apu_finished_handler(Apu *c);
... ...
@@ -32,7 +32,7 @@ static SDL_Rect gRect;
32 32
 static Ppu ppu;
33 33
 static Apu apu[POLYPHONY];
34 34
 static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
35
-static Uint32 stdin_event;
35
+static Uint32 stdin_event, audio0_event;
36 36
 
37 37
 static Uint8 zoom = 1, reqdraw = 0;
38 38
 
... ...
@@ -410,6 +410,14 @@ nil_talk(Device *d, Uint8 b0, Uint8 w)
410 410
 
411 411
 #pragma mark - Generics
412 412
 
413
+void
414
+apu_finished_handler(Apu *c)
415
+{
416
+	SDL_Event event;
417
+	event.type = audio0_event + (c - apu);
418
+	SDL_PushEvent(&event);
419
+}
420
+
413 421
 static int
414 422
 stdin_handler(void *p)
415 423
 {
... ...
@@ -474,6 +482,8 @@ run(Uxn *u)
474 482
 				if(event.type == stdin_event) {
475 483
 					devconsole->dat[0x2] = event.cbutton.button;
476 484
 					uxn_eval(u, mempeek16(devconsole->dat, 0));
485
+				} else if(event.type >= audio0_event && event.type < audio0_event + POLYPHONY) {
486
+					uxn_eval(u, mempeek16((devaudio0 + (event.type - audio0_event))->dat, 0));
477 487
 				}
478 488
 			}
479 489
 		}
... ...
@@ -504,6 +514,7 @@ main(int argc, char **argv)
504 514
 	Uxn u;
505 515
 
506 516
 	stdin_event = SDL_RegisterEvents(1);
517
+	audio0_event = SDL_RegisterEvents(POLYPHONY);
507 518
 	SDL_CreateThread(stdin_handler, "stdin", NULL);
508 519
 
509 520
 	if(argc < 2)