| 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 "gbadma.h" |
| 22 |
#include "../engine.h" |
| 23 |
#include "../filter.h" |
| 24 |
#include <math.h> |
| 25 |
|
| 26 |
#define CHIP_DIVIDER 16 |
| 27 |
|
| 28 |
void DivPlatformGBADMA::acquire(short** buf, size_t len) { |
| 29 |
// HLE for now |
| 30 |
int outL[2]={0,0}; |
| 31 |
int outR[2]={0,0}; |
| 32 |
for (int i=0; i<2; i++) { |
| 33 |
oscBuf[i]->begin(len); |
| 34 |
} |
| 35 |
for (size_t h=0; h<len; h++) { |
| 36 |
// internal mixing is always 10-bit |
| 37 |
for (int i=0; i<2; i++) { |
| 38 |
bool newSamp=h==0; |
| 39 |
chan[i].audDat=0; |
| 40 |
if (chan[i].active && (chan[i].useWave || (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen))) { |
| 41 |
chan[i].audSub+=(1<<outDepth); |
| 42 |
if (chan[i].useWave) { |
| 43 |
if (chan[i].audPos<(int)chan[i].audLen) { |
| 44 |
chan[i].audDat=wtMem[i*256+chan[i].audPos]; |
| 45 |
} |
| 46 |
newSamp=true; |
| 47 |
if (chan[i].audSub>=chan[i].freq) { |
| 48 |
int posInc=chan[i].audSub/chan[i].freq; |
| 49 |
chan[i].audSub-=chan[i].freq*posInc; |
| 50 |
chan[i].audPos+=posInc; |
| 51 |
chan[i].dmaCount+=posInc; |
| 52 |
if (chan[i].dmaCount>=16 && chan[i].audPos>=(int)chan[i].audLen) { |
| 53 |
chan[i].audPos%=chan[i].audLen; |
| 54 |
} |
| 55 |
chan[i].dmaCount&=15; |
| 56 |
} |
| 57 |
} else if (sampleLoaded[chan[i].sample]) { |
| 58 |
DivSample* s=parent->getSample(chan[i].sample); |
| 59 |
if (s->samples>0) { |
| 60 |
if (chan[i].audPos>=0) { |
| 61 |
unsigned int pos=(sampleOff[chan[i].sample]+chan[i].audPos)&0x01ffffff; |
| 62 |
chan[i].audDat=sampleMem[pos]; |
| 63 |
} |
| 64 |
newSamp=true; |
| 65 |
if (chan[i].audSub>=chan[i].freq) { |
| 66 |
int posInc=chan[i].audSub/chan[i].freq; |
| 67 |
chan[i].audSub-=chan[i].freq*posInc; |
| 68 |
chan[i].audPos+=posInc; |
| 69 |
chan[i].dmaCount+=posInc; |
| 70 |
if (s->isLoopable()) { |
| 71 |
if (chan[i].dmaCount>=16 && chan[i].audPos>=s->loopEnd) { |
| 72 |
int loopStart=s->loopStart&~3; |
| 73 |
int loopPos=chan[i].audPos-loopStart; |
| 74 |
chan[i].audPos=(loopPos%(s->loopEnd-s->loopStart))+loopStart; |
| 75 |
} |
| 76 |
} else if (chan[i].audPos>=(int)s->samples) { |
| 77 |
chan[i].sample=-1; |
| 78 |
} |
| 79 |
chan[i].dmaCount&=15; |
| 80 |
} |
| 81 |
} else { |
| 82 |
chan[i].sample=-1; |
| 83 |
chan[i].audSub=0; |
| 84 |
chan[i].audPos=0; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
if (!isMuted[i] && newSamp) { |
| 89 |
int out=chan[i].audDat*(chan[i].vol*chan[i].envVol/2)<<1; |
| 90 |
outL[i]=(chan[i].pan&2)?out:0; |
| 91 |
outR[i]=(chan[i].pan&1)?out:0; |
| 92 |
} |
| 93 |
oscBuf[i]->putSample(h,(short)((outL[i]+outR[i])<<5)); |
| 94 |
} |
| 95 |
int l=outL[0]+outL[1]; |
| 96 |
int r=outR[0]+outR[1]; |
| 97 |
l=(l>>(10-outDepth))<<(16-outDepth); |
| 98 |
r=(r>>(10-outDepth))<<(16-outDepth); |
| 99 |
if (l<-32768) l=-32768; |
| 100 |
if (l>32767) l=32767; |
| 101 |
if (r<-32768) r=-32768; |
| 102 |
if (r>32767) r=32767; |
| 103 |
buf[0][h]=(short)l; |
| 104 |
buf[1][h]=(short)r; |
| 105 |
} |
| 106 |
for (int i=0; i<2; i++) { |
| 107 |
oscBuf[i]->end(len); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void DivPlatformGBADMA::tick(bool sysTick) { |
| 112 |
for (int i=0; i<2; i++) { |
| 113 |
DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_AMIGA); |
| 114 |
chan[i].std.next(); |
| 115 |
if (chan[i].std.vol.had) { |
| 116 |
chan[i].envVol=chan[i].std.vol.val; |
| 117 |
if (ins->type==DIV_INS_AMIGA) chan[i].envVol/=32; |
| 118 |
else if (chan[i].envVol>2) chan[i].envVol=2; |
| 119 |
} |
| 120 |
if (NEW_ARP_STRAT) { |
| 121 |
chan[i].handleArp(); |
| 122 |
} else if (chan[i].std.arp.had) { |
| 123 |
if (!chan[i].inPorta) { |
| 124 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 125 |
} |
| 126 |
chan[i].freqChanged=true; |
| 127 |
} |
| 128 |
if (chan[i].useWave && chan[i].std.wave.had) { |
| 129 |
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) { |
| 130 |
chan[i].wave=chan[i].std.wave.val; |
| 131 |
chan[i].ws.changeWave1(chan[i].wave); |
| 132 |
if (!chan[i].keyOff) chan[i].keyOn=true; |
| 133 |
} |
| 134 |
} |
| 135 |
if (chan[i].useWave && chan[i].active) { |
| 136 |
if (chan[i].ws.tick()) { |
| 137 |
updateWave(i); |
| 138 |
} |
| 139 |
} |
| 140 |
if (chan[i].std.pitch.had) { |
| 141 |
if (chan[i].std.pitch.mode) { |
| 142 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 143 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 144 |
} else { |
| 145 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 146 |
} |
| 147 |
chan[i].freqChanged=true; |
| 148 |
} |
| 149 |
if (ins->type==DIV_INS_AMIGA) { |
| 150 |
if (chan[0].std.panL.had) { |
| 151 |
chan[0].pan=(chan[0].pan&~2)|(chan[0].std.panL.val>0?2:0); |
| 152 |
} |
| 153 |
if (chan[0].std.panR.had) { |
| 154 |
chan[0].pan=(chan[0].pan&~1)|(chan[0].std.panR.val>0?1:0); |
| 155 |
} |
| 156 |
} else { |
| 157 |
if (chan[i].std.panL.had) { |
| 158 |
chan[i].pan=chan[i].std.panL.val&3; |
| 159 |
} |
| 160 |
} |
| 161 |
if (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1) { |
| 162 |
chan[i].audPos=0; |
| 163 |
} |
| 164 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 165 |
double off=1.0; |
| 166 |
if (!chan[i].useWave && chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) { |
| 167 |
DivSample* s=parent->getSample(chan[i].sample); |
| 168 |
off=(s->centerRate>=1)?(parent->getCenterRate()/(double)s->centerRate):1.0; |
| 169 |
} |
| 170 |
chan[i].freq=off*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); |
| 171 |
|
| 172 |
// emulate prescaler rounding |
| 173 |
if (chan[i].freq<65536) { |
| 174 |
if (chan[i].freq<1) chan[i].freq=1; |
| 175 |
} else if (chan[i].freq<65536*64) { |
| 176 |
chan[i].freq=chan[i].freq&~63; |
| 177 |
} else if (chan[i].freq<65536*256) { |
| 178 |
chan[i].freq=chan[i].freq&~255; |
| 179 |
} else { |
| 180 |
chan[i].freq=chan[i].freq&~1024; |
| 181 |
if (chan[i].freq>65536*1024) chan[i].freq=65536*1024; |
| 182 |
} |
| 183 |
if (chan[i].keyOn) { |
| 184 |
if (!chan[i].std.vol.had) { |
| 185 |
chan[i].envVol=2; |
| 186 |
} |
| 187 |
chan[i].keyOn=false; |
| 188 |
} |
| 189 |
if (chan[i].keyOff) { |
| 190 |
chan[i].keyOff=false; |
| 191 |
} |
| 192 |
chan[i].freqChanged=false; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
int DivPlatformGBADMA::dispatch(DivCommand c) { |
| 198 |
switch (c.cmd) { |
| 199 |
case DIV_CMD_NOTE_ON: { |
| 200 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); |
| 201 |
if (ins->amiga.useWave) { |
| 202 |
chan[c.chan].useWave=true; |
| 203 |
chan[c.chan].audLen=ins->amiga.waveLen+1; |
| 204 |
wtMemCompo.entries[c.chan].end=wtMemCompo.entries[c.chan].begin+chan[c.chan].audLen; |
| 205 |
if (chan[c.chan].insChanged) { |
| 206 |
if (chan[c.chan].wave<0) { |
| 207 |
chan[c.chan].wave=0; |
| 208 |
chan[c.chan].ws.setWidth(chan[c.chan].audLen); |
| 209 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 210 |
} |
| 211 |
} |
| 212 |
} else { |
| 213 |
if (c.value!=DIV_NOTE_NULL) { |
| 214 |
chan[c.chan].sample=ins->amiga.getSample(c.value); |
| 215 |
c.value=ins->amiga.getFreq(c.value); |
| 216 |
} |
| 217 |
chan[c.chan].useWave=false; |
| 218 |
} |
| 219 |
if (c.value!=DIV_NOTE_NULL) { |
| 220 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 221 |
} |
| 222 |
if (chan[c.chan].useWave || chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) { |
| 223 |
chan[c.chan].sample=-1; |
| 224 |
} |
| 225 |
if (chan[c.chan].setPos) { |
| 226 |
chan[c.chan].setPos=false; |
| 227 |
} else { |
| 228 |
chan[c.chan].audPos=0; |
| 229 |
} |
| 230 |
chan[c.chan].audSub=0; |
| 231 |
chan[c.chan].audDat=0; |
| 232 |
chan[c.chan].dmaCount=0; |
| 233 |
if (c.value!=DIV_NOTE_NULL) { |
| 234 |
chan[c.chan].freqChanged=true; |
| 235 |
chan[c.chan].note=c.value; |
| 236 |
} |
| 237 |
chan[c.chan].active=true; |
| 238 |
chan[c.chan].keyOn=true; |
| 239 |
chan[c.chan].macroInit(ins); |
| 240 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 241 |
chan[c.chan].envVol=2; |
| 242 |
} |
| 243 |
if (chan[c.chan].useWave) { |
| 244 |
chan[c.chan].ws.init(ins,chan[c.chan].audLen,255,chan[c.chan].insChanged); |
| 245 |
} |
| 246 |
chan[c.chan].insChanged=false; |
| 247 |
break; |
| 248 |
} |
| 249 |
case DIV_CMD_NOTE_OFF: |
| 250 |
chan[c.chan].sample=-1; |
| 251 |
chan[c.chan].active=false; |
| 252 |
chan[c.chan].keyOff=true; |
| 253 |
chan[c.chan].macroInit(NULL); |
| 254 |
break; |
| 255 |
case DIV_CMD_NOTE_OFF_ENV: |
| 256 |
case DIV_CMD_ENV_RELEASE: |
| 257 |
chan[c.chan].std.release(); |
| 258 |
break; |
| 259 |
case DIV_CMD_INSTRUMENT: |
| 260 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 261 |
chan[c.chan].ins=c.value; |
| 262 |
chan[c.chan].insChanged=true; |
| 263 |
} |
| 264 |
break; |
| 265 |
case DIV_CMD_VOLUME: |
| 266 |
if (chan[c.chan].vol!=c.value) { |
| 267 |
chan[c.chan].vol=MIN(c.value,2); |
| 268 |
if (!chan[c.chan].std.vol.has) { |
| 269 |
chan[c.chan].envVol=2; |
| 270 |
} |
| 271 |
} |
| 272 |
break; |
| 273 |
case DIV_CMD_GET_VOLUME: |
| 274 |
return chan[c.chan].vol; |
| 275 |
break; |
| 276 |
case DIV_CMD_PANNING: |
| 277 |
chan[c.chan].pan=0; |
| 278 |
chan[c.chan].pan|=(c.value>0)?2:0; |
| 279 |
chan[c.chan].pan|=(c.value2>0)?1:0; |
| 280 |
break; |
| 281 |
case DIV_CMD_PITCH: |
| 282 |
chan[c.chan].pitch=c.value; |
| 283 |
chan[c.chan].freqChanged=true; |
| 284 |
break; |
| 285 |
case DIV_CMD_WAVE: |
| 286 |
if (!chan[c.chan].useWave) break; |
| 287 |
chan[c.chan].wave=c.value; |
| 288 |
chan[c.chan].keyOn=true; |
| 289 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 290 |
break; |
| 291 |
case DIV_CMD_NOTE_PORTA: { |
| 292 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); |
| 293 |
chan[c.chan].sample=ins->amiga.getSample(c.value2); |
| 294 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 295 |
bool return2=false; |
| 296 |
if (destFreq>chan[c.chan].baseFreq) { |
| 297 |
chan[c.chan].baseFreq+=c.value; |
| 298 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 299 |
chan[c.chan].baseFreq=destFreq; |
| 300 |
return2=true; |
| 301 |
} |
| 302 |
} else { |
| 303 |
chan[c.chan].baseFreq-=c.value; |
| 304 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 305 |
chan[c.chan].baseFreq=destFreq; |
| 306 |
return2=true; |
| 307 |
} |
| 308 |
} |
| 309 |
chan[c.chan].freqChanged=true; |
| 310 |
if (return2) { |
| 311 |
chan[c.chan].inPorta=false; |
| 312 |
return 2; |
| 313 |
} |
| 314 |
break; |
| 315 |
} |
| 316 |
case DIV_CMD_LEGATO: { |
| 317 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 318 |
chan[c.chan].freqChanged=true; |
| 319 |
chan[c.chan].note=c.value; |
| 320 |
break; |
| 321 |
} |
| 322 |
case DIV_CMD_PRE_PORTA: |
| 323 |
if (chan[c.chan].active && c.value2) { |
| 324 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA)); |
| 325 |
} |
| 326 |
chan[c.chan].inPorta=c.value; |
| 327 |
break; |
| 328 |
case DIV_CMD_SAMPLE_POS: |
| 329 |
if (chan[c.chan].useWave) break; |
| 330 |
chan[c.chan].audPos=c.value; |
| 331 |
chan[c.chan].setPos=true; |
| 332 |
break; |
| 333 |
case DIV_CMD_GET_VOLMAX: |
| 334 |
return 2; |
| 335 |
break; |
| 336 |
case DIV_CMD_MACRO_OFF: |
| 337 |
chan[c.chan].std.mask(c.value,true); |
| 338 |
break; |
| 339 |
case DIV_CMD_MACRO_ON: |
| 340 |
chan[c.chan].std.mask(c.value,false); |
| 341 |
break; |
| 342 |
case DIV_CMD_MACRO_RESTART: |
| 343 |
chan[c.chan].std.restart(c.value); |
| 344 |
break; |
| 345 |
default: |
| 346 |
break; |
| 347 |
} |
| 348 |
return 1; |
| 349 |
} |
| 350 |
|
| 351 |
void DivPlatformGBADMA::updateWave(int ch) { |
| 352 |
int addr=ch*256; |
| 353 |
for (unsigned int i=0; i<chan[ch].audLen; i++) { |
| 354 |
if (addr+i>=512) break; |
| 355 |
wtMem[addr+i]=(signed char)(chan[ch].ws.output[i]-128); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
void DivPlatformGBADMA::muteChannel(int ch, bool mute) { |
| 360 |
isMuted[ch]=mute; |
| 361 |
} |
| 362 |
|
| 363 |
void DivPlatformGBADMA::forceIns() { |
| 364 |
for (int i=0; i<2; i++) { |
| 365 |
chan[i].insChanged=true; |
| 366 |
chan[i].freqChanged=true; |
| 367 |
chan[i].audPos=0; |
| 368 |
chan[i].sample=-1; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
void* DivPlatformGBADMA::getChanState(int ch) { |
| 373 |
return &chan; |
| 374 |
} |
| 375 |
|
| 376 |
DivDispatchOscBuffer* DivPlatformGBADMA::getOscBuffer(int ch) { |
| 377 |
return oscBuf[ch]; |
| 378 |
} |
| 379 |
|
| 380 |
void DivPlatformGBADMA::reset() { |
| 381 |
for (int i=0; i<2; i++) { |
| 382 |
chan[i]=DivPlatformGBADMA::Channel(); |
| 383 |
chan[i].std.setEngine(parent); |
| 384 |
chan[i].ws.setEngine(parent); |
| 385 |
chan[i].ws.init(NULL,32,255); |
| 386 |
chan[i].audDat=0; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
int DivPlatformGBADMA::getOutputCount() { |
| 391 |
return 2; |
| 392 |
} |
| 393 |
|
| 394 |
DivMacroInt* DivPlatformGBADMA::getChanMacroInt(int ch) { |
| 395 |
return &chan[ch].std; |
| 396 |
} |
| 397 |
|
| 398 |
unsigned short DivPlatformGBADMA::getPan(int ch) { |
| 399 |
return ((chan[ch].pan&2)<<7)|(chan[ch].pan&1); |
| 400 |
} |
| 401 |
|
| 402 |
DivSamplePos DivPlatformGBADMA::getSamplePos(int ch) { |
| 403 |
if (ch>=2 || !chan[ch].active || |
| 404 |
chan[ch].sample<0 || chan[ch].sample>=parent->song.sampleLen) { |
| 405 |
return DivSamplePos(); |
| 406 |
} |
| 407 |
return DivSamplePos( |
| 408 |
chan[ch].sample, |
| 409 |
chan[ch].audPos, |
| 410 |
chipClock/chan[ch].freq |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
void DivPlatformGBADMA::notifyInsChange(int ins) { |
| 415 |
for (int i=0; i<2; i++) { |
| 416 |
if (chan[i].ins==ins) { |
| 417 |
chan[i].insChanged=true; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
void DivPlatformGBADMA::notifyWaveChange(int wave) { |
| 423 |
for (int i=0; i<2; i++) { |
| 424 |
if (chan[i].useWave && chan[i].wave==wave) { |
| 425 |
chan[i].ws.changeWave1(wave); |
| 426 |
updateWave(i); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
void DivPlatformGBADMA::notifyInsDeletion(void* ins) { |
| 432 |
for (int i=0; i<2; i++) { |
| 433 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
const void* DivPlatformGBADMA::getSampleMem(int index) { |
| 438 |
return index == 0 ? sampleMem : NULL; |
| 439 |
} |
| 440 |
|
| 441 |
size_t DivPlatformGBADMA::getSampleMemCapacity(int index) { |
| 442 |
return index == 0 ? 33554432 : 0; |
| 443 |
} |
| 444 |
|
| 445 |
size_t DivPlatformGBADMA::getSampleMemUsage(int index) { |
| 446 |
return index == 0 ? sampleMemLen : 0; |
| 447 |
} |
| 448 |
|
| 449 |
bool DivPlatformGBADMA::isSampleLoaded(int index, int sample) { |
| 450 |
if (index!=0) return false; |
| 451 |
if (sample<0 || sample>32767) return false; |
| 452 |
return sampleLoaded[sample]; |
| 453 |
} |
| 454 |
|
| 455 |
const DivMemoryComposition* DivPlatformGBADMA::getMemCompo(int index) { |
| 456 |
switch (index) { |
| 457 |
case 0: return &romMemCompo; |
| 458 |
case 1: return &wtMemCompo; |
| 459 |
} |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
|
| 463 |
void DivPlatformGBADMA::renderSamples(int sysID) { |
| 464 |
size_t maxPos=getSampleMemCapacity(); |
| 465 |
memset(sampleMem,0,maxPos); |
| 466 |
memset(sampleOff,0,32768*sizeof(unsigned int)); |
| 467 |
memset(sampleLoaded,0,32768*sizeof(bool)); |
| 468 |
romMemCompo.entries.clear(); |
| 469 |
romMemCompo.capacity=maxPos; |
| 470 |
|
| 471 |
size_t memPos=0; |
| 472 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 473 |
DivSample* s=parent->song.sample[i]; |
| 474 |
if (!s->renderOn[0][sysID]) { |
| 475 |
sampleOff[i]=0; |
| 476 |
continue; |
| 477 |
} |
| 478 |
int length=s->length8; |
| 479 |
int actualLength=MIN((int)(maxPos-memPos),length); |
| 480 |
if (actualLength>0) { |
| 481 |
sampleOff[i]=memPos; |
| 482 |
memcpy(&sampleMem[memPos],s->data8,actualLength); |
| 483 |
memPos+=actualLength; |
| 484 |
} |
| 485 |
if (actualLength<length) { |
| 486 |
logW("out of GBA DMA PCM memory for sample %d!",i); |
| 487 |
break; |
| 488 |
} |
| 489 |
sampleLoaded[i]=true; |
| 490 |
romMemCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"PCM",i,sampleOff[i],memPos)); |
| 491 |
// pad to multiple of 16 bytes |
| 492 |
memPos=(memPos+15)&~15; |
| 493 |
} |
| 494 |
sampleMemLen=memPos; |
| 495 |
romMemCompo.used=sampleMemLen; |
| 496 |
} |
| 497 |
|
| 498 |
void DivPlatformGBADMA::setFlags(const DivConfig& flags) { |
| 499 |
outDepth=flags.getInt("dacDepth",9); |
| 500 |
chipClock=1<<24; |
| 501 |
CHECK_CUSTOM_CLOCK; |
| 502 |
rate=chipClock>>outDepth; |
| 503 |
for (int i=0; i<2; i++) { |
| 504 |
oscBuf[i]->setRate(rate); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
int DivPlatformGBADMA::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 509 |
parent=p; |
| 510 |
dumpWrites=false; |
| 511 |
skipRegisterWrites=false; |
| 512 |
wtMemCompo=DivMemoryComposition(); |
| 513 |
wtMemCompo.name="Wavetable RAM"; |
| 514 |
wtMemCompo.used=256*2; |
| 515 |
wtMemCompo.capacity=256*2; |
| 516 |
wtMemCompo.memory=(unsigned char*)wtMem; |
| 517 |
wtMemCompo.waveformView=DIV_MEMORY_WAVE_8BIT_SIGNED; |
| 518 |
for (int i=0; i<2; i++) { |
| 519 |
isMuted[i]=false; |
| 520 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 521 |
wtMemCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_WAVE_RAM, fmt::sprintf("Channel %d",i),-1,i*256,i*256)); |
| 522 |
} |
| 523 |
sampleMem=new signed char[getSampleMemCapacity()]; |
| 524 |
sampleMemLen=0; |
| 525 |
romMemCompo=DivMemoryComposition(); |
| 526 |
romMemCompo.name="Sample ROM"; |
| 527 |
setFlags(flags); |
| 528 |
reset(); |
| 529 |
return 2; |
| 530 |
} |
| 531 |
|
| 532 |
void DivPlatformGBADMA::quit() { |
| 533 |
delete[] sampleMem; |
| 534 |
for (int i=0; i<2; i++) { |
| 535 |
delete oscBuf[i]; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
// initialization of important arrays |
| 540 |
DivPlatformGBADMA::DivPlatformGBADMA() { |
| 541 |
sampleOff=new unsigned int[32768]; |
| 542 |
sampleLoaded=new bool[32768]; |
| 543 |
} |
| 544 |
|
| 545 |
DivPlatformGBADMA::~DivPlatformGBADMA() { |
| 546 |
delete[] sampleOff; |
| 547 |
delete[] sampleLoaded; |
| 548 |
} |