| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program 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 |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#define _USE_MATH_DEFINES |
| 21 |
#include "pcspkr.h" |
| 22 |
#include "../engine.h" |
| 23 |
#include "../../ta-log.h" |
| 24 |
|
| 25 |
#include <math.h> |
| 26 |
|
| 27 |
#ifdef __linux__ |
| 28 |
#include <sys/ioctl.h> |
| 29 |
#include <sys/select.h> |
| 30 |
#include <fcntl.h> |
| 31 |
#include <unistd.h> |
| 32 |
#ifdef HAVE_LINUX_INPUT |
| 33 |
#include <linux/input.h> |
| 34 |
#endif |
| 35 |
#ifdef HAVE_LINUX_KD |
| 36 |
#include <linux/kd.h> |
| 37 |
#endif |
| 38 |
#include <time.h> |
| 39 |
#ifdef HAVE_SYS_IO |
| 40 |
#include <sys/io.h> |
| 41 |
#endif |
| 42 |
#endif |
| 43 |
|
| 44 |
#define PCSPKR_DIVIDER 4 |
| 45 |
#define CHIP_DIVIDER 1 |
| 46 |
|
| 47 |
const char* regCheatSheetPCSpeaker[]={ |
| 48 |
"Period", "0", |
| 49 |
NULL |
| 50 |
}; |
| 51 |
|
| 52 |
void _pcSpeakerThread(void* inst) { |
| 53 |
((DivPlatformPCSpeaker*)inst)->pcSpeakerThread(); |
| 54 |
} |
| 55 |
|
| 56 |
void DivPlatformPCSpeaker::pcSpeakerThread() { |
| 57 |
std::unique_lock<std::mutex> unique(realOutSelfLock); |
| 58 |
RealQueueVal r(0,0,0); |
| 59 |
logD("starting PC speaker out thread"); |
| 60 |
while (!realOutQuit) { |
| 61 |
realQueueLock.lock(); |
| 62 |
if (realQueue.empty()) { |
| 63 |
realQueueLock.unlock(); |
| 64 |
realOutCond.wait(unique); |
| 65 |
continue; |
| 66 |
} else { |
| 67 |
r=realQueue.front(); |
| 68 |
realQueue.pop(); |
| 69 |
} |
| 70 |
realQueueLock.unlock(); |
| 71 |
#ifdef __linux__ |
| 72 |
static struct timespec ts, tSleep, rSleep; |
| 73 |
if (clock_gettime(CLOCK_MONOTONIC,&ts)<0) { |
| 74 |
logW("could not get time!"); |
| 75 |
tSleep.tv_sec=0; |
| 76 |
tSleep.tv_nsec=0; |
| 77 |
} else { |
| 78 |
tSleep.tv_sec=r.tv_sec-ts.tv_sec; |
| 79 |
tSleep.tv_nsec=r.tv_nsec-ts.tv_nsec; |
| 80 |
if (tSleep.tv_nsec<0) { |
| 81 |
tSleep.tv_sec--; |
| 82 |
tSleep.tv_nsec+=1000000000; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if (tSleep.tv_nsec>0 || tSleep.tv_sec>0) { |
| 87 |
nanosleep(&tSleep,&rSleep); |
| 88 |
} |
| 89 |
if (beepFD>=0) { |
| 90 |
switch (realOutMethod) { |
| 91 |
#ifdef HAVE_LINUX_INPUT |
| 92 |
case 0: { // evdev |
| 93 |
static struct input_event ie; |
| 94 |
ie.time.tv_sec=r.tv_sec; |
| 95 |
ie.time.tv_usec=r.tv_nsec/1000; |
| 96 |
ie.type=EV_SND; |
| 97 |
ie.code=SND_TONE; |
| 98 |
if (r.val>0) { |
| 99 |
ie.value=chipClock/r.val; |
| 100 |
} else { |
| 101 |
ie.value=0; |
| 102 |
} |
| 103 |
if (write(beepFD,&ie,sizeof(struct input_event))<0) { |
| 104 |
logW("error while writing frequency! %s",strerror(errno)); |
| 105 |
} else { |
| 106 |
//logV("writing freq: %d",r.val); |
| 107 |
} |
| 108 |
break; |
| 109 |
} |
| 110 |
#endif |
| 111 |
#ifdef HAVE_LINUX_KD |
| 112 |
case 1: // KIOCSOUND (on tty) |
| 113 |
if (ioctl(beepFD,KIOCSOUND,r.val)<0) { |
| 114 |
logW("ioctl error! %s",strerror(errno)); |
| 115 |
} |
| 116 |
break; |
| 117 |
#endif |
| 118 |
case 2: { // /dev/port |
| 119 |
unsigned char bOut; |
| 120 |
bOut=0; |
| 121 |
if (r.val==0) { |
| 122 |
lseek(beepFD,0x61,SEEK_SET); |
| 123 |
if (read(beepFD,&bOut,1)<1) { |
| 124 |
logW("read from 0x61: %s",strerror(errno)); |
| 125 |
} |
| 126 |
bOut&=(~3); |
| 127 |
lseek(beepFD,0x61,SEEK_SET); |
| 128 |
if (write(beepFD,&bOut,1)<1) { |
| 129 |
logW("write to 0x61: %s",strerror(errno)); |
| 130 |
} |
| 131 |
} else { |
| 132 |
lseek(beepFD,0x43,SEEK_SET); |
| 133 |
bOut=0xb6; |
| 134 |
if (write(beepFD,&bOut,1)<1) { |
| 135 |
logW("write to 0x43: %s",strerror(errno)); |
| 136 |
} |
| 137 |
lseek(beepFD,0x42,SEEK_SET); |
| 138 |
bOut=r.val&0xff; |
| 139 |
if (write(beepFD,&bOut,1)<1) { |
| 140 |
logW("write to 0x42: %s",strerror(errno)); |
| 141 |
} |
| 142 |
lseek(beepFD,0x42,SEEK_SET); |
| 143 |
bOut=r.val>>8; |
| 144 |
if (write(beepFD,&bOut,1)<1) { |
| 145 |
logW("write to 0x42: %s",strerror(errno)); |
| 146 |
} |
| 147 |
lseek(beepFD,0x61,SEEK_SET); |
| 148 |
if (read(beepFD,&bOut,1)<1) { |
| 149 |
logW("read from 0x61: %s",strerror(errno)); |
| 150 |
} |
| 151 |
bOut|=3; |
| 152 |
lseek(beepFD,0x61,SEEK_SET); |
| 153 |
if (write(beepFD,&bOut,1)<1) { |
| 154 |
logW("write to 0x61: %s",strerror(errno)); |
| 155 |
} |
| 156 |
} |
| 157 |
break; |
| 158 |
} |
| 159 |
#ifdef HAVE_LINUX_KD |
| 160 |
case 3: // KIOCSOUND (on stdout) |
| 161 |
if (ioctl(beepFD,KIOCSOUND,r.val)<0) { |
| 162 |
logW("ioctl error! %s",strerror(errno)); |
| 163 |
} |
| 164 |
break; |
| 165 |
#endif |
| 166 |
#ifdef HAVE_SYS_IO |
| 167 |
case 4: // outb() |
| 168 |
if (r.val==0) { |
| 169 |
outb(inb(0x61)&(~3),0x61); |
| 170 |
realOutEnabled=false; |
| 171 |
} else { |
| 172 |
outb(0xb6,0x43); |
| 173 |
outb(r.val&0xff,0x42); |
| 174 |
outb(r.val>>8,0x42); |
| 175 |
if (!realOutEnabled) { |
| 176 |
outb(inb(0x61)|3,0x61); |
| 177 |
realOutEnabled=true; |
| 178 |
} |
| 179 |
} |
| 180 |
break; |
| 181 |
#endif |
| 182 |
} |
| 183 |
} else { |
| 184 |
//logV("not writing because fd is less than 0"); |
| 185 |
} |
| 186 |
#endif |
| 187 |
} |
| 188 |
logD("stopping PC speaker out thread"); |
| 189 |
} |
| 190 |
|
| 191 |
const char** DivPlatformPCSpeaker::getRegisterSheet() { |
| 192 |
return regCheatSheetPCSpeaker; |
| 193 |
} |
| 194 |
|
| 195 |
void DivPlatformPCSpeaker::acquire_unfilt(blip_buffer_t** bb, size_t len) { |
| 196 |
int out=0; |
| 197 |
int freq1=freq+1; |
| 198 |
int timeToNextToggle=0; |
| 199 |
oscBuf->begin(len); |
| 200 |
if (on) { |
| 201 |
// just in case |
| 202 |
if (pos>freq1) { |
| 203 |
pos=freq1; |
| 204 |
} |
| 205 |
if (pos>(freq>>1)) { |
| 206 |
posToggle=true; |
| 207 |
timeToNextToggle=pos-(freq>>1); |
| 208 |
} else { |
| 209 |
posToggle=false; |
| 210 |
timeToNextToggle=pos; |
| 211 |
} |
| 212 |
out=(posToggle && !isMuted[0])?32767:0; |
| 213 |
blip_add_delta(bb[0],0,out-oldOut); |
| 214 |
oscBuf->putSample(0,out); |
| 215 |
oldOut=out; |
| 216 |
if (freq>=1) { |
| 217 |
size_t boff=0; |
| 218 |
size_t i=len; |
| 219 |
while (true) { |
| 220 |
if ((int)i<timeToNextToggle) { |
| 221 |
pos-=i; |
| 222 |
break; |
| 223 |
} |
| 224 |
i-=timeToNextToggle; |
| 225 |
boff+=timeToNextToggle; |
| 226 |
pos-=timeToNextToggle; |
| 227 |
if (pos<=0) { |
| 228 |
pos=freq1; |
| 229 |
} |
| 230 |
if (pos>(freq>>1)) { |
| 231 |
posToggle=true; |
| 232 |
timeToNextToggle=pos-(freq>>1); |
| 233 |
} else { |
| 234 |
posToggle=false; |
| 235 |
timeToNextToggle=pos; |
| 236 |
} |
| 237 |
out=(posToggle && !isMuted[0])?32767:0; |
| 238 |
blip_add_delta(bb[0],boff,out-oldOut); |
| 239 |
oscBuf->putSample(boff,out); |
| 240 |
oldOut=out; |
| 241 |
} |
| 242 |
} |
| 243 |
} else { |
| 244 |
out=0; |
| 245 |
blip_add_delta(bb[0],0,out-oldOut); |
| 246 |
oscBuf->putSample(0,out); |
| 247 |
oldOut=out; |
| 248 |
} |
| 249 |
oscBuf->end(len); |
| 250 |
} |
| 251 |
|
| 252 |
void DivPlatformPCSpeaker::acquire_cone(short** buf, size_t len) { |
| 253 |
oscBuf->begin(len); |
| 254 |
for (size_t i=0; i<len; i++) { |
| 255 |
if (on) { |
| 256 |
pos-=PCSPKR_DIVIDER; |
| 257 |
if (pos>freq) pos=freq; |
| 258 |
while (pos<0) { |
| 259 |
if (freq<1) { |
| 260 |
pos=1; |
| 261 |
} else { |
| 262 |
pos+=freq; |
| 263 |
} |
| 264 |
} |
| 265 |
float next=(pos>((freq+16)>>1) && !isMuted[0])?1:0; |
| 266 |
low+=cut*band; |
| 267 |
band+=cut*(next-low-band); |
| 268 |
float out=(low+band)*0.75; |
| 269 |
if (out>1.0) out=1.0; |
| 270 |
if (out<-1.0) out=-1.0; |
| 271 |
buf[0][i]=out*32767; |
| 272 |
oscBuf->putSample(i,out*32767); |
| 273 |
} else { |
| 274 |
buf[0][i]=0; |
| 275 |
oscBuf->putSample(i,0); |
| 276 |
} |
| 277 |
} |
| 278 |
oscBuf->end(len); |
| 279 |
} |
| 280 |
|
| 281 |
void DivPlatformPCSpeaker::acquire_piezo(short** buf, size_t len) { |
| 282 |
oscBuf->begin(len); |
| 283 |
for (size_t i=0; i<len; i++) { |
| 284 |
if (on) { |
| 285 |
pos-=PCSPKR_DIVIDER; |
| 286 |
if (pos>freq) pos=freq; |
| 287 |
while (pos<0) { |
| 288 |
if (freq<1) { |
| 289 |
pos=1; |
| 290 |
} else { |
| 291 |
pos+=freq; |
| 292 |
} |
| 293 |
} |
| 294 |
float next=(pos>((freq+64)>>1) && !isMuted[0])?1:0; |
| 295 |
low+=cut*band; |
| 296 |
band+=cut*(next-low-(reso*band)); |
| 297 |
float out=band*0.15-(next-low)*0.06; |
| 298 |
if (out>1.0) out=1.0; |
| 299 |
if (out<-1.0) out=-1.0; |
| 300 |
buf[0][i]=out*32767; |
| 301 |
oscBuf->putSample(i,out*32767); |
| 302 |
} else { |
| 303 |
buf[0][i]=0; |
| 304 |
oscBuf->putSample(i,0); |
| 305 |
} |
| 306 |
} |
| 307 |
oscBuf->end(len); |
| 308 |
} |
| 309 |
|
| 310 |
void DivPlatformPCSpeaker::beepFreq(int freq, int delay) { |
| 311 |
realQueueLock.lock(); |
| 312 |
#ifdef __linux__ |
| 313 |
struct timespec ts; |
| 314 |
double addition=1000000000.0*(double)delay/parent->getAudioDescGot().rate; |
| 315 |
addition+=1500000000.0*((double)parent->getAudioDescGot().bufsize/parent->getAudioDescGot().rate); |
| 316 |
if (clock_gettime(CLOCK_MONOTONIC,&ts)<0) { |
| 317 |
ts.tv_sec=0; |
| 318 |
ts.tv_nsec=0; |
| 319 |
} else { |
| 320 |
ts.tv_nsec+=addition; |
| 321 |
while (ts.tv_nsec>=1000000000) { |
| 322 |
ts.tv_sec++; |
| 323 |
ts.tv_nsec-=1000000000; |
| 324 |
} |
| 325 |
} |
| 326 |
realQueue.push(RealQueueVal(ts.tv_sec,ts.tv_nsec,freq)); |
| 327 |
#else |
| 328 |
realQueue.push(RealQueueVal(0,0,freq)); |
| 329 |
#endif |
| 330 |
realQueueLock.unlock(); |
| 331 |
realOutCond.notify_one(); |
| 332 |
} |
| 333 |
|
| 334 |
void DivPlatformPCSpeaker::acquire_real(blip_buffer_t** bb, size_t len) { |
| 335 |
if (lastOn!=on || lastFreq!=freq) { |
| 336 |
lastOn=on; |
| 337 |
lastFreq=freq; |
| 338 |
beepFreq((on && !isMuted[0])?freq:0,parent->getBufferPos()); |
| 339 |
} |
| 340 |
int out=0; |
| 341 |
int freq1=freq+1; |
| 342 |
int timeToNextToggle=0; |
| 343 |
oscBuf->begin(len); |
| 344 |
if (on) { |
| 345 |
// just in case |
| 346 |
if (pos>freq1) { |
| 347 |
pos=freq1; |
| 348 |
} |
| 349 |
if (pos>(freq>>1)) { |
| 350 |
posToggle=true; |
| 351 |
timeToNextToggle=pos-(freq>>1); |
| 352 |
} else { |
| 353 |
posToggle=false; |
| 354 |
timeToNextToggle=pos; |
| 355 |
} |
| 356 |
out=(posToggle && !isMuted[0])?32767:0; |
| 357 |
oscBuf->putSample(0,out); |
| 358 |
oldOut=out; |
| 359 |
if (freq>=1) { |
| 360 |
size_t boff=0; |
| 361 |
size_t i=len; |
| 362 |
while (true) { |
| 363 |
if ((int)i<timeToNextToggle) { |
| 364 |
pos-=i; |
| 365 |
break; |
| 366 |
} |
| 367 |
i-=timeToNextToggle; |
| 368 |
boff+=timeToNextToggle; |
| 369 |
pos-=timeToNextToggle; |
| 370 |
if (pos<=0) { |
| 371 |
pos=freq1; |
| 372 |
} |
| 373 |
if (pos>(freq>>1)) { |
| 374 |
posToggle=true; |
| 375 |
timeToNextToggle=pos-(freq>>1); |
| 376 |
} else { |
| 377 |
posToggle=false; |
| 378 |
timeToNextToggle=pos; |
| 379 |
} |
| 380 |
out=(posToggle && !isMuted[0])?32767:0; |
| 381 |
oscBuf->putSample(boff,out); |
| 382 |
oldOut=out; |
| 383 |
} |
| 384 |
} |
| 385 |
} else { |
| 386 |
out=0; |
| 387 |
oscBuf->putSample(0,out); |
| 388 |
oldOut=out; |
| 389 |
} |
| 390 |
oscBuf->end(len); |
| 391 |
} |
| 392 |
|
| 393 |
void DivPlatformPCSpeaker::acquire(short** buf, size_t len) { |
| 394 |
switch (speakerType) { |
| 395 |
case 1: |
| 396 |
acquire_cone(buf,len); |
| 397 |
break; |
| 398 |
case 2: |
| 399 |
acquire_piezo(buf,len); |
| 400 |
break; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
void DivPlatformPCSpeaker::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 405 |
switch (speakerType) { |
| 406 |
case 0: |
| 407 |
acquire_unfilt(bb,len); |
| 408 |
break; |
| 409 |
case 3: |
| 410 |
acquire_real(bb,len); |
| 411 |
break; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
void DivPlatformPCSpeaker::tick(bool sysTick) { |
| 416 |
for (int i=0; i<1; i++) { |
| 417 |
chan[i].std.next(); |
| 418 |
if (chan[i].std.vol.had) { |
| 419 |
chan[i].outVol=(chan[i].vol && chan[i].std.vol.val); |
| 420 |
on=chan[i].outVol; |
| 421 |
} |
| 422 |
if (NEW_ARP_STRAT) { |
| 423 |
chan[i].handleArp(); |
| 424 |
} else if (chan[i].std.arp.had) { |
| 425 |
if (!chan[i].inPorta) { |
| 426 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 427 |
} |
| 428 |
chan[i].freqChanged=true; |
| 429 |
} |
| 430 |
if (chan[i].std.pitch.had) { |
| 431 |
if (chan[i].std.pitch.mode) { |
| 432 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 433 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 434 |
} else { |
| 435 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 436 |
} |
| 437 |
chan[i].freqChanged=true; |
| 438 |
} |
| 439 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 440 |
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER)-1; |
| 441 |
if (chan[i].freq<0) chan[i].freq=0; |
| 442 |
if (chan[i].freq>65535) chan[i].freq=65535; |
| 443 |
if (!chan[i].std.vol.had) { |
| 444 |
if (chan[i].keyOn) { |
| 445 |
on=true; |
| 446 |
} |
| 447 |
} |
| 448 |
if (chan[i].keyOff) { |
| 449 |
on=false; |
| 450 |
} |
| 451 |
if (freq!=chan[i].freq && resetPhase) { |
| 452 |
pos=0; |
| 453 |
} |
| 454 |
freq=chan[i].freq; |
| 455 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 456 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 457 |
chan[i].freqChanged=false; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
int DivPlatformPCSpeaker::dispatch(DivCommand c) { |
| 463 |
switch (c.cmd) { |
| 464 |
case DIV_CMD_NOTE_ON: |
| 465 |
if (c.value!=DIV_NOTE_NULL) { |
| 466 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 467 |
chan[c.chan].freqChanged=true; |
| 468 |
chan[c.chan].note=c.value; |
| 469 |
} |
| 470 |
chan[c.chan].active=true; |
| 471 |
chan[c.chan].keyOn=true; |
| 472 |
chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_BEEPER)); |
| 473 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 474 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 475 |
} |
| 476 |
break; |
| 477 |
case DIV_CMD_NOTE_OFF: |
| 478 |
chan[c.chan].active=false; |
| 479 |
chan[c.chan].keyOff=true; |
| 480 |
chan[c.chan].macroInit(NULL); |
| 481 |
break; |
| 482 |
case DIV_CMD_NOTE_OFF_ENV: |
| 483 |
case DIV_CMD_ENV_RELEASE: |
| 484 |
chan[c.chan].std.release(); |
| 485 |
break; |
| 486 |
case DIV_CMD_INSTRUMENT: |
| 487 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 488 |
chan[c.chan].ins=c.value; |
| 489 |
} |
| 490 |
break; |
| 491 |
case DIV_CMD_VOLUME: |
| 492 |
if (chan[c.chan].vol!=c.value) { |
| 493 |
chan[c.chan].vol=c.value; |
| 494 |
if (!chan[c.chan].std.vol.has) { |
| 495 |
chan[c.chan].outVol=c.value; |
| 496 |
} |
| 497 |
if (chan[c.chan].active) { |
| 498 |
on=chan[c.chan].vol; |
| 499 |
} |
| 500 |
} |
| 501 |
break; |
| 502 |
case DIV_CMD_GET_VOLUME: |
| 503 |
return chan[c.chan].vol; |
| 504 |
break; |
| 505 |
case DIV_CMD_PITCH: |
| 506 |
chan[c.chan].pitch=c.value; |
| 507 |
chan[c.chan].freqChanged=true; |
| 508 |
break; |
| 509 |
case DIV_CMD_NOTE_PORTA: { |
| 510 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 511 |
bool return2=false; |
| 512 |
if (destFreq>chan[c.chan].baseFreq) { |
| 513 |
chan[c.chan].baseFreq+=c.value; |
| 514 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 515 |
chan[c.chan].baseFreq=destFreq; |
| 516 |
return2=true; |
| 517 |
} |
| 518 |
} else { |
| 519 |
chan[c.chan].baseFreq-=c.value; |
| 520 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 521 |
chan[c.chan].baseFreq=destFreq; |
| 522 |
return2=true; |
| 523 |
} |
| 524 |
} |
| 525 |
chan[c.chan].freqChanged=true; |
| 526 |
if (return2) { |
| 527 |
chan[c.chan].inPorta=false; |
| 528 |
return 2; |
| 529 |
} |
| 530 |
break; |
| 531 |
} |
| 532 |
case DIV_CMD_LEGATO: |
| 533 |
if (c.chan==3) break; |
| 534 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 535 |
chan[c.chan].freqChanged=true; |
| 536 |
chan[c.chan].note=c.value; |
| 537 |
break; |
| 538 |
case DIV_CMD_PRE_PORTA: |
| 539 |
if (chan[c.chan].active && c.value2) { |
| 540 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_BEEPER)); |
| 541 |
} |
| 542 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note); |
| 543 |
chan[c.chan].inPorta=c.value; |
| 544 |
break; |
| 545 |
case DIV_CMD_GET_VOLMAX: |
| 546 |
return 1; |
| 547 |
break; |
| 548 |
case DIV_CMD_MACRO_OFF: |
| 549 |
chan[c.chan].std.mask(c.value,true); |
| 550 |
break; |
| 551 |
case DIV_CMD_MACRO_ON: |
| 552 |
chan[c.chan].std.mask(c.value,false); |
| 553 |
break; |
| 554 |
case DIV_CMD_MACRO_RESTART: |
| 555 |
chan[c.chan].std.restart(c.value); |
| 556 |
break; |
| 557 |
default: |
| 558 |
break; |
| 559 |
} |
| 560 |
return 1; |
| 561 |
} |
| 562 |
|
| 563 |
void DivPlatformPCSpeaker::muteChannel(int ch, bool mute) { |
| 564 |
isMuted[ch]=mute; |
| 565 |
} |
| 566 |
|
| 567 |
void DivPlatformPCSpeaker::forceIns() { |
| 568 |
for (int i=0; i<1; i++) { |
| 569 |
chan[i].insChanged=true; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
void* DivPlatformPCSpeaker::getChanState(int ch) { |
| 574 |
return &chan[ch]; |
| 575 |
} |
| 576 |
|
| 577 |
DivMacroInt* DivPlatformPCSpeaker::getChanMacroInt(int ch) { |
| 578 |
return &chan[ch].std; |
| 579 |
} |
| 580 |
|
| 581 |
DivDispatchOscBuffer* DivPlatformPCSpeaker::getOscBuffer(int ch) { |
| 582 |
return oscBuf; |
| 583 |
} |
| 584 |
|
| 585 |
unsigned char* DivPlatformPCSpeaker::getRegisterPool() { |
| 586 |
if (on) { |
| 587 |
regPool[0]=freq; |
| 588 |
regPool[1]=freq>>8; |
| 589 |
} else { |
| 590 |
regPool[0]=0; |
| 591 |
regPool[1]=0; |
| 592 |
} |
| 593 |
return regPool; |
| 594 |
} |
| 595 |
|
| 596 |
int DivPlatformPCSpeaker::getRegisterPoolSize() { |
| 597 |
return 2; |
| 598 |
} |
| 599 |
|
| 600 |
void DivPlatformPCSpeaker::reset() { |
| 601 |
for (int i=0; i<1; i++) { |
| 602 |
chan[i]=DivPlatformPCSpeaker::Channel(); |
| 603 |
chan[i].std.setEngine(parent); |
| 604 |
} |
| 605 |
if (dumpWrites) { |
| 606 |
addWrite(0xffffffff,0); |
| 607 |
} |
| 608 |
|
| 609 |
on=false; |
| 610 |
lastOn=false; |
| 611 |
freq=0; |
| 612 |
lastFreq=0; |
| 613 |
pos=0; |
| 614 |
posToggle=true; |
| 615 |
flip=false; |
| 616 |
low=0; |
| 617 |
band=0; |
| 618 |
oldOut=0; |
| 619 |
|
| 620 |
//if (speakerType==3) { |
| 621 |
#ifdef __linux__ |
| 622 |
if (beepFD==-1) { |
| 623 |
switch (realOutMethod) { |
| 624 |
case 0: // evdev |
| 625 |
beepFD=open("/dev/input/by-path/platform-pcspkr-event-spkr",O_WRONLY); |
| 626 |
break; |
| 627 |
case 1: // KIOCSOUND (on tty) |
| 628 |
beepFD=open("/dev/tty1",O_WRONLY); |
| 629 |
break; |
| 630 |
case 2: // /dev/port |
| 631 |
beepFD=open("/dev/port",O_WRONLY); |
| 632 |
break; |
| 633 |
case 3: // KIOCSOUND (on stdout) |
| 634 |
beepFD=STDOUT_FILENO; |
| 635 |
break; |
| 636 |
case 4: // outb() |
| 637 |
beepFD=-1; |
| 638 |
#ifdef HAVE_SYS_IO |
| 639 |
if (ioperm(0x61,8,1)<0) { |
| 640 |
logW("ioperm 0x61: %s",strerror(errno)); |
| 641 |
break; |
| 642 |
} |
| 643 |
if (ioperm(0x43,8,1)<0) { |
| 644 |
logW("ioperm 0x43: %s",strerror(errno)); |
| 645 |
break; |
| 646 |
} |
| 647 |
if (ioperm(0x42,8,1)<0) { |
| 648 |
logW("ioperm 0x42: %s",strerror(errno)); |
| 649 |
break; |
| 650 |
} |
| 651 |
beepFD=STDOUT_FILENO; |
| 652 |
#else |
| 653 |
errno=ENOSYS; |
| 654 |
#endif |
| 655 |
break; |
| 656 |
} |
| 657 |
if (beepFD<0) { |
| 658 |
logW("error while opening PC speaker! %s",strerror(errno)); |
| 659 |
} |
| 660 |
} |
| 661 |
#endif |
| 662 |
beepFreq(0); |
| 663 |
/*} else { |
| 664 |
beepFreq(0); |
| 665 |
}*/ |
| 666 |
|
| 667 |
if (realOutThread==NULL) { |
| 668 |
realOutThread=new std::thread(_pcSpeakerThread,this); |
| 669 |
} |
| 670 |
|
| 671 |
memset(regPool,0,2); |
| 672 |
} |
| 673 |
|
| 674 |
bool DivPlatformPCSpeaker::keyOffAffectsArp(int ch) { |
| 675 |
return true; |
| 676 |
} |
| 677 |
|
| 678 |
bool DivPlatformPCSpeaker::hasAcquireDirect() { |
| 679 |
return (speakerType==0 || speakerType==3); |
| 680 |
} |
| 681 |
|
| 682 |
void DivPlatformPCSpeaker::setFlags(const DivConfig& flags) { |
| 683 |
switch (flags.getInt("clockSel",0)) { |
| 684 |
case 1: // PC-98 |
| 685 |
chipClock=38400*52; |
| 686 |
break; |
| 687 |
case 2: // PC-98 |
| 688 |
chipClock=38400*64; |
| 689 |
break; |
| 690 |
default: // IBM PC |
| 691 |
chipClock=COLOR_NTSC/3.0; |
| 692 |
break; |
| 693 |
} |
| 694 |
CHECK_CUSTOM_CLOCK; |
| 695 |
speakerType=flags.getInt("speakerType",0)&3; |
| 696 |
resetPhase=flags.getBool("resetPhase",false); |
| 697 |
if (speakerType==0 || speakerType==3) { |
| 698 |
rate=chipClock; |
| 699 |
} else { |
| 700 |
rate=chipClock/PCSPKR_DIVIDER; |
| 701 |
} |
| 702 |
oscBuf->setRate(rate); |
| 703 |
|
| 704 |
switch (speakerType) { |
| 705 |
case 1: |
| 706 |
cut=2.0*sin(M_PI*1900.0/rate); |
| 707 |
reso=0.0; |
| 708 |
break; |
| 709 |
default: |
| 710 |
cut=2.0*sin(M_PI*2375.0/rate); |
| 711 |
reso=0.06; |
| 712 |
break; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
void DivPlatformPCSpeaker::notifyInsDeletion(void* ins) { |
| 717 |
for (int i=0; i<1; i++) { |
| 718 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
void DivPlatformPCSpeaker::notifyPlaybackStop() { |
| 723 |
beepFreq(0); |
| 724 |
} |
| 725 |
|
| 726 |
void DivPlatformPCSpeaker::poke(unsigned int addr, unsigned short val) { |
| 727 |
// ??? |
| 728 |
} |
| 729 |
|
| 730 |
void DivPlatformPCSpeaker::poke(std::vector<DivRegWrite>& wlist) { |
| 731 |
// ??? |
| 732 |
} |
| 733 |
|
| 734 |
int DivPlatformPCSpeaker::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 735 |
parent=p; |
| 736 |
dumpWrites=false; |
| 737 |
skipRegisterWrites=false; |
| 738 |
beepFD=-1; |
| 739 |
realOutQuit=false; |
| 740 |
realOutThread=NULL; |
| 741 |
realOutMethod=parent->getConfInt("pcSpeakerOutMethod",0); |
| 742 |
realOutEnabled=false; |
| 743 |
for (int i=0; i<1; i++) { |
| 744 |
isMuted[i]=false; |
| 745 |
} |
| 746 |
oscBuf=new DivDispatchOscBuffer; |
| 747 |
setFlags(flags); |
| 748 |
|
| 749 |
reset(); |
| 750 |
return 5; |
| 751 |
} |
| 752 |
|
| 753 |
void DivPlatformPCSpeaker::quit() { |
| 754 |
if (speakerType==3) { |
| 755 |
beepFreq(0); |
| 756 |
} |
| 757 |
if (realOutThread!=NULL) { |
| 758 |
realOutQuit=true; |
| 759 |
realOutCond.notify_one(); |
| 760 |
realOutThread->join(); |
| 761 |
delete realOutThread; |
| 762 |
} |
| 763 |
#ifdef __linux__ |
| 764 |
if (beepFD>=0 && realOutMethod<3) close(beepFD); |
| 765 |
#endif |
| 766 |
delete oscBuf; |
| 767 |
} |
| 768 |
|
| 769 |
DivPlatformPCSpeaker::~DivPlatformPCSpeaker() { |
| 770 |
} |