summaryrefslogtreecommitdiff
path: root/plugins/adplug/adplug/bam.cpp
blob: 20ee4d8dc4c018ca69ca07bad5be779a50d24569 (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
/*
 * 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
 *
 * bam.cpp - Bob's Adlib Music Player, by Simon Peter <dn.tlp@gmx.net>
 *
 * NOTES:
 * In my player, the loop counter is stored with the label. This can be
 * dangerous for some situations (see below), but there shouldn't be any BAM
 * files triggering this situation.
 *
 * From SourceForge Bug #476088:
 * -----------------------------
 * Using just one loop counter for each label, my player can't
 * handle files that loop twice to the same label (if that's at
 * all possible with BAM). Imagine the following situation:
 * 
 * ... [*] ---- [<- *] ---- [<- *] ...
 *  ^   ^    ^     ^     ^     ^    ^
 *  |   |    |     |     |     |    |
 *  +---|----+-----|-----+-----|----+--- normal song data
 *      +----------|-----------|-------- label 1
 *                 +-----------+-------- loop points to label 1
 * 
 * both loop points loop to the same label. Storing the loop 
 * count with the label would cause chaos with the counter, 
 * when the player executes the inner jump.
 * ------------------
 * Not to worry. my reference implementation of BAM does not
 * support the multiple loop situation you describe, and
 * neither do any BAM-creation programs. Then both loops point
 * to the same label, the inner loop's counter is just allowed
 * to clobber the outer loop's counter. No stack is neccisary.
 */

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

const unsigned short CbamPlayer::freq[] = {172,182,193,205,217,230,243,258,274,
290,307,326,345,365,387,410,435,460,489,517,547,580,614,651,1369,1389,1411,
1434,1459,1484,1513,1541,1571,1604,1638,1675,2393,2413,2435,2458,2483,2508,
2537,2565,2595,2628,2662,2699,3417,3437,3459,3482,3507,3532,3561,3589,3619,
3652,3686,3723,4441,4461,4483,4506,4531,4556,4585,4613,4643,4676,4710,4747,
5465,5485,5507,5530,5555,5580,5609,5637,5667,5700,5734,5771,6489,6509,6531,
6554,6579,6604,6633,6661,6691,6724,6758,6795,7513,7533,7555,7578,7603,7628,
7657,7685,7715,7748,7782,7819,7858,7898,7942,7988,8037,8089,8143,8191,8191,
8191,8191,8191,8191,8191,8191,8191,8191,8191,8191};

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

bool CbamPlayer::load(const char *filename, const CFileProvider &fp)
{
        binistream *f = fp.open(filename); if(!f) return false;
	char id[4];
	unsigned int i;

	size = fp.filesize(f) - 4;	// filesize minus header
	f->readString(id, 4);
	if(strncmp(id,"CBMF",4)) { fp.close(f); return false; }

	song = new unsigned char [size];
	for(i = 0; i < size; i++) song[i] = f->readInt(1);

	fp.close(f);
	rewind(0);
	return true;
}

bool CbamPlayer::update()
{
	unsigned char	cmd,c;

	if(del) {
		del--;
		return !songend;
	}

	if(pos >= size) {	// EOF detection
		pos = 0;
		songend = true;
	}

	while(song[pos] < 128) {
		cmd = song[pos] & 240;
		c = song[pos] & 15;
		switch(cmd) {
		case 0:		// stop song
			pos = 0;
			songend = true;
			break;
		case 16:	// start note
			if(c < 9) {
				opl->write(0xa0 + c, freq[song[++pos]] & 255);
				opl->write(0xb0 + c, (freq[song[pos]] >> 8) + 32);
			} else
				pos++;
			pos++;
			break;
		case 32:	// stop note
			if(c < 9)
				opl->write(0xb0 + c, 0);
			pos++;
			break;
		case 48:	// define instrument
			if(c < 9) {
				opl->write(0x20 + op_table[c],song[pos+1]);
				opl->write(0x23 + op_table[c],song[pos+2]);
				opl->write(0x40 + op_table[c],song[pos+3]);
				opl->write(0x43 + op_table[c],song[pos+4]);
				opl->write(0x60 + op_table[c],song[pos+5]);
				opl->write(0x63 + op_table[c],song[pos+6]);
				opl->write(0x80 + op_table[c],song[pos+7]);
				opl->write(0x83 + op_table[c],song[pos+8]);
				opl->write(0xe0 + op_table[c],song[pos+9]);
				opl->write(0xe3 + op_table[c],song[pos+10]);
				opl->write(0xc0 + c,song[pos+11]);
			}
			pos += 12;
			break;
		case 80:	// set label
			label[c].target = ++pos;
			label[c].defined = true;
			break;
		case 96:	// jump
			if(label[c].defined)
				switch(song[pos+1]) {
				case 254:	// infinite loop
					if(label[c].defined) {
						pos = label[c].target;
						songend = true;
						break;
					}
					// fall through...
				case 255:	// chorus
					if(!chorus && label[c].defined) {
						chorus = true;
						gosub = pos + 2;
						pos = label[c].target;
						break;
					}
					// fall through...
				case 0:		// end of loop
					pos += 2;
					break;
				default:	// finite loop
					if(!label[c].count) {	// loop elapsed
						label[c].count = 255;
						pos += 2;
						break;
					}
					if(label[c].count < 255)	// loop defined
						label[c].count--;
					else						// loop undefined
						label[c].count = song[pos+1] - 1;
					pos = label[c].target;
					break;
				}
			break;
		case 112:	// end of chorus
			if(chorus) {
				pos = gosub;
				chorus = false;
			} else
				pos++;
			break;
		default:	// reserved command (skip)
			pos++;
			break;
		}
	}
	if(song[pos] >= 128) {		// wait
		del = song[pos] - 127;
		pos++;
	}
	return !songend;
}

void CbamPlayer::rewind(int subsong)
{
        int i;

	pos = 0; songend = false; del = 0; gosub = 0; chorus = false;
	memset(label, 0, sizeof(label)); label[0].defined = true;
	for(i = 0; i < 16; i++) label[i].count = 255;	// 255 = undefined
	opl->init(); opl->write(1,32);
}