1 /*************************************************************************
2  *  readwad.c
3  *
4  *  Updated to lxdoom and several improvements (see NOTES)
5  *  Copyright (C) 1999 Rafael Reilova (rreilova@ececs.uc.edu)
6  *
7  *  Copyright (C) 1995 Michael Heasley (mheasley@hmc.edu)
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *************************************************************************/
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include "musserver.h"
29 #include "sequencer.h"
30 
31 
32 static const unsigned char mus_id[4] = {'M', 'U', 'S', 0x1a};
33 static MUSFILE mus;
34 static const unsigned char opl_id[8] = {"#OPL_II#"};
35 static OPLFILE opl;
36 
37 /*
38  * dump MUS file to disk
39  */
dump_mus(void)40 static inline void dump_mus(void)
41 {
42   static int nsong;
43   FILE *song;
44   char buf[16];
45 
46   sprintf(buf, "song%02d.mus", nsong++);
47   song = fopen(buf, "w");
48   if (song)
49     {
50       fwrite(&mus.header, sizeof(struct MUSheader), 1, song);
51       fwrite(mus.instr_pnum, sizeof(unsigned short), mus.header.instrCnt, song);
52       fwrite(mus.musdata, 1, mus.header.scoreLen, song);
53       fclose(song);
54       if (verbose)
55 	      printf("%s: wrote \"%s\" MUS file\n", progname, buf);
56     }
57 }
58 
59 /*
60  * dump raw OPL midi information
61  */
dump_opl(void)62 static inline void dump_opl(void)
63 {
64   FILE *fmid;
65   const char rmidp[] = "raw_midi.patch";
66 
67   fmid = fopen(rmidp, "w");
68   if (fmid)
69     {
70       fwrite(opl.id, sizeof(opl.id), 1, fmid);
71       fwrite(opl.opl_instruments, sizeof(struct opl_instr), N_INTR, fmid);
72       fwrite(opl.instrument_nam, sizeof(instr_nam), N_INTR, fmid);
73       if (verbose)
74 	      printf("%s: wrote \"%s\" OPL3 patch file\n", progname, rmidp);
75       fclose(fmid);
76     }
77 }
78 
79 /*
80  * read MUS file
81  */
readmus(FILE * fp)82 const MUSFILE *readmus(FILE *fp)
83 {
84   static int max_score, max_instr;
85 
86   errno = 0;
87   if (fread(&mus.header, sizeof(mus.header), 1, fp) != 1)
88     cleanup(IO_ERROR, "can't read MUS file header");
89 
90   /* MUS ID check */
91   if (memcmp(mus.header.id, mus_id, sizeof(mus_id)))
92     cleanup(MISC_ERROR, "MUS file ID check failed");
93 
94   /* Allocate new memory if required */
95   if (max_instr < mus.header.instrCnt)
96     {
97       mus.instr_pnum = (unsigned short *) realloc(mus.instr_pnum,
98 						  sizeof(unsigned short) *
99 						  mus.header.instrCnt);
100       max_instr = mus.header.instrCnt;
101     }
102   if (max_score < mus.header.scoreLen)
103     {
104       mus.musdata = (unsigned char *) realloc(mus.musdata, mus.header.scoreLen);
105       max_score = mus.header.scoreLen;
106     }
107   if (!mus.instr_pnum || !mus.musdata)
108     cleanup(MISC_ERROR, "couldn't allocate memory for MUS file");
109 
110   /* read the instruments' patch numbers */
111   if (fread(mus.instr_pnum, sizeof(unsigned short), mus.header.instrCnt, fp)
112       != mus.header.instrCnt)
113     cleanup(IO_ERROR, "can't read MUS instrument data");
114 
115   /* read the music data */
116   if (fread(mus.musdata, 1, mus.header.scoreLen, fp) != mus.header.scoreLen)
117     cleanup(IO_ERROR, "can't read MUS music data");
118 
119   if (fp != stdin)
120     fclose(fp);
121   else if (dumpmus)
122     dump_mus();
123 
124   return &mus;
125 }
126 
127 /*
128  * read OPL GENMIDI patches
129  */
read_genmidi(FILE * fp)130 const OPLFILE *read_genmidi(FILE *fp)
131 {
132   errno = 0;
133   /* read the ID */
134   if (fread(opl.id, sizeof(opl_id), 1, fp) != 1)
135     cleanup(IO_ERROR, "Can't read GENMIDI ID");
136 
137   /* verify the ID */
138   if (memcmp(opl.id, opl_id, sizeof(opl_id)))
139     cleanup(MISC_ERROR, "GENMIDI file failed id check");
140 
141   /* allocate memory */
142   opl.opl_instruments=(struct opl_instr *)malloc(sizeof(struct opl_instr)*N_INTR);
143   opl.instrument_nam = (instr_nam *) malloc(sizeof(instr_nam)*N_INTR);
144   if (!opl.opl_instruments || !opl.instrument_nam)
145     cleanup(MISC_ERROR, "couldn't allocate memory for instrument data");
146 
147   /* read the data */
148   if (fread(opl.opl_instruments, sizeof(struct opl_instr), N_INTR, fp)!=N_INTR)
149     cleanup(IO_ERROR, "can't read instrument patch data");
150 
151   if (fread(opl.instrument_nam, sizeof(instr_nam), N_INTR, fp) != N_INTR)
152     cleanup(IO_ERROR, "can't read instrument names");
153 
154   if (fp != stdin)
155     fclose(fp);
156   else if (dumpmus)
157     dump_opl();
158 
159   return &opl;
160 }
161