uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit d94a84bc936e4a2c41338c386a33ba8fb4cc5b43
parent c2b0667496096784fe28d7b89c86e26882849b82
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date:   Mon,  2 Jan 2023 15:01:55 +0000

Port Audio to new devices scheme.

Diffstat:
Msrc/devices/audio.c | 18+++++++++---------
Msrc/devices/audio.h | 19+------------------
Msrc/uxnemu.c | 60++++++++++++++++++++++++++++++++----------------------------
3 files changed, 42 insertions(+), 55 deletions(-)

diff --git a/src/devices/audio.c b/src/devices/audio.c @@ -72,21 +72,21 @@ audio_render(int instance, Sint16 *sample, Sint16 *end) } void -audio_start(int instance, Device *d) +audio_start(int instance, Uint8 *d, Uxn *u) { UxnAudio *c = &uxn_audio[instance]; Uint16 addr, adsr; Uint8 pitch; - DEVPEEK16(adsr, 0x8); - DEVPEEK16(c->len, 0xa); - DEVPEEK16(addr, 0xc); + PEKDEV(adsr, 0x8); + PEKDEV(c->len, 0xa); + PEKDEV(addr, 0xc); if(c->len > 0x10000 - addr) c->len = 0x10000 - addr; - c->addr = &d->u->ram[addr]; - c->volume[0] = d->dat[0xe] >> 4; - c->volume[1] = d->dat[0xe] & 0xf; - c->repeat = !(d->dat[0xf] & 0x80); - pitch = d->dat[0xf] & 0x7f; + c->addr = &u->ram[addr]; + c->volume[0] = d[0xe] >> 4; + c->volume[1] = d[0xe] & 0xf; + c->repeat = !(d[0xf] & 0x80); + pitch = d[0xf] & 0x7f; if(pitch < 108 && c->len) c->advance = advances[pitch % 12] >> (8 - pitch / 12); else { diff --git a/src/devices/audio.h b/src/devices/audio.h @@ -15,25 +15,8 @@ typedef signed int Sint32; #define SAMPLE_FREQUENCY 44100 #define POLYPHONY 4 -#define DEVPEEK16(o, x) \ - { \ - (o) = (d->dat[(x)] << 8) + d->dat[(x) + 1]; \ - } -#define DEVPOKE16(x, y) \ - { \ - d->dat[(x)] = (y) >> 8; \ - d->dat[(x) + 1] = (y); \ - } - -typedef struct Device { - struct Uxn *u; - Uint8 dat[16]; - Uint8 (*dei)(struct Device *d, Uint8); - void (*deo)(struct Device *d, Uint8); -} Device; - Uint8 audio_get_vu(int instance); Uint16 audio_get_position(int instance); int audio_render(int instance, Sint16 *sample, Sint16 *end); -void audio_start(int instance, Device *d); +void audio_start(int instance, Uint8 *d, Uxn *u); void audio_finished_handler(int instance); diff --git a/src/uxnemu.c b/src/uxnemu.c @@ -48,7 +48,6 @@ static SDL_Thread *stdin_thread; /* devices */ -static Device *devaudio0; static Uint8 zoom = 1; static Uint32 stdin_event, audio0_event; static Uint64 exec_deadline, deadline_interval, ms_interval; @@ -83,11 +82,38 @@ console_deo(Uint8 *d, Uint8 port) } static Uint8 +audio_dei(int instance, Uint8 *d, Uint8 port) +{ + if(!audio_id) return d[port]; + switch(port) { + case 0x4: return audio_get_vu(instance); + case 0x2: POKDEV(0x2, audio_get_position(instance)); /* fall through */ + default: return d[port]; + } +} + +static void +audio_deo(int instance, Uint8 *d, Uint8 port, Uxn *u) +{ + if(!audio_id) return; + if(port == 0xf) { + SDL_LockAudioDevice(audio_id); + audio_start(instance, d, u); + SDL_UnlockAudioDevice(audio_id); + SDL_PauseAudioDevice(audio_id, 0); + } +} + +static Uint8 emu_dei(Uxn *u, Uint8 addr) { Uint8 p = addr & 0x0f, d = addr & 0xf0; switch(d) { case 0x20: return screen_dei(&u->dev[d], p); + case 0x30: return audio_dei(0, &u->dev[d], p); + case 0x40: return audio_dei(1, &u->dev[d], p); + case 0x50: return audio_dei(2, &u->dev[d], p); + case 0x60: return audio_dei(3, &u->dev[d], p); case 0xa0: return file_dei(0, &u->dev[d], p); case 0xb0: return file_dei(1, &u->dev[d], p); case 0xc0: return datetime_dei(&u->dev[d], p); @@ -109,6 +135,10 @@ emu_deo(Uxn *u, Uint8 addr, Uint8 v) break; case 0x10: console_deo(&u->dev[d], p); break; case 0x20: screen_deo(u->ram, &u->dev[d], p); break; + case 0x30: audio_deo(0, &u->dev[d], p, u); break; + case 0x40: audio_deo(1, &u->dev[d], p, u); break; + case 0x50: audio_deo(2, &u->dev[d], p, u); break; + case 0x60: audio_deo(3, &u->dev[d], p, u); break; case 0xa0: file_deo(0, u->ram, &u->dev[d], p); break; case 0xb0: file_deo(1, u->ram, &u->dev[d], p); break; } @@ -227,31 +257,6 @@ init(void) #pragma mark - Devices -static Uint8 -audio_dei(Device *d, Uint8 port) -{ - int instance = d - devaudio0; - if(!audio_id) return d->dat[port]; - switch(port) { - case 0x4: return audio_get_vu(instance); - case 0x2: DEVPOKE16(0x2, audio_get_position(instance)); /* fall through */ - default: return d->dat[port]; - } -} - -static void -audio_deo(Device *d, Uint8 port) -{ - int instance = d - devaudio0; - if(!audio_id) return; - if(port == 0xf) { - SDL_LockAudioDevice(audio_id); - audio_start(instance, d); - SDL_UnlockAudioDevice(audio_id); - SDL_PauseAudioDevice(audio_id, 0); - } -} - /* Boot */ static int @@ -379,8 +384,7 @@ handle_events(Uxn *u) } /* Audio */ else if(event.type >= audio0_event && event.type < audio0_event + POLYPHONY) { - /* Device *d = devaudio0 + (event.type - audio0_event); - uxn_eval(u, GETVECTOR(d)); */ + uxn_eval(u, GETVEC(&u->dev[0x30 + 0x10 * (event.type - audio0_event)])); } /* Mouse */ else if(event.type == SDL_MOUSEMOTION)