-
Notifications
You must be signed in to change notification settings - Fork 1
/
eupplayer_townsInternal.cpp
237 lines (201 loc) · 5 KB
/
eupplayer_townsInternal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// $Id: eupplayer_townsInternal.cc,v 1.2 1996/08/20 16:33:38 tjh Exp $
/* Artistic Style
*
* ./astyle --style=stroustrup --convert-tabs --add-braces eupplayer_townsInternal.cpp
*/
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#if defined ( __MINGW32__ )
#include <_bsd_types.h>
#endif
#include "eupplayer_townsInternal.hpp"
#ifdef DEBUG
#include <stdarg.h>
static void dbprintf(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fflush(stderr);
}
#define DB1(a) dbprintf(a)
#define DB2(a,b) dbprintf(a,b)
#define DB3(a,b,c) dbprintf(a,b,c)
#else
#define DB1(a)
#define DB2(a,b)
#define DB3(a,b,c)
#endif
/* EUP_TownsInternal_MonophonicAudioDevice */
class EUP_TownsInternal_MonophonicAudioDevice {
public:
EUP_TownsInternal_MonophonicAudioDevice() {}
virtual ~EUP_TownsInternal_MonophonicAudioDevice() {}
virtual void setInstrumentParameter(u_char const *instrument) = 0;
virtual void nextTick() = 0;
virtual void note(int n, int onVelo, int offVelo, int gateTime) = 0;
virtual void pitchBend(int value) = 0;
};
/* TownsFm */
class TownsFm : public EUP_TownsInternal_MonophonicAudioDevice {
int _gateTime;
int _channel;
int _regBank;
FILE *_port;
public:
TownsFm(int channel);
~TownsFm();
void setInstrumentParameter(u_char const *instrument);
void nextTick();
void note(int n, int onVelo, int offVelo, int gateTime);
void pitchBend(int value);
void writeReg(int addr, int value);
};
TownsFm::TownsFm(int channel)
{
_port = fopen("/dev/port", "rbw");
if (_port == NULL) {
perror("TownsFm: /dev/port");
exit(1);
}
_gateTime = 0;
_channel = channel % 3;
_regBank = channel / 3;
this->writeReg(0x21, 0x00);
this->writeReg(0x2c, 0x00);
this->writeReg(0x27, 0x00);
}
TownsFm::~TownsFm()
{
fclose(_port);
}
void TownsFm::setInstrumentParameter(u_char const *instrument)
{
instrument += 8;
int addr = 0x30+_channel;
for (; addr < 0x90; addr += 4) {
this->writeReg(addr, *instrument++);
}
addr += 0x20;
for (; addr < 0xb8; addr += 4) {
this->writeReg(addr, *instrument++);
}
}
void TownsFm::nextTick()
{
if (_gateTime <= 0) {
return;
}
if (--_gateTime > 0) {
return;
}
this->writeReg(0x28, 0x00 | _channel);
}
void TownsFm::note(int n, int onVelo, int offVelo, int gateTime)
{
//DB3("note: v=%d, gt=%d\n", onVelo, gateTime);
_gateTime = gateTime;
this->writeReg(0xa4 + _channel, 0x24);
this->writeReg(0xa0 + _channel, 0x81);
this->writeReg(0x28, 0xf0 | _channel);
}
void TownsFm::pitchBend(int value)
{
}
void TownsFm::writeReg(int addr, int value)
{
DB3("writereg: addr=%02x, val=%02x\n", addr, value);
for (;;) {
fseek(_port, 0x04d8, SEEK_SET);
if (!(getc(_port) & 0x80)) {
break;
}
DB1("1\n");
}
fseek(_port, 0x04d8 + 4*_regBank, SEEK_SET);
putc(addr, _port);
fflush(_port);
for (;;) {
fseek(_port, 0x04d8, SEEK_SET);
if (!(getc(_port) & 0x80)) {
break;
}
DB1("2\n");
}
fseek(_port, 0x04da + 4*_regBank, SEEK_SET);
putc(value, _port);
fflush(_port);
}
/* TownsPcm */
class TownsPcm : public EUP_TownsInternal_MonophonicAudioDevice {
public:
TownsPcm() {}
~TownsPcm() {}
void setInstrumentParameter(u_char const *instrument);
void nextTick();
void note(int n, int onVelo, int offVelo, int gateTime);
void pitchBend(int value);
};
void TownsPcm::setInstrumentParameter(u_char const *instrument)
{
}
void TownsPcm::nextTick()
{
}
void TownsPcm::note(int n, int onVelo, int offVelo, int gateTime)
{
}
void TownsPcm::pitchBend(int value)
{
}
/* EUP_TownsInternal */
EUP_TownsInternal::EUP_TownsInternal()
{
{
int n = 0;
for (; n < _maxFmChannelNum; n++) {
_dev[n] = new TownsFm(n);
}
for (; n < _maxChannelNum; n++) {
_dev[n] = new TownsPcm;
}
}
for (int n = 0; n < _maxFmInstrumentNum; n++) {
_fmInstrument[n] = _fmInstrumentData + 8 + 48*n;
}
}
EUP_TownsInternal::~EUP_TownsInternal()
{
for (int n = 0; n < _maxChannelNum; n++) {
delete _dev[n];
}
}
void EUP_TownsInternal::setFmInstrumentParameter(int num, u_char const *instrument)
{
if (0 <= num && num < _maxFmInstrumentNum) {
memcpy(_fmInstrument[num], instrument, 48);
}
}
void EUP_TownsInternal::nextTick()
{
for (int n = 0; n < _maxChannelNum; n++) {
_dev[n]->nextTick();
}
}
void EUP_TownsInternal::note(int track, int n,
int onVelo, int offVelo, int gateTime)
{
_dev[track]->note(n, onVelo, offVelo, gateTime);
}
void EUP_TownsInternal::pitchBend(int track, int value)
{
}
void EUP_TownsInternal::programChange(int track, int num)
{
/* track -> channel の変換をしなきゃ */
int channel = track;
_dev[channel]->setInstrumentParameter(_fmInstrument[num]);
}