]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/adplug/src/xsm.cpp
wwww~
[16.git] / 16 / adplug / adplug / src / xsm.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  * xsm.cpp - eXtra Simple Music Player, by Simon Peter <dn.tlp@gmx.net>
20  */
21
22 #include <string.h>
23
24 #include "xsm.h"
25
26 CxsmPlayer::CxsmPlayer(Copl *newopl)
27   : CPlayer(newopl), music(0)
28 {
29 }
30
31 CxsmPlayer::~CxsmPlayer()
32 {
33   if(music) delete [] music;
34 }
35
36 bool CxsmPlayer::load(const std::string &filename, const CFileProvider &fp)
37 {
38   binistream *f = fp.open(filename); if(!f) return false;
39   char                  id[6];
40   int                   i, j;
41
42   // check if header matches
43   f->readString(id, 6); songlen = f->readInt(2);
44   if(strncmp(id, "ofTAZ!", 6) || songlen > 3200) { fp.close(f); return false; }
45
46   // read and set instruments
47   for(i = 0; i < 9; i++) {
48     opl->write(0x20 + op_table[i], f->readInt(1));
49     opl->write(0x23 + op_table[i], f->readInt(1));
50     opl->write(0x40 + op_table[i], f->readInt(1));
51     opl->write(0x43 + op_table[i], f->readInt(1));
52     opl->write(0x60 + op_table[i], f->readInt(1));
53     opl->write(0x63 + op_table[i], f->readInt(1));
54     opl->write(0x80 + op_table[i], f->readInt(1));
55     opl->write(0x83 + op_table[i], f->readInt(1));
56     opl->write(0xe0 + op_table[i], f->readInt(1));
57     opl->write(0xe3 + op_table[i], f->readInt(1));
58     opl->write(0xc0 + op_table[i], f->readInt(1));
59     f->ignore(5);
60   }
61
62   // read song data
63   music = new char [songlen * 9];
64   for(i = 0; i < 9; i++)
65     for(j = 0; j < songlen; j++)
66       music[j * 9 + i] = f->readInt(1);
67
68   // success
69   fp.close(f);
70   rewind(0);
71   return true;
72 }
73
74 bool CxsmPlayer::update()
75 {
76   int c;
77
78   if(notenum >= songlen) {
79     songend = true;
80     notenum = last = 0;
81   }
82
83   for(c = 0; c < 9; c++)
84     if(music[notenum * 9 + c] != music[last * 9 + c])
85       opl->write(0xb0 + c, 0);
86
87   for(c = 0; c < 9; c++) {
88     if(music[notenum * 9 + c])
89       play_note(c, music[notenum * 9 + c] % 12, music[notenum * 9 + c] / 12);
90     else
91       play_note(c, 0, 0);
92   }
93
94   last = notenum;
95   notenum++;
96   return !songend;
97 }
98
99 void CxsmPlayer::rewind(int subsong)
100 {
101   notenum = last = 0;
102   songend = false;
103 }
104
105 float CxsmPlayer::getrefresh()
106 {
107   return 5.0f;
108 }
109
110 void CxsmPlayer::play_note(int c, int note, int octv)
111 {
112   int freq = note_table[note];
113
114   if(!note && !octv) freq = 0;
115   opl->write(0xa0 + c, freq & 0xff);
116   opl->write(0xb0 + c, (freq / 0xff) | 32 | (octv * 4));
117 }