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
initmpu(Mpu *m, Uint8 device)
initmpu(Mpu *m, Uint8 dev_in, Uint8 dev_out)
{
#ifndef NO_PORTMIDI
int i;
Pm_Initialize();
for(i = 0; i < Pm_CountDevices(); ++i)
printf("Device #%d -> %s%s\n",
i,
Pm_GetDeviceInfo(i)->name,
i == device ? "[x]" : "[ ]");
Pm_OpenInput(&m->midi, device, NULL, 128, 0, NULL);
printf("Device #%d -> %s%s\n", i, Pm_GetDeviceInfo(i)->name, i == dev_in ? "[x]" : "[ ]");
Pm_OpenInput(&m->input, dev_in, NULL, 128, 0, NULL);
Pm_OpenOutput(&m->output, dev_out, NULL, 128, 0, NULL, 1);
m->queue = 0;
m->error = pmNoError;
#endif
(void)m;
(void)device;
(void)dev_in;
return 1;
}
void
listenmpu(Mpu *m)
getmidi(Mpu *m)
{
#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) {
m->error = (PmError)result;
m->queue = 0;
@ -46,3 +44,11 @@ listenmpu(Mpu *m)
#endif
(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
#include <portmidi.h>
#include <porttime.h>
#else
typedef struct {
int message;
@ -27,10 +28,11 @@ typedef struct {
Uint8 queue;
PmEvent events[32];
#ifndef NO_PORTMIDI
PmStream *midi;
PmStream *input, *output;
PmError error;
#endif
} Mpu;
int initmpu(Mpu *m, Uint8 device);
void listenmpu(Mpu *m);
int initmpu(Mpu *m, Uint8 dev_in, Uint8 dev_out);
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.w = ppu.width;
gRect.h = ppu.height;
if(!initmpu(&mpu, 1))
if(!initmpu(&mpu, 1, 0))
return error("MPU", "Init failure");
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
return error("Init", SDL_GetError());
@ -326,9 +326,11 @@ datetime_talk(Device *d, Uint8 b0, Uint8 w)
void
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)b0;
(void)w;
}
void
@ -382,7 +384,7 @@ start(Uxn *u)
break;
}
}
listenmpu(&mpu);
getmidi(&mpu);
for(i = 0; i < mpu.queue; ++i) {
devmidi->dat[2] = mpu.events[i].message;
devmidi->dat[3] = mpu.events[i].message >> 8;