summaryrefslogtreecommitdiff
path: root/plugins/adplug/adplug/msc.cpp
blob: c9eade06522b7d39fc154c960ac3368e9db0a887 (plain)
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
/*
 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
 * Copyright (C) 1999 - 2006 Simon Peter, <dn.tlp@gmx.net>, et al.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * msc.c - MSC Player by Lubomir Bulej (pallas@kadan.cz)
 */

#include <cstring>
#include <stdio.h>

#include "msc.h"
#include "debug.h"

const unsigned char CmscPlayer::msc_signature [MSC_SIGN_LEN] = {
  'C', 'e', 'r', 'e', 's', ' ', '\x13', ' ',
  'M', 'S', 'C', 'p', 'l', 'a', 'y', ' ' };

/*** public methods *************************************/

CPlayer *CmscPlayer::factory (Copl * newopl)
{
  return new CmscPlayer (newopl);
}

CmscPlayer::CmscPlayer(Copl * newopl) : CPlayer (newopl)
{
  desc = NULL;
  msc_data = NULL;
  raw_data = NULL;
  nr_blocks = 0;
}

CmscPlayer::~CmscPlayer()
{
  if (raw_data != NULL)
    delete [] raw_data;
	
  if (msc_data != NULL) {
    // free compressed blocks
    for (int blk_num = 0; blk_num < nr_blocks; blk_num++) {
      if (msc_data [blk_num].mb_data != NULL)
	delete [] msc_data [blk_num].mb_data;
    }
		
    delete [] msc_data;
  }
	
  if (desc != NULL)
    delete [] desc;
}

bool CmscPlayer::load(const std::string & filename, const CFileProvider & fp)
{
  binistream * 	bf;
  msc_header	hdr;

  // open and validate the file
  bf = fp.open (filename);
  if (! bf)
    return false;
	
  if (! load_header (bf, & hdr)) {
    fp.close (bf);
    return false;
  }
	
  // get stuff from the header
  version = hdr.mh_ver;
  timer_div = hdr.mh_timer;
  nr_blocks = hdr.mh_nr_blocks;
  block_len = hdr.mh_block_len;

  if (! nr_blocks) {
    fp.close (bf);
    return false;
  }

  // load compressed data blocks
  msc_data = new msc_block [nr_blocks];
  raw_data = new u8 [block_len];

  for (int blk_num = 0; blk_num < nr_blocks; blk_num++) {
    msc_block blk;

    blk.mb_length = bf->readInt (2);
    blk.mb_data = new u8 [blk.mb_length];
    for (int oct_num = 0; oct_num < blk.mb_length; oct_num++) {
      blk.mb_data [oct_num] = bf->readInt (1);
    }

    msc_data [blk_num] = blk;
  }

  // clean up & initialize
  fp.close (bf);
  rewind (0);

  return true;
}

bool CmscPlayer::update()
{
  // output data
  while (! delay) {
    u8	cmnd;
    u8	data;

    // decode data
    if (! decode_octet (& cmnd))
      return false;
			
    if (! decode_octet (& data))
      return false;
			
    // check for special commands
    switch (cmnd) {

      // delay
    case 0xff:
      delay = 1 + (u8) (data - 1);
      break;
		
      // play command & data
    default:
      opl->write (cmnd, data);
			
    } // command switch
  } // play pass
	
	
  // count delays
  if (delay)
    delay--;
	
  // advance player position
  play_pos++;
  return true;
}

void CmscPlayer::rewind(int subsong)
{
  // reset state
  dec_prefix = 0;
  block_num = 0;
  block_pos = 0;
  play_pos = 0;
  raw_pos = 0;
  delay = 0;
	
  // init the OPL chip and go to OPL2 mode
  opl->init();
  opl->write(1, 32);
}

float CmscPlayer::getrefresh()
{
  // PC timer oscillator frequency / wait register
  return 1193180 / (float) (timer_div ? timer_div : 0xffff);
}

std::string CmscPlayer::gettype()
{
  char vstr [40];

  sprintf(vstr, "AdLib MSCplay (version %d)", version);
  return std::string (vstr);
}

/*** private methods *************************************/

bool CmscPlayer::load_header(binistream * bf, msc_header * hdr)
{
  // check signature
  bf->readString ((char *) hdr->mh_sign, sizeof (hdr->mh_sign));
  if (memcmp (msc_signature, hdr->mh_sign, MSC_SIGN_LEN) != 0)
    return false;

  // check version
  hdr->mh_ver = bf->readInt (2);
  if (hdr->mh_ver != 0)
    return false;

  bf->readString ((char *) hdr->mh_desc, sizeof (hdr->mh_desc));
  hdr->mh_timer = bf->readInt (2);
  hdr->mh_nr_blocks = bf->readInt (2);
  hdr->mh_block_len = bf->readInt (2);
  return true;
}

bool CmscPlayer::decode_octet(u8 * output)
{
  msc_block blk;			// compressed data block
	
  if (block_num >= nr_blocks)
    return false;
		
  blk = msc_data [block_num];
  while (1) {
    u8 	octet;		// decoded octet
    u8	len_corr = 0;	// length correction
		
    // advance to next block if necessary
    if (block_pos >= blk.mb_length && dec_len == 0) {
      block_num++;
      if (block_num >= nr_blocks)
	return false;
				
      blk = msc_data [block_num];
      block_pos = 0;
      raw_pos = 0;
    }
		
    // decode the compressed music data
    switch (dec_prefix) {
		
      // decode prefix
    case 155:
    case 175:
      octet = blk.mb_data [block_pos++];
      if (octet == 0) {
	// invalid prefix, output original
	octet = dec_prefix;
	dec_prefix = 0;
	break;
      }
			
      // isolate length and distance
      dec_len = (octet & 0x0F);
      len_corr = 2;

      dec_dist = (octet & 0xF0) >> 4;
      if (dec_prefix == 155)
	dec_dist++;
				
      // next decode step for respective prefix type
      dec_prefix++;
      continue;
			

      // check for extended length
    case 156:
      if (dec_len == 15)
	dec_len += blk.mb_data [block_pos++];

      // add length correction and go for copy mode
      dec_len += len_corr;
      dec_prefix = 255;
      continue;
			
			
      // get extended distance
    case 176:
      dec_dist += 17 + 16 * blk.mb_data [block_pos++];
      len_corr = 3;
			
      // check for extended length
      dec_prefix = 156;
      continue;
			
			
      // prefix copy mode
    case 255:
      if((int)raw_pos >= dec_dist)
	octet = raw_data [raw_pos - dec_dist];
      else {
	AdPlug_LogWrite("error! read before raw_data buffer.\n");
	octet = 0;
      }

      dec_len--;
      if (dec_len == 0) {
	// back to normal mode
	dec_prefix = 0;
      }
			
      break;

			
      // normal mode
    default:
      octet = blk.mb_data [block_pos++];
      if (octet == 155 || octet == 175) {
	// it's a prefix, restart
	dec_prefix = octet;
	continue;
      }
    } // prefix switch

			
    // output the octet
    if (output != NULL)
      *output = octet;
			
    raw_data [raw_pos++] = octet;
    break;
  }; // decode pass

  return true;
}