2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2008 Simon Peter <dn.tlp@gmx.net>, et al.
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.
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.
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
19 * imf.cpp - IMF Player by Simon Peter <dn.tlp@gmx.net>
22 * There seem to be 2 different flavors of IMF formats out there. One version
23 * contains just the raw IMF music data. In this case, the first word of the
24 * file is always 0 (because the music data starts this way). This is already
25 * the music data! So read in the entire file and play it.
27 * If this word is greater than 0, it specifies the size of the following
28 * song data in bytes. In this case, the file has a footer that contains
29 * arbitrary infos about it. Mostly, this is plain ASCII text with some words
30 * of the author. Read and play the specified amount of song data and display
31 * the remaining data as ASCII text.
34 * This player handles the two above mentioned formats, as well as a third
35 * type, invented by Martin Fernandez <mfernan@cnba.uba.ar>, that's got a
36 * proper header to add title/game name information. After the header starts
37 * the normal IMF file in one of the two above mentioned formats.
39 * This player also handles a special footer format by Adam Nielsen,
40 * which has defined fields of information about the song, the author
49 /*** public methods *************************************/
51 CPlayer *CimfPlayer::factory(Copl *newopl)
53 return new CimfPlayer(newopl);
56 bool CimfPlayer::load(const std::string &filename, const CFileProvider &fp)
58 binistream *f = fp.open(filename); if(!f) return false;
59 unsigned long fsize, flsize, mfsize = 0;
62 // file validation section
67 f->readString(header, 5);
68 version = f->readInt(1);
70 if(strncmp(header, "ADLIB", 5) || version != 1) {
71 if(!fp.extension(filename, ".imf") && !fp.extension(filename, ".wlf")) {
72 // It's no IMF file at all
76 f->seek(0); // It's a normal IMF file
78 // It's a IMF file with header
79 track_name = f->readString('\0');
80 game_name = f->readString('\0');
82 mfsize = f->pos() + 2;
88 fsize = f->readInt(4);
90 fsize = f->readInt(2);
91 flsize = fp.filesize(f);
92 if(!fsize) { // footerless file (raw music data)
94 f->seek(-4, binio::Add);
96 f->seek(-2, binio::Add);
97 size = (flsize - mfsize) / 4;
98 } else // file has got a footer
101 data = new Sdata[size];
102 for(i = 0; i < size; i++) {
103 data[i].reg = f->readInt(1); data[i].val = f->readInt(1);
104 data[i].time = f->readInt(2);
107 // read footer, if any
108 if(fsize && (fsize < flsize - 2 - mfsize)) {
109 if(f->readInt(1) == 0x1a) {
110 // Adam Nielsen's footer format
111 track_name = f->readString();
112 author_name = f->readString();
113 remarks = f->readString();
116 unsigned long footerlen = flsize - fsize - 2 - mfsize;
118 footer = new char[footerlen + 1];
119 f->readString(footer, footerlen);
120 footer[footerlen] = '\0'; // Make ASCIIZ string
124 rate = getrate(filename, fp, f);
130 bool CimfPlayer::update()
133 opl->write(data[pos].reg,data[pos].val);
134 del = data[pos].time;
136 } while(!del && pos < size);
142 else timer = rate / (float)del;
147 void CimfPlayer::rewind(int subsong)
149 pos = 0; del = 0; timer = rate; songend = false;
150 opl->init(); opl->write(1,32); // go to OPL2 mode
153 std::string CimfPlayer::gettitle()
159 if(!track_name.empty() && !game_name.empty())
167 std::string CimfPlayer::getdesc()
172 desc = std::string(footer);
174 if(!remarks.empty() && footer)
182 /*** private methods *************************************/
184 float CimfPlayer::getrate(const std::string &filename, const CFileProvider &fp, binistream *f)
186 if(db) { // Database available
187 f->seek(0, binio::Set);
188 CClockRecord *record = (CClockRecord *)db->search(CAdPlugDatabase::CKey(*f));
189 if (record && record->type == CAdPlugDatabase::CRecord::ClockSpeed)
190 return record->clock;
193 // Otherwise the database is either unavailable, or there's no entry for this file
194 if (fp.extension(filename, ".imf")) return 560.0f;
195 if (fp.extension(filename, ".wlf")) return 700.0f;
196 return 700.0f; // default speed for unknown files that aren't .IMF or .WLF