-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmtk.cpp
More file actions
146 lines (124 loc) · 4.16 KB
/
Copy pathmtk.cpp
File metadata and controls
146 lines (124 loc) · 4.16 KB
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
/*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* mtk.cpp - MPU-401 Trakker Loader by Simon Peter (dn.tlp@gmx.net)
*/
#include <cstring>
#include "mtk.h"
/*** public methods **************************************/
CPlayer *CmtkLoader::factory(Copl *newopl)
{
return new CmtkLoader(newopl);
}
bool CmtkLoader::load(const std::string &filename, const CFileProvider &fp)
{
binistream *f = fp.open(filename);
if (!f) return false;
struct {
char id[18];
unsigned short crc, size;
} header;
struct mtkdata {
struct { char dummy, str[33]; } songname, composername, instname[0x80];
unsigned char insts[0x80][12], order[0x80], dummy;
// followed by pattern data:
// hscnote patterns[50][64*9];
} *data;
unsigned int i, cnt;
// read header
f->readString(header.id, sizeof(header.id));
header.crc = f->readInt(2);
header.size = f->readInt(2);
// file validation section
if (memcmp(header.id, "mpu401tr\x92kk\xeer@data", sizeof(header.id)) ||
header.size < sizeof(*data)) {
fp.close(f); return false;
}
// load & decompress section
unsigned short ctrlbits = 0, ctrlmask = 0;
unsigned char *org = new unsigned char[header.size];
for (size_t orgptr = 0; orgptr < header.size; orgptr += cnt) {
if (f->error()) goto err;
ctrlmask >>= 1;
if (!ctrlmask) {
ctrlbits = f->readInt(2);
ctrlmask = 0x8000;
}
if (!(ctrlbits & ctrlmask)) { // uncompressed data
org[orgptr] = f->readInt(1);
cnt = 1;
continue;
}
// compressed data
unsigned offs;
unsigned char cmd = f->readInt(1);
cnt = (cmd & 0x0f) + 3;
switch (cmd >> 4) {
case 0: // repeat a byte 3..18 times
repeat_byte:
if (orgptr + cnt > header.size) goto err;
memset(&org[orgptr], f->readInt(1), cnt);
break;
case 1: // repeat a byte 19..4114 times
cnt += (f->readInt(1) << 4) + 16;
goto repeat_byte;
case 2: // copy range (16..271 bytes)
offs = cnt + (f->readInt(1) << 4);
cnt = f->readInt(1) + 16;
copy_range:
if (orgptr + cnt > header.size || offs > orgptr) goto err;
// may overlap, can't use memcpy()
for (i = 0; i < cnt; i++)
org[orgptr + i] = org[orgptr - offs + i];
break;
default: // copy range (3..15 bytes)
offs = cnt + (f->readInt(1) << 4);
cnt = cmd >> 4;
goto copy_range;
}
}
if (f->error() || !f->ateof()) goto err;
fp.close(f);
// convert to HSC replay data
data = (struct mtkdata *) org;
strncpy(title, data->songname.str, sizeof(title) - 1);
title[sizeof(title) - 1] = 0;
strncpy(composer, data->composername.str, sizeof(composer) - 1);
composer[sizeof(composer) - 1] = 0;
for (i = 0; i < 0x80; i++) {
strncpy(instname[i], data->instname[i].str, sizeof(instname[i]) - 1);
instname[i][sizeof(instname[i]) - 1] = 0;
}
memcpy(instr, data->insts, sizeof(instr));
for (i = 0; i < 0x80; i++) { // correct instruments
instr[i][2] ^= (instr[i][2] & 0x40) << 1;
instr[i][3] ^= (instr[i][3] & 0x40) << 1;
instr[i][11] >>= 4; // make unsigned
}
memcpy(song, data->order, sizeof(song));
cnt = header.size - sizeof(*data); // was off by 1
if (cnt > sizeof(patterns)) cnt = sizeof(patterns); // fail?
memcpy(patterns, org + sizeof(*data), cnt);
delete [] org;
rewind(0);
return true;
err:
fp.close(f);
delete [] org;
return false;
}