File: miditest.c

package info (click to toggle)
allegro4 2%3A4.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,052 kB
  • ctags: 12,972
  • sloc: ansic: 109,525; asm: 16,672; cpp: 3,221; sh: 1,761; makefile: 556; pascal: 105; perl: 73
file content (467 lines) | stat: -rw-r--r-- 11,373 bytes parent folder | download
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
/*         ______   ___    ___
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      MIDI instrument playing test program for the Allegro library.
 *
 *      By Shawn Hargreaves.
 *
 *      See readme.txt for copyright information.
 */


#include "allegro.h"



extern DIALOG thedialog[];


#define DRIVER_STR   4
#define DESC_STR     6
#define INSTLIST     7
#define PIANO        8
#define VOLUME       10
#define PAN          12



void set_patch(int channel, int prog)
{
   unsigned char msg[2];

   msg[0] = 0xC0+channel;
   msg[1] = prog;

   midi_out(msg, 2);
}



void set_pan(int channel, int pan)
{
   unsigned char msg[3];

   msg[0] = 0xB0+channel;
   msg[1] = 10;
   msg[2] = pan / 2;

   midi_out(msg, 3);
}



void note_on(int channel, int pitch, int vel)
{
   unsigned char msg[3];

   msg[0] = 0x90+channel;
   msg[1] = pitch;
   msg[2] = vel / 2;

   midi_out(msg, 3);
}



void note_off(int channel, int pitch)
{
   unsigned char msg[3];

   msg[0] = 0x80+channel;
   msg[1] = pitch;
   msg[2] = 0;

   midi_out(msg, 3);
}



char *instlist_getter(int index, int *list_size)
{
   static char *names[] = 
   {
      "Acoustic Grand",
      "Bright Acoustic",
      "Electric Grand",
      "Honky-Tonk",
      "Electric Piano 1",
      "Electric Piano 2",
      "Harpsichord",
      "Clav",
      "Celesta",
      "Glockenspiel",
      "Music Box",
      "Vibraphone",
      "Marimba",
      "Xylophone",
      "Tubular Bells",
      "Dulcimer",
      "Drawbar Organ",
      "Percussive Organ",
      "Rock Organ",
      "Church Organ",
      "Reed Organ",
      "Accoridan",
      "Harmonica",
      "Tango Accordian",
      "Acoustic Guitar (nylon)",
      "Acoustic Guitar (steel)",
      "Electric Guitar (jazz)",
      "Electric Guitar (clean)",
      "Electric Guitar (muted)",
      "Overdriven Guitar",
      "Distortion Guitar",
      "Guitar Harmonics",
      "Acoustic Bass",
      "Electric Bass (finger)",
      "Electric Bass (pick)",
      "Fretless Bass",
      "Slap Bass 1",
      "Slap Bass 2",
      "Synth Bass 1",
      "Synth Bass 2",
      "Violin",
      "Viola",
      "Cello",
      "Contrabass",
      "Tremolo Strings",
      "Pizzicato Strings",
      "Orchestral Strings",
      "Timpani",
      "String Ensemble 1",
      "String Ensemble 2",
      "SynthStrings 1",
      "SynthStrings 2",
      "Choir Aahs",
      "Voice Oohs",
      "Synth Voice",
      "Orchestra Hit",
      "Trumpet",
      "Trombone",
      "Tuba",
      "Muted Trumpet",
      "French Horn",
      "Brass Section",
      "SynthBrass 1",
      "SynthBrass 2",
      "Soprano Sax",
      "Alto Sax",
      "Tenor Sax",
      "Baritone Sax",
      "Oboe",
      "English Horn",
      "Bassoon",
      "Clarinet",
      "Piccolo",
      "Flute",
      "Recorder",
      "Pan Flute",
      "Blown Bottle",
      "Skakuhachi",
      "Whistle",
      "Ocarina",
      "Lead 1 (square)",
      "Lead 2 (sawtooth)",
      "Lead 3 (calliope)",
      "Lead 4 (chiff)",
      "Lead 5 (charang)",
      "Lead 6 (voice)",
      "Lead 7 (fifths)",
      "Lead 8 (bass+lead)",
      "Pad 1 (new age)",
      "Pad 2 (warm)",
      "Pad 3 (polysynth)",
      "Pad 4 (choir)",
      "Pad 5 (bowed)",
      "Pad 6 (metallic)",
      "Pad 7 (halo)",
      "Pad 8 (sweep)",
      "FX 1 (rain)",
      "FX 2 (soundtrack)",
      "FX 3 (crystal)",
      "FX 4 (atmosphere)",
      "FX 5 (brightness)",
      "FX 6 (goblins)",
      "FX 7 (echoes)",
      "FX 8 (sci-fi)",
      "Sitar",
      "Banjo",
      "Shamisen",
      "Koto",
      "Kalimba",
      "Bagpipe",
      "Fiddle",
      "Shanai",
      "Tinkle Bell",
      "Agogo",
      "Steel Drums",
      "Woodblock",
      "Taiko Drum",
      "Melodic Tom",
      "Synth Drum",
      "Reverse Cymbal",
      "Guitar Fret Noise",
      "Breath Noise",
      "Seashore",
      "Bird Tweet",
      "Telephone ring",
      "Helicopter",
      "Applause",
      "Gunshot",
      "Acoustic Bass Drum",
      "Bass Drum 1",
      "Side Stick",
      "Acoustic Snare",
      "Hand Clap",
      "Electric Snare",
      "Low Floor Tom",
      "Closed Hi-Hat",
      "High Floor Tom",
      "Pedal Hi-Hat",
      "Low Tom",
      "Open Hi-Hat",
      "Low-Mid Tom",
      "Hi-Mid Tom",
      "Crash Cymbal 1",
      "High Tom",
      "Ride Cymbal 1",
      "Chinese Cymbal",
      "Ride Bell",
      "Tambourine",
      "Splash Cymbal",
      "Cowbell",
      "Crash Cymbal 2",
      "Vibraslap",
      "Ride Cymbal 2",
      "Hi Bongo",
      "Low Bongo",
      "Mute Hi Conga",
      "Open Hi Conga",
      "Low Conga",
      "High Timbale",
      "Low Timbale",
      "High Agogo",
      "Low Agogo",
      "Cabasa",
      "Maracas",
      "Short Whistle",
      "Long Whistle",
      "Short Guiro",
      "Long Guiro",
      "Claves",
      "Hi Wood Block",
      "Low Wood Block",
      "Mute Cuica",
      "Open Cuica",
      "Mute Triangle",
      "Open Triangle"
   };

   if (index < 0) {
      if (list_size)
	 *list_size = sizeof(names) / sizeof(char *);

      return NULL;
   }

   return names[index];
}



int instlist_proc(int msg, DIALOG *d, int c)
{
   int ret = d_list_proc(msg, d, c);

   if (ret & D_CLOSE) {
      ret &= ~D_CLOSE;
      object_message(thedialog+PIANO, MSG_KEY, 0);
   }

   return ret;
}



int piano_proc(int msg, DIALOG *d, int c)
{
   static char blackkey[12] = 
   { 
      FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, 
      TRUE, FALSE, TRUE, FALSE, TRUE, FALSE 
   };

   static int playing_channel = -1;
   static int playing_pitch = -1;

   int channel = 0;
   int patch = 0;
   int pitch = 60;
   int delay = 140;
   int i, l, r;

   switch (msg) {

      case MSG_START:
	 d->d1 = -1;
	 d->d2 = -1;
	 break;

      case MSG_DRAW:
	 for (i=0; i<d->w/12; i++) {
	    if (!blackkey[i%12]) {
	       l = i*12;
	       r = (i+1)*12;
	       if (blackkey[(i-1)%12])
		  l -= 6;
	       if (blackkey[(i+1)%12])
		  r += 6;
	       rectfill(screen, d->x+l+1, d->y+1, d->x+r-1, d->y+d->h-1, (i == d->d1) ? 1 : d->bg);
	       rect(screen, d->x+l, d->y, d->x+r, d->y+d->h, d->fg);
	    }
	 }
	 for (i=0; i<d->w/12; i++) {
	    if (blackkey[i%12]) {
	       l = i*12 + 1;
	       r = (i+1)*12 - 1;
	       rectfill(screen, d->x+l, d->y+d->h/4, d->x+r, d->y+d->h, (i == d->d1) ? 1 : d->fg);
	    }
	 }
	 break;

      case MSG_CLICK:
	 d->d1 = (mouse_x - d->x) / 12;

	 show_mouse(NULL);
	 set_clip(screen, d->x+d->d1*12-6, d->y, d->x+d->d1*12+18, d->y+d->h);
	 object_message(d, MSG_DRAW, 0);
	 set_clip(screen, 0, 0, SCREEN_W-1, SCREEN_H-1);
	 show_mouse(screen);

	 pitch = 36 + d->d1;
	 delay = 0;
	 /* fallthrough */

      case MSG_KEY:
	 d->d2 = retrace_count+delay;

	 if (playing_channel >= 0)
	    note_off(playing_channel, playing_pitch);

	 patch = thedialog[INSTLIST].d1;

	 if (patch >= 128) {
	    channel = 9;
	    pitch = patch - 93;
	 }
	 else {
	    set_patch(channel, patch);
	 }

	 set_pan(channel, MID(0, atoi(thedialog[PAN].dp), 127));
	 note_on(channel, pitch, MID(0, atoi(thedialog[VOLUME].dp), 127));

	 playing_channel = channel;
	 playing_pitch = pitch;

	 do {
	    poll_mouse();
	 } while ((mouse_b) && (d->d1 == (mouse_x - d->x) / 12));

	 if (d->d1 >= 0) {
	    show_mouse(NULL);
	    set_clip(screen, d->x+d->d1*12-6, d->y, d->x+d->d1*12+18, d->y+d->h);
	    d->d1 = -1;
	    object_message(d, MSG_DRAW, 0);
	    set_clip(screen, 0, 0, SCREEN_W-1, SCREEN_H-1);
	    show_mouse(screen);
	 }
	 break;

      case MSG_IDLE:
	 if ((d->d2 >= 0) && (retrace_count > d->d2)) {
	    if (playing_channel >= 0) {
	       note_off(playing_channel, playing_pitch);
	       playing_channel = -1;
	       playing_pitch = -1;
	    }
	    d->d2 = -1;
	 }
	 break;
   }

   return D_O_K;
}



char volume_str[4] = "255";
char pan_str[4] = "127";


DIALOG thedialog[] =
{
   /* (dialog proc)     (x)   (y)   (w)   (h)   (fg)  (bg)  (key)    (flags)     (d1)           (d2)     (dp)              (dp2) (dp3) */
   { d_clear_proc,      0,    0,    0,    0,    0,    8,    0,       0,          0,             0,       NULL,             NULL, NULL  },
   { d_ctext_proc,      320,  4,    0,    0,    255,  8,    0,       0,          0,             0,       "MIDI test program for Allegro " ALLEGRO_VERSION_STR ", " ALLEGRO_PLATFORM_STR, NULL, NULL },
   { d_ctext_proc,      320,  20,   0,    0,    255,  8,    0,       0,          0,             0,       "By Shawn Hargreaves, " ALLEGRO_DATE_STR, NULL, NULL },
   { d_text_proc,       320,  128,  0,    0,    255,  8,    0,       0,          0,             0,       "Driver:",        NULL, NULL  },
   { d_text_proc,       352,  152,  0,    0,    255,  8,    0,       0,          0,             0,       NULL,             NULL, NULL  },
   { d_text_proc,       320,  192,  0,    0,    255,  8,    0,       0,          0,             0,       "Description:",   NULL, NULL  },
   { d_text_proc,       352,  216,  0,    0,    255,  8,    0,       0,          0,             0,       NULL,             NULL, NULL  },
   { instlist_proc,     16,   56,   257,  356,  255,  0,    32,      D_EXIT,     0,             0,       instlist_getter,  NULL, NULL  },
   { piano_proc,        2,    440,  636,  40,   255,  0,    0,       0,          0,             0,       NULL,             NULL, NULL  },
   { d_text_proc,       320,  320,  0,    0,    255,  8,    0,       0,          0,             0,       "Volume:",        NULL, NULL  },
   { d_edit_proc,       392,  320,  64,   16,   255,  8,    0,       0,          3,             3,       volume_str,       NULL, NULL  },
   { d_text_proc,       320,  352,  0,    0,    255,  8,    0,       0,          0,             0,       "Pan:",           NULL, NULL  },
   { d_edit_proc,       392,  352,  64,   16,   255,  8,    0,       0,          3,             2,       pan_str,          NULL, NULL  },
   { d_yield_proc,      0,    0,    0,    0,    0,    0,    0,       0,          0,             0,       NULL,             NULL, NULL  },
   { NULL,              0,    0,    0,    0,    0,    0,    0,       0,          0,             0,       NULL,             NULL, NULL  }
};



int main()
{
   allegro_init();
   install_keyboard();
   install_mouse();
   install_timer();

   if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error setting graphics mode\n%s\n", allegro_error);
      return 1;
   }

   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error initialising sound\n%s\n", allegro_error);
      return 1;
   }

   set_palette(desktop_palette);

   #ifdef MIDI_DIGMID
      if (midi_driver->id == MIDI_DIGMID)
	 textout_centre(screen, font, "Loading patch set...", SCREEN_W/2, SCREEN_H/2, 255);
   #endif

   load_midi_patches();

   thedialog[DRIVER_STR].dp = (void*)midi_driver->name;
   thedialog[DESC_STR].dp = (void*)midi_driver->desc;

   do_dialog(thedialog, INSTLIST);

   return 0;
}

END_OF_MAIN();