| 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 |
#include "segapcm.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include <string.h> |
| 24 |
#include <math.h> |
| 25 |
|
| 26 |
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} } |
| 27 |
#define chWrite(c,a,v) rWrite(((c)<<3)+(a),v) |
| 28 |
|
| 29 |
void DivPlatformSegaPCM::acquire(short** buf, size_t len) { |
| 30 |
thread_local int os[2]; |
| 31 |
|
| 32 |
for (int i=0; i<16; i++) { |
| 33 |
oscBuf[i]->begin(len); |
| 34 |
} |
| 35 |
|
| 36 |
for (size_t h=0; h<len; h++) { |
| 37 |
while (!writes.empty()) { |
| 38 |
QueuedWrite w=writes.front(); |
| 39 |
pcm.write(w.addr,w.val); |
| 40 |
regPool[w.addr&0xff]=w.val; |
| 41 |
writes.pop(); |
| 42 |
} |
| 43 |
|
| 44 |
pcm.sound_stream_update(os); |
| 45 |
|
| 46 |
if (os[0]<-32768) os[0]=-32768; |
| 47 |
if (os[0]>32767) os[0]=32767; |
| 48 |
|
| 49 |
if (os[1]<-32768) os[1]=-32768; |
| 50 |
if (os[1]>32767) os[1]=32767; |
| 51 |
|
| 52 |
buf[0][h]=os[0]; |
| 53 |
buf[1][h]=os[1]; |
| 54 |
|
| 55 |
for (int i=0; i<16; i++) { |
| 56 |
oscBuf[i]->putSample(h,(pcm.lastOut[i][0]+pcm.lastOut[i][1])>>1); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
for (int i=0; i<16; i++) { |
| 61 |
oscBuf[i]->end(len); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void DivPlatformSegaPCM::tick(bool sysTick) { |
| 66 |
for (int i=0; i<16; i++) { |
| 67 |
chan[i].std.next(); |
| 68 |
|
| 69 |
if (parent->song.newSegaPCM) { |
| 70 |
if (chan[i].std.vol.had) { |
| 71 |
chan[i].outVol=(chan[i].vol*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul; |
| 72 |
chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/127; |
| 73 |
chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/127; |
| 74 |
rWrite(2+(i<<3),chan[i].chVolL); |
| 75 |
rWrite(3+(i<<3),chan[i].chVolR); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (NEW_ARP_STRAT) { |
| 80 |
chan[i].handleArp(); |
| 81 |
} else if (chan[i].std.arp.had) { |
| 82 |
if (!chan[i].inPorta) { |
| 83 |
chan[i].baseFreq=(parent->calcArp(chan[i].note,chan[i].std.arp.val)<<7); |
| 84 |
} |
| 85 |
chan[i].freqChanged=true; |
| 86 |
} |
| 87 |
|
| 88 |
if (parent->song.newSegaPCM) if (chan[i].std.panL.had) { |
| 89 |
chan[i].chPanL=chan[i].std.panL.val&127; |
| 90 |
chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/127; |
| 91 |
rWrite(2+(i<<3),chan[i].chVolL); |
| 92 |
} |
| 93 |
|
| 94 |
if (parent->song.newSegaPCM) if (chan[i].std.panR.had) { |
| 95 |
chan[i].chPanR=chan[i].std.panR.val&127; |
| 96 |
chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/127; |
| 97 |
rWrite(3+(i<<3),chan[i].chVolR); |
| 98 |
} |
| 99 |
|
| 100 |
if (chan[i].std.pitch.had) { |
| 101 |
if (chan[i].std.pitch.mode) { |
| 102 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 103 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 104 |
} else { |
| 105 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 106 |
} |
| 107 |
chan[i].freqChanged=true; |
| 108 |
} |
| 109 |
|
| 110 |
if (chan[i].std.phaseReset.had) { |
| 111 |
if (chan[i].std.phaseReset.val==1 && chan[i].active) { |
| 112 |
chan[i].keyOn=true; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 117 |
chan[i].freq=chan[i].baseFreq+(chan[i].pitch)-128+(oldSlides?0:chan[i].pitch2); |
| 118 |
if (!parent->song.oldArpStrategy) { |
| 119 |
if (chan[i].fixedArp) { |
| 120 |
chan[i].freq=(chan[i].baseNoteOverride<<7)+chan[i].pitch-128+(chan[i].pitch2<<(oldSlides?1:0)); |
| 121 |
} else { |
| 122 |
chan[i].freq+=chan[i].arpOff<<7; |
| 123 |
} |
| 124 |
} |
| 125 |
if (oldSlides) chan[i].freq&=~1; |
| 126 |
if (chan[i].furnacePCM) { |
| 127 |
double off=1.0; |
| 128 |
if (chan[i].pcm.sample>=0 && chan[i].pcm.sample<parent->song.sampleLen) { |
| 129 |
DivSample* s=parent->getSample(chan[i].pcm.sample); |
| 130 |
off=(double)s->centerRate/parent->getCenterRate(); |
| 131 |
} |
| 132 |
chan[i].pcm.freq=MIN(255,((rate*0.5)+(off*parent->song.tuning*pow(2.0,double(chan[i].freq+512)/(128.0*12.0)))*255)/rate)+(oldSlides?chan[i].pitch2:0); |
| 133 |
rWrite(7+(i<<3),chan[i].pcm.freq); |
| 134 |
} |
| 135 |
chan[i].freqChanged=false; |
| 136 |
if (chan[i].keyOn || chan[i].keyOff) { |
| 137 |
if (chan[i].keyOn && !chan[i].keyOff) { |
| 138 |
rWrite(0x86+(i<<3),3); |
| 139 |
if (chan[i].setPos) { |
| 140 |
chan[i].setPos=false; |
| 141 |
} else { |
| 142 |
chan[i].pcm.pos=0; |
| 143 |
} |
| 144 |
if (chan[i].furnacePCM) { |
| 145 |
DivSample* s=parent->getSample(chan[i].pcm.sample); |
| 146 |
int loopStart=s->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT); |
| 147 |
int actualLength=(s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT)); |
| 148 |
if (actualLength>0xfeff) actualLength=0xfeff; |
| 149 |
int actualPos=sampleOffSegaPCM[chan[i].pcm.sample]+chan[i].pcm.pos; |
| 150 |
rWrite(0x86+(i<<3),3+((actualPos>>16)<<3)); |
| 151 |
rWrite(0x84+(i<<3),(actualPos)&0xff); |
| 152 |
rWrite(0x85+(i<<3),(actualPos>>8)&0xff); |
| 153 |
rWrite(6+(i<<3),sampleEndSegaPCM[chan[i].pcm.sample]); |
| 154 |
if (!s->isLoopable()) { |
| 155 |
rWrite(0x86+(i<<3),2+((actualPos>>16)<<3)); |
| 156 |
} else { |
| 157 |
int loopPos=(sampleOffSegaPCM[chan[i].pcm.sample]&0xffff)+loopStart; |
| 158 |
logV("sampleOff: %x loopPos: %x",actualPos,loopPos); |
| 159 |
rWrite(4+(i<<3),loopPos&0xff); |
| 160 |
rWrite(5+(i<<3),(loopPos>>8)&0xff); |
| 161 |
rWrite(0x86+(i<<3),((actualPos>>16)<<3)); |
| 162 |
} |
| 163 |
} else { |
| 164 |
DivSample* s=parent->getSample(chan[i].pcm.sample); |
| 165 |
int loopStart=s->getLoopStartPosition(DIV_SAMPLE_DEPTH_8BIT); |
| 166 |
int actualLength=(s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT)); |
| 167 |
if (actualLength>0xfeff) actualLength=0xfeff; |
| 168 |
int actualPos=sampleOffSegaPCM[chan[i].pcm.sample]+chan[i].pcm.pos; |
| 169 |
rWrite(0x86+(i<<3),3+((actualPos>>16)<<3)); |
| 170 |
rWrite(0x84+(i<<3),(actualPos)&0xff); |
| 171 |
rWrite(0x85+(i<<3),(actualPos>>8)&0xff); |
| 172 |
rWrite(6+(i<<3),sampleEndSegaPCM[chan[i].pcm.sample]); |
| 173 |
if (!s->isLoopable()) { |
| 174 |
rWrite(0x86+(i<<3),2+((actualPos>>16)<<3)); |
| 175 |
} else { |
| 176 |
int loopPos=(sampleOffSegaPCM[chan[i].pcm.sample]&0xffff)+loopStart; |
| 177 |
rWrite(4+(i<<3),loopPos&0xff); |
| 178 |
rWrite(5+(i<<3),(loopPos>>8)&0xff); |
| 179 |
rWrite(0x86+(i<<3),((actualPos>>16)<<3)); |
| 180 |
} |
| 181 |
rWrite(7+(i<<3),chan[i].pcm.freq); |
| 182 |
} |
| 183 |
} |
| 184 |
chan[i].keyOn=false; |
| 185 |
chan[i].keyOff=false; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void DivPlatformSegaPCM::muteChannel(int ch, bool mute) { |
| 192 |
isMuted[ch]=mute; |
| 193 |
pcm.mute(ch,mute); |
| 194 |
} |
| 195 |
|
| 196 |
int DivPlatformSegaPCM::dispatch(DivCommand c) { |
| 197 |
switch (c.cmd) { |
| 198 |
case DIV_CMD_NOTE_ON: { |
| 199 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); |
| 200 |
if (skipRegisterWrites) break; |
| 201 |
if (ins->type==DIV_INS_AMIGA || ins->type==DIV_INS_SEGAPCM) { |
| 202 |
chan[c.chan].macroVolMul=(ins->type==DIV_INS_AMIGA)?64:127; |
| 203 |
chan[c.chan].isNewSegaPCM=(ins->type==DIV_INS_SEGAPCM); |
| 204 |
if (c.value!=DIV_NOTE_NULL) { |
| 205 |
chan[c.chan].pcm.sample=ins->amiga.getSample(c.value); |
| 206 |
chan[c.chan].sampleNote=c.value; |
| 207 |
c.value=ins->amiga.getFreq(c.value); |
| 208 |
chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote; |
| 209 |
} |
| 210 |
if (chan[c.chan].pcm.sample<0 || chan[c.chan].pcm.sample>=parent->song.sampleLen) { |
| 211 |
chan[c.chan].pcm.sample=-1; |
| 212 |
rWrite(0x86+(c.chan<<3),3); |
| 213 |
chan[c.chan].macroInit(NULL); |
| 214 |
break; |
| 215 |
} |
| 216 |
if (c.value!=DIV_NOTE_NULL) { |
| 217 |
chan[c.chan].note=c.value; |
| 218 |
chan[c.chan].baseFreq=(c.value<<7); |
| 219 |
chan[c.chan].freqChanged=true; |
| 220 |
} |
| 221 |
chan[c.chan].furnacePCM=true; |
| 222 |
chan[c.chan].macroInit(ins); |
| 223 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 224 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 225 |
|
| 226 |
if (parent->song.newSegaPCM) { |
| 227 |
chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/127; |
| 228 |
chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/127; |
| 229 |
rWrite(2+(c.chan<<3),chan[c.chan].chVolL); |
| 230 |
rWrite(3+(c.chan<<3),chan[c.chan].chVolR); |
| 231 |
} |
| 232 |
} |
| 233 |
chan[c.chan].active=true; |
| 234 |
chan[c.chan].keyOn=true; |
| 235 |
} else { |
| 236 |
chan[c.chan].macroInit(NULL); |
| 237 |
if (c.value!=DIV_NOTE_NULL) { |
| 238 |
chan[c.chan].note=c.value; |
| 239 |
} |
| 240 |
chan[c.chan].pcm.sample=12*sampleBank+chan[c.chan].note%12; |
| 241 |
if (chan[c.chan].pcm.sample>=parent->song.sampleLen) { |
| 242 |
chan[c.chan].pcm.sample=-1; |
| 243 |
rWrite(0x86+(c.chan<<3),3); |
| 244 |
break; |
| 245 |
} |
| 246 |
chan[c.chan].pcm.freq=MIN(255,(parent->getSample(chan[c.chan].pcm.sample)->rate*255)/rate); |
| 247 |
chan[c.chan].furnacePCM=false; |
| 248 |
chan[c.chan].active=true; |
| 249 |
chan[c.chan].keyOn=true; |
| 250 |
} |
| 251 |
break; |
| 252 |
} |
| 253 |
case DIV_CMD_NOTE_OFF: |
| 254 |
chan[c.chan].pcm.sample=-1; |
| 255 |
rWrite(0x86+(c.chan<<3),3); |
| 256 |
chan[c.chan].keyOff=true; |
| 257 |
chan[c.chan].keyOn=false; |
| 258 |
chan[c.chan].active=false; |
| 259 |
chan[c.chan].macroInit(NULL); |
| 260 |
break; |
| 261 |
case DIV_CMD_NOTE_OFF_ENV: |
| 262 |
chan[c.chan].keyOff=true; |
| 263 |
chan[c.chan].keyOn=false; |
| 264 |
chan[c.chan].active=false; |
| 265 |
chan[c.chan].std.release(); |
| 266 |
break; |
| 267 |
case DIV_CMD_ENV_RELEASE: |
| 268 |
chan[c.chan].std.release(); |
| 269 |
break; |
| 270 |
case DIV_CMD_VOLUME: { |
| 271 |
chan[c.chan].vol=c.value; |
| 272 |
if (!chan[c.chan].std.vol.has) { |
| 273 |
chan[c.chan].outVol=c.value; |
| 274 |
} |
| 275 |
if (parent->song.newSegaPCM) { |
| 276 |
chan[c.chan].chVolL=(c.value*chan[c.chan].chPanL)/127; |
| 277 |
chan[c.chan].chVolR=(c.value*chan[c.chan].chPanR)/127; |
| 278 |
} else { |
| 279 |
chan[c.chan].chVolL=c.value; |
| 280 |
chan[c.chan].chVolR=c.value; |
| 281 |
} |
| 282 |
rWrite(2+(c.chan<<3),chan[c.chan].chVolL); |
| 283 |
rWrite(3+(c.chan<<3),chan[c.chan].chVolR); |
| 284 |
break; |
| 285 |
} |
| 286 |
case DIV_CMD_GET_VOLUME: { |
| 287 |
return chan[c.chan].vol; |
| 288 |
break; |
| 289 |
} |
| 290 |
case DIV_CMD_INSTRUMENT: |
| 291 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 292 |
chan[c.chan].insChanged=true; |
| 293 |
} |
| 294 |
chan[c.chan].ins=c.value; |
| 295 |
break; |
| 296 |
case DIV_CMD_PANNING: { |
| 297 |
if (parent->song.newSegaPCM) { |
| 298 |
chan[c.chan].chPanL=c.value>>1; |
| 299 |
chan[c.chan].chPanR=c.value2>>1; |
| 300 |
chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/127; |
| 301 |
chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/127; |
| 302 |
} else { |
| 303 |
chan[c.chan].chVolL=c.value>>1; |
| 304 |
chan[c.chan].chVolR=c.value2>>1; |
| 305 |
} |
| 306 |
rWrite(2+(c.chan<<3),chan[c.chan].chVolL); |
| 307 |
rWrite(3+(c.chan<<3),chan[c.chan].chVolR); |
| 308 |
break; |
| 309 |
} |
| 310 |
case DIV_CMD_PITCH: { |
| 311 |
chan[c.chan].pitch=c.value; |
| 312 |
chan[c.chan].freqChanged=true; |
| 313 |
break; |
| 314 |
} |
| 315 |
case DIV_CMD_NOTE_PORTA: { |
| 316 |
int destFreq=((c.value2+chan[c.chan].sampleNoteDelta)<<7); |
| 317 |
int newFreq; |
| 318 |
int mul=(oldSlides || parent->song.linearPitch!=2)?8:1; |
| 319 |
bool return2=false; |
| 320 |
if (destFreq>chan[c.chan].baseFreq) { |
| 321 |
newFreq=chan[c.chan].baseFreq+c.value*mul; |
| 322 |
if (newFreq>=destFreq) { |
| 323 |
newFreq=destFreq; |
| 324 |
return2=true; |
| 325 |
} |
| 326 |
} else { |
| 327 |
newFreq=chan[c.chan].baseFreq-c.value*mul; |
| 328 |
if (newFreq<=destFreq) { |
| 329 |
newFreq=destFreq; |
| 330 |
return2=true; |
| 331 |
} |
| 332 |
} |
| 333 |
chan[c.chan].baseFreq=newFreq; |
| 334 |
chan[c.chan].freqChanged=true; |
| 335 |
if (return2) { |
| 336 |
chan[c.chan].inPorta=false; |
| 337 |
return 2; |
| 338 |
} |
| 339 |
break; |
| 340 |
} |
| 341 |
case DIV_CMD_LEGATO: { |
| 342 |
chan[c.chan].baseFreq=((c.value+chan[c.chan].sampleNoteDelta)<<7); |
| 343 |
chan[c.chan].freqChanged=true; |
| 344 |
break; |
| 345 |
} |
| 346 |
case DIV_CMD_SAMPLE_BANK: |
| 347 |
sampleBank=c.value; |
| 348 |
if (sampleBank>(parent->song.sample.size()/12)) { |
| 349 |
sampleBank=parent->song.sample.size()/12; |
| 350 |
} |
| 351 |
break; |
| 352 |
case DIV_CMD_SAMPLE_POS: |
| 353 |
chan[c.chan].pcm.pos=c.value; |
| 354 |
chan[c.chan].setPos=true; |
| 355 |
if (chan[c.chan].active) { |
| 356 |
chan[c.chan].keyOn=true; |
| 357 |
} |
| 358 |
break; |
| 359 |
case DIV_CMD_MACRO_OFF: |
| 360 |
chan[c.chan].std.mask(c.value,true); |
| 361 |
break; |
| 362 |
case DIV_CMD_MACRO_ON: |
| 363 |
chan[c.chan].std.mask(c.value,false); |
| 364 |
break; |
| 365 |
case DIV_CMD_MACRO_RESTART: |
| 366 |
chan[c.chan].std.restart(c.value); |
| 367 |
break; |
| 368 |
case DIV_CMD_GET_VOLMAX: |
| 369 |
return 127; |
| 370 |
break; |
| 371 |
case DIV_CMD_PRE_PORTA: |
| 372 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=(chan[c.chan].note<<7); |
| 373 |
chan[c.chan].inPorta=c.value; |
| 374 |
break; |
| 375 |
case DIV_CMD_PRE_NOTE: |
| 376 |
break; |
| 377 |
case DIV_CMD_SAMPLE_FREQ: |
| 378 |
chan[c.chan].pcm.freq=c.value; |
| 379 |
rWrite(7+(c.chan<<3),chan[c.chan].pcm.freq); |
| 380 |
break; |
| 381 |
default: |
| 382 |
//printf("WARNING: unimplemented command %d\n",c.cmd); |
| 383 |
break; |
| 384 |
} |
| 385 |
return 1; |
| 386 |
} |
| 387 |
|
| 388 |
void DivPlatformSegaPCM::forceIns() { |
| 389 |
for (int i=0; i<16; i++) { |
| 390 |
chan[i].insChanged=true; |
| 391 |
|
| 392 |
rWrite(2+(i<<3),chan[i].chVolL); |
| 393 |
rWrite(3+(i<<3),chan[i].chVolR); |
| 394 |
rWrite(7+(i<<3),chan[i].pcm.freq); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
bool DivPlatformSegaPCM::getLegacyAlwaysSetVolume() { |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
void DivPlatformSegaPCM::notifyInsChange(int ins) { |
| 403 |
for (int i=0; i<16; i++) { |
| 404 |
if (chan[i].ins==ins) { |
| 405 |
chan[i].insChanged=true; |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
void DivPlatformSegaPCM::notifyInsDeletion(void* ins) { |
| 411 |
for (int i=0; i<16; i++) { |
| 412 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
void* DivPlatformSegaPCM::getChanState(int ch) { |
| 417 |
return &chan[ch]; |
| 418 |
} |
| 419 |
|
| 420 |
DivMacroInt* DivPlatformSegaPCM::getChanMacroInt(int ch) { |
| 421 |
return &chan[ch].std; |
| 422 |
} |
| 423 |
|
| 424 |
unsigned short DivPlatformSegaPCM::getPan(int ch) { |
| 425 |
return (chan[ch].chPanL<<8)|chan[ch].chPanR; |
| 426 |
} |
| 427 |
|
| 428 |
DivSamplePos DivPlatformSegaPCM::getSamplePos(int ch) { |
| 429 |
if (ch>=16) return DivSamplePos(); |
| 430 |
if (chan[ch].pcm.sample<0 || chan[ch].pcm.sample>=parent->song.sampleLen) return DivSamplePos(); |
| 431 |
if (!pcm.is_playing(ch)) return DivSamplePos(); |
| 432 |
return DivSamplePos( |
| 433 |
chan[ch].pcm.sample, |
| 434 |
pcm.get_addr(ch)-sampleOffSegaPCM[chan[ch].pcm.sample], |
| 435 |
122*(chan[ch].pcm.freq+1) |
| 436 |
); |
| 437 |
} |
| 438 |
|
| 439 |
DivDispatchOscBuffer* DivPlatformSegaPCM::getOscBuffer(int ch) { |
| 440 |
return oscBuf[ch]; |
| 441 |
} |
| 442 |
|
| 443 |
unsigned char* DivPlatformSegaPCM::getRegisterPool() { |
| 444 |
return pcm.get_ram(); |
| 445 |
} |
| 446 |
|
| 447 |
int DivPlatformSegaPCM::getRegisterPoolSize() { |
| 448 |
return 256; |
| 449 |
} |
| 450 |
|
| 451 |
void DivPlatformSegaPCM::poke(unsigned int addr, unsigned short val) { |
| 452 |
rWrite(addr,val); |
| 453 |
} |
| 454 |
|
| 455 |
void DivPlatformSegaPCM::poke(std::vector<DivRegWrite>& wlist) { |
| 456 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 457 |
} |
| 458 |
|
| 459 |
const void* DivPlatformSegaPCM::getSampleMem(int index) { |
| 460 |
return index == 0 ? sampleMem : NULL; |
| 461 |
} |
| 462 |
|
| 463 |
size_t DivPlatformSegaPCM::getSampleMemCapacity(int index) { |
| 464 |
return index == 0 ? 2097152 : 0; |
| 465 |
} |
| 466 |
|
| 467 |
size_t DivPlatformSegaPCM::getSampleMemUsage(int index) { |
| 468 |
return index == 0 ? sampleMemLen : 0; |
| 469 |
} |
| 470 |
|
| 471 |
bool DivPlatformSegaPCM::isSampleLoaded(int index, int sample) { |
| 472 |
if (index!=0) return false; |
| 473 |
if (sample<0 || sample>32767) return false; |
| 474 |
return sampleLoaded[sample]; |
| 475 |
} |
| 476 |
|
| 477 |
void DivPlatformSegaPCM::reset() { |
| 478 |
while (!writes.empty()) writes.pop(); |
| 479 |
memset(regPool,0,256); |
| 480 |
for (int i=0; i<16; i++) { |
| 481 |
chan[i]=DivPlatformSegaPCM::Channel(); |
| 482 |
chan[i].std.setEngine(parent); |
| 483 |
chan[i].vol=0x7f; |
| 484 |
chan[i].outVol=0x7f; |
| 485 |
} |
| 486 |
|
| 487 |
lastBusy=60; |
| 488 |
pcmCycles=0; |
| 489 |
pcmL=0; |
| 490 |
pcmR=0; |
| 491 |
sampleBank=0; |
| 492 |
delay=0; |
| 493 |
|
| 494 |
pcm.device_start(); |
| 495 |
|
| 496 |
for (int i=0; i<16; i++) { |
| 497 |
rWrite(0x86+(i<<3),3); |
| 498 |
rWrite(2+(i<<3),0x7f); |
| 499 |
rWrite(3+(i<<3),0x7f); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
const DivMemoryComposition* DivPlatformSegaPCM::getMemCompo(int index) { |
| 504 |
if (index!=0) return NULL; |
| 505 |
return &memCompo; |
| 506 |
} |
| 507 |
|
| 508 |
void DivPlatformSegaPCM::renderSamples(int sysID) { |
| 509 |
size_t memPos=0; |
| 510 |
|
| 511 |
memset(sampleMem,0,2097152); |
| 512 |
memset(sampleLoaded,0,32768*sizeof(bool)); |
| 513 |
memset(sampleOffSegaPCM,0,32768*sizeof(unsigned int)); |
| 514 |
memset(sampleEndSegaPCM,0,32768); |
| 515 |
|
| 516 |
memCompo=DivMemoryComposition(); |
| 517 |
memCompo.name="Sample ROM"; |
| 518 |
|
| 519 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 520 |
DivSample* sample=parent->getSample(i); |
| 521 |
if (!sample->renderOn[0][sysID]) { |
| 522 |
sampleOffSegaPCM[i]=0; |
| 523 |
continue; |
| 524 |
} |
| 525 |
|
| 526 |
unsigned int alignedSize=sample->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT); |
| 527 |
if (alignedSize>=65279) alignedSize=65279; |
| 528 |
if ((memPos&(~0xffff))!=((memPos+alignedSize)&(~0xffff))) { |
| 529 |
memPos=(memPos+0xffff)&(~0xffff); |
| 530 |
} |
| 531 |
if (alignedSize&0xff) { |
| 532 |
memPos=((memPos+255)&(~0xff))+256-(alignedSize&0xff); |
| 533 |
} |
| 534 |
logV("- sample %d will be at %x with length %x",i,memPos,alignedSize); |
| 535 |
sampleLoaded[i]=true; |
| 536 |
if (memPos>=2097152) break; |
| 537 |
sampleOffSegaPCM[i]=memPos; |
| 538 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+alignedSize)); |
| 539 |
for (unsigned int j=0; j<alignedSize; j++) { |
| 540 |
if (j>=sample->samples) { |
| 541 |
sampleMem[memPos++]=0; |
| 542 |
} else { |
| 543 |
sampleMem[memPos++]=((unsigned char)sample->data8[j]+0x80); |
| 544 |
} |
| 545 |
sampleEndSegaPCM[i]=((memPos+0xff)>>8)-1; |
| 546 |
if (memPos>=2097152) break; |
| 547 |
} |
| 548 |
logV(" and it ends in %d",sampleEndSegaPCM[i]); |
| 549 |
if (memPos>=2097152) break; |
| 550 |
} |
| 551 |
sampleMemLen=memPos; |
| 552 |
|
| 553 |
memCompo.used=sampleMemLen; |
| 554 |
memCompo.capacity=getSampleMemCapacity(0); |
| 555 |
} |
| 556 |
|
| 557 |
void DivPlatformSegaPCM::setFlags(const DivConfig& flags) { |
| 558 |
chipClock=8000000.0; |
| 559 |
CHECK_CUSTOM_CLOCK; |
| 560 |
rate=chipClock/256; |
| 561 |
for (int i=0; i<16; i++) { |
| 562 |
oscBuf[i]->setRate(rate); |
| 563 |
} |
| 564 |
|
| 565 |
oldSlides=flags.getBool("oldSlides",false); |
| 566 |
} |
| 567 |
|
| 568 |
int DivPlatformSegaPCM::getOutputCount() { |
| 569 |
return 2; |
| 570 |
} |
| 571 |
|
| 572 |
int DivPlatformSegaPCM::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 573 |
parent=p; |
| 574 |
dumpWrites=false; |
| 575 |
skipRegisterWrites=false; |
| 576 |
for (int i=0; i<16; i++) { |
| 577 |
isMuted[i]=false; |
| 578 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 579 |
} |
| 580 |
sampleMem=new unsigned char[2097152]; |
| 581 |
pcm.set_bank(segapcm_device::BANK_12M|segapcm_device::BANK_MASKF8); |
| 582 |
pcm.set_read([this](unsigned int addr) -> unsigned char { |
| 583 |
return sampleMem[addr&0x1fffff]; |
| 584 |
}); |
| 585 |
setFlags(flags); |
| 586 |
reset(); |
| 587 |
|
| 588 |
return 16; |
| 589 |
} |
| 590 |
|
| 591 |
void DivPlatformSegaPCM::quit() { |
| 592 |
for (int i=0; i<16; i++) { |
| 593 |
delete oscBuf[i]; |
| 594 |
} |
| 595 |
delete sampleMem; |
| 596 |
} |
| 597 |
|
| 598 |
// initialization of important arrays |
| 599 |
DivPlatformSegaPCM::DivPlatformSegaPCM() { |
| 600 |
sampleOffSegaPCM=new unsigned int[32768]; |
| 601 |
sampleEndSegaPCM=new unsigned char[32768]; |
| 602 |
sampleLoaded=new bool[32768]; |
| 603 |
} |
| 604 |
|
| 605 |
DivPlatformSegaPCM::~DivPlatformSegaPCM() { |
| 606 |
delete[] sampleOffSegaPCM; |
| 607 |
delete[] sampleEndSegaPCM; |
| 608 |
delete[] sampleLoaded; |
| 609 |
} |