Implemented Midi device

This commit is contained in:
neauoire 2021-06-25 21:28:42 -07:00
parent 606c7707ff
commit 08b64ec33c
3 changed files with 26 additions and 16 deletions

View File

@ -13,30 +13,28 @@ WITH REGARD TO THIS SOFTWARE.
*/ */
int int
initmpu(Mpu *m, Uint8 device) initmpu(Mpu *m, Uint8 dev_in, Uint8 dev_out)
{ {
#ifndef NO_PORTMIDI #ifndef NO_PORTMIDI
int i; int i;
Pm_Initialize(); Pm_Initialize();
for(i = 0; i < Pm_CountDevices(); ++i) for(i = 0; i < Pm_CountDevices(); ++i)
printf("Device #%d -> %s%s\n", printf("Device #%d -> %s%s\n", i, Pm_GetDeviceInfo(i)->name, i == dev_in ? "[x]" : "[ ]");
i, Pm_OpenInput(&m->input, dev_in, NULL, 128, 0, NULL);
Pm_GetDeviceInfo(i)->name, Pm_OpenOutput(&m->output, dev_out, NULL, 128, 0, NULL, 1);
i == device ? "[x]" : "[ ]");
Pm_OpenInput(&m->midi, device, NULL, 128, 0, NULL);
m->queue = 0; m->queue = 0;
m->error = pmNoError; m->error = pmNoError;
#endif #endif
(void)m; (void)m;
(void)device; (void)dev_in;
return 1; return 1;
} }
void void
listenmpu(Mpu *m) getmidi(Mpu *m)
{ {
#ifndef NO_PORTMIDI #ifndef NO_PORTMIDI
const int result = Pm_Read(m->midi, m->events, 32); const int result = Pm_Read(m->input, m->events, 32);
if(result < 0) { if(result < 0) {
m->error = (PmError)result; m->error = (PmError)result;
m->queue = 0; m->queue = 0;
@ -46,3 +44,11 @@ listenmpu(Mpu *m)
#endif #endif
(void)m; (void)m;
} }
void
putmidi(Mpu *m, Uint8 chan, Uint8 note, Uint8 velo)
{
#ifndef NO_PORTMIDI
Pm_WriteShort(m->output, Pt_Time(), Pm_Message(0x90 + chan, note, velo));
#endif
}

View File

@ -15,6 +15,7 @@ WITH REGARD TO THIS SOFTWARE.
#ifndef NO_PORTMIDI #ifndef NO_PORTMIDI
#include <portmidi.h> #include <portmidi.h>
#include <porttime.h>
#else #else
typedef struct { typedef struct {
int message; int message;
@ -27,10 +28,11 @@ typedef struct {
Uint8 queue; Uint8 queue;
PmEvent events[32]; PmEvent events[32];
#ifndef NO_PORTMIDI #ifndef NO_PORTMIDI
PmStream *midi; PmStream *input, *output;
PmError error; PmError error;
#endif #endif
} Mpu; } Mpu;
int initmpu(Mpu *m, Uint8 device); int initmpu(Mpu *m, Uint8 dev_in, Uint8 dev_out);
void listenmpu(Mpu *m); void getmidi(Mpu *m);
void putmidi(Mpu *m, Uint8 chan, Uint8 note, Uint8 velo);

View File

@ -126,7 +126,7 @@ init(void)
gRect.y = PAD; gRect.y = PAD;
gRect.w = ppu.width; gRect.w = ppu.width;
gRect.h = ppu.height; gRect.h = ppu.height;
if(!initmpu(&mpu, 1)) if(!initmpu(&mpu, 1, 0))
return error("MPU", "Init failure"); return error("MPU", "Init failure");
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
return error("Init", SDL_GetError()); return error("Init", SDL_GetError());
@ -326,9 +326,11 @@ datetime_talk(Device *d, Uint8 b0, Uint8 w)
void void
midi_talk(Device *d, Uint8 b0, Uint8 w) midi_talk(Device *d, Uint8 b0, Uint8 w)
{ {
if(w && b0 == 0x9) {
putmidi(&mpu, d->dat[0x8], d->dat[0x9], 127);
putmidi(&mpu, d->dat[0x8], d->dat[0x9], 0);
}
(void)d; (void)d;
(void)b0;
(void)w;
} }
void void
@ -382,7 +384,7 @@ start(Uxn *u)
break; break;
} }
} }
listenmpu(&mpu); getmidi(&mpu);
for(i = 0; i < mpu.queue; ++i) { for(i = 0; i < mpu.queue; ++i) {
devmidi->dat[2] = mpu.events[i].message; devmidi->dat[2] = mpu.events[i].message;
devmidi->dat[3] = mpu.events[i].message >> 8; devmidi->dat[3] = mpu.events[i].message >> 8;