summaryrefslogtreecommitdiff
path: root/plugins/adplug/adplug/mad.cpp
blob: 76e694c5b976282878315952c553936bab27c543 (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
/*
  Adplug - Replayer for many OPL2/OPL3 audio file formats.
  Copyright (C) 1999 - 2003 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

  mad.cpp - MAD loader by Riven the Mage <riven@ok.ru>
*/

#include <string.h>
#include "mad.h"

/* -------- Public Methods -------------------------------- */

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

bool CmadLoader::load(const char *filename, const CFileProvider &fp)
{
  binistream *f = fp.open(filename); if(!f) return false;
  const unsigned char conv_inst[10] = { 2,1,10,9,4,3,6,5,8,7 };
  unsigned int i, j, k, t = 0;

  // 'MAD+' - signed ?
  char id[4]; f->readString(id, 4);
  if (strncmp(id,"MAD+",4)) { fp.close(f); return false; }

  // load instruments
  for(i = 0; i < 9; i++) {
    f->readString(instruments[i].name, 8);
    for(j = 0; j < 12; j++) instruments[i].data[j] = f->readInt(1);
  }

  f->ignore(1);

  // data for Protracker
  length = f->readInt(1); nop = f->readInt(1); timer = f->readInt(1);

  // init CmodPlayer
  realloc_instruments(9);
  realloc_order(length);
  realloc_patterns(nop,32,9);
  init_trackord();

  // load tracks
  for(i = 0; i < nop; i++)
    for(k = 0; k < 32; k++)
      for(j = 0; j < 9; j++) {
	t = i * 9 + j;

	// read event
	unsigned char event = f->readInt(1);

	// convert event
	if (event < 0x61)
	  tracks[t][k].note = event;
	if (event == 0xFF) // 0xFF: Release note
	  tracks[t][k].command = 8;
	if (event == 0xFE) // 0xFE: Pattern Break
	  tracks[t][k].command = 13;
      }

  // load order
  for(i = 0; i < length; i++) order[i] = f->readInt(1) - 1;

  fp.close(f);

  // convert instruments
  for(i = 0; i < 9; i++)
    for(j = 0; j < 10; j++)
      inst[i].data[conv_inst[j]] = instruments[i].data[j];

  // data for Protracker
  restartpos = 0;
  initspeed = 1;

  rewind(0);
  return true;
}

void CmadLoader::rewind(int subsong)
{
	CmodPlayer::rewind(subsong);

	// default instruments
	for (int i=0;i<9;i++)
	{
		channel[i].inst = i;

		channel[i].vol1 = 63 - (inst[i].data[10] & 63);
		channel[i].vol2 = 63 - (inst[i].data[9] & 63);
	}
}

float CmadLoader::getrefresh()
{
	return (float)timer;
}

const char * CmadLoader::gettype()
{
	return "Mlat Adlib Tracker";
}

const char * CmadLoader::getinstrument(unsigned int n)
{
	return instruments[n].name;
}

unsigned int CmadLoader::getinstruments()
{
	return 9;
}