]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/adplug-2.2.1/src/adtrack.cpp
Please enter the commit message for your changes. Lines starting
[16.git] / 16 / adplug / adplug-2.2.1 / src / adtrack.cpp
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  * adtrack.cpp - Adlib Tracker 1.0 Loader by Simon Peter <dn.tlp@gmx.net>
20  *
21  * NOTES:
22  * The original Adlib Tracker 1.0 is behaving a little different from the
23  * official spec: The 'octave' integer from the instrument file is stored
24  * "minus 1" from the actual value, underflowing from 0 to 0xffff.
25  *
26  * I also noticed that my player is playing everything transposed a few tones
27  * higher than the original tracker. As far as i can see, my player perfectly
28  * follows the official spec, so it "must" be the tracker that does something
29  * wrong here...
30  */
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "adtrack.h"
36 #include "debug.h"
37
38 /*** Public methods ***/
39
40 CPlayer *CadtrackLoader::factory(Copl *newopl)
41 {
42   return new CadtrackLoader(newopl);
43 }
44
45 bool CadtrackLoader::load(const std::string &filename, const CFileProvider &fp)
46 {
47   binistream *f = fp.open(filename); if(!f) return false;
48   binistream *instf;
49   char note[2];
50   unsigned short rwp;
51   unsigned char chp, octave, pnote = 0;
52   int i,j;
53   AdTrackInst myinst;
54
55   // file validation
56   if(!fp.extension(filename, ".sng") || fp.filesize(f) != 36000)
57     { fp.close(f); return false; }
58
59   // check for instruments file
60   std::string instfilename(filename, 0, filename.find_last_of('.'));
61   instfilename += ".ins";
62   AdPlug_LogWrite("CadtrackLoader::load(,\"%s\"): Checking for \"%s\"...\n",
63                   filename.c_str(), instfilename.c_str());
64   instf = fp.open(instfilename);
65   if(!instf || fp.filesize(instf) != 468) { fp.close(f); return false; }
66
67   // give CmodPlayer a hint on what we're up to
68   realloc_patterns(1,1000,9); realloc_instruments(9); realloc_order(1);
69   init_trackord(); flags = NoKeyOn;
70   (*order) = 0; length = 1; restartpos = 0; bpm = 120; initspeed = 3;
71
72   // load instruments from instruments file
73   for(i=0;i<9;i++) {
74     for(j=0;j<2;j++) {
75       myinst.op[j].appampmod = instf->readInt(2);
76       myinst.op[j].appvib = instf->readInt(2);
77       myinst.op[j].maintsuslvl = instf->readInt(2);
78       myinst.op[j].keybscale = instf->readInt(2);
79       myinst.op[j].octave = instf->readInt(2);
80       myinst.op[j].freqrisevollvldn = instf->readInt(2);
81       myinst.op[j].softness = instf->readInt(2);
82       myinst.op[j].attack = instf->readInt(2);
83       myinst.op[j].decay = instf->readInt(2);
84       myinst.op[j].release = instf->readInt(2);
85       myinst.op[j].sustain = instf->readInt(2);
86       myinst.op[j].feedback = instf->readInt(2);
87       myinst.op[j].waveform = instf->readInt(2);
88     }
89     convert_instrument(i, &myinst);
90   }
91   fp.close(instf);
92
93   // load file
94   for(rwp=0;rwp<1000;rwp++)
95     for(chp=0;chp<9;chp++) {
96       // read next record
97       f->readString(note, 2); octave = f->readInt(1); f->ignore();
98       switch(*note) {
99       case 'C': if(note[1] == '#') pnote = 2; else pnote = 1; break;
100       case 'D': if(note[1] == '#') pnote = 4; else pnote = 3; break;
101       case 'E': pnote = 5; break;
102       case 'F': if(note[1] == '#') pnote = 7; else pnote = 6; break;
103       case 'G': if(note[1] == '#') pnote = 9; else pnote = 8; break;
104       case 'A': if(note[1] == '#') pnote = 11; else pnote = 10; break;
105       case 'B': pnote = 12; break;
106       case '\0':
107         if(note[1] == '\0')
108           tracks[chp][rwp].note = 127;
109         else {
110           fp.close(f);
111           return false;
112         }
113         break;
114       default: fp.close(f); return false;
115       }
116       if((*note) != '\0') {
117         tracks[chp][rwp].note = pnote + (octave * 12);
118         tracks[chp][rwp].inst = chp + 1;
119       }
120     }
121
122   fp.close(f);
123   rewind(0);
124   return true;
125 }
126
127 float CadtrackLoader::getrefresh()
128 {
129   return 18.2f;
130 }
131
132 /*** Private methods ***/
133
134 void CadtrackLoader::convert_instrument(unsigned int n, AdTrackInst *i)
135 {
136   // Carrier "Amp Mod / Vib / Env Type / KSR / Multiple" register
137   inst[n].data[2] = i->op[Carrier].appampmod ? 1 << 7 : 0;
138   inst[n].data[2] += i->op[Carrier].appvib ? 1 << 6 : 0;
139   inst[n].data[2] += i->op[Carrier].maintsuslvl ? 1 << 5 : 0;
140   inst[n].data[2] += i->op[Carrier].keybscale ? 1 << 4 : 0;
141   inst[n].data[2] += (i->op[Carrier].octave + 1) & 0xffff; // Bug in original tracker
142   // Modulator...
143   inst[n].data[1] = i->op[Modulator].appampmod ? 1 << 7 : 0;
144   inst[n].data[1] += i->op[Modulator].appvib ? 1 << 6 : 0;
145   inst[n].data[1] += i->op[Modulator].maintsuslvl ? 1 << 5 : 0;
146   inst[n].data[1] += i->op[Modulator].keybscale ? 1 << 4 : 0;
147   inst[n].data[1] += (i->op[Modulator].octave + 1) & 0xffff; // Bug in original tracker
148
149   // Carrier "Key Scaling / Level" register
150   inst[n].data[10] = (i->op[Carrier].freqrisevollvldn & 3) << 6;
151   inst[n].data[10] += i->op[Carrier].softness & 63;
152   // Modulator...
153   inst[n].data[9] = (i->op[Modulator].freqrisevollvldn & 3) << 6;
154   inst[n].data[9] += i->op[Modulator].softness & 63;
155
156   // Carrier "Attack / Decay" register
157   inst[n].data[4] = (i->op[Carrier].attack & 0x0f) << 4;
158   inst[n].data[4] += i->op[Carrier].decay & 0x0f;
159   // Modulator...
160   inst[n].data[3] = (i->op[Modulator].attack & 0x0f) << 4;
161   inst[n].data[3] += i->op[Modulator].decay & 0x0f;
162
163   // Carrier "Release / Sustain" register
164   inst[n].data[6] = (i->op[Carrier].release & 0x0f) << 4;
165   inst[n].data[6] += i->op[Carrier].sustain & 0x0f;
166   // Modulator...
167   inst[n].data[5] = (i->op[Modulator].release & 0x0f) << 4;
168   inst[n].data[5] += i->op[Modulator].sustain & 0x0f;
169
170   // Channel "Feedback / Connection" register
171   inst[n].data[0] = (i->op[Carrier].feedback & 7) << 1;
172
173   // Carrier/Modulator "Wave Select" registers
174   inst[n].data[8] = i->op[Carrier].waveform & 3;
175   inst[n].data[7] = i->op[Modulator].waveform & 3;
176 }