]> 4ch.mooo.com Git - 16.git/blob - src/miditest.c
refresh wwww
[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                 //adv = ptick;
72                 if (adv >= 100UL) adv = 100UL;
73                 ptick = irq0_ticks;
74                 //ptick++;
75                 _sti();
76
77                 while (adv != 0) {
78                         midi_tick();
79                         adv--;
80                 }
81
82                 if (kbhit()) {
83                         c = getch();
84                         if (c == 0) c = getch() << 8;
85
86                         if (c == 27) {
87                                 break;
88                         }
89                 }
90         }
91
92         midi_playing = 0;
93         //adlib_shut_up();
94         shutdown_adlib();
95         _dos_setvect(8,old_irq0);
96         write_8254_system_timer(0); /* back to normal 18.2Hz */
97
98         for (i=0;i < MIDI_MAX_TRACKS;i++) {
99                 if (midi_trk[i].raw) {
100 #if TARGET_MSDOS == 16 && (defined(__LARGE__) || defined(__COMPACT__))
101                         _dos_freemem(FP_SEG(midi_trk[i].raw)); /* NTS: Because we allocated with _dos_allocmem */
102 #else
103                         free(midi_trk[i].raw);
104 #endif
105                         midi_trk[i].raw = NULL;
106                 }
107                 midi_trk[i].fence = NULL;
108                 midi_trk[i].read = NULL;
109         }
110
111         return 0;
112 }