]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/adplug/src/mad.cpp
wwww~
[16.git] / 16 / adplug / adplug / src / mad.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   mad.cpp - MAD loader by Riven the Mage <riven@ok.ru>
20 */
21
22 #include <cstring>
23 #include "mad.h"
24
25 /* -------- Public Methods -------------------------------- */
26
27 CPlayer *CmadLoader::factory(Copl *newopl)
28 {
29   return new CmadLoader(newopl);
30 }
31
32 bool CmadLoader::load(const std::string &filename, const CFileProvider &fp)
33 {
34   binistream *f = fp.open(filename); if(!f) return false;
35   const unsigned char conv_inst[10] = { 2,1,10,9,4,3,6,5,8,7 };
36   unsigned int i, j, k, t = 0;
37
38   // 'MAD+' - signed ?
39   char id[4]; f->readString(id, 4);
40   if (strncmp(id,"MAD+",4)) { fp.close(f); return false; }
41
42   // load instruments
43   for(i = 0; i < 9; i++) {
44     f->readString(instruments[i].name, 8);
45     for(j = 0; j < 12; j++) instruments[i].data[j] = f->readInt(1);
46   }
47
48   f->ignore(1);
49
50   // data for Protracker
51   length = f->readInt(1); nop = f->readInt(1); timer = f->readInt(1);
52
53   // init CmodPlayer
54   realloc_instruments(9);
55   realloc_order(length);
56   realloc_patterns(nop,32,9);
57   init_trackord();
58
59   // load tracks
60   for(i = 0; i < nop; i++)
61     for(k = 0; k < 32; k++)
62       for(j = 0; j < 9; j++) {
63         t = i * 9 + j;
64
65         // read event
66         unsigned char event = f->readInt(1);
67
68         // convert event
69         if (event < 0x61)
70           tracks[t][k].note = event;
71         if (event == 0xFF) // 0xFF: Release note
72           tracks[t][k].command = 8;
73         if (event == 0xFE) // 0xFE: Pattern Break
74           tracks[t][k].command = 13;
75       }
76
77   // load order
78   for(i = 0; i < length; i++) order[i] = f->readInt(1) - 1;
79
80   fp.close(f);
81
82   // convert instruments
83   for(i = 0; i < 9; i++)
84     for(j = 0; j < 10; j++)
85       inst[i].data[conv_inst[j]] = instruments[i].data[j];
86
87   // data for Protracker
88   restartpos = 0;
89   initspeed = 1;
90
91   rewind(0);
92   return true;
93 }
94
95 void CmadLoader::rewind(int subsong)
96 {
97         CmodPlayer::rewind(subsong);
98
99         // default instruments
100         for (int i=0;i<9;i++)
101         {
102                 channel[i].inst = i;
103
104                 channel[i].vol1 = 63 - (inst[i].data[10] & 63);
105                 channel[i].vol2 = 63 - (inst[i].data[9] & 63);
106         }
107 }
108
109 float CmadLoader::getrefresh()
110 {
111         return (float)timer;
112 }
113
114 std::string CmadLoader::gettype()
115 {
116         return std::string("Mlat Adlib Tracker");
117 }
118
119 std::string CmadLoader::getinstrument(unsigned int n)
120 {
121         return std::string(instruments[n].name,8);
122 }
123
124 unsigned int CmadLoader::getinstruments()
125 {
126         return 9;
127 }