File: readwad.c

package info (click to toggle)
lxmusserv 0.94-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 176 kB
  • ctags: 189
  • sloc: ansic: 1,530; makefile: 73
file content (176 lines) | stat: -rw-r--r-- 4,850 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
/*************************************************************************
 *  readwad.c
 *
 *  Updated to lxdoom and several improvements (see NOTES)
 *  Copyright (C) 1999 Rafael Reilova (rreilova@ececs.uc.edu)
 *
 *  Copyright (C) 1995 Michael Heasley (mheasley@hmc.edu)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "musserver.h"
#include "sequencer.h"


static const unsigned char mus_id[4] = {'M', 'U', 'S', 0x1a};
static MUSFILE mus;
static const unsigned char opl_id[8] = {"#OPL_II#"};
static OPLFILE opl;

/*
 * dump MUS file to disk
 */
static inline void dump_mus(void)
{
  static int nsong;
  FILE *song;
  char buf[16];

  sprintf(buf, "song%02d.mus", nsong++);
  song = fopen(buf, "w");
  if (song)
    {
      fwrite(&mus.header, sizeof(struct MUSheader), 1, song);
      fwrite(mus.instr_pnum, sizeof(unsigned short), mus.header.instrCnt, song);
      fwrite(mus.musdata, 1, mus.header.scoreLen, song);
      fclose(song);
      if (verbose)
	      printf("%s: wrote \"%s\" MUS file\n", progname, buf);
    }
}

/*
 * dump raw OPL midi information
 */
static inline void dump_opl(void)
{
  FILE *fmid;
  const char rmidp[] = "raw_midi.patch";

  fmid = fopen(rmidp, "w");
  if (fmid)
    {
      fwrite(opl.id, sizeof(opl.id), 1, fmid);
      fwrite(opl.opl_instruments, sizeof(struct opl_instr), N_INTR, fmid);
      fwrite(opl.instrument_nam, sizeof(instr_nam), N_INTR, fmid);
      if (verbose)
	      printf("%s: wrote \"%s\" OPL3 patch file\n", progname, rmidp);
      fclose(fmid);
    }
}

/*
 * read MUS file
 */
const MUSFILE *readmus(FILE *fp)
{
  static int max_score, max_instr;

  if (fp == stdin)
    mark_block ();

  errno = 0;
  if (fread(&mus.header, sizeof(mus.header), 1, fp) != 1)
    cleanup(IO_ERROR, "can't read MUS file header");

  /* MUS ID check */
  if (memcmp(mus.header.id, mus_id, sizeof(mus_id)))
    cleanup(MISC_ERROR, "MUS file ID check failed");

  /* Allocate new memory if required */
  if (max_instr < mus.header.instrCnt)
    {
      mus.instr_pnum = (unsigned short *) realloc(mus.instr_pnum,
						  sizeof(unsigned short) *
						  mus.header.instrCnt);
      max_instr = mus.header.instrCnt;
    }
  if (max_score < mus.header.scoreLen)
    {
      mus.musdata = (unsigned char *) realloc(mus.musdata, mus.header.scoreLen);
      max_score = mus.header.scoreLen;
    }
  if (!mus.instr_pnum || !mus.musdata)
    cleanup(MISC_ERROR, "couldn't allocate memory for MUS file");

  /* read the instruments' patch numbers */
  if (fread(mus.instr_pnum, sizeof(unsigned short), mus.header.instrCnt, fp)
      != mus.header.instrCnt)
    cleanup(IO_ERROR, "can't read MUS instrument data");

  /* read the music data */
  if (fread(mus.musdata, 1, mus.header.scoreLen, fp) != mus.header.scoreLen)
    cleanup(IO_ERROR, "can't read MUS music data");

  if (fp != stdin)
    fclose(fp);
  else
  {
    if (dumpmus)
      dump_mus();
    mark_nonblock ();
  }


  return &mus;
}

/*
 * read OPL GENMIDI patches
 */
const OPLFILE *read_genmidi(FILE *fp)
{
  errno = 0;

  if (fp == stdin)
    mark_block ();

  /* read the ID */
  if (fread(opl.id, sizeof(opl_id), 1, fp) != 1)
    cleanup(IO_ERROR, "Can't read GENMIDI ID");

  /* verify the ID */
  if (memcmp(opl.id, opl_id, sizeof(opl_id)))
    cleanup(MISC_ERROR, "GENMIDI file failed id check");

  /* allocate memory */
  opl.opl_instruments=(struct opl_instr *)malloc(sizeof(struct opl_instr)*N_INTR);
  opl.instrument_nam = (instr_nam *) malloc(sizeof(instr_nam)*N_INTR);
  if (!opl.opl_instruments || !opl.instrument_nam)
    cleanup(MISC_ERROR, "couldn't allocate memory for instrument data");

  /* read the data */
  if (fread(opl.opl_instruments, sizeof(struct opl_instr), N_INTR, fp)!=N_INTR)
    cleanup(IO_ERROR, "can't read instrument patch data");

  if (fread(opl.instrument_nam, sizeof(instr_nam), N_INTR, fp) != N_INTR)
    cleanup(IO_ERROR, "can't read instrument names");

  if (fp != stdin)
    fclose(fp);
  else
  {
    if (dumpmus)
      dump_opl();
    mark_nonblock ();
  }

  return &opl;
}