1 /*
2  * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3  * Copyright (C) 1999 - 2005 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  * raw.c - RAW Player by Simon Peter <dn.tlp@gmx.net>
20  */
21 
22 #include <string.h>
23 #include "raw.h"
24 
25 /*** public methods *************************************/
26 
factory(Copl * newopl)27 CPlayer *CrawPlayer::factory(Copl *newopl)
28 {
29   return new CrawPlayer(newopl);
30 }
31 
load(const char * filename,const CFileProvider & fp)32 bool CrawPlayer::load(const char *filename, const CFileProvider &fp)
33 {
34   binistream *f = fp.open(filename); if(!f) return false;
35   char id[8];
36   unsigned long i;
37 
38   // file validation section
39   f->readString(id, 8);
40   if(strncmp(id,"RAWADATA",8)) { fp.close (f); return false; }
41 
42   // load section
43   clock = f->readInt(2);	// clock speed
44   length = (fp.filesize(f) - 10) / 2;
45   data = new Tdata [length];
46   for(i = 0; i < length; i++) {
47     data[i].param = f->readInt(1);
48     data[i].command = f->readInt(1);
49   }
50 
51   fp.close(f);
52   rewind(0);
53   return true;
54 }
55 
update()56 bool CrawPlayer::update()
57 {
58   bool	setspeed;
59 
60   if(pos >= length) return false;
61 
62   if(del) {
63     del--;
64     return !songend;
65   }
66 
67   do {
68     setspeed = false;
69     switch(data[pos].command) {
70     case 0: del = data[pos].param - 1; break;
71     case 2:
72       if(!data[pos].param) {
73 	pos++;
74 	speed = data[pos].param + (data[pos].command << 8);
75 	setspeed = true;
76       } else
77 	opl->setchip(data[pos].param - 1);
78       break;
79     case 0xff:
80       if(data[pos].param == 0xff) {
81 	rewind(0);		// auto-rewind song
82 	songend = true;
83 	return !songend;
84       }
85       break;
86     default:
87       opl->write(data[pos].command,data[pos].param);
88       break;
89     }
90   } while(data[pos++].command || setspeed);
91 
92   return !songend;
93 }
94 
rewind(int subsong)95 void CrawPlayer::rewind(int subsong)
96 {
97   pos = del = 0; speed = clock; songend = false;
98   opl->init(); opl->write(1, 32);	// go to 9 channel mode
99 }
100 
getrefresh()101 float CrawPlayer::getrefresh()
102 {
103   return 1193180.0 / (speed ? speed : 0xffff);	// timer oscillator speed / wait register = clock frequency
104 }
105