Browse code

Port Audio to new devices scheme.

Andrew Alderwick authored on 02/01/2023 15:01:55
Showing 3 changed files
... ...
@@ -72,21 +72,21 @@ audio_render(int instance, Sint16 *sample, Sint16 *end)
72 72
 }
73 73
 
74 74
 void
75
-audio_start(int instance, Device *d)
75
+audio_start(int instance, Uint8 *d, Uxn *u)
76 76
 {
77 77
 	UxnAudio *c = &uxn_audio[instance];
78 78
 	Uint16 addr, adsr;
79 79
 	Uint8 pitch;
80
-	DEVPEEK16(adsr, 0x8);
81
-	DEVPEEK16(c->len, 0xa);
82
-	DEVPEEK16(addr, 0xc);
80
+	PEKDEV(adsr, 0x8);
81
+	PEKDEV(c->len, 0xa);
82
+	PEKDEV(addr, 0xc);
83 83
 	if(c->len > 0x10000 - addr)
84 84
 		c->len = 0x10000 - addr;
85
-	c->addr = &d->u->ram[addr];
86
-	c->volume[0] = d->dat[0xe] >> 4;
87
-	c->volume[1] = d->dat[0xe] & 0xf;
88
-	c->repeat = !(d->dat[0xf] & 0x80);
89
-	pitch = d->dat[0xf] & 0x7f;
85
+	c->addr = &u->ram[addr];
86
+	c->volume[0] = d[0xe] >> 4;
87
+	c->volume[1] = d[0xe] & 0xf;
88
+	c->repeat = !(d[0xf] & 0x80);
89
+	pitch = d[0xf] & 0x7f;
90 90
 	if(pitch < 108 && c->len)
91 91
 		c->advance = advances[pitch % 12] >> (8 - pitch / 12);
92 92
 	else {
... ...
@@ -15,25 +15,8 @@ typedef signed int Sint32;
15 15
 #define SAMPLE_FREQUENCY 44100
16 16
 #define POLYPHONY 4
17 17
 
18
-#define DEVPEEK16(o, x) \
19
-	{ \
20
-		(o) = (d->dat[(x)] << 8) + d->dat[(x) + 1]; \
21
-	}
22
-#define DEVPOKE16(x, y) \
23
-	{ \
24
-		d->dat[(x)] = (y) >> 8; \
25
-		d->dat[(x) + 1] = (y); \
26
-	}
27
-
28
-typedef struct Device {
29
-	struct Uxn *u;
30
-	Uint8 dat[16];
31
-	Uint8 (*dei)(struct Device *d, Uint8);
32
-	void (*deo)(struct Device *d, Uint8);
33
-} Device;
34
-
35 18
 Uint8 audio_get_vu(int instance);
36 19
 Uint16 audio_get_position(int instance);
37 20
 int audio_render(int instance, Sint16 *sample, Sint16 *end);
38
-void audio_start(int instance, Device *d);
21
+void audio_start(int instance, Uint8 *d, Uxn *u);
39 22
 void audio_finished_handler(int instance);
... ...
@@ -48,7 +48,6 @@ static SDL_Thread *stdin_thread;
48 48
 
49 49
 /* devices */
50 50
 
51
-static Device *devaudio0;
52 51
 static Uint8 zoom = 1;
53 52
 static Uint32 stdin_event, audio0_event;
54 53
 static Uint64 exec_deadline, deadline_interval, ms_interval;
... ...
@@ -82,12 +81,39 @@ console_deo(Uint8 *d, Uint8 port)
82 81
 	}
83 82
 }
84 83
 
84
+static Uint8
85
+audio_dei(int instance, Uint8 *d, Uint8 port)
86
+{
87
+	if(!audio_id) return d[port];
88
+	switch(port) {
89
+	case 0x4: return audio_get_vu(instance);
90
+	case 0x2: POKDEV(0x2, audio_get_position(instance)); /* fall through */
91
+	default: return d[port];
92
+	}
93
+}
94
+
95
+static void
96
+audio_deo(int instance, Uint8 *d, Uint8 port, Uxn *u)
97
+{
98
+	if(!audio_id) return;
99
+	if(port == 0xf) {
100
+		SDL_LockAudioDevice(audio_id);
101
+		audio_start(instance, d, u);
102
+		SDL_UnlockAudioDevice(audio_id);
103
+		SDL_PauseAudioDevice(audio_id, 0);
104
+	}
105
+}
106
+
85 107
 static Uint8
86 108
 emu_dei(Uxn *u, Uint8 addr)
87 109
 {
88 110
 	Uint8 p = addr & 0x0f, d = addr & 0xf0;
89 111
 	switch(d) {
90 112
 	case 0x20: return screen_dei(&u->dev[d], p);
113
+	case 0x30: return audio_dei(0, &u->dev[d], p);
114
+	case 0x40: return audio_dei(1, &u->dev[d], p);
115
+	case 0x50: return audio_dei(2, &u->dev[d], p);
116
+	case 0x60: return audio_dei(3, &u->dev[d], p);
91 117
 	case 0xa0: return file_dei(0, &u->dev[d], p);
92 118
 	case 0xb0: return file_dei(1, &u->dev[d], p);
93 119
 	case 0xc0: return datetime_dei(&u->dev[d], p);
... ...
@@ -109,6 +135,10 @@ emu_deo(Uxn *u, Uint8 addr, Uint8 v)
109 135
 		break;
110 136
 	case 0x10: console_deo(&u->dev[d], p); break;
111 137
 	case 0x20: screen_deo(u->ram, &u->dev[d], p); break;
138
+	case 0x30: audio_deo(0, &u->dev[d], p, u); break;
139
+	case 0x40: audio_deo(1, &u->dev[d], p, u); break;
140
+	case 0x50: audio_deo(2, &u->dev[d], p, u); break;
141
+	case 0x60: audio_deo(3, &u->dev[d], p, u); break;
112 142
 	case 0xa0: file_deo(0, u->ram, &u->dev[d], p); break;
113 143
 	case 0xb0: file_deo(1, u->ram, &u->dev[d], p); break;
114 144
 	}
... ...
@@ -227,31 +257,6 @@ init(void)
227 257
 
228 258
 #pragma mark - Devices
229 259
 
230
-static Uint8
231
-audio_dei(Device *d, Uint8 port)
232
-{
233
-	int instance = d - devaudio0;
234
-	if(!audio_id) return d->dat[port];
235
-	switch(port) {
236
-	case 0x4: return audio_get_vu(instance);
237
-	case 0x2: DEVPOKE16(0x2, audio_get_position(instance)); /* fall through */
238
-	default: return d->dat[port];
239
-	}
240
-}
241
-
242
-static void
243
-audio_deo(Device *d, Uint8 port)
244
-{
245
-	int instance = d - devaudio0;
246
-	if(!audio_id) return;
247
-	if(port == 0xf) {
248
-		SDL_LockAudioDevice(audio_id);
249
-		audio_start(instance, d);
250
-		SDL_UnlockAudioDevice(audio_id);
251
-		SDL_PauseAudioDevice(audio_id, 0);
252
-	}
253
-}
254
-
255 260
 /* Boot */
256 261
 
257 262
 static int
... ...
@@ -379,8 +384,7 @@ handle_events(Uxn *u)
379 384
 		}
380 385
 		/* Audio */
381 386
 		else if(event.type >= audio0_event && event.type < audio0_event + POLYPHONY) {
382
-			/* Device *d = devaudio0 + (event.type - audio0_event);
383
-			uxn_eval(u, GETVECTOR(d)); */
387
+			uxn_eval(u, GETVEC(&u->dev[0x30 + 0x10 * (event.type - audio0_event)]));
384 388
 		}
385 389
 		/* Mouse */
386 390
 		else if(event.type == SDL_MOUSEMOTION)