1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#include "portmidi.h"
#include "porttime.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "assert.h"
#define INPUT_BUFFER_SIZE 100
#define DRIVER_INFO NULL
#define TIME_PROC ((PmTimeProcPtr) Pt_Time)
#define TIME_INFO NULL
#define TIME_START Pt_Start(1, 0, 0) /* timer started w/millisecond accuracy */
#define STRING_MAX 80 /* used for console input */
static void prompt_and_exit(void)
{
printf("type ENTER...");
while (getchar() != '\n') ;
/* this will clean up open ports: */
exit(-1);
}
static PmError checkerror(PmError err)
{
if (err == pmHostError) {
/* it seems pointless to allocate memory and copy the string,
* so I will do the work of Pm_GetHostErrorText directly
*/
char errmsg[80];
Pm_GetHostErrorText(errmsg, 80);
printf("PortMidi found host error...\n %s\n", errmsg);
prompt_and_exit();
} else if (err < 0) {
printf("PortMidi call failed...\n %s\n", Pm_GetErrorText(err));
prompt_and_exit();
}
return err;
}
void main_test_input(int num)
{
PmStream *midi;
PmError status, length;
PmEvent buffer[1];
int id;
int i = 0; /* count messages as they arrive */
/* It is recommended to start timer before Midi; otherwise, PortMidi may
start the timer with its (default) parameters
*/
TIME_START;
/* create a virtual input device */
id = checkerror(Pm_CreateVirtualInput("portmidi", NULL, DRIVER_INFO));
checkerror(Pm_OpenInput(&midi, id, NULL, 0, NULL, NULL));
printf("Midi Input opened. Reading %d Midi messages...\n", num);
Pm_SetFilter(midi, PM_FILT_ACTIVE | PM_FILT_CLOCK | PM_FILT_SYSEX);
/* empty the buffer after setting filter, just in case anything
got through */
while (Pm_Poll(midi)) {
Pm_Read(midi, buffer, 1);
}
/* now start paying attention to messages */
while (i < num) {
status = Pm_Poll(midi);
if (status == TRUE) {
length = Pm_Read(midi, buffer, 1);
if (length > 0) {
printf("Got message %d: time %ld, %2lx %2lx %2lx\n",
i,
(long) buffer[0].timestamp,
(long) Pm_MessageStatus(buffer[0].message),
(long) Pm_MessageData1(buffer[0].message),
(long) Pm_MessageData2(buffer[0].message));
i++;
} else {
assert(0);
}
}
}
/* close device (this not explicitly needed in most implementations) */
printf("ready to close...");
Pm_Close(midi);
printf("done closing.\nNow delete the virtual device...");
checkerror(Pm_DeleteVirtualDevice(id));
printf("done deleting.\n");
}
void show_usage()
{
printf("Usage: recvvirtual [-h] [n]\n use -h for this message,\n"
" n is number of message to wait for.\n");
exit(0);
}
int main(int argc, char *argv[])
{
char line[STRING_MAX];
int num = 10;
if (argc > 2) {
show_usage();
} else if (argc == 2) {
if (strcmp(argv[1], "-h") == 0) {
show_usage();
} else {
num = atoi(argv[1]);
if (num <= 0) {
show_usage();
}
}
}
main_test_input(num);
printf("finished portMidi test...type ENTER to quit...");
while (getchar() != '\n') ;
return 0;
}
|