From 1e98f0c4a10e9e8ace73b0ffacaa7f08f23f47ce Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 24 Feb 2022 03:57:45 -0500 Subject: [PATCH] sample system rewrite! **PLEASE READ** this commit mostly rewrites the sample system. as of now samples can be ADPCM, 8-bit, BRR or 16-bit or something... consider this VERY EXPERIMENTAL. if you find any issues REPORT THEM immediately. it's nearly 4am... --- .gitmodules | 3 + CMakeLists.txt | 6 + extern/adpcm | 1 + papers/format.md | 18 ++- src/engine/engine.cpp | 153 ++---------------- src/engine/engine.h | 7 +- src/engine/fileOps.cpp | 140 +++++++++++++---- src/engine/platform/amiga.cpp | 12 +- src/engine/platform/genesis.cpp | 24 +-- src/engine/platform/nes.cpp | 12 +- src/engine/platform/pce.cpp | 15 +- src/engine/platform/qsound.cpp | 10 +- src/engine/platform/segapcm.cpp | 47 +++--- src/engine/platform/ym2610.cpp | 6 +- src/engine/playback.cpp | 12 +- src/engine/sample.cpp | 271 +++++++++++++++++++++++++++++--- src/engine/sample.h | 85 ++++++---- src/engine/vgmOps.cpp | 94 ++++------- src/gui/gui.cpp | 19 +-- 19 files changed, 545 insertions(+), 390 deletions(-) create mode 160000 extern/adpcm diff --git a/.gitmodules b/.gitmodules index d27443da..435f43c7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "extern/fmt"] path = extern/fmt url = https://github.com/fmtlib/fmt.git +[submodule "extern/adpcm"] + path = extern/adpcm + url = https://github.com/superctr/adpcm diff --git a/CMakeLists.txt b/CMakeLists.txt index d4305e28..3b7a669a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -236,6 +236,12 @@ extern/SAASound/src/SAANoise.cpp extern/SAASound/src/SAASndC.cpp extern/SAASound/src/SAASound.cpp +extern/adpcm/bs_codec.c +extern/adpcm/oki_codec.c +extern/adpcm/yma_codec.c +extern/adpcm/ymb_codec.c +extern/adpcm/ymz_codec.c + extern/Nuked-OPN2/ym3438.c extern/opm/opm.c src/engine/platform/sound/sn76496.cpp diff --git a/extern/adpcm b/extern/adpcm new file mode 160000 index 00000000..ef7a2171 --- /dev/null +++ b/extern/adpcm @@ -0,0 +1 @@ +Subproject commit ef7a217154badc3b99978ac481b268c8aab67bd8 diff --git a/papers/format.md b/papers/format.md index ec2f11a9..a61bb626 100644 --- a/papers/format.md +++ b/papers/format.md @@ -429,14 +429,26 @@ size | description STR | sample name 4 | length 4 | rate - 2 | volume - 2 | pitch + 2 | volume (<58) or reserved + 2 | pitch (<58) or reserved 1 | depth + | - 0: ZX Spectrum overlay drum (1-bit) + | - 1: 1-bit NES DPCM (1-bit) + | - 4: QSound ADPCM + | - 5: ADPCM-A + | - 6: ADPCM-B + | - 7: X68000 ADPCM + | - 8: 8-bit PCM + | - 9: BRR (SNES) + | - 10: VOX + | - 16: 16-bit PCM 1 | reserved 2 | C-4 rate (>=32) or reserved 4 | loop point (>=19) or reserved | - -1 means no loop - 2?? | sample data (always 16-bit) + ??? | sample data + | - version<58 size is length*2 + | - version>=58 size is length # pattern diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 276dba6a..9dee6ecf 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -434,26 +434,6 @@ void DivEngine::notifyWaveChange(int wave) { isBusy.unlock(); } -// ADPCM code attribution: https://wiki.neogeodev.org/index.php?title=ADPCM_codecs - -static short adSteps[49]={ - 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, - 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, - 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, - 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, - 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552 -}; - -static int adStepSeek[16]={ - -1, -1, -1, -1, 2, 5, 7, 9, -1, -1, -1, -1, 2, 5, 7, 9 -}; - -static double samplePitches[11]={ - 0.1666666666, 0.2, 0.25, 0.333333333, 0.5, - 1, - 2, 3, 4, 5, 6 -}; - void DivEngine::renderSamplesP() { isBusy.lock(); renderSamples(); @@ -463,117 +443,12 @@ void DivEngine::renderSamplesP() { void DivEngine::renderSamples() { sPreview.sample=-1; sPreview.pos=0; - if (jediTable==NULL) { - jediTable=new int[16*49]; - for (int step=0; step<49; step++) { - for (int nib=0; nib<16; nib++) { - int value=(2*(nib&0x07)+1)*adSteps[step]/8; - jediTable[step*16+nib]=((nib&0x08)!=0)?-value:value; - } - } - } for (int i=0; irendLength!=0) { - delete[] s->rendData; - delete[] s->adpcmRendData; - } - s->rendLength=(double)s->length/samplePitches[s->pitch]; - if (s->rendLength==0) { - s->adpcmRendLength=0; - continue; - } - s->rendData=new short[s->rendLength]; - size_t adpcmLen=((s->rendLength>>1)+255)&0xffffff00; - if (adpcmLen>1048576) adpcmLen=1048576; - s->adpcmRendLength=adpcmLen; - s->adpcmRendData=new unsigned char[adpcmLen]; - memset(s->adpcmRendData,0,adpcmLen); - - // step 1: render to PCM - unsigned int k=0; - float mult=(float)(s->vol)/50.0f; - for (double j=0; jlength; j+=samplePitches[s->pitch]) { - if (k>=s->rendLength) { - break; - } - if (s->depth==8) { - float next=(float)(s->data[(unsigned int)j]-0x80)*mult; - s->rendData[k++]=fmin(fmax(next,-128),127); - } else { - float next=(float)s->data[(unsigned int)j]*mult; - s->rendData[k++]=fmin(fmax(next,-32768),32767); - } - } - - // step 2: render to ADPCM - int acc=0; - int decstep=0; - int diff=0; - int step=0; - int predsample=0; - int index=0; - int prevsample=0; - int previndex=0; - for (unsigned int j=0; jadpcmRendLength*2; j++) { - unsigned char encoded=0; - int tempstep=0; - - predsample=prevsample; - index=previndex; - step=adSteps[index]; - - short sample=(jrendLength)?((s->depth==16)?(s->rendData[j]>>4):(s->rendData[j]<<4)):0; - if (sample>0x7d0) sample=0x7d0; - if (sample<-0x7d0) sample=-0x7d0; - diff=sample-predsample; - if (diff>=0) { - encoded=0; - } else { - encoded=8; - diff=-diff; - } - - tempstep=step; - if (diff>=tempstep) { - encoded|=4; - diff-=tempstep; - } - tempstep>>=1; - if (diff>=tempstep) { - encoded|=2; - diff-=tempstep; - } - tempstep>>=1; - if (diff>=tempstep) encoded|=1; - - acc+=jediTable[decstep+encoded]; - /*if (acc>0x7ff || acc<-0x800) { - logW("clipping! %d\n",acc); - }*/ - acc&=0xfff; - if (acc&0x800) acc|=~0xfff; - decstep+=adStepSeek[encoded&7]*16; - if (decstep<0) decstep=0; - if (decstep>48*16) decstep=48*16; - predsample=(short)acc; - - index+=adStepSeek[encoded]; - if (index<0) index=0; - if (index>48) index=48; - - prevsample=predsample; - previndex=index; - - if (j&1) { - s->adpcmRendData[j>>1]|=encoded; - } else { - s->adpcmRendData[j>>1]=encoded<<4; - } - } + song.sample[i]->render(); } + /* // step 3: allocate ADPCM samples if (adpcmMem==NULL) adpcmMem=new unsigned char[16777216]; @@ -631,6 +506,7 @@ void DivEngine::renderSamples() { memPos+=length+16; } qsoundMemLen=memPos+256; + */ } void DivEngine::createNew() { @@ -1901,22 +1777,23 @@ bool DivEngine::addSampleFromFile(const char* path) { sample->name=sName; int index=0; - sample->length=si.frames; - sample->data=new short[si.frames]; - sample->depth=16; - sample->vol=50; - sample->pitch=5; + if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) { + sample->depth=8; + } else { + sample->depth=16; + } + sample->init(si.frames); for (int i=0; idata[index++]=averaged; + if (((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8)) { + sample->data8[index++]=averaged; + } else { + sample->data16[index++]=averaged; + } } delete[] buf; sample->rate=si.samplerate; diff --git a/src/engine/engine.h b/src/engine/engine.h index 6e7eefcf..96594a3c 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -37,8 +37,8 @@ warnings+=(String("\n")+x); \ } -#define DIV_VERSION "dev57" -#define DIV_ENGINE_VERSION 57 +#define DIV_VERSION "dev58" +#define DIV_ENGINE_VERSION 58 enum DivStatusView { DIV_STATUS_NOTHING=0, @@ -223,8 +223,6 @@ class DivEngine { size_t totalProcessed; - private: int* jediTable; - DivSystem systemFromFile(unsigned char val); unsigned char systemToFile(DivSystem val); int dispatchCmd(DivCommand c); @@ -680,7 +678,6 @@ class DivEngine { metroPos(0), metroAmp(0.0f), totalProcessed(0), - jediTable(NULL), oscBuf{NULL,NULL}, oscSize(1), adpcmMem(NULL), diff --git a/src/engine/fileOps.cpp b/src/engine/fileOps.cpp index 0d0be21b..dd3ff37e 100644 --- a/src/engine/fileOps.cpp +++ b/src/engine/fileOps.cpp @@ -42,6 +42,12 @@ struct InflateBlock { } }; +static double samplePitches[11]={ + 0.1666666666, 0.2, 0.25, 0.333333333, 0.5, + 1, + 2, 3, 4, 5, 6 +}; + bool DivEngine::loadDMF(unsigned char* file, size_t len) { SafeReader reader=SafeReader(file,len); warnings=""; @@ -607,9 +613,12 @@ bool DivEngine::loadDMF(unsigned char* file, size_t len) { } for (int i=0; ilength=reader.readI(); - if (sample->length<0) { - logE("invalid sample length %d. are we doing something wrong?\n",sample->length); + int length=reader.readI(); + int pitch=5; + int vol=50; + short* data; + if (length<0) { + logE("invalid sample length %d. are we doing something wrong?\n",length); lastError="file is corrupt or unreadable at samples"; delete[] file; return false; @@ -619,31 +628,57 @@ bool DivEngine::loadDMF(unsigned char* file, size_t len) { } else { sample->name=""; } - logD("%d name %s (%d)\n",i,sample->name.c_str(),sample->length); - if (ds.version<0x0b) { - sample->rate=22050; - sample->pitch=5; - sample->vol=50; - } else { + logD("%d name %s (%d)\n",i,sample->name.c_str(),length); + sample->rate=22050; + if (ds.version>=0x0b) { sample->rate=fileToDivRate(reader.readC()); - sample->pitch=reader.readC(); - sample->vol=reader.readC(); + pitch=reader.readC(); + vol=reader.readC(); } - logI("pitch and vol: %d %d\n",sample->pitch,sample->vol); if (ds.version>0x15) { sample->depth=reader.readC(); + if (sample->depth!=8 && sample->depth!=16) { + logW("%d: sample depth is wrong! (%d)\n",i,sample->depth); + sample->depth=16; + } } else { sample->depth=16; } - if (sample->length>0) { + if (length>0) { if (ds.version<0x0b) { - sample->data=new short[1+(sample->length/2)]; - reader.read(sample->data,sample->length); - sample->length/=2; + data=new short[1+(length/2)]; + reader.read(data,length); + length/=2; } else { - sample->data=new short[sample->length]; - reader.read(sample->data,sample->length*2); + data=new short[length]; + reader.read(data,length*2); } + + if (pitch!=5) { + logD("%d: scaling from %d...\n",i,pitch); + } + + // render data + if (!sample->init((double)length/samplePitches[pitch])) { + logE("%d: error while initializing sample!\n",i); + } + + unsigned int k=0; + float mult=(float)(vol)/50.0f; + for (double j=0; j=sample->samples) { + break; + } + if (sample->depth==8) { + float next=(float)(data[(unsigned int)j]-0x80)*mult; + sample->data8[k++]=fmin(fmax(next,-128),127); + } else { + float next=(float)data[(unsigned int)j]*mult; + sample->data16[k++]=fmin(fmax(next,-32768),32767); + } + } + + delete[] data; } ds.sample.push_back(sample); } @@ -996,6 +1031,9 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) { // read samples for (int i=0; iname=reader.readString(); - sample->length=reader.readI(); + sample->samples=reader.readI(); sample->rate=reader.readI(); - sample->vol=reader.readS(); - sample->pitch=reader.readS(); + if (ds.version<58) { + vol=reader.readS(); + pitch=reader.readS(); + } else { + reader.readI(); + } sample->depth=reader.readC(); // reserved @@ -1030,8 +1072,42 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) { reader.readI(); } - sample->data=new short[sample->length]; - reader.read(sample->data,2*sample->length); + if (ds.version>=58) { // modern sample + sample->init(sample->samples); + reader.read(sample->getCurBuf(),sample->getCurBufLen()); + } else { // legacy sample + int length=sample->samples; + short* data=new short[length]; + reader.read(data,2*length); + + if (pitch!=5) { + logD("%d: scaling from %d...\n",i,pitch); + } + + // render data + if (sample->depth!=8 && sample->depth!=16) { + logW("%d: sample depth is wrong! (%d)\n",i,sample->depth); + sample->depth=16; + } + sample->init(sample->samples); + + unsigned int k=0; + float mult=(float)(vol)/50.0f; + for (double j=0; j=sample->samples) { + break; + } + if (sample->depth==8) { + float next=(float)(data[(unsigned int)j]-0x80)*mult; + sample->data8[k++]=fmin(fmax(next,-128),127); + } else { + float next=(float)data[(unsigned int)j]*mult; + sample->data16[k++]=fmin(fmax(next,-32768),32767); + } + } + + delete[] data; + } ds.sample.push_back(sample); } @@ -1390,16 +1466,15 @@ SafeWriter* DivEngine::saveFur() { w->writeI(0); w->writeString(sample->name,false); - w->writeI(sample->length); + w->writeI(sample->samples); w->writeI(sample->rate); - w->writeS(sample->vol); - w->writeS(sample->pitch); + w->writeI(0); // reserved (for now) w->writeC(sample->depth); w->writeC(0); w->writeS(sample->centerRate); w->writeI(sample->loopStart); - w->write(sample->data,sample->length*2); + w->write(sample->getCurBuf(),sample->getCurBufLen()); } /// PATTERN @@ -1728,13 +1803,14 @@ SafeWriter* DivEngine::saveDMF(unsigned char version) { w->writeC(song.sample.size()); for (DivSample* i: song.sample) { - w->writeI(i->length); + w->writeI(i->samples); w->writeString(i->name,true); w->writeC(divToFileRate(i->rate)); - w->writeC(i->pitch); - w->writeC(i->vol); - w->writeC(i->depth); - w->write(i->data,2*i->length); + w->writeC(5); + w->writeC(50); + // i'm too lazy to deal with .dmf's weird way of storing 8-bit samples + w->writeC(16); + w->write(i->data16,i->length16); } return w; diff --git a/src/engine/platform/amiga.cpp b/src/engine/platform/amiga.cpp index 67cb8573..98a95a33 100644 --- a/src/engine/platform/amiga.cpp +++ b/src/engine/platform/amiga.cpp @@ -72,14 +72,10 @@ void DivPlatformAmiga::acquire(short* bufL, short* bufR, size_t start, size_t le chan[i].audSub-=AMIGA_DIVIDER; if (chan[i].audSub<0) { DivSample* s=parent->song.sample[chan[i].sample]; - if (s->rendLength>0) { - if (s->depth==8) { - chan[i].audDat=s->rendData[chan[i].audPos++]; - } else { - chan[i].audDat=s->rendData[chan[i].audPos++]>>8; - } - if (chan[i].audPos>=s->rendLength || chan[i].audPos>=131071) { - if (s->loopStart>=0 && s->loopStart<=(int)s->rendLength) { + if (s->samples>0) { + chan[i].audDat=s->data8[chan[i].audPos++]; + if (chan[i].audPos>=s->samples || chan[i].audPos>=131071) { + if (s->loopStart>=0 && s->loopStart<=(int)s->samples) { chan[i].audPos=s->loopStart; } else { chan[i].sample=-1; diff --git a/src/engine/platform/genesis.cpp b/src/engine/platform/genesis.cpp index 81172662..9314c7b6 100644 --- a/src/engine/platform/genesis.cpp +++ b/src/engine/platform/genesis.cpp @@ -90,16 +90,12 @@ void DivPlatformGenesis::acquire_nuked(short* bufL, short* bufR, size_t start, s dacPeriod-=6; if (dacPeriod<1) { DivSample* s=parent->song.sample[dacSample]; - if (s->rendLength>0) { + if (s->samples>0) { if (!isMuted[5]) { - if (s->depth==8) { - immWrite(0x2a,(unsigned char)s->rendData[dacPos]+0x80); - } else { - immWrite(0x2a,((unsigned short)s->rendData[dacPos]+0x8000)>>8); - } + immWrite(0x2a,(unsigned char)s->data8[dacPos]+0x80); } - if (++dacPos>=s->rendLength) { - if (s->loopStart>=0 && s->loopStart<=(int)s->rendLength) { + if (++dacPos>=s->samples) { + if (s->loopStart>=0 && s->loopStart<=(int)s->samples) { dacPos=s->loopStart; } else { dacSample=-1; @@ -164,16 +160,12 @@ void DivPlatformGenesis::acquire_ymfm(short* bufL, short* bufR, size_t start, si dacPeriod-=24; if (dacPeriod<1) { DivSample* s=parent->song.sample[dacSample]; - if (s->rendLength>0) { + if (s->samples>0) { if (!isMuted[5]) { - if (s->depth==8) { - immWrite(0x2a,(unsigned char)s->rendData[dacPos]+0x80); - } else { - immWrite(0x2a,((unsigned short)s->rendData[dacPos]+0x8000)>>8); - } + immWrite(0x2a,(unsigned char)s->data8[dacPos]+0x80); } - if (++dacPos>=s->rendLength) { - if (s->loopStart>=0 && s->loopStart<=(int)s->rendLength) { + if (++dacPos>=s->samples) { + if (s->loopStart>=0 && s->loopStart<=(int)s->samples) { dacPos=s->loopStart; } else { dacSample=-1; diff --git a/src/engine/platform/nes.cpp b/src/engine/platform/nes.cpp index 773a58f2..aacadd86 100644 --- a/src/engine/platform/nes.cpp +++ b/src/engine/platform/nes.cpp @@ -76,16 +76,12 @@ void DivPlatformNES::acquire(short* bufL, short* bufR, size_t start, size_t len) dacPeriod+=dacRate; if (dacPeriod>=rate) { DivSample* s=parent->song.sample[dacSample]; - if (s->rendLength>0) { + if (s->samples>0) { if (!isMuted[4]) { - if (s->depth==8) { - rWrite(0x4011,((unsigned char)s->rendData[dacPos]+0x80)>>1); - } else { - rWrite(0x4011,((unsigned short)s->rendData[dacPos]+0x8000)>>9); - } + rWrite(0x4011,((unsigned char)s->data8[dacPos]+0x80)>>1); } - if (++dacPos>=s->rendLength) { - if (s->loopStart>=0 && s->loopStart<=(int)s->rendLength) { + if (++dacPos>=s->samples) { + if (s->loopStart>=0 && s->loopStart<=(int)s->samples) { dacPos=s->loopStart; } else { dacSample=-1; diff --git a/src/engine/platform/pce.cpp b/src/engine/platform/pce.cpp index ede1bd66..0ffe5043 100644 --- a/src/engine/platform/pce.cpp +++ b/src/engine/platform/pce.cpp @@ -82,21 +82,16 @@ void DivPlatformPCE::acquire(short* bufL, short* bufR, size_t start, size_t len) chan[i].dacPeriod+=chan[i].dacRate; if (chan[i].dacPeriod>rate) { DivSample* s=parent->song.sample[chan[i].dacSample]; - if (s->rendLength<=0) { + if (s->samples<=0) { chan[i].dacSample=-1; continue; } chWrite(i,0x07,0); - if (s->depth==8) { - chWrite(i,0x04,0xdf); - chWrite(i,0x06,(((unsigned char)s->rendData[chan[i].dacPos]+0x80)>>3)); - } else { - chWrite(i,0x04,0xdf); - chWrite(i,0x06,(((unsigned short)s->rendData[chan[i].dacPos]+0x8000)>>11)); - } + chWrite(i,0x04,0xdf); + chWrite(i,0x06,(((unsigned char)s->data8[chan[i].dacPos]+0x80)>>3)); chan[i].dacPos++; - if (chan[i].dacPos>=s->rendLength) { - if (s->loopStart>=0 && s->loopStart<=(int)s->rendLength) { + if (chan[i].dacPos>=s->samples) { + if (s->loopStart>=0 && s->loopStart<=(int)s->samples) { chan[i].dacPos=s->loopStart; } else { chan[i].dacSample=-1; diff --git a/src/engine/platform/qsound.cpp b/src/engine/platform/qsound.cpp index 02067ffd..3364e476 100644 --- a/src/engine/platform/qsound.cpp +++ b/src/engine/platform/qsound.cpp @@ -296,18 +296,18 @@ void DivPlatformQSound::tick() { } else { off=(double)s->centerRate/24038.0/16.0; } - qsound_bank = 0x8000 | (s->rendOffQsound >> 16); - qsound_addr = s->rendOffQsound & 0xffff; + qsound_bank = 0x8000 | (s->offQSound >> 16); + qsound_addr = s->offQSound & 0xffff; - int length = s->length; + int length = s->samples; if (length > 65536 - 16) { length = 65536 - 16; } if (s->loopStart == -1 || s->loopStart >= length) { - qsound_end = s->rendOffQsound + length + 15; + qsound_end = s->offQSound + length + 15; qsound_loop = 15; } else { - qsound_end = s->rendOffQsound + length; + qsound_end = s->offQSound + length; qsound_loop = length - s->loopStart; } } diff --git a/src/engine/platform/segapcm.cpp b/src/engine/platform/segapcm.cpp index 6a5276c5..ba04c099 100644 --- a/src/engine/platform/segapcm.cpp +++ b/src/engine/platform/segapcm.cpp @@ -44,22 +44,17 @@ void DivPlatformSegaPCM::acquire(short* bufL, short* bufR, size_t start, size_t for (int i=0; i<16; i++) { if (chan[i].pcm.sample>=0 && chan[i].pcm.samplesong.sampleLen) { DivSample* s=parent->song.sample[chan[i].pcm.sample]; - if (s->rendLength<=0) { + if (s->samples<=0) { chan[i].pcm.sample=-1; continue; } if (!isMuted[i]) { - if (s->depth==8) { - pcmL+=(s->rendData[chan[i].pcm.pos>>8]*chan[i].chVolL); - pcmR+=(s->rendData[chan[i].pcm.pos>>8]*chan[i].chVolR); - } else { - pcmL+=(s->rendData[chan[i].pcm.pos>>8]*chan[i].chVolL)>>8; - pcmR+=(s->rendData[chan[i].pcm.pos>>8]*chan[i].chVolR)>>8; - } + pcmL+=(s->data8[chan[i].pcm.pos>>8]*chan[i].chVolL); + pcmR+=(s->data8[chan[i].pcm.pos>>8]*chan[i].chVolR); } chan[i].pcm.pos+=chan[i].pcm.freq; - if (chan[i].pcm.pos>=(s->rendLength<<8)) { - if (s->loopStart>=0 && s->loopStart<=(int)s->rendLength) { + if (chan[i].pcm.pos>=(s->samples<<8)) { + if (s->loopStart>=0 && s->loopStart<=(int)s->samples) { chan[i].pcm.pos=s->loopStart<<8; } else { chan[i].pcm.sample=-1; @@ -152,17 +147,17 @@ int DivPlatformSegaPCM::dispatch(DivCommand c) { chan[c.chan].furnacePCM=true; if (dumpWrites) { // Sega PCM writes DivSample* s=parent->song.sample[chan[c.chan].pcm.sample]; - addWrite(0x10086+(c.chan<<3),3+((s->rendOffP>>16)<<3)); - addWrite(0x10084+(c.chan<<3),(s->rendOffP)&0xff); - addWrite(0x10085+(c.chan<<3),(s->rendOffP>>8)&0xff); - addWrite(0x10006+(c.chan<<3),MIN(255,((s->rendOffP&0xffff)+s->rendLength-1)>>8)); - if (s->loopStart<0 || s->loopStart>=(int)s->rendLength) { - addWrite(0x10086+(c.chan<<3),2+((s->rendOffP>>16)<<3)); + addWrite(0x10086+(c.chan<<3),3+((s->offSegaPCM>>16)<<3)); + addWrite(0x10084+(c.chan<<3),(s->offSegaPCM)&0xff); + addWrite(0x10085+(c.chan<<3),(s->offSegaPCM>>8)&0xff); + addWrite(0x10006+(c.chan<<3),MIN(255,((s->offSegaPCM&0xffff)+s->length8-1)>>8)); + if (s->loopStart<0 || s->loopStart>=(int)s->length8) { + addWrite(0x10086+(c.chan<<3),2+((s->offSegaPCM>>16)<<3)); } else { - int loopPos=(s->rendOffP&0xffff)+s->loopStart+s->loopOffP; + int loopPos=(s->offSegaPCM&0xffff)+s->loopStart+s->loopOffP; addWrite(0x10004+(c.chan<<3),loopPos&0xff); addWrite(0x10005+(c.chan<<3),(loopPos>>8)&0xff); - addWrite(0x10086+(c.chan<<3),((s->rendOffP>>16)<<3)); + addWrite(0x10086+(c.chan<<3),((s->offSegaPCM>>16)<<3)); } } } else { @@ -182,17 +177,17 @@ int DivPlatformSegaPCM::dispatch(DivCommand c) { chan[c.chan].furnacePCM=false; if (dumpWrites) { // Sega PCM writes DivSample* s=parent->song.sample[chan[c.chan].pcm.sample]; - addWrite(0x10086+(c.chan<<3),3+((s->rendOffP>>16)<<3)); - addWrite(0x10084+(c.chan<<3),(s->rendOffP)&0xff); - addWrite(0x10085+(c.chan<<3),(s->rendOffP>>8)&0xff); - addWrite(0x10006+(c.chan<<3),MIN(255,((s->rendOffP&0xffff)+s->rendLength-1)>>8)); - if (s->loopStart<0 || s->loopStart>=(int)s->rendLength) { - addWrite(0x10086+(c.chan<<3),2+((s->rendOffP>>16)<<3)); + addWrite(0x10086+(c.chan<<3),3+((s->offSegaPCM>>16)<<3)); + addWrite(0x10084+(c.chan<<3),(s->offSegaPCM)&0xff); + addWrite(0x10085+(c.chan<<3),(s->offSegaPCM>>8)&0xff); + addWrite(0x10006+(c.chan<<3),MIN(255,((s->offSegaPCM&0xffff)+s->length8-1)>>8)); + if (s->loopStart<0 || s->loopStart>=(int)s->length8) { + addWrite(0x10086+(c.chan<<3),2+((s->offSegaPCM>>16)<<3)); } else { - int loopPos=(s->rendOffP&0xffff)+s->loopStart+s->loopOffP; + int loopPos=(s->offSegaPCM&0xffff)+s->loopStart+s->loopOffP; addWrite(0x10004+(c.chan<<3),loopPos&0xff); addWrite(0x10005+(c.chan<<3),(loopPos>>8)&0xff); - addWrite(0x10086+(c.chan<<3),((s->rendOffP>>16)<<3)); + addWrite(0x10086+(c.chan<<3),((s->offSegaPCM>>16)<<3)); } addWrite(0x10007+(c.chan<<3),chan[c.chan].pcm.freq); } diff --git a/src/engine/platform/ym2610.cpp b/src/engine/platform/ym2610.cpp index 378a7b9c..eb2b43f9 100644 --- a/src/engine/platform/ym2610.cpp +++ b/src/engine/platform/ym2610.cpp @@ -437,9 +437,9 @@ int DivPlatformYM2610::dispatch(DivCommand c) { break; } DivSample* s=parent->song.sample[12*sampleBank+c.value%12]; - immWrite(0x110+c.chan-7,(s->rendOff>>8)&0xff); - immWrite(0x118+c.chan-7,s->rendOff>>16); - int end=s->rendOff+s->adpcmRendLength-1; + immWrite(0x110+c.chan-7,(s->offA>>8)&0xff); + immWrite(0x118+c.chan-7,s->offA>>16); + int end=s->offA+s->lengthA-1; immWrite(0x120+c.chan-7,(end>>8)&0xff); immWrite(0x128+c.chan-7,end>>16); immWrite(0x108+(c.chan-7),isMuted[c.chan]?0:((chan[c.chan].pan<<6)|chan[c.chan].vol)); diff --git a/src/engine/playback.cpp b/src/engine/playback.cpp index e5a4d920..6c825cb7 100644 --- a/src/engine/playback.cpp +++ b/src/engine/playback.cpp @@ -1132,24 +1132,24 @@ void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsi DivSample* s=song.sample[sPreview.sample]; for (size_t i=0; i=s->rendLength) { + if (sPreview.pos>=s->samples) { samp_temp=0; } else { - samp_temp=s->rendData[sPreview.pos++]; + samp_temp=s->data16[sPreview.pos++]; } if (s->depth==8) samp_temp<<=8; blip_add_delta(samp_bb,i,samp_temp-samp_prevSample); samp_prevSample=samp_temp; - if (sPreview.pos>=s->rendLength) { - if (s->loopStart>=0 && s->loopStart<(int)s->rendLength) { + if (sPreview.pos>=s->samples) { + if (s->loopStart>=0 && s->loopStart<(int)s->samples) { sPreview.pos=s->loopStart; } } } - if (sPreview.pos>=s->rendLength) { - if (s->loopStart>=0 && s->loopStart<(int)s->rendLength) { + if (sPreview.pos>=s->samples) { + if (s->loopStart>=0 && s->loopStart<(int)s->samples) { sPreview.pos=s->loopStart; } else { sPreview.sample=-1; diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index b44f5ba0..ec0ffad5 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -23,20 +23,24 @@ #include #include +extern "C" { +#include "../../extern/adpcm/bs_codec.h" +#include "../../extern/adpcm/oki_codec.h" +#include "../../extern/adpcm/yma_codec.h" +#include "../../extern/adpcm/ymb_codec.h" +#include "../../extern/adpcm/ymz_codec.h" +} + bool DivSample::save(const char* path) { SNDFILE* f; SF_INFO si; memset(&si,0,sizeof(SF_INFO)); - if (length<1) return false; + if (length16<1) return false; si.channels=1; si.samplerate=rate; - if (depth==16) { - si.format=SF_FORMAT_PCM_16|SF_FORMAT_WAV; - } else { - si.format=SF_FORMAT_PCM_U8|SF_FORMAT_WAV; - } + si.format=SF_FORMAT_PCM_16|SF_FORMAT_WAV; f=sf_open(path,SFM_WRITE,&si); @@ -58,28 +62,253 @@ bool DivSample::save(const char* path) { inst.loop_count = 1; inst.loops[0].mode = SF_LOOP_FORWARD; inst.loops[0].start = loopStart; - inst.loops[0].end = length; + inst.loops[0].end = samples; } sf_command(f, SFC_SET_INSTRUMENT, &inst, sizeof(inst)); - if (depth==16) { - sf_writef_short(f,data,length); - } else { - short* cbuf=new short[length]; - for (int i=0; i>3]>>(i&7))&1)?0x7fff:-0x7fff; + } + break; + case 1: { // DPCM + int accum=0; + for (unsigned int i=0; i>3]>>(i&7))&1)?1:-1; + if (accum>63) accum=63; + if (accum<-64) accum=-64; + data16[i]=accum*512; + } + break; + } + case 4: // QSound ADPCM + bs_decode(dataQSoundA,data16,samples); + break; + case 5: // ADPCM-A + yma_decode(dataA,data16,samples); + break; + case 6: // ADPCM-B + ymb_decode(dataB,data16,samples); + break; + case 7: // X6800 ADPCM + oki6258_decode(dataX68,data16,samples); + break; + case 8: // 8-bit PCM + for (unsigned int i=0; i0) { + data1[i>>3]|=1<<(i&7); + } + } + } + if (depth!=1) { // DPCM + if (!initInternal(1,samples)) return; + int accum=63; + for (unsigned int i=0; i>9; + if (next>accum) { + dataDPCM[i>>3]|=1<<(i&7); + accum++; + } else { + accum--; + } + if (accum<0) accum=0; + if (accum>127) accum=127; + } + } + if (depth!=4) { // QSound ADPCM + if (!initInternal(4,samples)) return; + bs_encode(data16,dataQSoundA,samples); + } + if (depth!=5) { // ADPCM-A + if (!initInternal(5,samples)) return; + yma_encode(data16,dataA,samples); + } + if (depth!=6) { // ADPCM-B + if (!initInternal(6,samples)) return; + ymb_encode(data16,dataB,samples); + } + if (depth!=7) { // X68000 ADPCM + if (!initInternal(7,samples)) return; + oki6258_encode(data16,dataX68,samples); + } + if (depth!=8) { // 8-bit PCM + if (!initInternal(8,samples)) return; + for (unsigned int i=0; i>8; + } + } + // TODO: BRR! + if (depth!=10) { // VOX + if (!initInternal(10,samples)) return; + oki_encode(data16,dataVOX,samples); + } +} + +void* DivSample::getCurBuf() { + switch (depth) { + case 0: + return data1; + case 1: + return dataDPCM; + case 4: + return dataQSoundA; + case 5: + return dataA; + case 6: + return dataB; + case 7: + return dataX68; + case 8: + return data8; + case 9: + return dataBRR; + case 10: + return dataVOX; + case 16: + return data16; + } + return NULL; +} + +unsigned int DivSample::getCurBufLen() { + switch (depth) { + case 0: + return length1; + case 1: + return lengthDPCM; + case 4: + return lengthQSoundA; + case 5: + return lengthA; + case 6: + return lengthB; + case 7: + return lengthX68; + case 8: + return length8; + case 9: + return lengthBRR; + case 10: + return lengthVOX; + case 16: + return length16; + } + return 0; +} + +DivSample::~DivSample() { + if (data8) delete[] data8; + if (data16) delete[] data16; + if (data1) delete[] data1; + if (dataDPCM) delete[] dataDPCM; + if (dataQSoundA) delete[] dataQSoundA; + if (dataA) delete[] dataA; + if (dataB) delete[] dataB; + if (dataX68) delete[] dataX68; + if (dataBRR) delete[] dataBRR; + if (dataVOX) delete[] dataVOX; } diff --git a/src/engine/sample.h b/src/engine/sample.h index 713c8af1..2915f2ce 100644 --- a/src/engine/sample.h +++ b/src/engine/sample.h @@ -21,12 +21,11 @@ struct DivSample { String name; - int length, rate, centerRate, loopStart, loopOffP; - signed char vol, pitch; + int rate, centerRate, loopStart, loopOffP; // valid values are: // - 0: ZX Spectrum overlay drum (1-bit) // - 1: 1-bit NES DPCM (1-bit) - // - 4: QSound ADPCM () + // - 4: QSound ADPCM // - 5: ADPCM-A // - 6: ADPCM-B // - 7: X68000 ADPCM @@ -36,45 +35,69 @@ struct DivSample { // - 16: 16-bit PCM unsigned char depth; - // TODO: drop - short* data; - // these are the new data structures. - signed char* data8; - short* data16; - unsigned char* data1; - unsigned char* dataDPCM; - unsigned char* dataQSound; - unsigned char* dataA; - unsigned char* dataB; - unsigned char* dataX68; - unsigned char* dataBRR; - unsigned char* dataVOX; + signed char* data8; // 8 + short* data16; // 16 + unsigned char* data1; // 0 + unsigned char* dataDPCM; // 1 + unsigned char* dataQSoundA; // 4 + unsigned char* dataA; // 5 + unsigned char* dataB; // 6 + unsigned char* dataX68; // 7 + unsigned char* dataBRR; // 9 + unsigned char* dataVOX; // 10 + unsigned int length8, length16, length1, lengthDPCM, lengthQSoundA, lengthA, lengthB, lengthX68, lengthBRR, lengthVOX; + unsigned int off8, off16, off1, offDPCM, offQSoundA, offA, offB, offX68, offBRR, offVOX; + unsigned int offSegaPCM, offQSound; - unsigned int rendLength, adpcmRendLength, rendOff, rendOffP, rendOffContiguous, rendOffQsound; - short* rendData; - unsigned char* adpcmRendData; + unsigned int samples; bool save(const char* path); + bool initInternal(unsigned char d, int count); + bool init(unsigned int count); + void render(); + void* getCurBuf(); + unsigned int getCurBufLen(); DivSample(): name(""), - length(0), rate(32000), centerRate(8363), loopStart(-1), loopOffP(0), - vol(0), - pitch(0), depth(16), - data(NULL), - rendLength(0), - adpcmRendLength(0), - rendOff(0), - rendOffP(0), - rendOffContiguous(0), - rendOffQsound(0), - rendData(NULL), - adpcmRendData(NULL) {} + data8(NULL), + data16(NULL), + data1(NULL), + dataDPCM(NULL), + dataQSoundA(NULL), + dataA(NULL), + dataB(NULL), + dataX68(NULL), + dataBRR(NULL), + dataVOX(NULL), + length8(0), + length16(0), + length1(0), + lengthDPCM(0), + lengthQSoundA(0), + lengthA(0), + lengthB(0), + lengthX68(0), + lengthBRR(0), + lengthVOX(0), + off8(0), + off16(0), + off1(0), + offDPCM(0), + offQSoundA(0), + offA(0), + offB(0), + offX68(0), + offBRR(0), + offVOX(0), + offSegaPCM(0), + offQSound(0), + samples(0) {} ~DivSample(); }; diff --git a/src/engine/vgmOps.cpp b/src/engine/vgmOps.cpp index 0d5eb521..93a2bcf4 100644 --- a/src/engine/vgmOps.cpp +++ b/src/engine/vgmOps.cpp @@ -310,7 +310,7 @@ void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write w->writeS(write.val); // sample number w->writeC((sample->loopStart==0)); // flags if (sample->loopStart>0) { - loopTimer[streamID]=sample->rendLength; + loopTimer[streamID]=sample->length8; loopSample[streamID]=write.val; } } @@ -848,8 +848,8 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop) { for (int i=0; irendOffContiguous=sampleSeek; - sampleSeek+=sample->rendLength; + sample->off8=sampleSeek; + sampleSeek+=sample->length8; } if (writeDACSamples) for (int i=0; iwriteC(0x67); w->writeC(0x66); w->writeC(0); - w->writeI(sample->rendLength); - if (sample->depth==8) { - for (unsigned int j=0; jrendLength; j++) { - w->writeC((unsigned char)sample->rendData[j]+0x80); - } - } else { - for (unsigned int j=0; jrendLength; j++) { - w->writeC(((unsigned short)sample->rendData[j]+0x8000)>>8); - } + w->writeI(sample->length8); + for (unsigned int j=0; jlength8; j++) { + w->writeC((unsigned char)sample->data8[j]+0x80); } } @@ -874,15 +868,9 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop) { w->writeC(0x67); w->writeC(0x66); w->writeC(7); - w->writeI(sample->rendLength); - if (sample->depth==8) { - for (unsigned int j=0; jrendLength; j++) { - w->writeC(((unsigned char)sample->rendData[j]+0x80)>>1); - } - } else { - for (unsigned int j=0; jrendLength; j++) { - w->writeC(((unsigned short)sample->rendData[j]+0x8000)>>9); - } + w->writeI(sample->length8); + for (unsigned int j=0; jlength8; j++) { + w->writeC(((unsigned char)sample->data8[j]+0x80)>>1); } } @@ -891,15 +879,9 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop) { w->writeC(0x67); w->writeC(0x66); w->writeC(5); - w->writeI(sample->rendLength); - if (sample->depth==8) { - for (unsigned int j=0; jrendLength; j++) { - w->writeC(((unsigned char)sample->rendData[j]+0x80)>>3); - } - } else { - for (unsigned int j=0; jrendLength; j++) { - w->writeC(((unsigned short)sample->rendData[j]+0x8000)>>11); - } + w->writeI(sample->length8); + for (unsigned int j=0; jlength8; j++) { + w->writeC(((unsigned char)sample->data8[j]+0x80)>>3); } } @@ -909,47 +891,29 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop) { size_t memPos=0; for (int i=0; irendLength)&0xff0000)) { + if ((memPos&0xff0000)!=((memPos+sample->length8)&0xff0000)) { memPos=(memPos+0xffff)&0xff0000; } if (memPos>=16777216) break; - sample->rendOffP=memPos; - unsigned int alignedSize=(sample->rendLength+0xff)&(~0xff); + sample->offSegaPCM=memPos; + unsigned int alignedSize=(sample->length8+0xff)&(~0xff); unsigned int readPos=0; if (alignedSize>65536) alignedSize=65536; - if (sample->depth==8) { - for (unsigned int j=0; j=sample->rendLength) { - if (sample->loopStart>=0 && sample->loopStart<(int)sample->rendLength) { - readPos=sample->loopStart; - pcmMem[memPos++]=((unsigned char)sample->rendData[readPos]+0x80); - } else { - pcmMem[memPos++]=0x80; - } + for (unsigned int j=0; j=sample->length8) { + if (sample->loopStart>=0 && sample->loopStart<(int)sample->length8) { + readPos=sample->loopStart; + pcmMem[memPos++]=((unsigned char)sample->data8[readPos]+0x80); } else { - pcmMem[memPos++]=((unsigned char)sample->rendData[readPos]+0x80); + pcmMem[memPos++]=0x80; } - readPos++; - if (memPos>=16777216) break; + } else { + pcmMem[memPos++]=((unsigned char)sample->data8[readPos]+0x80); } - sample->loopOffP=readPos-sample->loopStart; - } else { - for (unsigned int j=0; j=sample->rendLength) { - if (sample->loopStart>=0 && sample->loopStart<(int)sample->rendLength) { - readPos=sample->loopStart; - pcmMem[memPos++]=(((unsigned short)sample->rendData[readPos]+0x8000)>>8); - } else { - pcmMem[memPos++]=0x80; - } - } else { - pcmMem[memPos++]=(((unsigned short)sample->rendData[readPos]+0x8000)>>8); - } - readPos++; - if (memPos>=16777216) break; - } - sample->loopOffP=readPos-sample->loopStart; + readPos++; + if (memPos>=16777216) break; } + sample->loopOffP=readPos-sample->loopStart; if (memPos>=16777216) break; } @@ -1137,12 +1101,12 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop) { if (loopSample[nextToTouch]loopStart<(int)sample->rendLength) { + if (sample->loopStart<(int)sample->length8) { w->writeC(0x93); w->writeC(nextToTouch); - w->writeI(sample->rendOffContiguous+sample->loopStart); + w->writeI(sample->off8+sample->loopStart); w->writeC(0x81); - w->writeI(sample->rendLength-sample->loopStart); + w->writeI(sample->length8-sample->loopStart); } } loopSample[nextToTouch]=-1; diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index d96d525b..ee8d5a95 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -1266,7 +1266,8 @@ void FurnaceGUI::drawSampleEdit() { } else { DivSample* sample=e->song.sample[curSample]; ImGui::InputText("Name",&sample->name); - ImGui::Text("Length: %d",sample->length); + ImGui::Text("Length: %d",sample->samples); + ImGui::Text("Type: %d-bit",sample->depth); if (ImGui::InputInt("Rate (Hz)",&sample->rate,10,200)) { if (sample->rate<100) sample->rate=100; if (sample->rate>96000) sample->rate=96000; @@ -1287,19 +1288,11 @@ void FurnaceGUI::drawSampleEdit() { if (doLoop) { ImGui::SameLine(); if (ImGui::InputInt("##LoopPosition",&sample->loopStart,1,10)) { - if (sample->loopStart<0 || sample->loopStart>=sample->length) { + if (sample->loopStart<0 || sample->loopStart>=(int)sample->samples) { sample->loopStart=0; } } } - if (ImGui::SliderScalar("Volume",ImGuiDataType_S8,&sample->vol,&_ZERO,&_ONE_HUNDRED,fmt::sprintf("%d%%%%",sample->vol*2).c_str())) { - if (sample->vol<0) sample->vol=0; - if (sample->vol>100) sample->vol=100; - } - if (ImGui::SliderScalar("Pitch",ImGuiDataType_S8,&sample->pitch,&_ZERO,&_TEN,pitchLabel[sample->pitch])) { - if (sample->pitch<0) sample->pitch=0; - if (sample->pitch>10) sample->pitch=10; - } if (ImGui::Button("Apply")) { e->renderSamplesP(); } @@ -1321,15 +1314,15 @@ void FurnaceGUI::drawSampleEdit() { ImGui::Text("- sample loop start will be aligned to the nearest even sample on Amiga"); } } - if (sample->length&1) { + if (sample->samples&1) { considerations=true; ImGui::Text("- sample length will be aligned to the nearest even sample on Amiga"); } - if (sample->length>65535) { + if (sample->samples>65535) { considerations=true; ImGui::Text("- maximum sample length on Sega PCM is 65536 samples"); } - if (sample->length>2097151) { + if (sample->samples>2097151) { considerations=true; ImGui::Text("- maximum sample length on Neo Geo ADPCM is 2097152 samples"); }