]> 4ch.mooo.com Git - 16.git/blob - src/miditest.c
012301662a294366dce88c777f37189e114731d0
[16.git] / src / miditest.c
1 /* miditest.c
2  *
3  * Adlib OPL2/OPL3 FM synthesizer chipset test program.
4  * Play MIDI file using the OPLx synthesizer (well, poorly anyway)
5  * (C) 2010-2012 Jonathan Campbell.
6  * Hackipedia DOS library.
7  *
8  * This code is licensed under the LGPL.
9  * <insert LGPL legal text here>
10  *
11  * Compiles for intended target environments:
12  *   - MS-DOS [pure DOS mode, or Windows or OS/2 DOS Box]
13  */
14
15 #include "src/lib/midi.h"
16
17 int main(int argc,char **argv) {
18         unsigned long ptick;
19         int i,c;
20
21         printf("ADLIB FM test program\n");
22         if (argc < 2) {
23                 printf("You must specify a MIDI file to play\n");
24                 return 1;
25         }
26
27         /*if (!probe_vga()) {
28                 printf("Cannot init VGA\n");
29                 return 1;
30         }*/
31         if (!init_adlib()) {
32                 printf("Cannot init library\n");
33                 return 1;
34         }
35         if (!probe_8254()) { // we need the timer to keep time with the music
36                 printf("8254 timer not found\n");
37                 return 1;
38         }
39
40         for (i=0;i < MIDI_MAX_TRACKS;i++) {
41                 midi_trk[i].raw = NULL;
42                 midi_trk[i].read = NULL;
43                 midi_trk[i].fence = NULL;
44         }
45
46         if (load_midi_file(argv[1]) == 0) {
47                 printf("Failed to load MIDI\n");
48                 return 1;
49         }
50
51         write_8254_system_timer(T8254_REF_CLOCK_HZ / 100); // tick faster at 100Hz please
52         irq0_cnt = 0;
53         irq0_add = 182;
54         irq0_max = 1000; // about 18.2Hz
55         old_irq0 = _dos_getvect(8);//IRQ0
56         _dos_setvect(8,irq0);
57
58         adlib_shut_up();
59         midi_reset_channels();
60         midi_reset_tracks();
61         _cli();
62         irq0_ticks = ptick = 0;
63         _sti();
64         midi_playing = 1;
65
66         while (1) {
67                 unsigned long adv;
68
69                 _cli();
70                 adv = irq0_ticks - ptick;
71                 if (adv >= 100UL) adv = 100UL;
72                 ptick = irq0_ticks;
73                 _sti();
74
75                 while (adv != 0) {
76                         midi_tick();
77                         adv--;
78                 }
79
80                 if (kbhit()) {
81                         c = getch();
82                         if (c == 0) c = getch() << 8;
83
84                         if (c == 27) {
85                                 break;
86                         }
87                 }
88         }
89
90         midi_playing = 0;
91         adlib_shut_up();
92         shutdown_adlib();
93         _dos_setvect(8,old_irq0);
94         write_8254_system_timer(0); // back to normal 18.2Hz
95
96         for (i=0;i < MIDI_MAX_TRACKS;i++) {
97                 if (midi_trk[i].raw) {
98 #if TARGET_MSDOS == 16 && (defined(__LARGE__) || defined(__COMPACT__))
99                         _dos_freemem(FP_SEG(midi_trk[i].raw)); // NTS: Because we allocated with _dos_allocmem
100 #else
101                         free(midi_trk[i].raw);
102 #endif
103                         midi_trk[i].raw = NULL;
104                 }
105                 midi_trk[i].fence = NULL;
106                 midi_trk[i].read = NULL;
107         }
108
109         return 0;
110 }