comparison Plugins/Input/adplug/core/amd.cpp @ 359:8df427a314a8 trunk

[svn] Adlib synthesizer (AdPlug) support.
author chainsaw
date Fri, 30 Dec 2005 16:31:39 -0800
parents
children 6ad7eb96dd26
comparison
equal deleted inserted replaced
358:70075730e187 359:8df427a314a8
1 /*
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2003 Simon Peter, <dn.tlp@gmx.net>, et al.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * amd.cpp - AMD Loader by Simon Peter <dn.tlp@gmx.net>
20 */
21
22 #include <string.h>
23
24 #include "amd.h"
25 #include "debug.h"
26
27 CPlayer *CamdLoader::factory(Copl *newopl)
28 {
29 return new CamdLoader(newopl);
30 }
31
32 bool CamdLoader::load(const std::string &filename, const CFileProvider &fp)
33 {
34 binistream *f = fp.open(filename); if(!f) return false;
35 struct {
36 char id[9];
37 unsigned char version;
38 } header;
39 int i, j, k, t, numtrax;
40 unsigned char buf, buf2, buf3;
41 const unsigned char convfx[10] = {0,1,2,9,17,11,13,18,3,14};
42
43 // file validation section
44 if(fp.filesize(f) < 1072) { fp.close(f); return false; }
45 f->seek(1062); f->readString(header.id, 9);
46 header.version = f->readInt(1);
47 if(strncmp(header.id, "<o\xefQU\xeeRoR", 9) &&
48 strncmp(header.id, "MaDoKaN96", 9)) { fp.close(f); return false; }
49
50 // load section
51 memset(inst, 0, sizeof(inst));
52 f->seek(0);
53 f->readString(songname, sizeof(songname));
54 f->readString(author, sizeof(author));
55 for(i = 0; i < 26; i++) {
56 f->readString(instname[i], 23);
57 for(j = 0; j < 11; j++) inst[i].data[j] = f->readInt(1);
58 }
59 length = f->readInt(1); nop = f->readInt(1) + 1;
60 for(i=0;i<128;i++) order[i] = f->readInt(1);
61 f->seek(10, binio::Add);
62 if(header.version == 0x10) { // unpacked module
63 for(i=0;i<64*9;i++)
64 trackord[i/9][i%9] = i+1;
65 t = 0;
66 while(!f->ateof()) {
67 for(j=0;j<64;j++)
68 for(i=t;i<t+9;i++) {
69 buf = f->readInt(1);
70 tracks[i][j].param2 = (buf&127) % 10;
71 tracks[i][j].param1 = (buf&127) / 10;
72 buf = f->readInt(1);
73 tracks[i][j].inst = buf >> 4;
74 tracks[i][j].command = buf & 0x0f;
75 buf = f->readInt(1);
76 if(buf >> 4) // fix bug in AMD save routine
77 tracks[i][j].note = ((buf & 14) >> 1) * 12 + (buf >> 4);
78 else
79 tracks[i][j].note = 0;
80 tracks[i][j].inst += (buf & 1) << 4;
81 }
82 t += 9;
83 }
84 } else { // packed module
85 for(i=0;i<nop;i++)
86 for(j=0;j<9;j++)
87 trackord[i][j] = f->readInt(2) + 1;
88 numtrax = f->readInt(2);
89 for(k=0;k<numtrax;k++) {
90 i = f->readInt(2);
91 if(i > 575) i = 575; // fix corrupted modules
92 j = 0;
93 do {
94 buf = f->readInt(1);
95 if(buf & 128) {
96 for(t = j; t < j + (buf & 127) && t < 64; t++) {
97 tracks[i][t].command = 0;
98 tracks[i][t].inst = 0;
99 tracks[i][t].note = 0;
100 tracks[i][t].param1 = 0;
101 tracks[i][t].param2 = 0;
102 }
103 j += buf & 127;
104 continue;
105 }
106 tracks[i][j].param2 = buf % 10;
107 tracks[i][j].param1 = buf / 10;
108 buf = f->readInt(1);
109 tracks[i][j].inst = buf >> 4;
110 tracks[i][j].command = buf & 0x0f;
111 buf = f->readInt(1);
112 if(buf >> 4) // fix bug in AMD save routine
113 tracks[i][j].note = ((buf & 14) >> 1) * 12 + (buf >> 4);
114 else
115 tracks[i][j].note = 0;
116 tracks[i][j].inst += (buf & 1) << 4;
117 j++;
118 } while(j<64);
119 }
120 }
121 fp.close(f);
122
123 // convert to protracker replay data
124 bpm = 50; restartpos = 0; activechan = 0xffff; flags = Decimal;
125 for(i=0;i<26;i++) { // convert instruments
126 buf = inst[i].data[0];
127 buf2 = inst[i].data[1];
128 inst[i].data[0] = inst[i].data[10];
129 inst[i].data[1] = buf;
130 buf = inst[i].data[2];
131 inst[i].data[2] = inst[i].data[5];
132 buf3 = inst[i].data[3];
133 inst[i].data[3] = buf;
134 buf = inst[i].data[4];
135 inst[i].data[4] = inst[i].data[7];
136 inst[i].data[5] = buf3;
137 buf3 = inst[i].data[6];
138 inst[i].data[6] = inst[i].data[8];
139 inst[i].data[7] = buf;
140 inst[i].data[8] = inst[i].data[9];
141 inst[i].data[9] = buf2;
142 inst[i].data[10] = buf3;
143 for(j=0;j<23;j++) // convert names
144 if(instname[i][j] == '\xff')
145 instname[i][j] = '\x20';
146 }
147 for(i=0;i<nop*9;i++) // convert patterns
148 for(j=0;j<64;j++) {
149 tracks[i][j].command = convfx[tracks[i][j].command];
150 if(tracks[i][j].command == 14) {
151 if(tracks[i][j].param1 == 2) {
152 tracks[i][j].command = 10;
153 tracks[i][j].param1 = tracks[i][j].param2;
154 tracks[i][j].param2 = 0;
155 }
156 if(tracks[i][j].param1 == 3) {
157 tracks[i][j].command = 10;
158 tracks[i][j].param1 = 0;
159 }
160 }
161 }
162
163 rewind(0);
164 return true;
165 }
166
167 float CamdLoader::getrefresh()
168 {
169 if(tempo)
170 return (float) (tempo);
171 else
172 return 18.2f;
173 }