Browse code

Implemented Midi device

neauoire authored on 26/06/2021 04:28:42
Showing 3 changed files
... ...
@@ -13,30 +13,28 @@ WITH REGARD TO THIS SOFTWARE.
13 13
 */
14 14
 
15 15
 int
16
-initmpu(Mpu *m, Uint8 device)
16
+initmpu(Mpu *m, Uint8 dev_in, Uint8 dev_out)
17 17
 {
18 18
 #ifndef NO_PORTMIDI
19 19
 	int i;
20 20
 	Pm_Initialize();
21 21
 	for(i = 0; i < Pm_CountDevices(); ++i)
22
-		printf("Device #%d -> %s%s\n",
23
-			i,
24
-			Pm_GetDeviceInfo(i)->name,
25
-			i == device ? "[x]" : "[ ]");
26
-	Pm_OpenInput(&m->midi, device, NULL, 128, 0, NULL);
22
+		printf("Device #%d -> %s%s\n", i, Pm_GetDeviceInfo(i)->name, i == dev_in ? "[x]" : "[ ]");
23
+	Pm_OpenInput(&m->input, dev_in, NULL, 128, 0, NULL);
24
+	Pm_OpenOutput(&m->output, dev_out, NULL, 128, 0, NULL, 1);
27 25
 	m->queue = 0;
28 26
 	m->error = pmNoError;
29 27
 #endif
30 28
 	(void)m;
31
-	(void)device;
29
+	(void)dev_in;
32 30
 	return 1;
33 31
 }
34 32
 
35 33
 void
36
-listenmpu(Mpu *m)
34
+getmidi(Mpu *m)
37 35
 {
38 36
 #ifndef NO_PORTMIDI
39
-	const int result = Pm_Read(m->midi, m->events, 32);
37
+	const int result = Pm_Read(m->input, m->events, 32);
40 38
 	if(result < 0) {
41 39
 		m->error = (PmError)result;
42 40
 		m->queue = 0;
... ...
@@ -46,3 +44,11 @@ listenmpu(Mpu *m)
46 44
 #endif
47 45
 	(void)m;
48 46
 }
47
+
48
+void
49
+putmidi(Mpu *m, Uint8 chan, Uint8 note, Uint8 velo)
50
+{
51
+#ifndef NO_PORTMIDI
52
+	Pm_WriteShort(m->output, Pt_Time(), Pm_Message(0x90 + chan, note, velo));
53
+#endif
54
+}
49 55
\ No newline at end of file
... ...
@@ -15,6 +15,7 @@ WITH REGARD TO THIS SOFTWARE.
15 15
 
16 16
 #ifndef NO_PORTMIDI
17 17
 #include <portmidi.h>
18
+#include <porttime.h>
18 19
 #else
19 20
 typedef struct {
20 21
 	int message;
... ...
@@ -27,10 +28,11 @@ typedef struct {
27 28
 	Uint8 queue;
28 29
 	PmEvent events[32];
29 30
 #ifndef NO_PORTMIDI
30
-	PmStream *midi;
31
+	PmStream *input, *output;
31 32
 	PmError error;
32 33
 #endif
33 34
 } Mpu;
34 35
 
35
-int initmpu(Mpu *m, Uint8 device);
36
-void listenmpu(Mpu *m);
36
+int initmpu(Mpu *m, Uint8 dev_in, Uint8 dev_out);
37
+void getmidi(Mpu *m);
38
+void putmidi(Mpu *m, Uint8 chan, Uint8 note, Uint8 velo);
37 39
\ No newline at end of file
... ...
@@ -126,7 +126,7 @@ init(void)
126 126
 	gRect.y = PAD;
127 127
 	gRect.w = ppu.width;
128 128
 	gRect.h = ppu.height;
129
-	if(!initmpu(&mpu, 1))
129
+	if(!initmpu(&mpu, 1, 0))
130 130
 		return error("MPU", "Init failure");
131 131
 	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
132 132
 		return error("Init", SDL_GetError());
... ...
@@ -326,9 +326,11 @@ datetime_talk(Device *d, Uint8 b0, Uint8 w)
326 326
 void
327 327
 midi_talk(Device *d, Uint8 b0, Uint8 w)
328 328
 {
329
+	if(w && b0 == 0x9) {
330
+		putmidi(&mpu, d->dat[0x8], d->dat[0x9], 127);
331
+		putmidi(&mpu, d->dat[0x8], d->dat[0x9], 0);
332
+	}
329 333
 	(void)d;
330
-	(void)b0;
331
-	(void)w;
332 334
 }
333 335
 
334 336
 void
... ...
@@ -382,7 +384,7 @@ start(Uxn *u)
382 384
 				break;
383 385
 			}
384 386
 		}
385
-		listenmpu(&mpu);
387
+		getmidi(&mpu);
386 388
 		for(i = 0; i < mpu.queue; ++i) {
387 389
 			devmidi->dat[2] = mpu.events[i].message;
388 390
 			devmidi->dat[3] = mpu.events[i].message >> 8;