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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/* OPL implementation for hardware OPL using ALSA Direct FM API.
*
* Caveats and limitations:
* - Pretends to be a softsynth (emitting silence).
* - Dual OPL2 mode requires OPL3 hardware.
* - Every register write leads to a series of register writes on the hardware,
* due to the lack of direct register access in the ALSA Direct FM API.
* - No timers
*/
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include "common/scummsys.h"
#include "common/debug.h"
#include "common/str.h"
#include "audio/fmopl.h"
#include <sys/ioctl.h>
#include <alsa/asoundlib.h>
#include <sound/asound_fm.h>
namespace OPL {
namespace ALSA {
class OPL : public ::OPL::RealOPL {
private:
enum {
kOpl2Voices = 9,
kVoices = 18,
kOpl2Operators = 18,
kOperators = 36
};
Config::OplType _type;
int _iface;
snd_hwdep_t *_opl;
snd_dm_fm_voice _oper[kOperators];
snd_dm_fm_note _voice[kVoices];
snd_dm_fm_params _params;
int index[2];
static const int voiceToOper0[kVoices];
static const int regOffsetToOper[0x20];
void writeOplReg(int c, int r, int v);
void clear();
public:
OPL(Config::OplType type);
~OPL();
bool init();
void reset();
void write(int a, int v);
byte read(int a);
void writeReg(int r, int v);
};
const int OPL::voiceToOper0[OPL::kVoices] =
{ 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32 };
const int OPL::regOffsetToOper[0x20] =
{ 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
OPL::OPL(Config::OplType type) : _type(type), _opl(nullptr), _iface(0) {
}
OPL::~OPL() {
stop();
if (_opl) {
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_RESET, nullptr);
snd_hwdep_close(_opl);
}
}
void OPL::clear() {
index[0] = index[1] = 0;
memset(_oper, 0, sizeof(_oper));
memset(_voice, 0, sizeof(_voice));
memset(&_params, 0, sizeof(_params));
for (int i = 0; i < kOperators; ++i) {
_oper[i].op = (i / 3) % 2;
_oper[i].voice = (i / 6) * 3 + (i % 3);
}
for (int i = 0; i < kVoices; ++i)
_voice[i].voice = i;
// For OPL3 hardware we need to set up the panning in OPL2 modes
if (_iface == SND_HWDEP_IFACE_OPL3) {
if (_type == Config::kDualOpl2) {
for (int i = 0; i < kOpl2Operators; ++i)
_oper[i].left = 1; // FIXME below
for (int i = kOpl2Operators; i < kOperators; ++i)
_oper[i].right = 1;
} else if (_type == Config::kOpl2) {
for (int i = 0; i < kOpl2Operators; ++i) {
_oper[i].left = 1;
_oper[i].right = 1;
}
}
}
}
bool OPL::init() {
clear();
int card = -1;
snd_ctl_t *ctl;
snd_hwdep_info_t *info;
snd_hwdep_info_alloca(&info);
int iface = SND_HWDEP_IFACE_OPL3;
if (_type == Config::kOpl2)
iface = SND_HWDEP_IFACE_OPL2;
// Look for OPL hwdep interface
while (!snd_card_next(&card) && card >= 0) {
int dev = -1;
Common::String name = Common::String::format("hw:%d", card);
if (snd_ctl_open(&ctl, name.c_str(), 0) < 0)
continue;
while (!snd_ctl_hwdep_next_device(ctl, &dev) && dev >= 0) {
name = Common::String::format("hw:%d,%d", card, dev);
if (snd_hwdep_open(&_opl, name.c_str(), SND_HWDEP_OPEN_WRITE) < 0)
continue;
if (!snd_hwdep_info(_opl, info)) {
int found = snd_hwdep_info_get_iface(info);
// OPL3 can be used for (Dual) OPL2 mode
if (found == iface || found == SND_HWDEP_IFACE_OPL3) {
snd_ctl_close(ctl);
_iface = found;
reset();
return true;
}
}
// Wrong interface, try next device
snd_hwdep_close(_opl);
_opl = nullptr;
}
snd_ctl_close(ctl);
}
return false;
}
void OPL::reset() {
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_RESET, nullptr);
if (_iface == SND_HWDEP_IFACE_OPL3)
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_MODE, (void *)SNDRV_DM_FM_MODE_OPL3);
clear();
// Sync up with the hardware
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_PARAMS, (void *)&_params);
for (uint i = 0; i < (_iface == SND_HWDEP_IFACE_OPL3 ? kVoices : kOpl2Voices); ++i)
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_PLAY_NOTE, (void *)&_voice[i]);
for (uint i = 0; i < (_iface == SND_HWDEP_IFACE_OPL3 ? kOperators : kOpl2Operators); ++i)
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_VOICE, (void *)&_oper[i]);
}
void OPL::write(int port, int val) {
val &= 0xff;
int chip = (port & 2) >> 1;
if (port & 1) {
switch(_type) {
case Config::kOpl2:
writeOplReg(0, index[0], val);
break;
case Config::kDualOpl2:
if (port & 8) {
writeOplReg(0, index[0], val);
writeOplReg(1, index[1], val);
} else
writeOplReg(chip, index[chip], val);
break;
case Config::kOpl3:
writeOplReg(chip, index[chip], val);
}
} else {
switch(_type) {
case Config::kOpl2:
index[0] = val;
break;
case Config::kDualOpl2:
if (port & 8) {
index[0] = val;
index[1] = val;
} else
index[chip] = val;
break;
case Config::kOpl3:
index[chip] = val;
}
}
}
byte OPL::read(int port) {
return 0;
}
void OPL::writeReg(int r, int v) {
switch (_type) {
case Config::kOpl2:
writeOplReg(0, r, v);
break;
case Config::kDualOpl2:
writeOplReg(0, r, v);
writeOplReg(1, r, v);
break;
case Config::kOpl3:
writeOplReg(r >= 0x100, r & 0xff, v);
}
}
void OPL::writeOplReg(int c, int r, int v) {
if (r == 0x04 && c == 1 && _type == Config::kOpl3) {
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_CONNECTION, reinterpret_cast<void *>(v & 0x3f));
} else if (r == 0x08 && c == 0) {
_params.kbd_split = (v >> 6) & 0x1;
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_PARAMS, (void *)&_params);
} else if (r == 0xbd && c == 0) {
_params.hihat = v & 0x1;
_params.cymbal = (v >> 1) & 0x1;
_params.tomtom = (v >> 2) & 0x1;
_params.snare = (v >> 3) & 0x1;
_params.bass = (v >> 4) & 0x1;
_params.rhythm = (v >> 5) & 0x1;
_params.vib_depth = (v >> 6) & 0x1;
_params.am_depth = (v >> 7) & 0x1;
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_PARAMS, (void *)&_params);
} else if (r < 0xa0 || r >= 0xe0) {
// Operator
int idx = regOffsetToOper[r & 0x1f];
if (idx == -1)
return;
if (c == 1)
idx += kOpl2Operators;
switch (r & 0xf0) {
case 0x20:
case 0x30:
_oper[idx].harmonic = v & 0xf;
_oper[idx].kbd_scale = (v >> 4) & 0x1;
_oper[idx].do_sustain = (v >> 5) & 0x1;
_oper[idx].vibrato = (v >> 6) & 0x1;
_oper[idx].am = (v >> 7) & 0x1;
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_VOICE, (void *)&_oper[idx]);
break;
case 0x40:
case 0x50:
_oper[idx].volume = ~v & 0x3f;
_oper[idx].scale_level = (v >> 6) & 0x3;
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_VOICE, (void *)&_oper[idx]);
break;
case 0x60:
case 0x70:
_oper[idx].decay = v & 0xf;
_oper[idx].attack = (v >> 4) & 0xf;
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_VOICE, (void *)&_oper[idx]);
break;
case 0x80:
case 0x90:
_oper[idx].release = v & 0xf;
_oper[idx].sustain = (v >> 4) & 0xf;
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_VOICE, (void *)&_oper[idx]);
break;
case 0xe0:
case 0xf0:
_oper[idx].waveform = v & (_type == Config::kOpl3 ? 0x7 : 0x3);
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_VOICE, (void *)&_oper[idx]);
}
} else {
// Voice
int idx = r & 0xf;
if (idx >= kOpl2Voices)
return;
if (c == 1)
idx += kOpl2Voices;
int opIdx = voiceToOper0[idx];
switch (r & 0xf0) {
case 0xa0:
_voice[idx].fnum = (_voice[idx].fnum & 0x300) | (v & 0xff);
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_PLAY_NOTE, (void *)&_voice[idx]);
break;
case 0xb0:
_voice[idx].fnum = ((v << 8) & 0x300) | (_voice[idx].fnum & 0xff);
_voice[idx].octave = (v >> 2) & 0x7;
_voice[idx].key_on = (v >> 5) & 0x1;
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_PLAY_NOTE, (void *)&_voice[idx]);
break;
case 0xc0:
_oper[opIdx].connection = _oper[opIdx + 3].connection = v & 0x1;
_oper[opIdx].feedback = _oper[opIdx + 3].feedback = (v >> 1) & 0x7;
if (_type == Config::kOpl3) {
_oper[opIdx].left = _oper[opIdx + 3].left = (v >> 4) & 0x1;
_oper[opIdx].right = _oper[opIdx + 3].right = (v >> 5) & 0x1;
}
snd_hwdep_ioctl(_opl, SNDRV_DM_FM_IOCTL_SET_VOICE, (void *)&_oper[opIdx]);
}
}
}
OPL *create(Config::OplType type) {
return new OPL(type);
}
} // End of namespace ALSA
} // End of namespace OPL
|