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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
|
--- a/pm_test/CMakeLists.txt
+++ b/pm_test/CMakeLists.txt
@@ -12,11 +12,11 @@
macro(make_a_test name)
add_executable(${name} ${name}.c)
- target_link_libraries(${name} portmidi-static ${PM_NEEDED_LIBS})
- add_dependencies(${name} portmidi-static)
+ target_link_libraries(${name} portmidi-dynamic ${PM_NEEDED_LIBS})
+ add_dependencies(${name} portmidi-dynamic)
endmacro(make_a_test)
-make_a_test(test)
+make_a_test(simple_test)
make_a_test(midithread)
make_a_test(midithru)
make_a_test(sysex)
--- /dev/null
+++ b/pm_test/simple_test.c
@@ -0,0 +1,489 @@
+#include "portmidi.h"
+#include "porttime.h"
+#include "stdlib.h"
+#include "stdio.h"
+#include "string.h"
+#include "assert.h"
+
+#define INPUT_BUFFER_SIZE 100
+#define OUTPUT_BUFFER_SIZE 0
+#define DRIVER_INFO NULL
+#define TIME_PROC ((int32_t (*)(void *)) 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 */
+
+int32_t latency = 0;
+
+/* crash the program to test whether midi ports are closed */
+/**/
+void doSomethingReallyStupid() {
+ int * tmp = NULL;
+ *tmp = 5;
+}
+
+
+/* exit the program without any explicit cleanup */
+/**/
+void doSomethingStupid() {
+ assert(0);
+}
+
+
+/* read a number from console */
+/**/
+int get_number(char *prompt)
+{
+ char line[STRING_MAX];
+ int n = 0, i;
+ printf("%s\n", prompt);
+ while (n != 1) {
+ n = scanf("%d", &i);
+ fgets(line, STRING_MAX, stdin);
+
+ }
+ return i;
+}
+
+
+/*
+ * the somethingStupid parameter can be set to simulate a program crash.
+ * We want PortMidi to close Midi ports automatically in the event of a
+ * crash because Windows does not (and this may cause an OS crash)
+ */
+void main_test_input(unsigned int somethingStupid) {
+ PmStream * midi;
+ PmError status, length;
+ PmEvent buffer[1];
+ int num = 10;
+ int i = get_number("Type input number: ");
+ /* It is recommended to start timer before Midi; otherwise, PortMidi may
+ start the timer with its (default) parameters
+ */
+ TIME_START;
+
+ /* open input device */
+ Pm_OpenInput(&midi,
+ i,
+ DRIVER_INFO,
+ INPUT_BUFFER_SIZE,
+ TIME_PROC,
+ TIME_INFO);
+
+ 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 */
+ i = 0; /* count messages as they arrive */
+ 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);
+ }
+ }
+ /* simulate crash if somethingStupid is 1 or 2 */
+ if ((i > (num/2)) && (somethingStupid == 1)) {
+ doSomethingStupid();
+ } else if ((i > (num/2)) && (somethingStupid == 2)) {
+ doSomethingReallyStupid();
+ }
+ }
+
+ /* close device (this not explicitly needed in most implementations) */
+ printf("ready to close...");
+
+ Pm_Close(midi);
+ printf("done closing...");
+}
+
+
+
+void main_test_output() {
+ PmStream * midi;
+ char line[80];
+ int32_t off_time;
+ int chord[] = { 60, 67, 76, 83, 90 };
+ #define chord_size 5
+ PmEvent buffer[chord_size];
+ PmTimestamp timestamp;
+
+ /* determine which output device to use */
+ int i = get_number("Type output number: ");
+
+ /* It is recommended to start timer before PortMidi */
+ TIME_START;
+
+ /* open output device -- since PortMidi avoids opening a timer
+ when latency is zero, we will pass in a NULL timer pointer
+ for that case. If PortMidi tries to access the time_proc,
+ we will crash, so this test will tell us something. */
+ Pm_OpenOutput(&midi,
+ i,
+ DRIVER_INFO,
+ OUTPUT_BUFFER_SIZE,
+ (latency == 0 ? NULL : TIME_PROC),
+ (latency == 0 ? NULL : TIME_INFO),
+ latency);
+ printf("Midi Output opened with %ld ms latency.\n", (long) latency);
+
+ /* output note on/off w/latency offset; hold until user prompts */
+ printf("ready to send program 1 change... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+ /* if we were writing midi for immediate output, we could always use
+ timestamps of zero, but since we may be writing with latency, we
+ will explicitly set the timestamp to "now" by getting the time.
+ The source of timestamps should always correspond to the TIME_PROC
+ and TIME_INFO parameters used in Pm_OpenOutput(). */
+ buffer[0].timestamp = TIME_PROC(TIME_INFO);
+ /* Send a program change to increase the chances we will hear notes */
+ /* Program 0 is usually a piano, but you can change it here: */
+#define PROGRAM 0
+ buffer[0].message = Pm_Message(0xC0, PROGRAM, 0);
+ Pm_Write(midi, buffer, 1);
+
+ printf("ready to note-on... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+ buffer[0].timestamp = TIME_PROC(TIME_INFO);
+ buffer[0].message = Pm_Message(0x90, 60, 100);
+ Pm_Write(midi, buffer, 1);
+ printf("ready to note-off... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+ buffer[0].timestamp = TIME_PROC(TIME_INFO);
+ buffer[0].message = Pm_Message(0x90, 60, 0);
+ Pm_Write(midi, buffer, 1);
+
+ /* output short note on/off w/latency offset; hold until user prompts */
+ printf("ready to note-on (short form)... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+ Pm_WriteShort(midi, TIME_PROC(TIME_INFO),
+ Pm_Message(0x90, 60, 100));
+ printf("ready to note-off (short form)... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+ Pm_WriteShort(midi, TIME_PROC(TIME_INFO),
+ Pm_Message(0x90, 60, 0));
+
+ /* output several note on/offs to test timing.
+ Should be 1s between notes */
+ printf("chord will arpeggiate if latency > 0\n");
+ printf("ready to chord-on/chord-off... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+ timestamp = TIME_PROC(TIME_INFO);
+ for (i = 0; i < chord_size; i++) {
+ buffer[i].timestamp = timestamp + 1000 * i;
+ buffer[i].message = Pm_Message(0x90, chord[i], 100);
+ }
+ Pm_Write(midi, buffer, chord_size);
+
+ off_time = timestamp + 1000 + chord_size * 1000;
+ while (TIME_PROC(TIME_INFO) < off_time)
+ /* busy wait */;
+ for (i = 0; i < chord_size; i++) {
+ buffer[i].timestamp = timestamp + 1000 * i;
+ buffer[i].message = Pm_Message(0x90, chord[i], 0);
+ }
+ Pm_Write(midi, buffer, chord_size);
+
+ /* close device (this not explicitly needed in most implementations) */
+ printf("ready to close and terminate... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+
+ Pm_Close(midi);
+ Pm_Terminate();
+ printf("done closing and terminating...\n");
+}
+
+
+void main_test_both()
+{
+ int i = 0;
+ int in, out;
+ PmStream * midi, * midiOut;
+ PmEvent buffer[1];
+ PmError status, length;
+ int num = 10;
+
+ in = get_number("Type input number: ");
+ out = get_number("Type output number: ");
+
+ /* In is recommended to start timer before PortMidi */
+ TIME_START;
+
+ Pm_OpenOutput(&midiOut,
+ out,
+ DRIVER_INFO,
+ OUTPUT_BUFFER_SIZE,
+ TIME_PROC,
+ TIME_INFO,
+ latency);
+ printf("Midi Output opened with %ld ms latency.\n", (long) latency);
+ /* open input device */
+ Pm_OpenInput(&midi,
+ in,
+ DRIVER_INFO,
+ INPUT_BUFFER_SIZE,
+ TIME_PROC,
+ TIME_INFO);
+ printf("Midi Input opened. Reading %d Midi messages...\n",num);
+ Pm_SetFilter(midi, PM_FILT_ACTIVE | PM_FILT_CLOCK);
+ /* empty the buffer after setting filter, just in case anything
+ got through */
+ while (Pm_Poll(midi)) {
+ Pm_Read(midi, buffer, 1);
+ }
+ i = 0;
+ while (i < num) {
+ status = Pm_Poll(midi);
+ if (status == TRUE) {
+ length = Pm_Read(midi,buffer,1);
+ if (length > 0) {
+ Pm_Write(midiOut, buffer, 1);
+ 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 midi devices */
+ Pm_Close(midi);
+ Pm_Close(midiOut);
+ Pm_Terminate();
+}
+
+
+/* main_test_stream exercises windows winmm API's stream mode */
+/* The winmm stream mode is used for latency>0, and sends
+ timestamped messages. The timestamps are relative (delta)
+ times, whereas PortMidi times are absolute. Since peculiar
+ things happen when messages are not always sent in advance,
+ this function allows us to exercise the system and test it.
+ */
+void main_test_stream() {
+ PmStream * midi;
+ char line[80];
+ PmEvent buffer[16];
+
+ /* determine which output device to use */
+ int i = get_number("Type output number: ");
+
+ latency = 500; /* ignore LATENCY for this test and
+ fix the latency at 500ms */
+
+ /* It is recommended to start timer before PortMidi */
+ TIME_START;
+
+ /* open output device */
+ Pm_OpenOutput(&midi,
+ i,
+ DRIVER_INFO,
+ OUTPUT_BUFFER_SIZE,
+ TIME_PROC,
+ TIME_INFO,
+ latency);
+ printf("Midi Output opened with %ld ms latency.\n", (long) latency);
+
+ /* output note on/off w/latency offset; hold until user prompts */
+ printf("ready to send output... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+
+ /* if we were writing midi for immediate output, we could always use
+ timestamps of zero, but since we may be writing with latency, we
+ will explicitly set the timestamp to "now" by getting the time.
+ The source of timestamps should always correspond to the TIME_PROC
+ and TIME_INFO parameters used in Pm_OpenOutput(). */
+ buffer[0].timestamp = TIME_PROC(TIME_INFO);
+ buffer[0].message = Pm_Message(0xC0, 0, 0);
+ buffer[1].timestamp = buffer[0].timestamp;
+ buffer[1].message = Pm_Message(0x90, 60, 100);
+ buffer[2].timestamp = buffer[0].timestamp + 1000;
+ buffer[2].message = Pm_Message(0x90, 62, 100);
+ buffer[3].timestamp = buffer[0].timestamp + 2000;
+ buffer[3].message = Pm_Message(0x90, 64, 100);
+ buffer[4].timestamp = buffer[0].timestamp + 3000;
+ buffer[4].message = Pm_Message(0x90, 66, 100);
+ buffer[5].timestamp = buffer[0].timestamp + 4000;
+ buffer[5].message = Pm_Message(0x90, 60, 0);
+ buffer[6].timestamp = buffer[0].timestamp + 4000;
+ buffer[6].message = Pm_Message(0x90, 62, 0);
+ buffer[7].timestamp = buffer[0].timestamp + 4000;
+ buffer[7].message = Pm_Message(0x90, 64, 0);
+ buffer[8].timestamp = buffer[0].timestamp + 4000;
+ buffer[8].message = Pm_Message(0x90, 66, 0);
+
+ Pm_Write(midi, buffer, 9);
+#ifdef SEND8
+ /* Now, we're ready for the real test.
+ Play 4 notes at now, now+500, now+1000, and now+1500
+ Then wait until now+2000.
+ Play 4 more notes as before.
+ We should hear 8 evenly spaced notes. */
+ now = TIME_PROC(TIME_INFO);
+ for (i = 0; i < 4; i++) {
+ buffer[i * 2].timestamp = now + (i * 500);
+ buffer[i * 2].message = Pm_Message(0x90, 60, 100);
+ buffer[i * 2 + 1].timestamp = now + 250 + (i * 500);
+ buffer[i * 2 + 1].message = Pm_Message(0x90, 60, 0);
+ }
+ Pm_Write(midi, buffer, 8);
+
+ while (Pt_Time() < now + 2500)
+ /* busy wait */;
+ /* now we are 500 ms behind schedule, but since the latency
+ is 500, the delay should not be audible */
+ now += 2000;
+ for (i = 0; i < 4; i++) {
+ buffer[i * 2].timestamp = now + (i * 500);
+ buffer[i * 2].message = Pm_Message(0x90, 60, 100);
+ buffer[i * 2 + 1].timestamp = now + 250 + (i * 500);
+ buffer[i * 2 + 1].message = Pm_Message(0x90, 60, 0);
+ }
+ Pm_Write(midi, buffer, 8);
+#endif
+ /* close device (this not explicitly needed in most implementations) */
+ printf("ready to close and terminate... (type RETURN):");
+ fgets(line, STRING_MAX, stdin);
+
+ Pm_Close(midi);
+ Pm_Terminate();
+ printf("done closing and terminating...\n");
+}
+
+
+void show_usage()
+{
+ printf("Usage: test [-h] [-l latency-in-ms]\n");
+ exit(0);
+}
+
+int main(int argc, char *argv[])
+{
+ int default_in;
+ int default_out;
+ int i = 0, n = 0;
+ char line[STRING_MAX];
+ int test_input = 0, test_output = 0, test_both = 0, somethingStupid = 0;
+ int stream_test = 0;
+ int latency_valid = FALSE;
+
+ if (sizeof(void *) == 8)
+ printf("Apparently this is a 64-bit machine.\n");
+ else if (sizeof(void *) == 4)
+ printf ("Apparently this is a 32-bit machine.\n");
+
+ for (i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "-h") == 0) {
+ show_usage();
+ } else if (strcmp(argv[i], "-l") == 0 && (i + 1 < argc)) {
+ i = i + 1;
+ latency = atoi(argv[i]);
+ printf("Latency will be %ld\n", (long) latency);
+ latency_valid = TRUE;
+ } else {
+ show_usage();
+ }
+ }
+
+ while (!latency_valid) {
+ int lat; // declared int to match "%d"
+ printf("Latency in ms: ");
+ if (scanf("%d", &lat) == 1) {
+ latency = (int32_t) lat; // coerce from "%d" to known size
+ latency_valid = TRUE;
+ }
+ }
+
+ /* determine what type of test to run */
+ printf("begin portMidi test...\n");
+ printf("%s%s%s%s%s",
+ "enter your choice...\n 1: test input\n",
+ " 2: test input (fail w/assert)\n",
+ " 3: test input (fail w/NULL assign)\n",
+ " 4: test output\n 5: test both\n",
+ " 6: stream test\n");
+ while (n != 1) {
+ n = scanf("%d", &i);
+ fgets(line, STRING_MAX, stdin);
+ switch(i) {
+ case 1:
+ test_input = 1;
+ break;
+ case 2:
+ test_input = 1;
+ somethingStupid = 1;
+ break;
+ case 3:
+ test_input = 1;
+ somethingStupid = 2;
+ break;
+ case 4:
+ test_output = 1;
+ break;
+ case 5:
+ test_both = 1;
+ break;
+ case 6:
+ stream_test = 1;
+ break;
+ default:
+ printf("got %d (invalid input)\n", n);
+ break;
+ }
+ }
+
+ /* list device information */
+ default_in = Pm_GetDefaultInputDeviceID();
+ default_out = Pm_GetDefaultOutputDeviceID();
+ for (i = 0; i < Pm_CountDevices(); i++) {
+ char *deflt;
+ const PmDeviceInfo *info = Pm_GetDeviceInfo(i);
+ if (((test_input | test_both) & info->input) |
+ ((test_output | test_both | stream_test) & info->output)) {
+ printf("%d: %s, %s", i, info->interf, info->name);
+ if (info->input) {
+ deflt = (i == default_in ? "default " : "");
+ printf(" (%sinput)", deflt);
+ }
+ if (info->output) {
+ deflt = (i == default_out ? "default " : "");
+ printf(" (%soutput)", deflt);
+ }
+ printf("\n");
+ }
+ }
+
+ /* run test */
+ if (stream_test) {
+ main_test_stream();
+ } else if (test_input) {
+ main_test_input(somethingStupid);
+ } else if (test_output) {
+ main_test_output();
+ } else if (test_both) {
+ main_test_both();
+ }
+
+ printf("finished portMidi test...type ENTER to quit...");
+ fgets(line, STRING_MAX, stdin);
+ return 0;
+}
--- a/pm_test/test.c
+++ /dev/null
@@ -1,489 +0,0 @@
-#include "portmidi.h"
-#include "porttime.h"
-#include "stdlib.h"
-#include "stdio.h"
-#include "string.h"
-#include "assert.h"
-
-#define INPUT_BUFFER_SIZE 100
-#define OUTPUT_BUFFER_SIZE 0
-#define DRIVER_INFO NULL
-#define TIME_PROC ((int32_t (*)(void *)) 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 */
-
-int32_t latency = 0;
-
-/* crash the program to test whether midi ports are closed */
-/**/
-void doSomethingReallyStupid() {
- int * tmp = NULL;
- *tmp = 5;
-}
-
-
-/* exit the program without any explicit cleanup */
-/**/
-void doSomethingStupid() {
- assert(0);
-}
-
-
-/* read a number from console */
-/**/
-int get_number(char *prompt)
-{
- char line[STRING_MAX];
- int n = 0, i;
- printf(prompt);
- while (n != 1) {
- n = scanf("%d", &i);
- fgets(line, STRING_MAX, stdin);
-
- }
- return i;
-}
-
-
-/*
- * the somethingStupid parameter can be set to simulate a program crash.
- * We want PortMidi to close Midi ports automatically in the event of a
- * crash because Windows does not (and this may cause an OS crash)
- */
-void main_test_input(unsigned int somethingStupid) {
- PmStream * midi;
- PmError status, length;
- PmEvent buffer[1];
- int num = 10;
- int i = get_number("Type input number: ");
- /* It is recommended to start timer before Midi; otherwise, PortMidi may
- start the timer with its (default) parameters
- */
- TIME_START;
-
- /* open input device */
- Pm_OpenInput(&midi,
- i,
- DRIVER_INFO,
- INPUT_BUFFER_SIZE,
- TIME_PROC,
- TIME_INFO);
-
- 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 */
- i = 0; /* count messages as they arrive */
- 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);
- }
- }
- /* simulate crash if somethingStupid is 1 or 2 */
- if ((i > (num/2)) && (somethingStupid == 1)) {
- doSomethingStupid();
- } else if ((i > (num/2)) && (somethingStupid == 2)) {
- doSomethingReallyStupid();
- }
- }
-
- /* close device (this not explicitly needed in most implementations) */
- printf("ready to close...");
-
- Pm_Close(midi);
- printf("done closing...");
-}
-
-
-
-void main_test_output() {
- PmStream * midi;
- char line[80];
- int32_t off_time;
- int chord[] = { 60, 67, 76, 83, 90 };
- #define chord_size 5
- PmEvent buffer[chord_size];
- PmTimestamp timestamp;
-
- /* determine which output device to use */
- int i = get_number("Type output number: ");
-
- /* It is recommended to start timer before PortMidi */
- TIME_START;
-
- /* open output device -- since PortMidi avoids opening a timer
- when latency is zero, we will pass in a NULL timer pointer
- for that case. If PortMidi tries to access the time_proc,
- we will crash, so this test will tell us something. */
- Pm_OpenOutput(&midi,
- i,
- DRIVER_INFO,
- OUTPUT_BUFFER_SIZE,
- (latency == 0 ? NULL : TIME_PROC),
- (latency == 0 ? NULL : TIME_INFO),
- latency);
- printf("Midi Output opened with %ld ms latency.\n", (long) latency);
-
- /* output note on/off w/latency offset; hold until user prompts */
- printf("ready to send program 1 change... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
- /* if we were writing midi for immediate output, we could always use
- timestamps of zero, but since we may be writing with latency, we
- will explicitly set the timestamp to "now" by getting the time.
- The source of timestamps should always correspond to the TIME_PROC
- and TIME_INFO parameters used in Pm_OpenOutput(). */
- buffer[0].timestamp = TIME_PROC(TIME_INFO);
- /* Send a program change to increase the chances we will hear notes */
- /* Program 0 is usually a piano, but you can change it here: */
-#define PROGRAM 0
- buffer[0].message = Pm_Message(0xC0, PROGRAM, 0);
- Pm_Write(midi, buffer, 1);
-
- printf("ready to note-on... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
- buffer[0].timestamp = TIME_PROC(TIME_INFO);
- buffer[0].message = Pm_Message(0x90, 60, 100);
- Pm_Write(midi, buffer, 1);
- printf("ready to note-off... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
- buffer[0].timestamp = TIME_PROC(TIME_INFO);
- buffer[0].message = Pm_Message(0x90, 60, 0);
- Pm_Write(midi, buffer, 1);
-
- /* output short note on/off w/latency offset; hold until user prompts */
- printf("ready to note-on (short form)... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
- Pm_WriteShort(midi, TIME_PROC(TIME_INFO),
- Pm_Message(0x90, 60, 100));
- printf("ready to note-off (short form)... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
- Pm_WriteShort(midi, TIME_PROC(TIME_INFO),
- Pm_Message(0x90, 60, 0));
-
- /* output several note on/offs to test timing.
- Should be 1s between notes */
- printf("chord will arpeggiate if latency > 0\n");
- printf("ready to chord-on/chord-off... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
- timestamp = TIME_PROC(TIME_INFO);
- for (i = 0; i < chord_size; i++) {
- buffer[i].timestamp = timestamp + 1000 * i;
- buffer[i].message = Pm_Message(0x90, chord[i], 100);
- }
- Pm_Write(midi, buffer, chord_size);
-
- off_time = timestamp + 1000 + chord_size * 1000;
- while (TIME_PROC(TIME_INFO) < off_time)
- /* busy wait */;
- for (i = 0; i < chord_size; i++) {
- buffer[i].timestamp = timestamp + 1000 * i;
- buffer[i].message = Pm_Message(0x90, chord[i], 0);
- }
- Pm_Write(midi, buffer, chord_size);
-
- /* close device (this not explicitly needed in most implementations) */
- printf("ready to close and terminate... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
-
- Pm_Close(midi);
- Pm_Terminate();
- printf("done closing and terminating...\n");
-}
-
-
-void main_test_both()
-{
- int i = 0;
- int in, out;
- PmStream * midi, * midiOut;
- PmEvent buffer[1];
- PmError status, length;
- int num = 10;
-
- in = get_number("Type input number: ");
- out = get_number("Type output number: ");
-
- /* In is recommended to start timer before PortMidi */
- TIME_START;
-
- Pm_OpenOutput(&midiOut,
- out,
- DRIVER_INFO,
- OUTPUT_BUFFER_SIZE,
- TIME_PROC,
- TIME_INFO,
- latency);
- printf("Midi Output opened with %ld ms latency.\n", (long) latency);
- /* open input device */
- Pm_OpenInput(&midi,
- in,
- DRIVER_INFO,
- INPUT_BUFFER_SIZE,
- TIME_PROC,
- TIME_INFO);
- printf("Midi Input opened. Reading %d Midi messages...\n",num);
- Pm_SetFilter(midi, PM_FILT_ACTIVE | PM_FILT_CLOCK);
- /* empty the buffer after setting filter, just in case anything
- got through */
- while (Pm_Poll(midi)) {
- Pm_Read(midi, buffer, 1);
- }
- i = 0;
- while (i < num) {
- status = Pm_Poll(midi);
- if (status == TRUE) {
- length = Pm_Read(midi,buffer,1);
- if (length > 0) {
- Pm_Write(midiOut, buffer, 1);
- 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 midi devices */
- Pm_Close(midi);
- Pm_Close(midiOut);
- Pm_Terminate();
-}
-
-
-/* main_test_stream exercises windows winmm API's stream mode */
-/* The winmm stream mode is used for latency>0, and sends
- timestamped messages. The timestamps are relative (delta)
- times, whereas PortMidi times are absolute. Since peculiar
- things happen when messages are not always sent in advance,
- this function allows us to exercise the system and test it.
- */
-void main_test_stream() {
- PmStream * midi;
- char line[80];
- PmEvent buffer[16];
-
- /* determine which output device to use */
- int i = get_number("Type output number: ");
-
- latency = 500; /* ignore LATENCY for this test and
- fix the latency at 500ms */
-
- /* It is recommended to start timer before PortMidi */
- TIME_START;
-
- /* open output device */
- Pm_OpenOutput(&midi,
- i,
- DRIVER_INFO,
- OUTPUT_BUFFER_SIZE,
- TIME_PROC,
- TIME_INFO,
- latency);
- printf("Midi Output opened with %ld ms latency.\n", (long) latency);
-
- /* output note on/off w/latency offset; hold until user prompts */
- printf("ready to send output... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
-
- /* if we were writing midi for immediate output, we could always use
- timestamps of zero, but since we may be writing with latency, we
- will explicitly set the timestamp to "now" by getting the time.
- The source of timestamps should always correspond to the TIME_PROC
- and TIME_INFO parameters used in Pm_OpenOutput(). */
- buffer[0].timestamp = TIME_PROC(TIME_INFO);
- buffer[0].message = Pm_Message(0xC0, 0, 0);
- buffer[1].timestamp = buffer[0].timestamp;
- buffer[1].message = Pm_Message(0x90, 60, 100);
- buffer[2].timestamp = buffer[0].timestamp + 1000;
- buffer[2].message = Pm_Message(0x90, 62, 100);
- buffer[3].timestamp = buffer[0].timestamp + 2000;
- buffer[3].message = Pm_Message(0x90, 64, 100);
- buffer[4].timestamp = buffer[0].timestamp + 3000;
- buffer[4].message = Pm_Message(0x90, 66, 100);
- buffer[5].timestamp = buffer[0].timestamp + 4000;
- buffer[5].message = Pm_Message(0x90, 60, 0);
- buffer[6].timestamp = buffer[0].timestamp + 4000;
- buffer[6].message = Pm_Message(0x90, 62, 0);
- buffer[7].timestamp = buffer[0].timestamp + 4000;
- buffer[7].message = Pm_Message(0x90, 64, 0);
- buffer[8].timestamp = buffer[0].timestamp + 4000;
- buffer[8].message = Pm_Message(0x90, 66, 0);
-
- Pm_Write(midi, buffer, 9);
-#ifdef SEND8
- /* Now, we're ready for the real test.
- Play 4 notes at now, now+500, now+1000, and now+1500
- Then wait until now+2000.
- Play 4 more notes as before.
- We should hear 8 evenly spaced notes. */
- now = TIME_PROC(TIME_INFO);
- for (i = 0; i < 4; i++) {
- buffer[i * 2].timestamp = now + (i * 500);
- buffer[i * 2].message = Pm_Message(0x90, 60, 100);
- buffer[i * 2 + 1].timestamp = now + 250 + (i * 500);
- buffer[i * 2 + 1].message = Pm_Message(0x90, 60, 0);
- }
- Pm_Write(midi, buffer, 8);
-
- while (Pt_Time() < now + 2500)
- /* busy wait */;
- /* now we are 500 ms behind schedule, but since the latency
- is 500, the delay should not be audible */
- now += 2000;
- for (i = 0; i < 4; i++) {
- buffer[i * 2].timestamp = now + (i * 500);
- buffer[i * 2].message = Pm_Message(0x90, 60, 100);
- buffer[i * 2 + 1].timestamp = now + 250 + (i * 500);
- buffer[i * 2 + 1].message = Pm_Message(0x90, 60, 0);
- }
- Pm_Write(midi, buffer, 8);
-#endif
- /* close device (this not explicitly needed in most implementations) */
- printf("ready to close and terminate... (type RETURN):");
- fgets(line, STRING_MAX, stdin);
-
- Pm_Close(midi);
- Pm_Terminate();
- printf("done closing and terminating...\n");
-}
-
-
-void show_usage()
-{
- printf("Usage: test [-h] [-l latency-in-ms]\n");
- exit(0);
-}
-
-int main(int argc, char *argv[])
-{
- int default_in;
- int default_out;
- int i = 0, n = 0;
- char line[STRING_MAX];
- int test_input = 0, test_output = 0, test_both = 0, somethingStupid = 0;
- int stream_test = 0;
- int latency_valid = FALSE;
-
- if (sizeof(void *) == 8)
- printf("Apparently this is a 64-bit machine.\n");
- else if (sizeof(void *) == 4)
- printf ("Apparently this is a 32-bit machine.\n");
-
- for (i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-h") == 0) {
- show_usage();
- } else if (strcmp(argv[i], "-l") == 0 && (i + 1 < argc)) {
- i = i + 1;
- latency = atoi(argv[i]);
- printf("Latency will be %ld\n", (long) latency);
- latency_valid = TRUE;
- } else {
- show_usage();
- }
- }
-
- while (!latency_valid) {
- int lat; // declared int to match "%d"
- printf("Latency in ms: ");
- if (scanf("%d", &lat) == 1) {
- latency = (int32_t) lat; // coerce from "%d" to known size
- latency_valid = TRUE;
- }
- }
-
- /* determine what type of test to run */
- printf("begin portMidi test...\n");
- printf("%s%s%s%s%s",
- "enter your choice...\n 1: test input\n",
- " 2: test input (fail w/assert)\n",
- " 3: test input (fail w/NULL assign)\n",
- " 4: test output\n 5: test both\n",
- " 6: stream test\n");
- while (n != 1) {
- n = scanf("%d", &i);
- fgets(line, STRING_MAX, stdin);
- switch(i) {
- case 1:
- test_input = 1;
- break;
- case 2:
- test_input = 1;
- somethingStupid = 1;
- break;
- case 3:
- test_input = 1;
- somethingStupid = 2;
- break;
- case 4:
- test_output = 1;
- break;
- case 5:
- test_both = 1;
- break;
- case 6:
- stream_test = 1;
- break;
- default:
- printf("got %d (invalid input)\n", n);
- break;
- }
- }
-
- /* list device information */
- default_in = Pm_GetDefaultInputDeviceID();
- default_out = Pm_GetDefaultOutputDeviceID();
- for (i = 0; i < Pm_CountDevices(); i++) {
- char *deflt;
- const PmDeviceInfo *info = Pm_GetDeviceInfo(i);
- if (((test_input | test_both) & info->input) |
- ((test_output | test_both | stream_test) & info->output)) {
- printf("%d: %s, %s", i, info->interf, info->name);
- if (info->input) {
- deflt = (i == default_in ? "default " : "");
- printf(" (%sinput)", deflt);
- }
- if (info->output) {
- deflt = (i == default_out ? "default " : "");
- printf(" (%soutput)", deflt);
- }
- printf("\n");
- }
- }
-
- /* run test */
- if (stream_test) {
- main_test_stream();
- } else if (test_input) {
- main_test_input(somethingStupid);
- } else if (test_output) {
- main_test_output();
- } else if (test_both) {
- main_test_both();
- }
-
- printf("finished portMidi test...type ENTER to quit...");
- fgets(line, STRING_MAX, stdin);
- return 0;
-}
|