Merge branch 'master' into master

This commit is contained in:
tildearrow 2024-09-05 18:13:46 -05:00
commit eb95024fb9
321 changed files with 78416 additions and 69816 deletions

View file

@ -215,6 +215,10 @@ bool DivCSPlayer::tick() {
case DIV_CMD_HINT_VOL_SLIDE:
arg0=(short)stream.readS();
break;
case DIV_CMD_HINT_VOL_SLIDE_TARGET:
arg0=(short)stream.readS();
arg1=(short)stream.readS();
break;
case DIV_CMD_HINT_LEGATO:
arg0=(unsigned char)stream.readC();
if (arg0==0xff) {
@ -321,6 +325,11 @@ bool DivCSPlayer::tick() {
break;
case DIV_CMD_HINT_VOL_SLIDE:
chan[i].volSpeed=arg0;
chan[i].volSpeedTarget=-1;
break;
case DIV_CMD_HINT_VOL_SLIDE_TARGET:
chan[i].volSpeed=arg0;
chan[i].volSpeedTarget=arg0==0 ? -1 : arg1;
break;
case DIV_CMD_HINT_PITCH:
chan[i].pitch=arg0;
@ -356,13 +365,29 @@ bool DivCSPlayer::tick() {
if (sendVolume || chan[i].volSpeed!=0) {
chan[i].volume+=chan[i].volSpeed;
if (chan[i].volSpeedTarget!=-1) {
bool atTarget=false;
if (chan[i].volSpeed>0) {
atTarget=(chan[i].volume>=chan[i].volSpeedTarget);
} else if (chan[i].volSpeed<0) {
atTarget=(chan[i].volume<=chan[i].volSpeedTarget);
} else {
atTarget=true;
chan[i].volSpeedTarget=chan[i].volume;
}
if (atTarget) {
chan[i].volume=chan[i].volSpeedTarget;
chan[i].volSpeed=0;
chan[i].volSpeedTarget=-1;
}
}
if (chan[i].volume<0) {
chan[i].volume=0;
}
if (chan[i].volume>chan[i].volMax) {
chan[i].volume=chan[i].volMax;
}
e->dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
}

View file

@ -34,7 +34,7 @@ struct DivCSChannelState {
int lastWaitLen;
int note, pitch;
int volume, volMax, volSpeed;
int volume, volMax, volSpeed, volSpeedTarget;
int vibratoDepth, vibratoRate, vibratoPos;
int portaTarget, portaSpeed;
unsigned char arp, arpStage, arpTicks;
@ -56,6 +56,7 @@ struct DivCSChannelState {
volume(0x7f00),
volMax(0),
volSpeed(0),
volSpeedTarget(-1),
vibratoDepth(0),
vibratoRate(0),
vibratoPos(0),

View file

@ -59,6 +59,7 @@ void writePackedCommandValues(SafeWriter* w, const DivCommand& c) {
case DIV_CMD_HINT_VOLUME:
case DIV_CMD_HINT_PORTA:
case DIV_CMD_HINT_VOL_SLIDE:
case DIV_CMD_HINT_VOL_SLIDE_TARGET:
case DIV_CMD_HINT_LEGATO:
w->writeC((unsigned char)c.cmd+0xb4);
break;
@ -100,6 +101,10 @@ void writePackedCommandValues(SafeWriter* w, const DivCommand& c) {
case DIV_CMD_HINT_VOL_SLIDE:
w->writeS(c.value);
break;
case DIV_CMD_HINT_VOL_SLIDE_TARGET:
w->writeS(c.value);
w->writeS(c.value2);
break;
case DIV_CMD_SAMPLE_MODE:
case DIV_CMD_SAMPLE_FREQ:
case DIV_CMD_SAMPLE_BANK:

View file

@ -67,6 +67,7 @@ enum DivDispatchCmds {
DIV_CMD_HINT_ARPEGGIO, // (note1, note2)
DIV_CMD_HINT_VOLUME, // (vol)
DIV_CMD_HINT_VOL_SLIDE, // (amount, oneTick)
DIV_CMD_HINT_VOL_SLIDE_TARGET, // (amount, target)
DIV_CMD_HINT_PORTA, // (target, speed)
DIV_CMD_HINT_LEGATO, // (note)
@ -265,6 +266,8 @@ enum DivDispatchCmds {
DIV_CMD_FDS_MOD_AUTO,
DIV_CMD_FM_OPMASK, // (mask)
DIV_CMD_MAX
};

View file

@ -98,6 +98,10 @@ const char* DivEngine::getEffectDesc(unsigned char effect, int chan, bool notNul
break;
case 0xc0: case 0xc1: case 0xc2: case 0xc3:
return _("Cxxx: Set tick rate (hz)");
case 0xd3:
return _("D3xx: Volume portamento");
case 0xd4:
return _("D4xx: Volume portamento (fast)");
case 0xdc:
return _("DCxx: Delayed mute");
case 0xe0:
@ -133,9 +137,9 @@ const char* DivEngine::getEffectDesc(unsigned char effect, int chan, bool notNul
case 0xf0:
return _("F0xx: Set tick rate (bpm)");
case 0xf1:
return _("F1xx: Single tick note slide up");
return _("F1xx: Single tick pitch up");
case 0xf2:
return _("F2xx: Single tick note slide down");
return _("F2xx: Single tick pitch down");
case 0xf3:
return _("F3xx: Fine volume slide up");
case 0xf4:
@ -147,9 +151,9 @@ const char* DivEngine::getEffectDesc(unsigned char effect, int chan, bool notNul
case 0xf7:
return _("F7xx: Restart macro (see manual)");
case 0xf8:
return _("F8xx: Single tick volume slide up");
return _("F8xx: Single tick volume up");
case 0xf9:
return _("F9xx: Single tick volume slide down");
return _("F9xx: Single tick volume down");
case 0xfa:
return _("FAxx: Fast volume slide (0y: down; x0: up)");
case 0xfc:
@ -199,6 +203,12 @@ void DivEngine::walkSong(int& loopOrder, int& loopRow, int& loopEnd) {
}
}
void DivEngine::findSongLength(int loopOrder, int loopRow, double fadeoutLen, int& rowsForFadeout, bool& hasFFxx, std::vector<int>& orders, int& length) {
if (curSubSong!=NULL) {
curSubSong->findLength(loopOrder,loopRow,fadeoutLen,rowsForFadeout,hasFFxx,orders,song.grooves,length,chans,song.jumpTreatment,song.ignoreJumpAtEnd);
}
}
#define EXPORT_BUFSIZE 2048
double DivEngine::benchmarkPlayback() {
@ -510,6 +520,15 @@ void DivEngine::initSongWithDesc(const char* description, bool inBase64, bool ol
if (song.subsong[0]->hz<1.0) song.subsong[0]->hz=1.0;
if (song.subsong[0]->hz>999.0) song.subsong[0]->hz=999.0;
curChanMask=c.getIntList("chanMask",{});
for (unsigned char i:curChanMask) {
int j=i-1;
if (j<0) j=0;
if (j>DIV_MAX_CHANS) j=DIV_MAX_CHANS-1;
curSubSong->chanShow[j]=false;
curSubSong->chanShowChanOsc[j]=false;
}
song.author=getConfString("defaultAuthorName","");
}
@ -744,6 +763,13 @@ int DivEngine::addSubSong() {
BUSY_BEGIN;
saveLock.lock();
song.subsong.push_back(new DivSubSong);
for (unsigned char i:curChanMask) {
int j=i-1;
if (j<0) j=0;
if (j>DIV_MAX_CHANS) j=DIV_MAX_CHANS-1;
song.subsong.back()->chanShow[j]=false;
song.subsong.back()->chanShowChanOsc[j]=false;
}
saveLock.unlock();
BUSY_END;
return song.subsong.size()-1;
@ -3589,6 +3615,12 @@ void DivEngine::synchronized(const std::function<void()>& what) {
BUSY_END;
}
void DivEngine::synchronizedSoft(const std::function<void()>& what) {
BUSY_BEGIN_SOFT;
what();
BUSY_END;
}
void DivEngine::lockSave(const std::function<void()>& what) {
saveLock.lock();
what();
@ -3916,6 +3948,9 @@ bool DivEngine::preInit(bool noSafeMode) {
// register systems
if (!systemsRegistered) registerSystems();
// register ROM exports
if (!romExportsRegistered) registerROMExports();
// TODO: re-enable with a better approach
// see issue #1581
/*

View file

@ -54,8 +54,8 @@ class DivWorkPool;
#define DIV_UNSTABLE
#define DIV_VERSION "Import Test"
#define DIV_ENGINE_VERSION 216
#define DIV_VERSION "dev220"
#define DIV_ENGINE_VERSION 220
// for imports
#define DIV_VERSION_MOD 0xff01
#define DIV_VERSION_FC 0xff02
@ -133,7 +133,7 @@ struct DivAudioExportOptions {
struct DivChannelState {
std::vector<DivDelayedCommand> delayed;
int note, oldNote, lastIns, pitch, portaSpeed, portaNote;
int volume, volSpeed, cut, volCut, legatoDelay, legatoTarget, rowDelay, volMax;
int volume, volSpeed, volSpeedTarget, cut, volCut, legatoDelay, legatoTarget, rowDelay, volMax;
int delayOrder, delayRow, retrigSpeed, retrigTick;
int vibratoDepth, vibratoRate, vibratoPos, vibratoPosGiant, vibratoShape, vibratoFine;
int tremoloDepth, tremoloRate, tremoloPos;
@ -157,6 +157,7 @@ struct DivChannelState {
portaNote(-1),
volume(0x7f00),
volSpeed(0),
volSpeedTarget(-1),
cut(-1),
volCut(-1),
legatoDelay(-1),
@ -465,6 +466,7 @@ class DivEngine {
bool midiIsDirectProgram;
bool lowLatency;
bool systemsRegistered;
bool romExportsRegistered;
bool hasLoadedSomething;
bool midiOutClock;
bool midiOutTime;
@ -473,7 +475,7 @@ class DivEngine {
int midiOutTimeRate;
float midiVolExp;
int softLockCount;
int subticks, ticks, curRow, curOrder, prevRow, prevOrder, remainingLoops, totalLoops, lastLoopPos, exportLoopCount, nextSpeed, elapsedBars, elapsedBeats, curSpeed;
int subticks, ticks, curRow, curOrder, prevRow, prevOrder, remainingLoops, totalLoops, lastLoopPos, exportLoopCount, curExportChan, nextSpeed, elapsedBars, elapsedBeats, curSpeed;
size_t curSubSongIndex;
size_t bufferPos;
double divider;
@ -497,6 +499,7 @@ class DivEngine {
DivAudioExportModes exportMode;
DivAudioExportFormats exportFormat;
double exportFadeOut;
bool isFadingOut;
int exportOutputs;
bool exportChannelMask[DIV_MAX_CHANS];
DivConfig conf;
@ -515,9 +518,11 @@ class DivEngine {
std::vector<DivCommand> cmdStream;
std::vector<DivInstrumentType> possibleInsTypes;
std::vector<DivEffectContainer> effectInst;
std::vector<int> curChanMask;
static DivSysDef* sysDefs[DIV_MAX_CHIP_DEFS];
static DivSystem sysFileMapFur[DIV_MAX_CHIP_DEFS];
static DivSystem sysFileMapDMF[DIV_MAX_CHIP_DEFS];
static DivROMExportDef* romExportDefs[DIV_ROM_MAX];
DivCSPlayer* cmdStreamInt;
@ -577,7 +582,7 @@ class DivEngine {
void processRow(int i, bool afterDelay);
void nextOrder();
void nextRow();
void performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write, int streamOff, double* loopTimer, double* loopFreq, int* loopSample, bool* sampleDir, bool isSecond, int* pendingFreq, int* playingSample, int* setPos, unsigned int* sampleOff8, unsigned int* sampleLen8, size_t bankOffset, bool directStream);
void performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write, int streamOff, double* loopTimer, double* loopFreq, int* loopSample, bool* sampleDir, bool isSecond, int* pendingFreq, int* playingSample, int* setPos, unsigned int* sampleOff8, unsigned int* sampleLen8, size_t bankOffset, bool directStream, bool* sampleStoppable);
// returns true if end of song.
bool nextTick(bool noAccum=false, bool inhibitLowLat=false);
bool perSystemEffect(int ch, unsigned char effect, unsigned char effectVal);
@ -617,6 +622,17 @@ class DivEngine {
void loadFF(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath);
void loadWOPL(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath);
void loadWOPN(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath);
//sample banks
void loadP(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath);
void loadPPC(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath);
void loadPPS(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath);
void loadPVI(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath);
void loadPDX(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath);
void loadPZI(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath);
void loadP86(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath);
int loadSampleROM(String path, ssize_t expectedSize, unsigned char*& ret);
@ -624,6 +640,7 @@ class DivEngine {
bool deinitAudioBackend(bool dueToSwitchMaster=false);
void registerSystems();
void registerROMExports();
void initSongWithDesc(const char* description, bool inBase64=true, bool oldVol=false);
void exchangeIns(int one, int two);
@ -654,6 +671,8 @@ class DivEngine {
// add every export method here
friend class DivROMExport;
friend class DivExportAmigaValidation;
friend class DivExportTiuna;
friend class DivExportZSM;
public:
DivSong song;
@ -697,9 +716,8 @@ class DivEngine {
// save as .fur.
// if notPrimary is true then the song will not be altered
SafeWriter* saveFur(bool notPrimary=false, bool newPatternFormat=true);
// build a ROM file (TODO).
// specify system to build ROM for.
std::vector<DivROMExportOutput> buildROM(DivROMExportOptions sys);
// return a ROM exporter.
DivROMExport* buildROM(DivROMExportOptions sys);
// dump to VGM.
// set trailingTicks to:
// - 0 to add one tick of trailing
@ -707,8 +725,6 @@ class DivEngine {
// - -1 to auto-determine trailing
// - -2 to add a whole loop of trailing
SafeWriter* saveVGM(bool* sysToExport=NULL, bool loop=true, int version=0x171, bool patternHints=false, bool directStream=false, int trailingTicks=-1);
// dump to ZSM.
SafeWriter* saveZSM(unsigned int zsmrate=60, bool loop=true, bool optimize=true);
// dump to TIunA.
SafeWriter* saveTiuna(const bool* sysToExport, const char* baseLabel, int firstBankSize, int otherBankSize);
// dump command stream.
@ -803,6 +819,9 @@ class DivEngine {
// find song loop position
void walkSong(int& loopOrder, int& loopRow, int& loopEnd);
// find song length in rows (up to specified loop point), and find length of every order
void findSongLength(int loopOrder, int loopRow, double fadeoutLen, int& rowsForFadeout, bool& hasFFxx, std::vector<int>& orders, int& length);
// play (returns whether successful)
bool play();
@ -885,6 +904,9 @@ class DivEngine {
// get sys definition
const DivSysDef* getSystemDef(DivSystem sys);
// get ROM export definition
const DivROMExportDef* getROMExportDef(DivROMExportOptions opt);
// convert sample rate format
int fileToDivRate(int frate);
int divToFileRate(int drate);
@ -991,6 +1013,24 @@ class DivEngine {
// is exporting
bool isExporting();
// get how many loops is left
void getLoopsLeft(int& loops);
// get how many loops in total export needs to do
void getTotalLoops(int& loops);
// get current position in song
void getCurSongPos(int& row, int& order);
// get how many files export needs to create
void getTotalAudioFiles(int& files);
// get which file is processed right now (progress for e.g. per-channel export)
void getCurFileIndex(int& file);
// get fadeout state
bool getIsFadingOut();
// add instrument
int addInstrument(int refChan=0, DivInstrumentType fallbackType=DIV_INS_STD);
@ -1028,7 +1068,8 @@ class DivEngine {
int addSamplePtr(DivSample* which);
// get sample from file
DivSample* sampleFromFile(const char* path);
//DivSample* sampleFromFile(const char* path);
std::vector<DivSample*> sampleFromFile(const char* path);
// get raw sample
DivSample* sampleFromFileRaw(const char* path, DivSampleDepth depth, int channels, bool bigEndian, bool unsign, bool swapNibbles, int rate);
@ -1294,6 +1335,9 @@ class DivEngine {
// perform secure/sync operation
void synchronized(const std::function<void()>& what);
// perform secure/sync operation (soft)
void synchronizedSoft(const std::function<void()>& what);
// perform secure/sync song operation
void lockSave(const std::function<void()>& what);
@ -1361,6 +1405,7 @@ class DivEngine {
midiIsDirectProgram(false),
lowLatency(false),
systemsRegistered(false),
romExportsRegistered(false),
hasLoadedSomething(false),
midiOutClock(false),
midiOutTime(false),
@ -1379,6 +1424,7 @@ class DivEngine {
totalLoops(0),
lastLoopPos(0),
exportLoopCount(0),
curExportChan(0),
nextSpeed(3),
elapsedBars(0),
elapsedBeats(0),
@ -1417,6 +1463,7 @@ class DivEngine {
exportMode(DIV_EXPORT_MODE_ONE),
exportFormat(DIV_EXPORT_FORMAT_S16),
exportFadeOut(0.0),
isFadingOut(false),
exportOutputs(2),
cmdStreamInt(NULL),
midiBaseChan(0),
@ -1467,6 +1514,7 @@ class DivEngine {
memset(pitchTable,0,4096*sizeof(int));
memset(effectSlotMap,-1,4096*sizeof(short));
memset(sysDefs,0,DIV_MAX_CHIP_DEFS*sizeof(void*));
memset(romExportDefs,0,DIV_ROM_MAX*sizeof(void*));
memset(walked,0,8192);
memset(oscBuf,0,DIV_MAX_OUTPUTS*(sizeof(float*)));
memset(exportChannelMask,1,DIV_MAX_CHANS*sizeof(bool));

View file

@ -20,18 +20,24 @@
#include "engine.h"
#include "export/amigaValidation.h"
#include "export/tiuna.h"
#include "export/zsm.h"
std::vector<DivROMExportOutput> DivEngine::buildROM(DivROMExportOptions sys) {
DivROMExport* DivEngine::buildROM(DivROMExportOptions sys) {
DivROMExport* exporter=NULL;
switch (sys) {
case DIV_ROM_AMIGA_VALIDATION:
exporter=new DivExportAmigaValidation;
break;
case DIV_ROM_TIUNA:
exporter=new DivExportTiuna;
break;
case DIV_ROM_ZSM:
exporter=new DivExportZSM;
break;
default:
exporter=new DivROMExport;
break;
}
std::vector<DivROMExportOutput> ret=exporter->go(this);
delete exporter;
return ret;
return exporter;
}

View file

@ -29,6 +29,8 @@ class DivEngine;
enum DivROMExportOptions {
DIV_ROM_ABSTRACT=0,
DIV_ROM_AMIGA_VALIDATION,
DIV_ROM_ZSM,
DIV_ROM_TIUNA,
DIV_ROM_MAX
};
@ -45,30 +47,61 @@ struct DivROMExportOutput {
data(NULL) {}
};
struct DivROMExportProgress {
String name;
float amount;
};
class DivROMExport {
protected:
DivConfig conf;
std::vector<DivROMExportOutput> output;
void logAppend(String what);
public:
virtual std::vector<DivROMExportOutput> go(DivEngine* e);
std::vector<String> exportLog;
std::mutex logLock;
void setConf(DivConfig& c);
virtual bool go(DivEngine* eng);
virtual void abort();
virtual void wait();
std::vector<DivROMExportOutput>& getResult();
virtual bool hasFailed();
virtual bool isRunning();
virtual DivROMExportProgress getProgress(int index=0);
virtual ~DivROMExport() {}
};
#define logAppendf(...) logAppend(fmt::sprintf(__VA_ARGS__))
enum DivROMExportReqPolicy {
// exactly these chips.
DIV_REQPOL_EXACT=0,
// specified chips must be present but any amount of them is acceptable.
DIV_REQPOL_ANY,
// at least one of the specified chips.
DIV_REQPOL_LAX
};
struct DivROMExportDef {
const char* name;
const char* author;
const char* description;
DivSystem requisites[32];
int requisitesLen;
const char* fileType;
const char* fileExt;
std::vector<DivSystem> requisites;
bool multiOutput;
DivROMExportReqPolicy requisitePolicy;
DivROMExportDef(const char* n, const char* a, const char* d, std::initializer_list<DivSystem> req, bool multiOut):
DivROMExportDef(const char* n, const char* a, const char* d, const char* ft, const char* fe, std::initializer_list<DivSystem> req, bool multiOut, DivROMExportReqPolicy reqPolicy):
name(n),
author(a),
description(d),
multiOutput(multiOut) {
requisitesLen=0;
memset(requisites,0,32*sizeof(DivSystem));
for (DivSystem i: req) {
requisites[requisitesLen++]=i;
}
fileType(ft),
fileExt(fe),
multiOutput(multiOut),
requisitePolicy(reqPolicy) {
requisites=req;
}
};

View file

@ -20,7 +20,42 @@
#include "../export.h"
#include "../../ta-log.h"
std::vector<DivROMExportOutput> DivROMExport::go(DivEngine* e) {
bool DivROMExport::go(DivEngine* eng) {
logW("what's this? the null ROM export?");
return std::vector<DivROMExportOutput>();
return false;
}
void DivROMExport::abort() {
}
std::vector<DivROMExportOutput>& DivROMExport::getResult() {
return output;
}
bool DivROMExport::hasFailed() {
return true;
}
DivROMExportProgress DivROMExport::getProgress(int index) {
DivROMExportProgress ret;
ret.name="";
ret.amount=0.0f;
return ret;
}
void DivROMExport::logAppend(String what) {
logLock.lock();
exportLog.push_back(what);
logLock.unlock();
}
void DivROMExport::wait() {
}
bool DivROMExport::isRunning() {
return false;
}
void DivROMExport::setConf(DivConfig& c) {
conf=c;
}

View file

@ -40,8 +40,7 @@ struct SampleBookEntry {
len(0) {}
};
std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
std::vector<DivROMExportOutput> ret;
void DivExportAmigaValidation::run() {
std::vector<WaveEntry> waves;
std::vector<SampleBookEntry> sampleBook;
unsigned int wavesDataPtr=0;
@ -71,6 +70,7 @@ std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
bool done=false;
// sample.bin
logAppend("writing samples...");
SafeWriter* sample=new SafeWriter;
sample->init();
for (int i=0; i<256; i++) {
@ -80,6 +80,7 @@ std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
if (sample->tell()&1) sample->writeC(0);
// seq.bin
logAppend("making sequence...");
SafeWriter* seq=new SafeWriter;
seq->init();
@ -240,6 +241,7 @@ std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
EXTERN_BUSY_END;
// wave.bin
logAppend("writing wavetables...");
SafeWriter* wave=new SafeWriter;
wave->init();
for (WaveEntry& i: waves) {
@ -247,6 +249,7 @@ std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
}
// sbook.bin
logAppend("writing sample book...");
SafeWriter* sbook=new SafeWriter;
sbook->init();
for (SampleBookEntry& i: sampleBook) {
@ -256,6 +259,7 @@ std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
}
// wbook.bin
logAppend("writing wavetable book...");
SafeWriter* wbook=new SafeWriter;
wbook->init();
for (WaveEntry& i: waves) {
@ -266,12 +270,40 @@ std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
}
// finish
ret.reserve(5);
ret.push_back(DivROMExportOutput("sbook.bin",sbook));
ret.push_back(DivROMExportOutput("wbook.bin",wbook));
ret.push_back(DivROMExportOutput("sample.bin",sample));
ret.push_back(DivROMExportOutput("wave.bin",wave));
ret.push_back(DivROMExportOutput("seq.bin",seq));
output.reserve(5);
output.push_back(DivROMExportOutput("sbook.bin",sbook));
output.push_back(DivROMExportOutput("wbook.bin",wbook));
output.push_back(DivROMExportOutput("sample.bin",sample));
output.push_back(DivROMExportOutput("wave.bin",wave));
output.push_back(DivROMExportOutput("seq.bin",seq));
return ret;
logAppend("finished!");
running=false;
}
bool DivExportAmigaValidation::go(DivEngine* eng) {
e=eng;
running=true;
exportThread=new std::thread(&DivExportAmigaValidation::run,this);
return true;
}
void DivExportAmigaValidation::wait() {
if (exportThread!=NULL) {
exportThread->join();
delete exportThread;
}
}
void DivExportAmigaValidation::abort() {
wait();
}
bool DivExportAmigaValidation::isRunning() {
return running;
}
bool DivExportAmigaValidation::hasFailed() {
return false;
}

View file

@ -19,8 +19,18 @@
#include "../export.h"
#include <thread>
class DivExportAmigaValidation: public DivROMExport {
DivEngine* e;
std::thread* exportThread;
bool running;
void run();
public:
std::vector<DivROMExportOutput> go(DivEngine* e);
bool go(DivEngine* e);
bool isRunning();
bool hasFailed();
void abort();
void wait();
~DivExportAmigaValidation() {}
};

View file

@ -17,13 +17,14 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "tiuna.h"
#include "../engine.h"
#include "../ta-log.h"
#include <fmt/printf.h>
#include <algorithm>
#include <map>
#include <tuple>
#include <vector>
#include "engine.h"
#include "../fileutils.h"
#include "../ta-log.h"
struct TiunaNew {
short pitch;
@ -126,7 +127,6 @@ struct TiunaMatches {
static void writeCmd(std::vector<TiunaBytes>& cmds, TiunaCmd& cmd, unsigned char ch, int& lastWait, int fromTick, int toTick) {
while (fromTick<toTick) {
int val=MIN(toTick-fromTick,256);
assert(val>0);
if (lastWait!=val) {
cmd.wait=val;
lastWait=val;
@ -180,140 +180,161 @@ static void writeCmd(std::vector<TiunaBytes>& cmds, TiunaCmd& cmd, unsigned char
}
}
SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel, int firstBankSize, int otherBankSize) {
stop();
repeatPattern=false;
shallStop=false;
setOrder(0);
BUSY_BEGIN_SOFT;
// determine loop point
// bool stopped=false;
int loopOrder=0;
int loopOrderRow=0;
int loopEnd=0;
walkSong(loopOrder,loopOrderRow,loopEnd);
logI("loop point: %d %d",loopOrder,loopOrderRow);
SafeWriter* w=new SafeWriter;
w->init();
int tiaIdx=-1;
for (int i=0; i<song.systemLen; i++) {
if (sysToExport!=NULL && !sysToExport[i]) continue;
if (song.system[i]==DIV_SYSTEM_TIA) {
tiaIdx=i;
disCont[i].dispatch->toggleRegisterDump(true);
break;
}
}
if (tiaIdx<0) {
lastError="selected TIA system not found";
return NULL;
}
// write patterns
// bool writeLoop=false;
bool done=false;
playSub(false);
void DivExportTiuna::run() {
int loopOrder, loopOrderRow, loopEnd;
int tick=0;
// int loopTick=-1;
TiunaLast last[2];
TiunaNew news[2];
SafeWriter* w;
std::map<int,TiunaCmd> allCmds[2];
while (!done) {
// TODO implement loop
// if (loopTick<0 && loopOrder==curOrder && loopOrderRow==curRow
// && (ticks-((tempoAccum+virtualTempoN)/virtualTempoD))<=0
// ) {
// writeLoop=true;
// loopTick=tick;
// // invalidate last register state so it always force an absolute write after loop
// for (int i=0; i<2; i++) {
// last[i]=TiunaLast();
// last[i].pitch=-1;
// last[i].ins=-1;
// last[i].vol=-1;
// }
// }
if (nextTick(false,true) || !playing) {
// stopped=!playing;
done=true;
break;
}
for (int i=0; i<2; i++) {
news[i]=TiunaNew();
}
// get register dumps
std::vector<DivRegWrite>& writes=disCont[tiaIdx].dispatch->getRegisterWrites();
for (const DivRegWrite& i: writes) {
switch (i.addr) {
case 0xfffe0000:
case 0xfffe0001:
news[i.addr&1].pitch=i.val;
break;
case 0xfffe0002:
news[0].sync=i.val;
break;
case 0x15:
case 0x16:
news[i.addr-0x15].ins=i.val;
break;
case 0x19:
case 0x1a:
news[i.addr-0x19].vol=i.val;
break;
default: break;
}
}
writes.clear();
// collect changes
for (int i=0; i<2; i++) {
TiunaCmd cmds;
bool hasCmd=false;
if (news[i].pitch>=0 && (last[i].forcePitch || news[i].pitch!=last[i].pitch)) {
int dt=news[i].pitch-last[i].pitch;
if (!last[i].forcePitch && abs(dt)<=16) {
if (dt<0) cmds.pitchChange=15-dt;
else cmds.pitchChange=dt-1;
}
else cmds.pitchSet=news[i].pitch;
last[i].pitch=news[i].pitch;
last[i].forcePitch=false;
hasCmd=true;
}
if (news[i].ins>=0 && news[i].ins!=last[i].ins) {
cmds.ins=news[i].ins;
last[i].ins=news[i].ins;
hasCmd=true;
}
if (news[i].vol>=0 && news[i].vol!=last[i].vol) {
cmds.vol=(news[i].vol-last[i].vol)&0xf;
last[i].vol=news[i].vol;
hasCmd=true;
}
if (news[i].sync>=0) {
cmds.sync=news[i].sync;
hasCmd=true;
}
if (hasCmd) allCmds[i][tick]=cmds;
}
cmdStream.clear();
tick++;
}
for (int i=0; i<song.systemLen; i++) {
disCont[i].dispatch->getRegisterWrites().clear();
disCont[i].dispatch->toggleRegisterDump(false);
}
remainingLoops=-1;
playing=false;
freelance=false;
extValuePresent=false;
BUSY_END;
// config
String baseLabel=conf.getString("baseLabel","song");
int firstBankSize=conf.getInt("firstBankSize",3072);
int otherBankSize=conf.getInt("otherBankSize",4096-48);
int tiaIdx=conf.getInt("sysToExport",-1);
e->stop();
e->repeatPattern=false;
e->shallStop=false;
e->setOrder(0);
e->synchronizedSoft([&]() {
// determine loop point
// bool stopped=false;
loopOrder=0;
loopOrderRow=0;
loopEnd=0;
e->walkSong(loopOrder,loopOrderRow,loopEnd);
logAppendf("loop point: %d %d",loopOrder,loopOrderRow);
w=new SafeWriter;
w->init();
if (tiaIdx<0 || tiaIdx>=e->song.systemLen) {
tiaIdx=-1;
for (int i=0; i<e->song.systemLen; i++) {
if (e->song.system[i]==DIV_SYSTEM_TIA) {
tiaIdx=i;
break;
}
}
if (tiaIdx<0) {
logAppend("ERROR: selected TIA system not found");
failed=true;
running=false;
return;
}
} else if (e->song.system[tiaIdx]!=DIV_SYSTEM_TIA) {
logAppend("ERROR: selected chip is not a TIA!");
failed=true;
running=false;
return;
}
e->disCont[tiaIdx].dispatch->toggleRegisterDump(true);
// write patterns
// bool writeLoop=false;
logAppend("recording sequence...");
bool done=false;
e->playSub(false);
// int loopTick=-1;
TiunaLast last[2];
TiunaNew news[2];
while (!done) {
// TODO implement loop
// if (loopTick<0 && loopOrder==curOrder && loopOrderRow==curRow
// && (ticks-((tempoAccum+virtualTempoN)/virtualTempoD))<=0
// ) {
// writeLoop=true;
// loopTick=tick;
// // invalidate last register state so it always force an absolute write after loop
// for (int i=0; i<2; i++) {
// last[i]=TiunaLast();
// last[i].pitch=-1;
// last[i].ins=-1;
// last[i].vol=-1;
// }
// }
if (e->nextTick(false,true) || !e->playing) {
// stopped=!playing;
done=true;
break;
}
for (int i=0; i<2; i++) {
news[i]=TiunaNew();
}
// get register dumps
std::vector<DivRegWrite>& writes=e->disCont[tiaIdx].dispatch->getRegisterWrites();
for (const DivRegWrite& i: writes) {
switch (i.addr) {
case 0xfffe0000:
case 0xfffe0001:
news[i.addr&1].pitch=i.val;
break;
case 0xfffe0002:
news[0].sync=i.val;
break;
case 0x15:
case 0x16:
news[i.addr-0x15].ins=i.val;
break;
case 0x19:
case 0x1a:
news[i.addr-0x19].vol=i.val;
break;
default: break;
}
}
writes.clear();
// collect changes
for (int i=0; i<2; i++) {
TiunaCmd cmds;
bool hasCmd=false;
if (news[i].pitch>=0 && (last[i].forcePitch || news[i].pitch!=last[i].pitch)) {
int dt=news[i].pitch-last[i].pitch;
if (!last[i].forcePitch && abs(dt)<=16) {
if (dt<0) cmds.pitchChange=15-dt;
else cmds.pitchChange=dt-1;
}
else cmds.pitchSet=news[i].pitch;
last[i].pitch=news[i].pitch;
last[i].forcePitch=false;
hasCmd=true;
}
if (news[i].ins>=0 && news[i].ins!=last[i].ins) {
cmds.ins=news[i].ins;
last[i].ins=news[i].ins;
hasCmd=true;
}
if (news[i].vol>=0 && news[i].vol!=last[i].vol) {
cmds.vol=(news[i].vol-last[i].vol)&0xf;
last[i].vol=news[i].vol;
hasCmd=true;
}
if (news[i].sync>=0) {
cmds.sync=news[i].sync;
hasCmd=true;
}
if (hasCmd) allCmds[i][tick]=cmds;
}
e->cmdStream.clear();
tick++;
}
for (int i=0; i<e->song.systemLen; i++) {
e->disCont[i].dispatch->getRegisterWrites().clear();
e->disCont[i].dispatch->toggleRegisterDump(false);
}
e->remainingLoops=-1;
e->playing=false;
e->freelance=false;
e->extValuePresent=false;
});
if (failed) return;
// render commands
logAppend("rendering commands...");
std::vector<TiunaBytes> renderedCmds;
w->writeText(fmt::format(
"; Generated by Furnace " DIV_VERSION "\n"
@ -321,7 +342,7 @@ SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel,
"; Author: {}\n"
"; Album: {}\n"
"; Subsong #{}: {}\n\n",
song.name,song.author,song.category,curSubSongIndex+1,curSubSong->name
e->song.name,e->song.author,e->song.category,e->curSubSongIndex+1,e->curSubSong->name
));
for (int i=0; i<2; i++) {
TiunaCmd lastCmd;
@ -347,17 +368,37 @@ SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel,
std::vector<int> callTicks;
int cmId=0;
int cmdSize=renderedCmds.size();
std::vector<bool> processed=std::vector<bool>(cmdSize,false);
while (firstBankSize>768 && cmId<(MAX(firstBankSize/1024,1))*256) {
bool* processed=new bool[cmdSize];
memset(processed,0,cmdSize*sizeof(bool));
logAppend("compressing! this may take a while.");
int maxCmId=(MAX(firstBankSize/1024,1))*256;
int lastMaxPMVal=100000;
logAppendf("max cmId: %d",maxCmId);
logAppendf("commands: %d",cmdSize);
while (firstBankSize>768 && cmId<maxCmId) {
if (mustAbort) {
logAppend("aborted!");
failed=true;
running=false;
delete[] processed;
return;
}
float theOtherSide=pow(1.0/float(MAX(1,lastMaxPMVal)),0.2)*0.98;
progress[0].amount=theOtherSide+(1.0-theOtherSide)*((float)cmId/(float)maxCmId);
logAppendf("start CM %04x...",cmId);
std::map<int,TiunaMatches> potentialMatches;
for (int i=0; i<cmdSize-1;) {
// continue and skip if it's part of previous confirmed matches
while (i<cmdSize-1 && processed[i]) i++;
if (i>=cmdSize-1) break;
progress[1].amount=(float)i/(float)(cmdSize-1);
std::vector<TiunaMatch> match;
int ch=renderedCmds[i].ch;
for (int j=i+1; j<cmdSize;) {
while (j<cmdSize && processed[i]) j++;
if (processed[i]) break;
//while (j<cmdSize && processed[i]) j++;
if (j>=cmdSize) break;
int k=0;
int ticks=0;
@ -421,7 +462,10 @@ SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel,
}
i++;
}
if (potentialMatches.empty()) break;
if (potentialMatches.empty()) {
logAppend("potentialMatches is empty");
break;
}
int maxPMIdx=0;
int maxPMVal=0;
for (const auto& i: potentialMatches) {
@ -433,12 +477,18 @@ SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel,
int maxPMLen=potentialMatches[maxPMIdx].length;
for (const int i: potentialMatches[maxPMIdx].pos) {
confirmedMatches.push_back({i,i+maxPMLen,0,cmId});
std::fill(processed.begin()+i,processed.begin()+(i+maxPMLen),true);
memset(processed+i,1,maxPMLen);
//std::fill(processed.begin()+i,processed.begin()+(i+maxPMLen),true);
}
callTicks.push_back(potentialMatches[maxPMIdx].ticks);
logI("CM %04x added: pos=%d,len=%d,matches=%d,saved=%d",cmId,maxPMIdx,maxPMLen,potentialMatches[maxPMIdx].pos.size(),maxPMVal);
logAppendf("CM %04x added: pos=%d,len=%d,matches=%d,saved=%d",cmId,maxPMIdx,maxPMLen,potentialMatches[maxPMIdx].pos.size(),maxPMVal);
lastMaxPMVal=maxPMVal;
cmId++;
}
progress[0].amount=1.0f;
progress[1].amount=1.0f;
logAppend("generating data...");
delete[] processed;
std::sort(confirmedMatches.begin(),confirmedMatches.end(),[](const TiunaMatch& l, const TiunaMatch& r){
return l.pos<r.pos;
});
@ -448,14 +498,10 @@ SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel,
// overlap check
for (int i=1; i<(int)confirmedMatches.size(); i++) {
if (confirmedMatches[i-1].endPos<=confirmedMatches[i].pos) continue;
lastError="impossible overlap found in matches list, please report";
return NULL;
}
SafeWriter dbg;
dbg.init();
dbg.writeText(fmt::format("renderedCmds size={}\n",renderedCmds.size()));
for (const auto& i: confirmedMatches) {
dbg.writeText(fmt::format("pos={},end={},id={}\n",i.pos,i.endPos,i.id,i.size));
logAppend("ERROR: impossible overlap found in matches list, please report");
failed=true;
running=false;
return;
}
// write commands
@ -498,8 +544,10 @@ SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel,
}
w->writeC('\n');
if (totalSize>firstBankSize) {
lastError="first bank is not large enough to contain call table";
return NULL;
logAppend("ERROR: first bank is not large enough to contain call table");
failed=true;
running=false;
return;
}
int curBank=0;
@ -560,13 +608,50 @@ SafeWriter* DivEngine::saveTiuna(const bool* sysToExport, const char* baseLabel,
}
w->writeText(" .text x\"e0\"\n .endsection\n");
totalSize++;
logI("total size: %d bytes (%d banks)",totalSize,curBank+1);
//FILE* f=ps_fopen("confirmedMatches.txt","wb");
//if (f!=NULL) {
// fwrite(dbg.getFinalBuf(),1,dbg.size(),f);
// fclose(f);
//}
logAppendf("total size: %d bytes (%d banks)",totalSize,curBank+1);
return w;
output.push_back(DivROMExportOutput("export.asm",w));
logAppend("finished!");
running=false;
}
bool DivExportTiuna::go(DivEngine* eng) {
progress[0].name="Compression";
progress[0].amount=0.0f;
progress[1].name="Confirmed Matches";
progress[1].amount=0.0f;
e=eng;
running=true;
failed=false;
mustAbort=false;
exportThread=new std::thread(&DivExportTiuna::run,this);
return true;
}
void DivExportTiuna::wait() {
if (exportThread!=NULL) {
exportThread->join();
delete exportThread;
}
}
void DivExportTiuna::abort() {
mustAbort=true;
wait();
}
bool DivExportTiuna::isRunning() {
return running;
}
bool DivExportTiuna::hasFailed() {
return failed;
}
DivROMExportProgress DivExportTiuna::getProgress(int index) {
if (index<0 || index>2) return progress[2];
return progress[index];
}

38
src/engine/export/tiuna.h Normal file
View file

@ -0,0 +1,38 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "../export.h"
#include <thread>
class DivExportTiuna: public DivROMExport {
DivEngine* e;
std::thread* exportThread;
DivROMExportProgress progress[3];
bool running, failed, mustAbort;
void run();
public:
bool go(DivEngine* e);
bool isRunning();
bool hasFailed();
void abort();
void wait();
DivROMExportProgress getProgress(int index=0);
~DivExportTiuna() {}
};

View file

@ -18,9 +18,77 @@
*/
#include "zsm.h"
#include "../engine.h"
#include "../ta-log.h"
#include "../utfutils.h"
#include "song.h"
#include <fmt/printf.h>
/// DivZSM definitions
#define ZSM_HEADER_SIZE 16
#define ZSM_VERSION 1
#define ZSM_YM_CMD 0x40
#define ZSM_DELAY_CMD 0x80
#define ZSM_YM_MAX_WRITES 63
#define ZSM_SYNC_MAX_WRITES 31
#define ZSM_DELAY_MAX 127
#define ZSM_EOF ZSM_DELAY_CMD
#define ZSM_EXT ZSM_YM_CMD
#define ZSM_EXT_PCM 0x00
#define ZSM_EXT_CHIP 0x40
#define ZSM_EXT_SYNC 0x80
#define ZSM_EXT_CUSTOM 0xC0
enum YM_STATE { ym_PREV, ym_NEW, ym_STATES };
enum PSG_STATE { psg_PREV, psg_NEW, psg_STATES };
class DivZSM {
private:
struct S_pcmInst {
int geometry;
unsigned int offset, length, loopPoint;
bool isLooped;
};
SafeWriter* w;
int ymState[ym_STATES][256];
int psgState[psg_STATES][64];
int pcmRateCache;
int pcmCtrlRVCache;
int pcmCtrlDCCache;
unsigned int pcmLoopPointCache;
bool pcmIsLooped;
std::vector<DivRegWrite> ymwrites;
std::vector<DivRegWrite> pcmMeta;
std::vector<unsigned char> pcmData;
std::vector<unsigned char> pcmCache;
std::vector<S_pcmInst> pcmInsts;
std::vector<DivRegWrite> syncCache;
int loopOffset;
int numWrites;
int ticks;
int tickRate;
int ymMask;
int psgMask;
bool optimize;
public:
DivZSM();
~DivZSM();
void init(unsigned int rate = 60);
int getoffset();
void writeYM(unsigned char a, unsigned char v);
void writePSG(unsigned char a, unsigned char v);
void writePCM(unsigned char a, unsigned char v);
void writeSync(unsigned char a, unsigned char v);
void setOptimize(bool o);
void tick(int numticks = 1);
void setLoopPoint();
SafeWriter* finish();
private:
void flushWrites();
void flushTicks();
};
/// DivZSM implementation
DivZSM::DivZSM() {
w=NULL;
@ -133,14 +201,6 @@ void DivZSM::writePSG(unsigned char a, unsigned char v) {
} else if (a>=64) {
return writePCM(a-64,v);
}
if (optimize) {
if ((a&3)==3 && v>64) {
// Pulse width on non-pulse waves is nonsense and wasteful
// No need to preserve state here because the next write that
// selects pulse will also set the pulse width in this register
v&=0xc0;
}
}
if (psgState[psg_PREV][a]==v) {
if (psgState[psg_NEW][a]!=v) {
// NEW value is being reset to the same as PREV value
@ -455,3 +515,243 @@ void DivZSM::flushTicks() {
}
ticks=0;
}
/// ZSM export
constexpr int MASTER_CLOCK_PREC=(sizeof(void*)==8)?8:0;
constexpr int MASTER_CLOCK_MASK=(sizeof(void*)==8)?0xff:0;
void DivExportZSM::run() {
// settings
unsigned int zsmrate=conf.getInt("zsmrate",60);
bool loop=conf.getBool("loop",true);
bool optimize=conf.getBool("optimize",true);
// system IDs
int VERA=-1;
int YM=-1;
int IGNORED=0;
// find indexes for YM and VERA. Ignore other systems.
for (int i=0; i<e->song.systemLen; i++) {
switch (e->song.system[i]) {
case DIV_SYSTEM_VERA:
if (VERA>=0) {
IGNORED++;
break;
}
VERA=i;
logAppendf("VERA detected as chip id %d",i);
break;
case DIV_SYSTEM_YM2151:
if (YM>=0) {
IGNORED++;
break;
}
YM=i;
logAppendf("YM detected as chip id %d",i);
break;
default:
IGNORED++;
logAppendf("Ignoring chip %d systemID %d",i,(int)e->song.system[i]);
break;
}
}
if (VERA<0 && YM<0) {
logAppend("ERROR: No supported systems for ZSM");
failed=true;
running=false;
return;
}
if (IGNORED>0) {
logAppendf("ZSM export ignoring %d unsupported system%c",IGNORED,IGNORED>1?'s':' ');
}
DivZSM zsm;
e->stop();
e->repeatPattern=false;
e->setOrder(0);
e->synchronizedSoft([&]() {
double origRate=e->got.rate;
e->got.rate=zsmrate&0xffff;
// determine loop point
int loopOrder=0;
int loopRow=0;
int loopEnd=0;
e->walkSong(loopOrder,loopRow,loopEnd);
logAppendf("loop point: %d %d",loopOrder,loopRow);
zsm.init(zsmrate);
// reset the playback state
e->curOrder=0;
e->freelance=false;
e->playing=false;
e->extValuePresent=false;
e->remainingLoops=-1;
// Prepare to write song data
e->playSub(false);
//size_t tickCount=0;
bool done=false;
bool loopNow=false;
int loopPos=-1;
int fracWait=0; // accumulates fractional ticks
if (VERA>=0) e->disCont[VERA].dispatch->toggleRegisterDump(true);
if (YM>=0) {
e->disCont[YM].dispatch->toggleRegisterDump(true);
// emit LFO initialization commands
zsm.writeYM(0x18,0); // freq=0
zsm.writeYM(0x19,0x7F); // AMD =7F
zsm.writeYM(0x19,0xFF); // PMD =7F
// TODO: incorporate the Furnace meta-command for init data and filter
// out writes to otherwise-unused channels.
}
// Indicate the song's tuning as a sync meta-event
// specified in terms of how many 1/256th semitones
// the song is offset from standard A-440 tuning.
// This is mainly to benefit visualizations in players
// for non-standard tunings so that they can avoid
// displaying the entire song held in pitch bend.
// Tunings offsets that exceed a half semitone
// will simply be represented in a different key
// by nature of overflowing the signed char value
signed char tuningoffset=(signed char)(round(3072*(log(e->song.tuning/440.0)/log(2))))&0xff;
zsm.writeSync(0x01,tuningoffset);
// Set optimize flag, which mainly buffers PSG writes
// whenever the channel is silent
zsm.setOptimize(optimize);
while (!done) {
if (loopPos==-1) {
if (loopOrder==e->curOrder && loopRow==e->curRow && loop)
loopNow=true;
if (loopNow) {
// If Virtual Tempo is in use, our exact loop point
// might be skipped due to quantization error.
// If this happens, the tick immediately following is our loop point.
if (e->ticks==1 || !(loopOrder==e->curOrder && loopRow==e->curRow)) {
loopPos=zsm.getoffset();
zsm.setLoopPoint();
loopNow=false;
}
}
}
if (e->nextTick() || !e->playing) {
done=true;
if (!loop) {
for (int i=0; i<e->song.systemLen; i++) {
e->disCont[i].dispatch->getRegisterWrites().clear();
}
break;
}
if (!e->playing) {
loopPos=-1;
}
}
// get register dumps
for (int j=0; j<2; j++) {
int i=0;
// dump YM writes first
if (j==0) {
if (YM<0) {
continue;
} else {
i=YM;
}
}
// dump VERA writes second
if (j==1) {
if (VERA<0) {
continue;
} else {
i=VERA;
}
}
std::vector<DivRegWrite>& writes=e->disCont[i].dispatch->getRegisterWrites();
if (writes.size()>0)
logD("zsmOps: Writing %d messages to chip %d",writes.size(),i);
for (DivRegWrite& write: writes) {
if (i==YM) {
if (done && write.addr==0x08 && (write.val&0x78)>0) continue; // don't process keydown on lookahead
zsm.writeYM(write.addr&0xff,write.val);
}
if (i==VERA) {
if (done && write.addr>=64) continue; // don't process any PCM or sync events on the loop lookahead
zsm.writePSG(write.addr&0xff,write.val);
}
}
writes.clear();
}
// write wait
int totalWait=e->cycles>>MASTER_CLOCK_PREC;
fracWait+=e->cycles&MASTER_CLOCK_MASK;
totalWait+=fracWait>>MASTER_CLOCK_PREC;
fracWait&=MASTER_CLOCK_MASK;
if (totalWait>0 && !done) {
zsm.tick(totalWait);
//tickCount+=totalWait;
}
}
// end of song
// done - close out.
e->got.rate=origRate;
if (VERA>=0) e->disCont[VERA].dispatch->toggleRegisterDump(false);
if (YM>=0) e->disCont[YM].dispatch->toggleRegisterDump(false);
e->remainingLoops=-1;
e->playing=false;
e->freelance=false;
e->extValuePresent=false;
});
progress[0].amount=1.0f;
logAppend("finished!");
output.push_back(DivROMExportOutput("out.zsm",zsm.finish()));
running=false;
}
/// DivExpottZSM - FRONTEND
bool DivExportZSM::go(DivEngine* eng) {
progress[0].name="Generate";
progress[0].amount=0.0f;
e=eng;
running=true;
failed=false;
mustAbort=false;
exportThread=new std::thread(&DivExportZSM::run,this);
return true;
}
void DivExportZSM::wait() {
if (exportThread!=NULL) {
exportThread->join();
delete exportThread;
}
}
void DivExportZSM::abort() {
mustAbort=true;
wait();
}
bool DivExportZSM::isRunning() {
return running;
}
bool DivExportZSM::hasFailed() {
return failed;
}
DivROMExportProgress DivExportZSM::getProgress(int index) {
if (index<0 || index>1) return progress[1];
return progress[index];
}

38
src/engine/export/zsm.h Normal file
View file

@ -0,0 +1,38 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "../export.h"
#include <thread>
class DivExportZSM: public DivROMExport {
DivEngine* e;
std::thread* exportThread;
DivROMExportProgress progress[2];
bool running, failed, mustAbort;
void run();
public:
bool go(DivEngine* e);
bool isRunning();
bool hasFailed();
void abort();
void wait();
DivROMExportProgress getProgress(int index=0);
~DivExportZSM() {}
};

63
src/engine/exportDef.cpp Normal file
View file

@ -0,0 +1,63 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "engine.h"
DivROMExportDef* DivEngine::romExportDefs[DIV_ROM_MAX];
const DivROMExportDef* DivEngine::getROMExportDef(DivROMExportOptions opt) {
return romExportDefs[opt];
}
void DivEngine::registerROMExports() {
logD("registering ROM exports...");
romExportDefs[DIV_ROM_AMIGA_VALIDATION]=new DivROMExportDef(
"Amiga Validation", "tildearrow",
"a test export for ensuring Amiga emulation is accurate. do not use!",
NULL, NULL,
{DIV_SYSTEM_AMIGA},
true, DIV_REQPOL_EXACT
);
romExportDefs[DIV_ROM_ZSM]=new DivROMExportDef(
"Commander X16 ZSM", "ZeroByteOrg and MooingLemur",
"Commander X16 Zsound Music File.\n"
"for use with Melodius, Calliope and/or ZSMKit:\n"
"- https://github.com/mooinglemur/zsmkit (development)\n"
"- https://github.com/mooinglemur/melodius (player)\n"
"- https://github.com/ZeroByteOrg/calliope (player)\n",
"ZSM file", ".zsm",
{
DIV_SYSTEM_YM2151, DIV_SYSTEM_VERA
},
false, DIV_REQPOL_LAX
);
romExportDefs[DIV_ROM_TIUNA]=new DivROMExportDef(
"Atari 2600 (TIunA)", "Natt Akuma",
"advanced driver with software tuning support.\n"
"see https://github.com/AYCEdemo/twin-tiuna for code.",
"assembly files", ".asm",
{
DIV_SYSTEM_TIA
},
false, DIV_REQPOL_ANY
);
}

View file

@ -63,3 +63,34 @@ enum DivFurVariants: int {
DIV_FUR_VARIANT_VANILLA=0,
DIV_FUR_VARIANT_B=1,
};
// MIDI-related
struct midibank_t {
String name;
uint8_t bankMsb,
bankLsb;
};
// Reused patch data structures
// SBI and some other OPL containers
struct sbi_t {
uint8_t Mcharacteristics,
Ccharacteristics,
Mscaling_output,
Cscaling_output,
Meg_AD,
Ceg_AD,
Meg_SR,
Ceg_SR,
Mwave,
Cwave,
FeedConnect;
};
//bool stringNotBlank(String& str);
// detune needs extra translation from register to furnace format
//uint8_t fmDtRegisterToFurnace(uint8_t&& dtNative);
//void readSbiOpData(sbi_t& sbi, SafeReader& reader);

View file

@ -2094,8 +2094,24 @@ bool DivEngine::loadFur(unsigned char* file, size_t len, int variantID) {
ds.systemFlags[i].set("oldPitch",true);
}
}
} else if (ds.version<217) {
for (int i=0; i<ds.systemLen; i++) {
if (ds.system[i]==DIV_SYSTEM_VERA) {
ds.systemFlags[i].set("chipType",1);
}
}
}
// SNES no anti-click
if (ds.version<220) {
for (int i=0; i<ds.systemLen; i++) {
if (ds.system[i]==DIV_SYSTEM_SNES) {
ds.systemFlags[i].set("antiClick",false);
}
}
}
if (active) quitDispatch();
BUSY_BEGIN_SOFT;
saveLock.lock();

View file

@ -639,6 +639,8 @@ bool DivEngine::loadIT(unsigned char* file, size_t len) {
logD("seek not needed...");
}
logV("reading sample data (%d)",s->samples);
if (flags&8) { // compressed sample
unsigned int ret=0;
logV("decompression begin... (%d)",s->samples);
@ -672,62 +674,66 @@ bool DivEngine::loadIT(unsigned char* file, size_t len) {
}
logV("got: %d",ret);
} else {
if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
if (flags&4) { // downmix stereo
for (unsigned int i=0; i<s->samples; i++) {
short l;
if (convert&2) {
l=reader.readS_BE();
} else {
l=reader.readS();
try {
if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
if (flags&4) { // downmix stereo
for (unsigned int i=0; i<s->samples; i++) {
short l;
if (convert&2) {
l=reader.readS_BE();
} else {
l=reader.readS();
}
if (!(convert&1)) {
l^=0x8000;
}
s->data16[i]=l;
}
if (!(convert&1)) {
l^=0x8000;
for (unsigned int i=0; i<s->samples; i++) {
short r;
if (convert&2) {
r=reader.readS_BE();
} else {
r=reader.readS();
}
if (!(convert&1)) {
r^=0x8000;
}
s->data16[i]=(s->data16[i]+r)>>1;
}
s->data16[i]=l;
}
for (unsigned int i=0; i<s->samples; i++) {
short r;
if (convert&2) {
r=reader.readS_BE();
} else {
r=reader.readS();
} else {
for (unsigned int i=0; i<s->samples; i++) {
if (convert&2) {
s->data16[i]=reader.readS_BE()^((convert&1)?0:0x8000);
} else {
s->data16[i]=reader.readS()^((convert&1)?0:0x8000);
}
}
if (!(convert&1)) {
r^=0x8000;
}
s->data16[i]=(s->data16[i]+r)>>1;
}
} else {
for (unsigned int i=0; i<s->samples; i++) {
if (convert&2) {
s->data16[i]=reader.readS_BE()^((convert&1)?0:0x8000);
} else {
s->data16[i]=reader.readS()^((convert&1)?0:0x8000);
if (flags&4) { // downmix stereo
for (unsigned int i=0; i<s->samples; i++) {
signed char l=reader.readC();
if (!(convert&1)) {
l^=0x80;
}
s->data8[i]=l;
}
for (unsigned int i=0; i<s->samples; i++) {
signed char r=reader.readC();
if (!(convert&1)) {
r^=0x80;
}
s->data8[i]=(s->data8[i]+r)>>1;
}
} else {
for (unsigned int i=0; i<s->samples; i++) {
s->data8[i]=reader.readC()^((convert&1)?0:0x80);
}
}
}
} else {
if (flags&4) { // downmix stereo
for (unsigned int i=0; i<s->samples; i++) {
signed char l=reader.readC();
if (!(convert&1)) {
l^=0x80;
}
s->data8[i]=l;
}
for (unsigned int i=0; i<s->samples; i++) {
signed char r=reader.readC();
if (!(convert&1)) {
r^=0x80;
}
s->data8[i]=(s->data8[i]+r)>>1;
}
} else {
for (unsigned int i=0; i<s->samples; i++) {
s->data8[i]=reader.readC()^((convert&1)?0:0x80);
}
}
} catch (EndOfFileException& e) {
logW("premature end of file...");
}
}

124
src/engine/fileOps/p.cpp Normal file
View file

@ -0,0 +1,124 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fileOpsCommon.h"
class DivEngine;
//P VOX ADPCM sample bank
/* =======================================
Header
=======================================
0x0000 - 0x03FF 256 * {
Sample start (uint32_t)
- 0x00000000 = unused
}
=======================================
Body
=======================================
Stream of Sample Data {
MSM6258 ADPCM encoding
nibble-swapped VOX / Dialogic ADPCM
Mono
Sample rate?
16000Hz seems fine
} */
#define P_BANK_SIZE 256
#define P_SAMPLE_RATE 16000
typedef struct
{
uint32_t start_pointer;
} P_HEADER;
void DivEngine::loadP(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
{
try
{
reader.seek(0, SEEK_SET);
P_HEADER headers[P_BANK_SIZE];
for(int i = 0; i < P_BANK_SIZE; i++)
{
headers[i].start_pointer = (unsigned int)reader.readI_BE();
}
for(int i = 0; i < P_BANK_SIZE; i++)
{
if(headers[i].start_pointer != 0)
{
DivSample* s = new DivSample;
s->rate = P_SAMPLE_RATE;
s->centerRate = P_SAMPLE_RATE;
s->depth = DIV_SAMPLE_DEPTH_VOX;
reader.seek((int)headers[i].start_pointer, SEEK_SET);
int sample_pos = 0;
int sample_len = 0;
if(i < P_BANK_SIZE - 1)
{
sample_len = headers[i + 1].start_pointer - headers[i].start_pointer;
}
else
{
sample_len = (int)reader.size() - headers[i].start_pointer;
}
if(sample_len > 0)
{
s->init(sample_len * 2);
for(int j = 0; j < sample_len; j++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
curr_byte = (curr_byte << 4) | (curr_byte >> 4);
s->dataVOX[sample_pos] = curr_byte;
sample_pos++;
}
ret.push_back(s);
logI("p: start %d len %d", headers[i].start_pointer, sample_len);
}
else
{
delete s;
}
}
}
}
catch (EndOfFileException& e)
{
lastError=_("premature end of file");
logE("premature end of file");
}
}

142
src/engine/fileOps/p86.cpp Normal file
View file

@ -0,0 +1,142 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fileOpsCommon.h"
class DivEngine;
//P86 8-bit PCM sample bank
/* =======================================
Header
=======================================
0x0000 Identifier (12b)
"PCM86 DATA(\n)(\0)"
0x000C Targeted P86DRV version (1b)
version <high nibble>.<low nibble>
0x000D File Length (3b)
0x0010 - 0x060F 256 * {
Pointer to Sample Data Start (3b)
Length of Sample Data (3b)
(0x000000 0x000000 -> no sample for this instrument ID)
}
=======================================
Body
=======================================
Stream of Sample Data {
8-Bit Signed
Mono
16540Hz
(above sample rate according to KAJA's documentation
any sample rate possible, for different base note & octave)
} */
#define P86_BANK_SIZE 256
#define P86_SAMPLE_RATE 16540
#define P86_FILE_SIG "PCM86 DATA\n\0"
typedef struct
{
uint32_t start_pointer;
uint32_t sample_length;
} P86_HEADER;
#define UNUSED(x) (void)(x)
uint32_t read_3bytes(SafeReader& reader)
{
unsigned char arr[3];
for (int i = 0; i < 3; i++)
{
arr[i] = (unsigned char)reader.readC();
}
return (arr[0] | (arr[1] << 8) | (arr[2] << 16));
}
void DivEngine::loadP86(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
{
try
{
reader.seek(0, SEEK_SET);
P86_HEADER headers[P86_BANK_SIZE];
String file_sig = reader.readString(12);
if(file_sig != P86_FILE_SIG) return;
uint8_t version = reader.readC();
UNUSED(version);
uint32_t file_size = read_3bytes(reader);
UNUSED(file_size);
for(int i = 0; i < P86_BANK_SIZE; i++)
{
headers[i].start_pointer = read_3bytes(reader);
headers[i].sample_length = read_3bytes(reader);
}
for(int i = 0; i < P86_BANK_SIZE; i++)
{
if(headers[i].start_pointer != 0 && headers[i].sample_length != 0)
{
DivSample* s = new DivSample;
s->rate = P86_SAMPLE_RATE;
s->centerRate = P86_SAMPLE_RATE;
s->depth = DIV_SAMPLE_DEPTH_8BIT;
s->init(headers[i].sample_length); //byte per sample
reader.seek((int)headers[i].start_pointer, SEEK_SET);
int sample_pos = 0;
for(uint32_t j = 0; j < headers[i].sample_length; j++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
//curr_byte += 0x80;
s->data8[sample_pos] = curr_byte;
sample_pos++;
}
ret.push_back(s);
logI("p86: start %06X len %06X", headers[i].start_pointer, headers[i].sample_length);
}
}
}
catch (EndOfFileException& e)
{
lastError=_("premature end of file");
logE("premature end of file");
}
}

101
src/engine/fileOps/pdx.cpp Normal file
View file

@ -0,0 +1,101 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fileOpsCommon.h"
class DivEngine;
//PDX 8-bit OKI ADPCM sample bank
/* File format
The file starts with a header with 96 8-byte pairs, with each pair containing offset and length of ADPCM data chunks for each note value, like so:
[[byte pointer to sample in file -- 32-bit unsigned integer (4 bytes)] [empty: $0000 -- 16-bit integer] [length of sample, amount in bytes -- 16-bit unsigned integer] ×96] [ADPCM data] EOF
The first sample (1) is mapped to 0x80 (= the lowest note within MXDRV) and the last sample (96) is mapped to 0xDF (= the highest note within MXDRV).
Samples are encoded in 4-bit OKI ADPCM encoded nibbles, where each byte contains 2 nibbles: [nibble 2 << 4 || nibble 1]
Unfortunately, sample rates for each samples are not defined within the .PDX file and have to be set manually with the appropriate command for that at play-time */
#define PDX_BANK_SIZE 96
#define PDX_SAMPLE_RATE 16000
typedef struct
{
unsigned int start_pointer;
unsigned short sample_length;
} PDX_HEADER;
#define UNUSED(x) (void)(x)
void DivEngine::loadPDX(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
{
try
{
reader.seek(0, SEEK_SET);
PDX_HEADER headers[PDX_BANK_SIZE];
for(int i = 0; i < PDX_BANK_SIZE; i++)
{
headers[i].start_pointer = (unsigned int)reader.readI_BE();
unsigned short empty = (unsigned short)reader.readS_BE(); //skip 1st 2 bytes
UNUSED(empty);
headers[i].sample_length = (unsigned short)reader.readS_BE();
}
for(int i = 0; i < PDX_BANK_SIZE; i++)
{
if(headers[i].start_pointer != 0 && headers[i].sample_length != 0)
{
DivSample* s = new DivSample;
s->rate = PDX_SAMPLE_RATE;
s->centerRate = PDX_SAMPLE_RATE;
s->depth = DIV_SAMPLE_DEPTH_VOX;
s->init(headers[i].sample_length * 2);
reader.seek((int)headers[i].start_pointer, SEEK_SET);
int sample_pos = 0;
for(unsigned short j = 0; j < headers[i].sample_length; j++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
curr_byte = (curr_byte << 4) | (curr_byte >> 4);
s->dataVOX[sample_pos] = curr_byte;
sample_pos++;
}
ret.push_back(s);
logI("pdx: start %d len %d", headers[i].start_pointer, headers[i].sample_length);
}
}
}
catch (EndOfFileException& e)
{
lastError=_("premature end of file");
logE("premature end of file");
}
}

142
src/engine/fileOps/ppc.cpp Normal file
View file

@ -0,0 +1,142 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fileOpsCommon.h"
class DivEngine;
//PPC PMD's YM2608 ADPCM-B sample bank
/* ========================================
General
========================================
ADPCM RAM addresses: see docs/common.txt {
address_start = 0x0026
file_header_size = 0x0420
}
========================================
Header
========================================
0x0000 Identifier (30b)
"ADPCM DATA for PMD ver.4.4- "
0x001E Address of End of Data (32B blocks) in ADPCM RAM (1h)
File Size == address -> file offset
0x0020 - 0x041F 256 * {
Start of Sample (32b blocks) in ADPCM RAM (1h)
End of Sample (32b blocks) in ADPCM RAM (1h)
(0x0000 0x0000 -> no sample for this instrument ID)
}
========================================
Body
========================================
Stream of Sample Data {
Yamaha ADPCM-B encoding (4-Bit Signed ADPCM)
Mono
16kHz
(above sample rate according to KAJA's documentation
any sample rate possible, for different base note & octave)
} */
#define PPC_FILE_SIG "ADPCM DATA for PMD ver.4.4- "
#define PPC_BANK_SIZE 256
#define PPC_SAMPLE_RATE 16000
typedef struct
{
uint16_t start_pointer;
uint16_t end_pointer;
} PPC_HEADER;
#define UNUSED(x) (void)(x)
#define ADPCM_DATA_START 0x0420
void DivEngine::loadPPC(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
{
try
{
reader.seek(0, SEEK_SET);
String file_sig = reader.readString(30);
unsigned short end_of_data = (unsigned short)reader.readS();
UNUSED(end_of_data);
if(file_sig != PPC_FILE_SIG) return;
PPC_HEADER headers[PPC_BANK_SIZE];
for(int i = 0; i < PPC_BANK_SIZE; i++)
{
headers[i].start_pointer = (unsigned short)reader.readS();
headers[i].end_pointer = (unsigned short)reader.readS();
}
for(int i = 0; i < PPC_BANK_SIZE; i++)
{
if((headers[i].start_pointer != 0 || headers[i].end_pointer != 0) && headers[i].start_pointer < headers[i].end_pointer)
{
DivSample* s = new DivSample;
s->rate = PPC_SAMPLE_RATE;
s->centerRate = PPC_SAMPLE_RATE;
s->depth = DIV_SAMPLE_DEPTH_ADPCM_B;
s->init((headers[i].end_pointer - headers[i].start_pointer) * 32 * 2);
int sample_pos = 0;
int sample_length = (headers[i].end_pointer - headers[i].start_pointer) * 32;
//reader.seek(ADPCM_DATA_START + headers[i].start_pointer * 32, SEEK_SET);
for(int j = 0; j < sample_length; j++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
//curr_byte=(curr_byte<<4)|(curr_byte>>4);
s->dataB[sample_pos] = curr_byte;
sample_pos++;
}
logI("ppc: start %d end %d len in bytes %d", headers[i].start_pointer, headers[i].end_pointer, (headers[i].end_pointer - headers[i].start_pointer) * 32);
ret.push_back(s);
}
}
}
catch (EndOfFileException& e)
{
lastError=_("premature end of file");
logE("premature end of file");
}
}

125
src/engine/fileOps/pps.cpp Normal file
View file

@ -0,0 +1,125 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fileOpsCommon.h"
class DivEngine;
//PPS AY-3-8910 sample bank
/* =======================================
Header
=======================================
0x0000 - 0x0053 14 * {
Pointer to Sample Data Start (1h)
Length of Sample Data (1h)
"Pitch"(?) (1b)
Volume Reduction (1b)
}
(0x0000 0x0000 [0x00 0x00] -> no sample for this instrument ID)
}
=======================================
Body
=======================================
Stream of Sample Data {
4-Bit Unsigned
(afaict)
Mono
16Hz
(based on tests, maybe alternatively 8kHz)
} */
#define PPS_BANK_SIZE 14
#define PPS_SAMPLE_RATE 16000
typedef struct
{
uint16_t start_pointer;
uint16_t sample_length;
uint8_t _pitch;
uint8_t _vol;
} PPS_HEADER;
void DivEngine::loadPPS(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
{
try
{
reader.seek(0, SEEK_SET);
PPS_HEADER headers[PPS_BANK_SIZE];
for(int i = 0; i < PPS_BANK_SIZE; i++)
{
headers[i].start_pointer = (unsigned short)reader.readS();
headers[i].sample_length = (unsigned short)reader.readS();
headers[i]._pitch = (unsigned char)reader.readC();
headers[i]._vol = (unsigned char)reader.readC();
}
for(int i = 0; i < PPS_BANK_SIZE; i++)
{
if(headers[i].start_pointer != 0 || headers[i].sample_length != 0
|| headers[i]._pitch != 0 || headers[i]._vol != 0)
{
DivSample* s = new DivSample;
s->rate = PPS_SAMPLE_RATE;
s->centerRate = PPS_SAMPLE_RATE;
s->depth = DIV_SAMPLE_DEPTH_8BIT;
s->init(headers[i].sample_length * 2); //byte per sample
reader.seek((int)headers[i].start_pointer, SEEK_SET);
int sample_pos = 0;
for(int j = 0; j < headers[i].sample_length; j++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
s->data8[sample_pos] = (curr_byte >> 4) | (curr_byte & 0xf0);
s->data8[sample_pos] += 0x80;
sample_pos++;
s->data8[sample_pos] = (curr_byte << 4) | (curr_byte & 0xf);
s->data8[sample_pos] += 0x80;
sample_pos++;
}
ret.push_back(s);
logI("pps: start %d len %d", headers[i].start_pointer, headers[i].sample_length);
}
}
}
catch (EndOfFileException& e)
{
lastError=_("premature end of file");
logE("premature end of file");
}
}

158
src/engine/fileOps/pvi.cpp Normal file
View file

@ -0,0 +1,158 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fileOpsCommon.h"
class DivEngine;
//PVI YM2608 ADPCM-B sample bank
/* =======================================
General
=======================================
ADPCM RAM addresses: see docs/common.txt {
address_start = 0x0000
file_header_size = 0x0210
}
=======================================
Header
=======================================
0x0000 Identifier (4b)
"PVI2"
0x0004 - 0x0007 Unknown Settings (4b)
Unknown mappings PCME switches <-> Values
"0x10 0x00 0x10 0x02" in all example files
First 1h may be Start Address in ADPCM RAM?
0x0008 - 0x0009 "Delta-N" playback frequency (1h)
Default 0x49BA "== 16kHz"??
0x000A Unknown (1b)
RAM type? (Docs indicate values 0 or 8, all example files have value 0x02?)
0x000B Amount of defined Samples (1b)
0x000C - 0x000F Unknown (4b)
Padding?
"0x00 0x00 0x00 0x00" in all example files
0x0010 - 0x020F 128 * {
Start of Sample (32b blocks) in ADPCM RAM (1h)
End of Sample (32b blocks) in ADPCM RAM (1h)
(0x0000 0x0000 -> no sample for this instrument ID)
}
=======================================
Body
=======================================
Stream of Sample Data {
Yamaha ADPCM-B encoding (4-Bit Signed ADPCM)
Mono
Sample rate as specified earlier
(examples i have seems Stereo or 32kHz despite "16kHz" playback frequency setting?)
} */
#define PVIV2_FILE_SIG "PVI2"
#define PVIV1_FILE_SIG "PVI1"
#define PVI_BANK_SIZE 128
#define PVI_SAMPLE_RATE 16000
typedef struct
{
uint16_t start_pointer;
uint16_t end_pointer;
} PVI_HEADER;
#define UNUSED(x) (void)(x)
#define ADPCM_DATA_START 0x0210
void DivEngine::loadPVI(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
{
try
{
reader.seek(0, SEEK_SET);
String file_sig = reader.readString(4);
if(file_sig != PVIV1_FILE_SIG && file_sig != PVIV2_FILE_SIG) return;
unsigned int unknown_settings = (unsigned int)reader.readI();
UNUSED(unknown_settings);
unsigned short delta_n = (unsigned short)reader.readS();
UNUSED(delta_n);
unsigned char one_byte = (unsigned char)reader.readC();
UNUSED(one_byte);
unsigned char amount_of_samples = (unsigned char)reader.readC();
UNUSED(amount_of_samples);
unsigned int padding = (unsigned int)reader.readI();
UNUSED(padding);
PVI_HEADER headers[PVI_BANK_SIZE];
for(int i = 0; i < PVI_BANK_SIZE; i++)
{
headers[i].start_pointer = (unsigned short)reader.readS();
headers[i].end_pointer = (unsigned short)reader.readS();
}
for(int i = 0; i < PVI_BANK_SIZE; i++)
{
if((headers[i].start_pointer != 0 || headers[i].end_pointer != 0) && headers[i].start_pointer < headers[i].end_pointer)
{
DivSample* s = new DivSample;
s->rate = PVI_SAMPLE_RATE;
s->centerRate = PVI_SAMPLE_RATE;
s->depth = DIV_SAMPLE_DEPTH_ADPCM_B;
s->init((headers[i].end_pointer - headers[i].start_pointer) * 32 * 2);
int sample_pos = 0;
int sample_length = (headers[i].end_pointer - headers[i].start_pointer) * 32;
reader.seek(ADPCM_DATA_START + headers[i].start_pointer * 32, SEEK_SET);
for(int j = 0; j < sample_length; j++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
//curr_byte=(curr_byte<<4)|(curr_byte>>4);
s->dataB[sample_pos] = curr_byte;
sample_pos++;
}
logI("pvi: start %d end %d len in bytes %d", headers[i].start_pointer, headers[i].end_pointer, (headers[i].end_pointer - headers[i].start_pointer) * 32);
ret.push_back(s);
}
}
}
catch (EndOfFileException& e)
{
lastError=_("premature end of file");
logE("premature end of file");
}
}

155
src/engine/fileOps/pzi.cpp Normal file
View file

@ -0,0 +1,155 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fileOpsCommon.h"
class DivEngine;
//PZI 8-bit PCM sample bank
/* =======================================
Header
=======================================
0x0000 Identifier (4b)
"PZI1"
0x0004 - 0x001F Unknown (28b)
Part of identifier? Settings?
All (\0)s in all example files
0x0020 - 0x091F 128 * {
Start of Sample after header (2h)
Length of Sample (2h)
Offset of loop start from sample start (2h)
Offset of loop end from sample start (2h)
Sample rate (1h)
(0xFFFFFFFF 0xFFFFFFFF loop offsets -> no loop information)
}
=======================================
Body
=======================================
Stream of Sample Data {
Unsigned 8-Bit
Mono
Sample rate as specified in header
} */
#define PZI_BANK_SIZE 128
#define PZI_FILE_SIG "PZI1"
#define NO_LOOP (0xFFFFFFFFU)
#define SAMPLE_DATA_OFFSET 0x0920
#define MAX_SANITY_CAP 9999999
#define HEADER_JUNK_SIZE 28
typedef struct
{
uint32_t start_pointer;
uint32_t sample_length;
uint32_t loop_start;
uint32_t loop_end;
uint16_t sample_rate;
} PZI_HEADER;
#define UNUSED(x) (void)(x)
void DivEngine::loadPZI(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
{
try
{
reader.seek(0, SEEK_SET);
PZI_HEADER headers[PZI_BANK_SIZE];
String file_sig = reader.readString(4);
if(file_sig != PZI_FILE_SIG) return;
for (int i = 0; i < HEADER_JUNK_SIZE; i++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
UNUSED(curr_byte);
}
for(int i = 0; i < PZI_BANK_SIZE; i++)
{
headers[i].start_pointer = (unsigned int)reader.readI();
headers[i].sample_length = (unsigned int)reader.readI();
headers[i].loop_start = (unsigned int)reader.readI();
headers[i].loop_end = (unsigned int)reader.readI();
headers[i].sample_rate = (unsigned short)reader.readS();
}
for(int i = 0; i < PZI_BANK_SIZE; i++)
{
if (headers[i].start_pointer < MAX_SANITY_CAP && headers[i].sample_length < MAX_SANITY_CAP &&
headers[i].loop_start < MAX_SANITY_CAP && headers[i].loop_end < MAX_SANITY_CAP &&
headers[i].start_pointer > 0 && headers[i].sample_length > 0)
{
DivSample* s = new DivSample;
s->rate = headers[i].sample_rate;
s->centerRate = headers[i].sample_rate;
s->depth = DIV_SAMPLE_DEPTH_8BIT;
s->init(headers[i].sample_length); //byte per sample
reader.seek((int)headers[i].start_pointer + SAMPLE_DATA_OFFSET, SEEK_SET);
int sample_pos = 0;
for (uint32_t j = 0; j < headers[i].sample_length; j++)
{
unsigned char curr_byte = (unsigned char)reader.readC();
curr_byte += 0x80;
s->data8[sample_pos] = curr_byte;
sample_pos++;
}
if (headers[i].loop_start != NO_LOOP && headers[i].loop_end != NO_LOOP)
{
s->loop = true;
s->loopMode = DIV_SAMPLE_LOOP_FORWARD;
s->loopStart = headers[i].loop_start;
s->loopEnd = headers[i].loop_end;
}
ret.push_back(s);
logI("pzi: start %d len %d sample rate %d loop start %d loop end %d", headers[i].start_pointer, headers[i].sample_length,
headers[i].sample_rate, headers[i].loop_start, headers[i].loop_end);
}
}
}
catch (EndOfFileException& e)
{
lastError=_("premature end of file");
logE("premature end of file");
}
}

View file

@ -19,21 +19,6 @@
#include "fileOpsCommon.h"
// SBI and some other OPL containers
struct sbi_t {
uint8_t Mcharacteristics,
Ccharacteristics,
Mscaling_output,
Cscaling_output,
Meg_AD,
Ceg_AD,
Meg_SR,
Ceg_SR,
Mwave,
Cwave,
FeedConnect;
};
static void readSbiOpData(sbi_t& sbi, SafeReader& reader) {
sbi.Mcharacteristics = reader.readC();
sbi.Ccharacteristics = reader.readC();
@ -51,6 +36,7 @@ static void readSbiOpData(sbi_t& sbi, SafeReader& reader) {
bool DivEngine::loadS3M(unsigned char* file, size_t len) {
struct InvalidHeaderException {};
bool success=false;
bool opl2=!getConfInt("s3mOPL3",0);
char magic[4]={0,0,0,0};
SafeReader reader=SafeReader(file,len);
warnings="";
@ -273,11 +259,16 @@ bool DivEngine::loadS3M(unsigned char* file, size_t len) {
bool hasPCM=false;
bool hasFM=false;
int numChans=0;
int realNumChans=0;
for (int i=0; i<32; i++) {
if (chanSettings[i]==255) continue;
if ((chanSettings[i]&127)>=32) continue;
if ((chanSettings[i]&127)>=16) {
for (int ch=0; ch<32; ch++) {
if (chanSettings[ch]!=255) realNumChans++;
}
for (int ch=0; ch<32; ch++) {
if (chanSettings[ch]==255) continue;
if ((chanSettings[ch]&127)>=32) continue;
if ((chanSettings[ch]&127)>=16) {
hasFM=true;
} else {
hasPCM=true;
@ -287,34 +278,69 @@ bool DivEngine::loadS3M(unsigned char* file, size_t len) {
if (hasFM && hasPCM) break;
}
int pcmChan=hasFM?9:0;
int pcmChan=hasFM?(opl2 ? 9 : 18):0;
int fmChan=hasPCM?32:0;
int invalidChan=40;
for (int i=0; i<32; i++) {
if (chanSettings[i]==255) {
chanMap[i]=invalidChan++;
for (int ch=0; ch<32; ch++) {
if (chanSettings[ch]==255) {
chanMap[ch]=invalidChan++;
continue;
}
if ((chanSettings[i]&127)>=32) {
chanMap[i]=invalidChan++;
if ((chanSettings[ch]&127)>=32) {
chanMap[ch]=invalidChan++;
continue;
}
if ((chanSettings[i]&127)>=16) {
chanMap[i]=fmChan++;
if ((chanSettings[ch]&127)>=16) {
chanMap[ch]=fmChan++;
} else {
chanMap[i]=pcmChan++;
chanMap[ch]=pcmChan++;
}
}
char buffer[40];
int chanIndex = 1;
if (hasPCM) {
for (int i=pcmChan; i<32; i++) {
ds.subsong[0]->chanShow[i]=false;
ds.subsong[0]->chanShowChanOsc[i]=false;
for(int ch = 0; ch < pcmChan - (realNumChans - (hasFM ? 9 : 0)); ch++)
{
ds.subsong[0]->chanShow[ch]=false;
ds.subsong[0]->chanShowChanOsc[ch]=false;
}
for (int ch=pcmChan; ch<32; ch++) {
ds.subsong[0]->chanShow[ch]=false;
ds.subsong[0]->chanShowChanOsc[ch]=false;
}
for(int ch = 0; ch < 32; ch++)
{
if(ds.subsong[0]->chanShow[ch])
{
snprintf(buffer, 40, _("Channel %d"), chanIndex);
ds.subsong[0]->chanName[ch] = buffer;
chanIndex++;
}
}
}
if (hasFM && !opl2) {
for (int ch=(hasPCM?32:0) + 9; ch<(hasPCM?32:0) + 18; ch++) {
ds.subsong[0]->chanShow[ch]=false;
ds.subsong[0]->chanShowChanOsc[ch]=false;
}
chanIndex = 1;
for (int ch=(hasPCM?32:0); ch<(hasPCM?32:0) + 9; ch++) {
snprintf(buffer, 40, _("FM %d"), chanIndex);
ds.subsong[0]->chanName[ch] = buffer;
chanIndex++;
}
}
logV("numChans: %d",numChans);
logV("realNumChans: %d",realNumChans);
ds.systemName="PC";
if (hasPCM) {
@ -327,7 +353,7 @@ bool DivEngine::loadS3M(unsigned char* file, size_t len) {
ds.systemLen++;
}
if (hasFM) {
ds.system[ds.systemLen]=DIV_SYSTEM_OPL2;
ds.system[ds.systemLen]=opl2 ? DIV_SYSTEM_OPL2 : DIV_SYSTEM_OPL3;
ds.systemVol[ds.systemLen]=1.0f;
ds.systemPan[ds.systemLen]=0;
ds.systemLen++;

View file

@ -24,10 +24,12 @@
#include "sfWrapper.h"
#endif
DivSample* DivEngine::sampleFromFile(const char* path) {
std::vector<DivSample*> DivEngine::sampleFromFile(const char* path) {
std::vector<DivSample*> ret;
if (song.sample.size()>=256) {
lastError="too many samples!";
return NULL;
return ret;
}
BUSY_BEGIN;
warnings="";
@ -58,6 +60,110 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
}
extS+=i;
}
if(extS == ".pps" || extS == ".ppc" || extS == ".pvi" ||
extS == ".pdx" || extS == ".pzi" || extS == ".p86" ||
extS == ".p") //sample banks!
{
String stripPath;
const char* pathReduxEnd=strrchr(pathRedux,'.');
if (pathReduxEnd==NULL) {
stripPath=pathRedux;
} else {
for (const char* i=pathRedux; i!=pathReduxEnd && (*i); i++) {
stripPath+=*i;
}
}
FILE* f=ps_fopen(path,"rb");
if (f==NULL) {
lastError=strerror(errno);
return ret;
}
unsigned char* buf;
ssize_t len;
if (fseek(f,0,SEEK_END)!=0) {
lastError=strerror(errno);
fclose(f);
return ret;
}
len=ftell(f);
if (len<0) {
lastError=strerror(errno);
fclose(f);
return ret;
}
if (len==(SIZE_MAX>>1)) {
lastError=strerror(errno);
fclose(f);
return ret;
}
if (len==0) {
lastError=strerror(errno);
fclose(f);
return ret;
}
if (fseek(f,0,SEEK_SET)!=0) {
lastError=strerror(errno);
fclose(f);
return ret;
}
buf=new unsigned char[len];
if (fread(buf,1,len,f)!=(size_t)len) {
logW("did not read entire sample bank file buffer!");
lastError=_("did not read entire sample bank file!");
delete[] buf;
return ret;
}
fclose(f);
SafeReader reader = SafeReader(buf,len);
if(extS == ".pps")
{
loadPPS(reader,ret,stripPath);
}
if(extS == ".ppc")
{
loadPPC(reader,ret,stripPath);
}
if(extS == ".pvi")
{
loadPVI(reader,ret,stripPath);
}
if(extS == ".pdx")
{
loadPDX(reader,ret,stripPath);
}
if(extS == ".pzi")
{
loadPZI(reader,ret,stripPath);
}
if(extS == ".p86")
{
loadP86(reader,ret,stripPath);
}
if(extS == ".p")
{
loadP(reader,ret,stripPath);
}
if((int)ret.size() > 0)
{
int counter = 0;
for(DivSample* s: ret)
{
s->name = fmt::sprintf("%s sample %d", stripPath, counter);
counter++;
}
}
delete[] buf; //done with buffer
BUSY_END;
return ret;
}
if (extS==".dmc" || extS==".brr") { // read as .dmc or .brr
size_t len=0;
DivSample* sample=new DivSample;
@ -68,7 +174,7 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError=fmt::sprintf("could not open file! (%s)",strerror(errno));
delete sample;
return NULL;
return ret;
}
if (fseek(f,0,SEEK_END)<0) {
@ -76,7 +182,7 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError=fmt::sprintf("could not get file length! (%s)",strerror(errno));
delete sample;
return NULL;
return ret;
}
len=ftell(f);
@ -86,7 +192,7 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError="file is empty!";
delete sample;
return NULL;
return ret;
}
if (len==(SIZE_MAX>>1)) {
@ -94,7 +200,7 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError="file is invalid!";
delete sample;
return NULL;
return ret;
}
if (fseek(f,0,SEEK_SET)<0) {
@ -102,7 +208,7 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError=fmt::sprintf("could not seek to beginning of file! (%s)",strerror(errno));
delete sample;
return NULL;
return ret;
}
if (extS==".dmc") {
@ -120,7 +226,7 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError="wait... is that right? no I don't think so...";
delete sample;
return NULL;
return ret;
}
unsigned char* dataBuf=sample->dataDPCM;
@ -147,14 +253,14 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError="BRR sample is empty!";
delete sample;
return NULL;
return ret;
}
} else if ((len%9)!=0) {
fclose(f);
BUSY_END;
lastError="possibly corrupt BRR sample!";
delete sample;
return NULL;
return ret;
}
}
@ -163,16 +269,17 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
BUSY_END;
lastError=fmt::sprintf("could not read file! (%s)",strerror(errno));
delete sample;
return NULL;
return ret;
}
BUSY_END;
return sample;
ret.push_back(sample);
return ret;
}
}
#ifndef HAVE_SNDFILE
lastError="Furnace was not compiled with libsndfile!";
return NULL;
return ret;
#else
SF_INFO si;
SFWrapper sfWrap;
@ -186,13 +293,13 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
} else {
lastError=fmt::sprintf("could not open file! (%s)\nif this is raw sample data, you may import it by right-clicking the Load Sample icon and selecting \"import raw\".",sf_error_number(err));
}
return NULL;
return ret;
}
if (si.frames>16777215) {
lastError="this sample is too big! max sample size is 16777215.";
sfWrap.doClose();
BUSY_END;
return NULL;
return ret;
}
void* buf=NULL;
sf_count_t sampleLen=sizeof(short);
@ -294,11 +401,12 @@ DivSample* DivEngine::sampleFromFile(const char* path) {
sample->loop=false;
}
if (sample->centerRate<4000) sample->centerRate=4000;
if (sample->centerRate>64000) sample->centerRate=64000;
if (sample->centerRate<100) sample->centerRate=100;
if (sample->centerRate>384000) sample->centerRate=384000;
sfWrap.doClose();
BUSY_END;
return sample;
ret.push_back(sample);
return ret;
#endif
}

View file

@ -363,6 +363,141 @@ void DivInstrument::writeFeatureFM(SafeWriter* w, bool fui) {
FEATURE_END;
}
bool MemPatch::calcDiff(const void* pre, const void* post, size_t inputSize) {
bool diffValid=false;
size_t firstDiff=0;
size_t lastDiff=0;
const unsigned char* preBytes=(const unsigned char*)pre;
const unsigned char* postBytes=(const unsigned char*)post;
// @NOTE: consider/profile using a memcmp==0 check to early-out, if it's potentially faster
// for the common case, which is no change
for (size_t ii=0; ii<inputSize; ++ii) {
if (preBytes[ii] != postBytes[ii]) {
lastDiff=ii;
firstDiff=diffValid ? firstDiff : ii;
diffValid=true;
}
}
if (diffValid) {
offset=firstDiff;
size=lastDiff - firstDiff + 1;
data=new unsigned char[size];
// the diff is to make pre into post (MemPatch is general, not specific to
// undo), so copy from postBytes
memcpy(data, postBytes+offset, size);
}
return diffValid;
}
void MemPatch::applyAndReverse(void* target, size_t targetSize) {
if (size==0) return;
if (offset+size>targetSize) {
logW("MemPatch (offset %d, size %d) exceeds target size (%d), can't apply!",offset,size,targetSize);
return;
}
unsigned char* targetBytes=(unsigned char*)target;
// swap this->data and its segment on target
for (size_t ii=0; ii<size; ++ii) {
unsigned char tmp=targetBytes[offset+ii];
targetBytes[offset+ii] = data[ii];
data[ii] = tmp;
}
}
void DivInstrumentUndoStep::applyAndReverse(DivInstrument* target) {
if (nameValid) {
name.swap(target->name);
}
podPatch.applyAndReverse((DivInstrumentPOD*)target, sizeof(DivInstrumentPOD));
}
bool DivInstrumentUndoStep::makeUndoPatch(size_t processTime_, const DivInstrument* pre, const DivInstrument* post) {
processTime=processTime_;
// create the patch that will make post into pre
podPatch.calcDiff((const DivInstrumentPOD*)post, (const DivInstrumentPOD*)pre, sizeof(DivInstrumentPOD));
if (pre->name!=post->name) {
nameValid=true;
name=pre->name;
}
return nameValid || podPatch.isValid();
}
bool DivInstrument::recordUndoStepIfChanged(size_t processTime, const DivInstrument* old) {
DivInstrumentUndoStep step;
// generate a patch to go back to old
if (step.makeUndoPatch(processTime, old, this)) {
// make room
if (undoHist.size()>=undoHist.capacity()) {
delete undoHist.front();
undoHist.pop_front();
}
// clear redo
while (!redoHist.empty()) {
delete redoHist.back();
redoHist.pop_back();
}
DivInstrumentUndoStep* stepPtr=new DivInstrumentUndoStep;
*stepPtr=step;
step.podPatch.data=NULL; // don't let it delete the data ptr that's been copied!
undoHist.push_back(stepPtr);
// logI("DivInstrument::undoHist push (%u off, %u size)", stepPtr->podPatch.offset, stepPtr->podPatch.size);
return true;
}
return false;
}
int DivInstrument::undo() {
if (undoHist.empty()) return 0;
DivInstrumentUndoStep* step=undoHist.back();
undoHist.pop_back();
// logI("DivInstrument::undo (%u off, %u size)", step->podPatch.offset, step->podPatch.size);
step->applyAndReverse(this);
// make room
if (redoHist.size()>=redoHist.capacity()) {
DivInstrumentUndoStep* step=redoHist.front();
delete step;
redoHist.pop_front();
}
redoHist.push_back(step);
return 1;
}
int DivInstrument::redo() {
if (redoHist.empty()) return 0;
DivInstrumentUndoStep* step = redoHist.back();
redoHist.pop_back();
// logI("DivInstrument::redo (%u off, %u size)", step->podPatch.offset, step->podPatch.size);
step->applyAndReverse(this);
// make room
if (undoHist.size()>=undoHist.capacity()) {
DivInstrumentUndoStep* step=undoHist.front();
delete step;
undoHist.pop_front();
}
undoHist.push_back(step);
return 1;
}
void DivInstrument::writeMacro(SafeWriter* w, const DivInstrumentMacro& m) {
if (!m.len) return;
@ -3333,3 +3468,28 @@ bool DivInstrument::saveDMP(const char* path) {
w->finish();
return true;
}
DivInstrument::~DivInstrument() {
// free undoHist/redoHist
while (!undoHist.empty()) {
delete undoHist.back();
undoHist.pop_back();
}
while (!redoHist.empty()) {
delete redoHist.back();
redoHist.pop_back();
}
}
DivInstrument::DivInstrument( const DivInstrument& ins ) {
// undo/redo history is specifically not copied
*(DivInstrumentPOD*)this=ins;
name=ins.name;
}
DivInstrument& DivInstrument::operator=( const DivInstrument& ins ) {
// undo/redo history is specifically not copied
*(DivInstrumentPOD*)this=ins;
name=ins.name;
return *this;
}

View file

@ -23,8 +23,10 @@
#include "dataErrors.h"
#include "../ta-utils.h"
#include "../pch.h"
#include "../fixedQueue.h"
struct DivSong;
struct DivInstrument;
// NOTICE!
// before adding new instrument types to this struct, please ask me first.
@ -862,8 +864,7 @@ struct DivInstrumentSID2 {
noiseMode(0) {}
};
struct DivInstrument {
String name;
struct DivInstrumentPOD {
DivInstrumentType type;
DivInstrumentFM fm;
DivInstrumentSTD std;
@ -882,6 +883,77 @@ struct DivInstrument {
DivInstrumentPowerNoise powernoise;
DivInstrumentSID2 sid2;
DivInstrumentPOD() :
type(DIV_INS_FM) {
}
};
struct MemPatch {
MemPatch() :
data(NULL)
, offset(0)
, size(0) {
}
~MemPatch() {
if (data) {
delete[] data;
data=NULL;
}
}
bool calcDiff(const void* pre, const void* post, size_t size);
void applyAndReverse(void* target, size_t inputSize);
bool isValid() const { return size>0; }
unsigned char* data;
size_t offset;
size_t size;
};
struct DivInstrumentUndoStep {
DivInstrumentUndoStep() :
name(""),
nameValid(false),
processTime(0) {
}
MemPatch podPatch;
String name;
bool nameValid;
size_t processTime;
void applyAndReverse(DivInstrument* target);
bool makeUndoPatch(size_t processTime_, const DivInstrument* pre, const DivInstrument* post);
};
struct DivInstrument : DivInstrumentPOD {
String name;
DivInstrument() :
name("") {
// clear and construct DivInstrumentPOD so it doesn't have any garbage in the padding
memset((unsigned char*)(DivInstrumentPOD*)this, 0, sizeof(DivInstrumentPOD));
new ((DivInstrumentPOD*)this) DivInstrumentPOD;
}
~DivInstrument();
/**
* copy/assignment to specifically avoid leaking or dangling pointers to undo step
*/
DivInstrument( const DivInstrument& ins );
DivInstrument& operator=( const DivInstrument& ins );
/**
* undo stuff
*/
FixedQueue<DivInstrumentUndoStep*, 128> undoHist;
FixedQueue<DivInstrumentUndoStep*, 128> redoHist;
bool recordUndoStepIfChanged(size_t processTime, const DivInstrument* old);
int undo();
int redo();
/**
* these are internal functions.
*/
@ -966,9 +1038,5 @@ struct DivInstrument {
* @return whether it was successful.
*/
bool saveDMP(const char* path);
DivInstrument():
name(""),
type(DIV_INS_FM) {
}
};
#endif

View file

@ -761,6 +761,25 @@ int DivPlatformArcade::dispatch(DivCommand c) {
immWrite(0x19,0x80|pmDepth);
break;
}
case DIV_CMD_FM_OPMASK:
switch (c.value>>4) {
case 1:
case 2:
case 3:
case 4:
chan[c.chan].opMask&=~(1<<((c.value>>4)-1));
if (c.value&15) {
chan[c.chan].opMask|=(1<<((c.value>>4)-1));
}
break;
default:
chan[c.chan].opMask=c.value&15;
break;
}
if (chan[c.chan].active) {
chan[c.chan].opMaskChanged=true;
}
break;
case DIV_CMD_FM_HARD_RESET:
chan[c.chan].hardReset=c.value;
break;

View file

@ -113,14 +113,15 @@ const unsigned char dacLogTableAY[256]={
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15
};
void DivPlatformAY8910::runDAC() {
void DivPlatformAY8910::runDAC(int runRate) {
if (runRate==0) runRate=dacRate;
for (int i=0; i<3; i++) {
if (chan[i].active && (chan[i].curPSGMode.val&8) && chan[i].dac.sample!=-1) {
chan[i].dac.period+=chan[i].dac.rate;
bool end=false;
bool changed=false;
int prevOut=chan[i].dac.out;
while (chan[i].dac.period>dacRate && !end) {
while (chan[i].dac.period>runRate && !end) {
DivSample* s=parent->getSample(chan[i].dac.sample);
if (s->samples<=0 || chan[i].dac.pos<0 || chan[i].dac.pos>=(int)s->samples) {
chan[i].dac.sample=-1;
@ -143,7 +144,7 @@ void DivPlatformAY8910::runDAC() {
end=true;
break;
}
chan[i].dac.period-=dacRate;
chan[i].dac.period-=runRate;
}
if (changed && !end) {
if (!isMuted[i]) {
@ -154,44 +155,79 @@ void DivPlatformAY8910::runDAC() {
}
}
void DivPlatformAY8910::runTFX() {
if (selCore) return;
void DivPlatformAY8910::runTFX(int runRate) {
/*
developer's note: if you are checking for intellivision
make sure to add "&& selCore"
because for some reason, the register remap doesn't work
when the user uses AtomicSSG core
*/
float counterRatio=1.0;
if (runRate!=0) counterRatio=(double)rate/(double)runRate;
int timerPeriod, output;
for (int i=0; i<3; i++) {
if (chan[i].active && (chan[i].curPSGMode.val&16) && !(chan[i].curPSGMode.val&8) && chan[i].tfx.mode!=-1) {
chan[i].tfx.counter += 1;
if (chan[i].tfx.mode == -1 && !isMuted[i]) {
/*
bug: if in the timer FX macro the user enables
and then disables PWM while there is no volume macro
there is now a random chance that the resulting output
is silent or has volume set incorrectly
i've tried to implement a fix, but it seems to be
ineffective, so...
TODO: actually implement a proper fix
*/
if (intellivision && chan[i].curPSGMode.getEnvelope()) {
immWrite(0x08+i,(chan[i].outVol&0xc)<<2);
continue;
} else {
immWrite(0x08+i,(chan[i].outVol&15)|((chan[i].curPSGMode.getEnvelope())<<2));
continue;
}
}
chan[i].tfx.counter += counterRatio;
if (chan[i].tfx.counter >= chan[i].tfx.period && chan[i].tfx.mode == 0) {
chan[i].tfx.counter = 0;
chan[i].tfx.counter -= chan[i].tfx.period;
chan[i].tfx.out ^= 1;
output = MAX(0, ((chan[i].tfx.out) ? (chan[i].outVol&15) : (chan[i].tfx.lowBound-(15-chan[i].outVol))));
output &= 15;
output = ((chan[i].tfx.out) ? chan[i].outVol : (chan[i].tfx.lowBound-(15-chan[i].outVol)));
// TODO: fix this stupid crackling noise that happens
// everytime the volume changes
output = (output <= 0) ? 0 : output; // underflow
output = (output >= 15) ? 15 : output; // overflow
output &= 15; // i don't know if i need this but i'm too scared to remove it
if (!isMuted[i]) {
immWrite(0x08+i,output|(chan[i].curPSGMode.getEnvelope()<<2));
if (intellivision && selCore) {
immWrite(0x0b+i,(output&0xc)<<2);
} else {
immWrite(0x08+i,output|(chan[i].curPSGMode.getEnvelope()<<2));
}
}
}
if (chan[i].tfx.counter >= chan[i].tfx.period && chan[i].tfx.mode == 1) {
chan[i].tfx.counter = 0;
chan[i].tfx.counter -= chan[i].tfx.period;
if (!isMuted[i]) {
immWrite(0xd, ayEnvMode);
if (intellivision && selCore) {
immWrite(0xa, ayEnvMode);
} else {
immWrite(0xd, ayEnvMode);
}
}
}
if (chan[i].tfx.counter >= chan[i].tfx.period && chan[i].tfx.mode == 2) {
chan[i].tfx.counter = 0;
}
if (chan[i].tfx.mode == -1 && !isMuted[i]) {
if (intellivision && chan[i].curPSGMode.getEnvelope()) {
immWrite(0x08+i,(chan[i].outVol&0xc)<<2);
} else {
immWrite(0x08+i,(chan[i].outVol&15)|((chan[i].curPSGMode.getEnvelope())<<2));
}
chan[i].tfx.counter -= chan[i].tfx.period;
}
}
if (chan[i].tfx.num > 0) {
timerPeriod = chan[i].freq*chan[i].tfx.den/chan[i].tfx.num;
} else {
timerPeriod = chan[i].freq*chan[i].tfx.den;
}
timerPeriod = chan[i].freq*chan[i].tfx.den/chan[i].tfx.num;
} else {
timerPeriod = chan[i].freq*chan[i].tfx.den;
}
if (chan[i].tfx.num > 0 && chan[i].tfx.den > 0) chan[i].tfx.period=timerPeriod+chan[i].tfx.offset;
// stupid pitch correction because:
// YM2149 half-clock and Sunsoft 5B: timers run an octave too high
// on AtomicSSG core timers run 2 octaves too high
if (clockSel || sunsoft) chan[i].tfx.period = chan[i].tfx.period * 2;
if (selCore) chan[i].tfx.period = chan[i].tfx.period * 4;
}
}
@ -294,12 +330,9 @@ void DivPlatformAY8910::acquire(short** buf, size_t len) {
void DivPlatformAY8910::fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len) {
writes.clear();
int rate=(int)(chipClock/sRate);
for (size_t i=0; i<len; i++) {
for (int h=0; h<rate; h++) {
runDAC();
runTFX();
}
runDAC(sRate);
runTFX(sRate);
while (!writes.empty()) {
QueuedWrite& w=writes.front();
stream.push_back(DivDelayedWrite(i,w.addr,w.val));
@ -388,12 +421,13 @@ void DivPlatformAY8910::tick(bool sysTick) {
if (chan[i].std.phaseReset.had) {
if (chan[i].std.phaseReset.val==1) {
chan[i].tfx.counter = 0;
chan[i].tfx.out = 0;
if (chan[i].nextPSGMode.val&8) {
if (dumpWrites) addWrite(0xffff0002+(i<<8),0);
//if (dumpWrites) addWrite(0xffff0002+(i<<8),0);
if (chan[i].dac.sample<0 || chan[i].dac.sample>=parent->song.sampleLen) {
if (dumpWrites) {
rWrite(0x08+i,0);
addWrite(0xffff0000+(i<<8),chan[i].dac.sample);
//addWrite(0xffff0000+(i<<8),chan[i].dac.sample);
}
if (chan[i].dac.setPos) {
chan[i].dac.setPos=false;
@ -483,7 +517,7 @@ void DivPlatformAY8910::tick(bool sysTick) {
}
}
chan[i].dac.rate=((double)rate*((sunsoft||clockSel)?8.0:16.0))/(double)(MAX(1,off*chan[i].freq));
if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].dac.rate);
//if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].dac.rate);
}
if (chan[i].freq<0) chan[i].freq=0;
if (chan[i].freq>4095) chan[i].freq=4095;
@ -570,12 +604,12 @@ int DivPlatformAY8910::dispatch(DivCommand c) {
}
if (chan[c.chan].dac.sample<0 || chan[c.chan].dac.sample>=parent->song.sampleLen) {
chan[c.chan].dac.sample=-1;
if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
//if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
break;
} else {
if (dumpWrites) {
rWrite(0x08+c.chan,0);
addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
//addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
}
}
if (chan[c.chan].dac.setPos) {
@ -603,10 +637,10 @@ int DivPlatformAY8910::dispatch(DivCommand c) {
chan[c.chan].dac.sample=12*sampleBank+chan[c.chan].note%12;
if (chan[c.chan].dac.sample>=parent->song.sampleLen) {
chan[c.chan].dac.sample=-1;
if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
//if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
break;
} else {
if (dumpWrites) addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
//if (dumpWrites) addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dac.sample);
}
if (chan[c.chan].dac.setPos) {
chan[c.chan].dac.setPos=false;
@ -617,7 +651,7 @@ int DivPlatformAY8910::dispatch(DivCommand c) {
chan[c.chan].dac.rate=parent->getSample(chan[c.chan].dac.sample)->rate*2048;
if (dumpWrites) {
rWrite(0x08+c.chan,0);
addWrite(0xffff0001+(c.chan<<8),chan[c.chan].dac.rate);
//addWrite(0xffff0001+(c.chan<<8),chan[c.chan].dac.rate);
}
chan[c.chan].dac.furnaceDAC=false;
}
@ -652,7 +686,7 @@ int DivPlatformAY8910::dispatch(DivCommand c) {
}
case DIV_CMD_NOTE_OFF:
chan[c.chan].dac.sample=-1;
if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
//if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
chan[c.chan].nextPSGMode.val&=~8;
chan[c.chan].keyOff=true;
chan[c.chan].active=false;
@ -726,12 +760,10 @@ int DivPlatformAY8910::dispatch(DivCommand c) {
break;
}
case DIV_CMD_STD_NOISE_MODE:
if (c.value&0xf0 && !(chan[c.chan].nextPSGMode.val&8)) {
chan[c.chan].nextPSGMode.val|=16;
chan[c.chan].tfx.mode = (c.value&3);
}
if (!(chan[c.chan].nextPSGMode.val&8)) {
if (c.value<16) {
chan[c.chan].nextPSGMode.val|=16;
chan[c.chan].tfx.mode=(((c.value&0xf0)>>4)&3)-1;
if ((c.value&15)<16) {
chan[c.chan].nextPSGMode.val=(c.value+1)&7;
chan[c.chan].nextPSGMode.val|=chan[c.chan].curPSGMode.val&16;
if (chan[c.chan].active) {
@ -805,9 +837,16 @@ int DivPlatformAY8910::dispatch(DivCommand c) {
updateOutSel(true);
immWrite(14+(c.value?1:0),(c.value?portBVal:portAVal));
break;
case DIV_CMD_AY_AUTO_PWM:
chan[c.chan].tfx.offset=c.value;
case DIV_CMD_AY_NOISE_MASK_AND:
chan[c.chan].tfx.num=c.value>>4;
chan[c.chan].tfx.den=c.value&15;
break;
case DIV_CMD_AY_AUTO_PWM: {
// best way i could find to do signed :/
signed char signVal=c.value;
chan[c.chan].tfx.offset=signVal;
break;
}
case DIV_CMD_SAMPLE_MODE:
if (c.value>0) {
chan[c.chan].nextPSGMode.val|=8;
@ -828,7 +867,7 @@ int DivPlatformAY8910::dispatch(DivCommand c) {
case DIV_CMD_SAMPLE_POS:
chan[c.chan].dac.pos=c.value;
chan[c.chan].dac.setPos=true;
if (dumpWrites) addWrite(0xffff0005,chan[c.chan].dac.pos);
//if (dumpWrites) addWrite(0xffff0005,chan[c.chan].dac.pos);
break;
case DIV_CMD_MACRO_OFF:
chan[c.chan].std.mask(c.value,true);

View file

@ -78,7 +78,9 @@ class DivPlatformAY8910: public DivDispatch {
} dac;
struct TFX {
int period, counter, offset, den, num, mode, lowBound, out;
int period;
float counter;
int offset, den, num, mode, lowBound, out;
TFX():
period(0),
counter(0),
@ -156,8 +158,8 @@ class DivPlatformAY8910: public DivDispatch {
friend void putDispatchChan(void*,int,int);
public:
void runDAC();
void runTFX();
void runDAC(int runRate=0);
void runTFX(int runRate=0);
void setExtClockDiv(unsigned int eclk=COLOR_NTSC, unsigned char ediv=8);
void acquire(short** buf, size_t len);
void fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len);

View file

@ -651,7 +651,7 @@ void DivPlatformGB::reset() {
immWrite(0x26,0x8f);
lastPan=0xff;
immWrite(0x25,procMute());
immWrite(0x24,0x77);
immWrite(0x24,0xff);
antiClickPeriodCount=0;
antiClickWavePos=0;

View file

@ -1463,6 +1463,26 @@ int DivPlatformGenesis::dispatch(DivCommand c) {
}
break;
}
case DIV_CMD_FM_OPMASK:
if (c.chan>=psgChanOffs) break;
switch (c.value>>4) {
case 1:
case 2:
case 3:
case 4:
chan[c.chan].opMask&=~(1<<((c.value>>4)-1));
if (c.value&15) {
chan[c.chan].opMask|=(1<<((c.value>>4)-1));
}
break;
default:
chan[c.chan].opMask=c.value&15;
break;
}
if (chan[c.chan].active) {
chan[c.chan].opMaskChanged=true;
}
break;
case DIV_CMD_FM_HARD_RESET:
if (c.chan>=6) break;
chan[c.chan].hardReset=c.value;

View file

@ -39,14 +39,25 @@ const char** DivPlatformMSM6295::getRegisterSheet() {
}
u8 DivPlatformMSM6295::read_byte(u32 address) {
if (adpcmMem==NULL || address>=getSampleMemCapacity(0)) {
if (adpcmMem==NULL) {
return 0;
}
if (isBanked) {
if (address<0x400) {
return adpcmMem[(bank[(address>>8)&0x3]<<16)|(address&0x3ff)];
unsigned int bankedAddress=(bank[(address>>8)&0x3]<<16)|(address&0x3ff);
if (bankedAddress>=getSampleMemCapacity(0)) {
return 0;
}
return adpcmMem[bankedAddress&0xffffff];
}
return adpcmMem[(bank[(address>>16)&0x3]<<16)|(address&0xffff)];
unsigned int bankedAddress=(bank[(address>>16)&0x3]<<16)|(address&0xffff);
if (bankedAddress>=getSampleMemCapacity(0)) {
return 0;
}
return adpcmMem[bankedAddress&0xffffff];
}
if (address>=getSampleMemCapacity(0)) {
return 0;
}
return adpcmMem[address&0x3ffff];
}

View file

@ -28,7 +28,7 @@ struct _nla_table nla_table;
#define CHIP_DIVIDER 16
#define rWrite(a,v) if (!skipRegisterWrites) {doWrite(a,v); regPool[(a)&0x7f]=v; if (dumpWrites) {addWrite(a,v);} }
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite((a),v)); if (dumpWrites) {addWrite((a),v);} }
const char* regCheatSheetNES[]={
"S0Volume", "4000",
@ -86,10 +86,10 @@ void DivPlatformNES::doWrite(unsigned short addr, unsigned char data) {
unsigned char next=((unsigned char)s->data8[dacPos]+0x80)>>1; \
if (dacAntiClickOn && dacAntiClick<next) { \
dacAntiClick+=8; \
rWrite(0x4011,dacAntiClick); \
doWrite(0x4011,dacAntiClick); \
} else { \
dacAntiClickOn=false; \
rWrite(0x4011,next); \
doWrite(0x4011,next); \
} \
} \
dacPos++; \
@ -108,6 +108,13 @@ void DivPlatformNES::doWrite(unsigned short addr, unsigned char data) {
void DivPlatformNES::acquire_puNES(short** buf, size_t len) {
for (size_t i=0; i<len; i++) {
doPCM;
if (!writes.empty()) {
QueuedWrite w=writes.front();
doWrite(w.addr,w.val);
regPool[w.addr&0x1f]=w.val;
writes.pop();
}
apu_tick(nes,NULL);
nes->apu.odd_cycle=!nes->apu.odd_cycle;
@ -134,6 +141,13 @@ void DivPlatformNES::acquire_NSFPlay(short** buf, size_t len) {
int out2[2];
for (size_t i=0; i<len; i++) {
doPCM;
if (!writes.empty()) {
QueuedWrite w=writes.front();
doWrite(w.addr,w.val);
regPool[w.addr&0x1f]=w.val;
writes.pop();
}
nes1_NP->Tick(8);
nes2_NP->TickFrameSequence(8);
@ -161,6 +175,13 @@ void DivPlatformNES::acquire_NSFPlayE(short** buf, size_t len) {
int out2[2];
for (size_t i=0; i<len; i++) {
doPCM;
if (!writes.empty()) {
QueuedWrite w=writes.front();
doWrite(w.addr,w.val);
regPool[w.addr&0x1f]=w.val;
writes.pop();
}
e1_NP->Tick(8);
e2_NP->TickFrameSequence(8);
@ -297,7 +318,7 @@ void DivPlatformNES::tick(bool sysTick) {
if (chan[i].sweepChanged) {
chan[i].sweepChanged=false;
if (i==0) {
//rWrite(16+i*5,chan[i].sweep);
// rWrite(16+i*5,chan[i].sweep);
}
}
if (i<3) if (chan[i].std.phaseReset.had) {
@ -338,7 +359,7 @@ void DivPlatformNES::tick(bool sysTick) {
}
}
if (chan[i].keyOff) {
//rWrite(16+i*5+2,8);
// rWrite(16+i*5+2,8);
if (i==2) { // triangle
rWrite(0x4000+i*4,0x00);
} else {
@ -400,6 +421,34 @@ void DivPlatformNES::tick(bool sysTick) {
logV("switching bank to %d",dpcmBank);
if (dumpWrites) addWrite(0xffff0004,dpcmBank);
}
// sample custom loop point...
DivSample* lsamp=parent->getSample(dacSample);
// how it works:
// when the initial sample info is written (see above) and playback is launched,
// the parameters (start point in memory and length) are locked until sample end
// is reached.
// thus, if we write new data after just several APU clock cycles, it will be used only when
// sample finishes one full loop.
// thus we can write sample's loop point as "start address" and sample's looped part length
// as "full sample length".
// APU will play full sample once and then repeatedly cycle through the looped part.
// sources:
// https://www.nesdev.org/wiki/APU_DMC
// https://www.youtube.com/watch?v=vB4P8x2Am6Y
if (lsamp->loopEnd>lsamp->loopStart && goingToLoop) {
int loopStartAddr=sampleOffDPCM[dacSample]+(lsamp->loopStart>>3);
int loopLen=(lsamp->loopEnd-lsamp->loopStart)>>3;
rWrite(0x4012,(loopStartAddr>>6)&0xff);
rWrite(0x4013,(loopLen>>4)&0xff);
}
}
} else {
if (nextDPCMFreq>=0) {
@ -450,14 +499,14 @@ int DivPlatformNES::dispatch(DivCommand c) {
}
}
if (c.value!=DIV_NOTE_NULL) {
dacSample=ins->amiga.getSample(c.value);
dacSample=(int)ins->amiga.getSample(c.value);
if (ins->type==DIV_INS_AMIGA) {
chan[c.chan].sampleNote=c.value;
c.value=ins->amiga.getFreq(c.value);
chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
}
} else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
dacSample=ins->amiga.getSample(chan[c.chan].sampleNote);
dacSample=(int)ins->amiga.getSample(chan[c.chan].sampleNote);
if (ins->type==DIV_INS_AMIGA) {
c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
}
@ -790,6 +839,7 @@ float DivPlatformNES::getPostAmp() {
}
void DivPlatformNES::reset() {
while (!writes.empty()) writes.pop();
for (int i=0; i<5; i++) {
chan[i]=DivPlatformNES::Channel();
chan[i].std.setEngine(parent);

View file

@ -24,6 +24,7 @@
#include "sound/nes_nsfplay/nes_apu.h"
#include "sound/nes_nsfplay/5e01_apu.h"
#include "../../fixedQueue.h"
class DivPlatformNES: public DivDispatch {
struct Channel: public SharedChannel<signed char> {
@ -44,6 +45,13 @@ class DivPlatformNES: public DivDispatch {
Channel chan[5];
DivDispatchOscBuffer* oscBuf[5];
bool isMuted[5];
struct QueuedWrite {
unsigned short addr;
unsigned char val;
QueuedWrite(): addr(0), val(0) {}
QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v) {}
};
FixedQueue<QueuedWrite,128> writes;
int dacPeriod, dacRate, dpcmPos;
unsigned int dacPos, dacAntiClick;
int dacSample;

View file

@ -27,7 +27,7 @@ uint8_t DivOPLAInterface::ymfm_external_read(ymfm::access_class type, uint32_t a
if (adpcmBMem==NULL) {
return 0;
}
return adpcmBMem[address&0xffffff];
return adpcmBMem[address&0x3ffff];
default:
return 0;
}

View file

@ -165,8 +165,12 @@ int DivPlatformSMS::snCalcFreq(int ch) {
if (ch==3) CHIP_DIVIDER=noiseDivider;
int easyStartingPeriod=16;
int easyThreshold=round(128.0*12.0*log((chipClock/(easyStartingPeriod*CHIP_DIVIDER))/(0.0625*parent->song.tuning))/log(2.0))-384+64;
if (parent->song.linearPitch==2 && easyNoise && chan[ch].baseFreq+chan[ch].pitch+chan[ch].pitch2>(easyThreshold)) {
int ret=(((easyStartingPeriod<<7))-(chan[ch].baseFreq+chan[ch].pitch+chan[ch].pitch2-(easyThreshold)))>>7;
int curFreq=chan[ch].baseFreq+chan[ch].pitch+chan[ch].pitch2+(chan[ch].arpOff<<7);
if (chan[ch].fixedArp) {
curFreq=chan[ch].baseNoteOverride<<7;
}
if (parent->song.linearPitch==2 && easyNoise && curFreq>easyThreshold) {
int ret=(((easyStartingPeriod<<7))-(curFreq-(easyThreshold)))>>7;
if (ret<0) ret=0;
return ret;
}

View file

@ -27,6 +27,8 @@
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
#define chWrite(c,a,v) {rWrite((a)+(c)*16,v)}
#define rWriteDelay(a,v,d) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v,d)); if (dumpWrites) {addWrite(a,v);} }
#define chWriteDelay(c,a,v,d) {rWrite((a)+(c)*16,v,d)}
#define sampleTableAddr(c) (sampleTableBase+(c)*4)
#define waveTableAddr(c) (sampleTableBase+8*4+(c)*9*16)
@ -77,7 +79,7 @@ void DivPlatformSNES::acquire(short** buf, size_t len) {
dsp.write(w.addr,w.val);
regPool[w.addr&0x7f]=w.val;
writes.pop();
delay=(w.addr==0x5c)?8:1;
delay=w.delay;
}
}
dsp.set_output(out,1);
@ -253,7 +255,18 @@ void DivPlatformSNES::tick(bool sysTick) {
}
}
if (koff!=0) {
rWrite(0x5c,koff);
// TODO: improve
if (antiClick) {
for (int i=0; i<8; i++) {
if (koff&(1<<i)) {
chWrite(i,5,0);
chWrite(i,7,0x9f);
chan[i].shallWriteEnv=true;
}
}
rWriteDelay(0x7e,0,64);
}
rWriteDelay(0x5c,koff,8);
}
if (writeControl) {
unsigned char control=(noiseFreq&0x1f)|(echoOn?0:0x20);
@ -314,10 +327,7 @@ void DivPlatformSNES::tick(bool sysTick) {
}
}
if (koff!=0) {
rWrite(0x5c,0);
}
if (kon!=0) {
rWrite(0x4c,kon);
rWriteDelay(0x5c,0,8);
}
for (int i=0; i<8; i++) {
if (chan[i].shallWriteVol) {
@ -325,6 +335,9 @@ void DivPlatformSNES::tick(bool sysTick) {
chan[i].shallWriteVol=false;
}
}
if (kon!=0) {
rWrite(0x4c,kon);
}
}
int DivPlatformSNES::dispatch(DivCommand c) {
@ -841,6 +854,8 @@ void DivPlatformSNES::reset() {
memcpy(sampleMem,copyOfSampleMem,65536);
dsp.init(sampleMem);
dsp.set_output(NULL,0);
dsp.setupInterpolation(!interpolationOff);
memset(regPool,0,128);
// this can't be 0 or channel 1 won't play
// this can't be 0x100 either as that's used by SPC700 page 1 and the stack
@ -1023,6 +1038,9 @@ void DivPlatformSNES::setFlags(const DivConfig& flags) {
initEchoFIR[7]=flags.getInt("echoFilter7",0);
initEchoMask=flags.getInt("echoMask",0);
interpolationOff=flags.getBool("interpolationOff",false);
antiClick=flags.getBool("antiClick",true);
}
int DivPlatformSNES::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {

View file

@ -69,6 +69,8 @@ class DivPlatformSNES: public DivDispatch {
bool writeEcho;
bool writeDryVol;
bool echoOn;
bool interpolationOff;
bool antiClick;
bool initEchoOn;
signed char initEchoVolL;
@ -81,8 +83,10 @@ class DivPlatformSNES: public DivDispatch {
struct QueuedWrite {
unsigned char addr;
unsigned char val;
QueuedWrite(): addr(0), val(0) {}
QueuedWrite(unsigned char a, unsigned char v): addr(a), val(v) {}
unsigned char delay;
unsigned char padding;
QueuedWrite(): addr(0), val(0), delay(0), padding(0) {}
QueuedWrite(unsigned char a, unsigned char v, unsigned char d=0): addr(a), val(v), delay(d), padding(0) {}
};
FixedQueue<QueuedWrite,256> writes;

View file

@ -135,24 +135,30 @@ static short const gauss [512] =
1299,1300,1300,1301,1302,1302,1303,1303,1303,1304,1304,1304,1304,1304,1305,1305,
};
void SPC_DSP::setupInterpolation(bool interpolate){for(int i=0;i<voice_count;i++){m.voices[i].interpolate=interpolate;}}
inline int SPC_DSP::interpolate( voice_t const* v )
{
// Make pointers into gaussian based on fractional position between samples
int offset = v->interp_pos >> 4 & 0xFF;
short const* fwd = gauss + 255 - offset;
short const* rev = gauss + offset; // mirror left half of gaussian
if (v->interpolate) {
int offset = v->interp_pos >> 4 & 0xFF;
short const* fwd = gauss + 255 - offset;
short const* rev = gauss + offset; // mirror left half of gaussian
int const* in = &v->buf [(v->interp_pos >> 12) + v->buf_pos];
int out;
out = (fwd [ 0] * in [0]) >> 11;
out += (fwd [256] * in [1]) >> 11;
out += (rev [256] * in [2]) >> 11;
out = (int16_t) out;
out += (rev [ 0] * in [3]) >> 11;
int const* in = &v->buf [(v->interp_pos >> 12) + v->buf_pos];
int out;
out = (fwd [ 0] * in [0]) >> 11;
out += (fwd [256] * in [1]) >> 11;
out += (rev [256] * in [2]) >> 11;
out = (int16_t) out;
out += (rev [ 0] * in [3]) >> 11;
CLAMP16( out );
out &= ~1;
return out;
CLAMP16( out );
out &= ~1;
return out;
} else {
return v->buf [(v->interp_pos >> 12) + v->buf_pos]; //Furnace addition -- no interpolation
}
}

View file

@ -27,6 +27,9 @@ public:
// output buffer could hold.
int sample_count() const;
// Furnace addition: disable/enable Gaussian interpolation
void setupInterpolation(bool interpolate);
// Emulation
// Resets DSP to power-on state
@ -122,6 +125,7 @@ public:
int hidden_env; // used by GAIN mode 7, very obscure quirk
uint8_t t_envx_out;
sample_t out[2]; // Furnace addition, for per-channel oscilloscope
bool interpolate; // Furnace addition, to disable interpolation
};
// Furnace addition, gets a voice

View file

@ -5,6 +5,7 @@
// Chip revisions
// 0: V 0.3.0
// 1: V 47.0.0 (9-bit volume, phase reset on mute)
// 2: V 47.0.2 (Pulse Width XOR on Saw and Triangle)
#include "vera_psg.h"
@ -88,8 +89,8 @@ render(struct VERA_PSG* psg, int16_t *left, int16_t *right)
uint8_t v = 0;
switch (ch->waveform) {
case WF_PULSE: v = (ch->phase >> 10) > ch->pw ? 0 : 63; break;
case WF_SAWTOOTH: v = ch->phase >> 11; break;
case WF_TRIANGLE: v = (ch->phase & 0x10000) ? (~(ch->phase >> 10) & 0x3F) : ((ch->phase >> 10) & 0x3F); break;
case WF_SAWTOOTH: v = (ch->phase >> 11) ^ (psg->chipType < 2 ? 0 : (ch->pw ^ 0x3f) & 0x3f); break;
case WF_TRIANGLE: v = ((ch->phase & 0x10000) ? (~(ch->phase >> 10) & 0x3F) : ((ch->phase >> 10) & 0x3F)) ^ (psg->chipType < 2 ? 0 : (ch->pw ^ 0x3f) & 0x3f); break;
case WF_NOISE: v = ch->noiseval; break;
}
int8_t sv = (v ^ 0x20);

View file

@ -858,6 +858,9 @@ int DivPlatformTX81Z::dispatch(DivCommand c) {
immWrite(0x17,0x80|pmDepth);
break;
}
case DIV_CMD_FM_OPMASK:
// TODO: if OPZ supports op mask
break;
case DIV_CMD_FM_HARD_RESET:
chan[c.chan].hardReset=c.value;
break;

View file

@ -532,7 +532,7 @@ void DivPlatformVERA::poke(std::vector<DivRegWrite>& wlist) {
}
void DivPlatformVERA::setFlags(const DivConfig& flags) {
psg->chipType=flags.getInt("chipType",1);
psg->chipType=flags.getInt("chipType",2);
chipClock=25000000;
CHECK_CUSTOM_CLOCK;
rate=chipClock/512;

View file

@ -173,6 +173,7 @@ void DivPlatformYM2203::acquire_combo(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -255,6 +256,7 @@ void DivPlatformYM2203::acquire_ymfm(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -311,6 +313,16 @@ void DivPlatformYM2203::acquire_lle(short** buf, size_t len) {
fmOut[i]=0;
}
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
immWrite(i.addr&15,i.val);
}
ay->getRegisterWrites().clear();
while (true) {
bool canWeWrite=fm_lle.prescaler_latch[1]&1;
@ -444,6 +456,10 @@ void DivPlatformYM2203::acquire_lle(short** buf, size_t len) {
}
}
void DivPlatformYM2203::fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len) {
ay->fillStream(stream,sRate,len);
}
void DivPlatformYM2203::tick(bool sysTick) {
// PSG
ay->tick(sysTick);
@ -1004,6 +1020,26 @@ int DivPlatformYM2203::dispatch(DivCommand c) {
}
break;
}
case DIV_CMD_FM_OPMASK:
if (c.chan>=psgChanOffs) break;
switch (c.value>>4) {
case 1:
case 2:
case 3:
case 4:
chan[c.chan].opMask&=~(1<<((c.value>>4)-1));
if (c.value&15) {
chan[c.chan].opMask|=(1<<((c.value>>4)-1));
}
break;
default:
chan[c.chan].opMask=c.value&15;
break;
}
if (chan[c.chan].active) {
chan[c.chan].opMaskChanged=true;
}
break;
case DIV_CMD_FM_HARD_RESET:
chan[c.chan].hardReset=c.value;
break;

View file

@ -74,6 +74,7 @@ class DivPlatformYM2203: public DivPlatformOPN {
public:
void acquire(short** buf, size_t len);
void fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len);
int dispatch(DivCommand c);
void* getChanState(int chan);
DivMacroInt* getChanMacroInt(int ch);

View file

@ -325,6 +325,7 @@ void DivPlatformYM2608::acquire_combo(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -440,6 +441,7 @@ void DivPlatformYM2608::acquire_ymfm(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -680,6 +682,10 @@ void DivPlatformYM2608::acquire_lle(short** buf, size_t len) {
}
}
void DivPlatformYM2608::fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len) {
ay->fillStream(stream,sRate,len);
}
void DivPlatformYM2608::tick(bool sysTick) {
// FM
for (int i=0; i<6; i++) {
@ -1537,6 +1543,26 @@ int DivPlatformYM2608::dispatch(DivCommand c) {
}
break;
}
case DIV_CMD_FM_OPMASK:
if (c.chan>=psgChanOffs) break;
switch (c.value>>4) {
case 1:
case 2:
case 3:
case 4:
chan[c.chan].opMask&=~(1<<((c.value>>4)-1));
if (c.value&15) {
chan[c.chan].opMask|=(1<<((c.value>>4)-1));
}
break;
default:
chan[c.chan].opMask=c.value&15;
break;
}
if (chan[c.chan].active) {
chan[c.chan].opMaskChanged=true;
}
break;
case DIV_CMD_FM_HARD_RESET:
chan[c.chan].hardReset=c.value;
break;

View file

@ -93,6 +93,7 @@ class DivPlatformYM2608: public DivPlatformOPN {
public:
void acquire(short** buf, size_t len);
void fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len);
int dispatch(DivCommand c);
void* getChanState(int chan);
DivMacroInt* getChanMacroInt(int ch);

View file

@ -260,6 +260,7 @@ void DivPlatformYM2610::acquire_combo(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -373,6 +374,7 @@ void DivPlatformYM2610::acquire_ymfm(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -1507,6 +1509,26 @@ int DivPlatformYM2610::dispatch(DivCommand c) {
}
break;
}
case DIV_CMD_FM_OPMASK:
if (c.chan>=psgChanOffs) break;
switch (c.value>>4) {
case 1:
case 2:
case 3:
case 4:
chan[c.chan].opMask&=~(1<<((c.value>>4)-1));
if (c.value&15) {
chan[c.chan].opMask|=(1<<((c.value>>4)-1));
}
break;
default:
chan[c.chan].opMask=c.value&15;
break;
}
if (chan[c.chan].active) {
chan[c.chan].opMaskChanged=true;
}
break;
case DIV_CMD_FM_HARD_RESET:
chan[c.chan].hardReset=c.value;
break;

View file

@ -324,6 +324,7 @@ void DivPlatformYM2610B::acquire_combo(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -439,6 +440,7 @@ void DivPlatformYM2610B::acquire_ymfm(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
// AY -> OPN
ay->runDAC();
ay->runTFX(rate);
ay->flushWrites();
for (DivRegWrite& i: ay->getRegisterWrites()) {
if (i.addr>15) continue;
@ -1576,6 +1578,26 @@ int DivPlatformYM2610B::dispatch(DivCommand c) {
}
break;
}
case DIV_CMD_FM_OPMASK:
if (c.chan>=psgChanOffs) break;
switch (c.value>>4) {
case 1:
case 2:
case 3:
case 4:
chan[c.chan].opMask&=~(1<<((c.value>>4)-1));
if (c.value&15) {
chan[c.chan].opMask|=(1<<((c.value>>4)-1));
}
break;
default:
chan[c.chan].opMask=c.value&15;
break;
}
if (chan[c.chan].active) {
chan[c.chan].opMaskChanged=true;
}
break;
case DIV_CMD_FM_HARD_RESET:
chan[c.chan].hardReset=c.value;
break;

View file

@ -108,6 +108,10 @@ class DivPlatformYM2610Base: public DivPlatformOPN {
}
public:
void fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len) {
ay->fillStream(stream,sRate,len);
}
void reset() {
writeADPCMAOff=0;
writeADPCMAOn=0;

View file

@ -67,6 +67,7 @@ const char* cmdName[]={
"HINT_ARPEGGIO",
"HINT_VOLUME",
"HINT_VOL_SLIDE",
"HINT_VOL_SLIDE_TARGET",
"HINT_PORTA",
"HINT_LEGATO",
@ -263,7 +264,9 @@ const char* cmdName[]={
"BIFURCATOR_STATE_LOAD",
"BIFURCATOR_PARAMETER",
"FDS_MOD_AUTO"
"FDS_MOD_AUTO",
"FM_OPMASK"
};
static_assert((sizeof(cmdName)/sizeof(void*))==DIV_CMD_MAX,"update cmdName!");
@ -639,7 +642,23 @@ void DivEngine::processRow(int i, bool afterDelay) {
}
// volume
if (pat->data[whatRow][3]!=-1) {
int volPortaTarget=-1;
bool noApplyVolume=false;
for (int j=0; j<curPat[i].effectCols; j++) {
short effect=pat->data[whatRow][4+(j<<1)];
if (effect==0xd3 || effect==0xd4) { // vol porta
volPortaTarget=pat->data[whatRow][3]<<8; // can be -256
short effectVal=pat->data[whatRow][5+(j<<1)];
if (effectVal==-1) effectVal=0;
effectVal&=255;
noApplyVolume=effectVal>0; // "D3.." or "D300" shouldn't stop volume from applying
break; // technically you could have both D3 and D4... let's not care
}
}
if (pat->data[whatRow][3]!=-1 && !noApplyVolume) {
if (!song.oldAlwaysSetVolume || disCont[dispatchOfChan[i]].dispatch->getLegacyAlwaysSetVolume() || (MIN(chan[i].volMax,chan[i].volume)>>8)!=pat->data[whatRow][3]) {
if (pat->data[whatRow][0]==0 && pat->data[whatRow][1]==0) {
chan[i].midiAftertouch=true;
@ -828,6 +847,7 @@ void DivEngine::processRow(int i, bool afterDelay) {
} else {
chan[i].volSpeed=0;
}
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,chan[i].volSpeed));
break;
case 0x06: // vol slide + porta
@ -869,6 +889,7 @@ void DivEngine::processRow(int i, bool afterDelay) {
} else {
chan[i].volSpeed=0;
}
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,chan[i].volSpeed));
break;
case 0x07: // tremolo
@ -879,6 +900,7 @@ void DivEngine::processRow(int i, bool afterDelay) {
chan[i].tremoloRate=effectVal>>4;
if (chan[i].tremoloDepth!=0) {
chan[i].volSpeed=0;
chan[i].volSpeedTarget=-1;
} else {
dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_HINT_VOLUME,i,chan[i].volume>>8));
@ -898,6 +920,7 @@ void DivEngine::processRow(int i, bool afterDelay) {
} else {
chan[i].volSpeed=0;
}
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,chan[i].volSpeed));
break;
case 0x00: // arpeggio
@ -940,6 +963,22 @@ void DivEngine::processRow(int i, bool afterDelay) {
chan[i].cutType=0;
}
break;
case 0xd3: // volume portamento (vol porta)
// tremolo and vol slides are incompatible
chan[i].tremoloDepth=0;
chan[i].tremoloRate=0;
chan[i].volSpeed=volPortaTarget<0 ? 0 : volPortaTarget>chan[i].volume ? effectVal : -effectVal;
chan[i].volSpeedTarget=chan[i].volSpeed==0 ? -1 : volPortaTarget;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE_TARGET,i,chan[i].volSpeed,chan[i].volSpeedTarget));
break;
case 0xd4: // volume portamento fast (vol porta fast)
// tremolo and vol slides are incompatible
chan[i].tremoloDepth=0;
chan[i].tremoloRate=0;
chan[i].volSpeed=volPortaTarget<0 ? 0 : volPortaTarget>chan[i].volume ? 256*effectVal : -256*effectVal;
chan[i].volSpeedTarget=chan[i].volSpeed==0 ? -1 : volPortaTarget;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE_TARGET,i,chan[i].volSpeed,chan[i].volSpeedTarget));
break;
case 0xe0: // arp speed
if (effectVal>0) {
curSubSong->arpLen=effectVal;
@ -1077,6 +1116,7 @@ void DivEngine::processRow(int i, bool afterDelay) {
chan[i].tremoloDepth=0;
chan[i].tremoloRate=0;
chan[i].volSpeed=effectVal;
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,chan[i].volSpeed));
break;
case 0xf4: // fine volume ramp down
@ -1084,6 +1124,7 @@ void DivEngine::processRow(int i, bool afterDelay) {
chan[i].tremoloDepth=0;
chan[i].tremoloRate=0;
chan[i].volSpeed=-effectVal;
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,chan[i].volSpeed));
break;
case 0xf5: // disable macro
@ -1097,12 +1138,14 @@ void DivEngine::processRow(int i, bool afterDelay) {
break;
case 0xf8: // single volume ramp up
chan[i].volSpeed=0; // add compat flag?
chan[i].volSpeedTarget=-1;
chan[i].volume=MIN(chan[i].volume+effectVal*256,chan[i].volMax);
dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_HINT_VOLUME,i,chan[i].volume>>8));
break;
case 0xf9: // single volume ramp down
chan[i].volSpeed=0; // add compat flag?
chan[i].volSpeedTarget=-1;
chan[i].volume=MAX(chan[i].volume-effectVal*256,0);
dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_HINT_VOLUME,i,chan[i].volume>>8));
@ -1120,9 +1163,9 @@ void DivEngine::processRow(int i, bool afterDelay) {
} else {
chan[i].volSpeed=0;
}
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,chan[i].volSpeed));
break;
case 0xfc: // delayed note release
if (song.delayBehavior==2 || effectVal<nextSpeed) {
chan[i].cut=effectVal+1;
@ -1590,9 +1633,30 @@ bool DivEngine::nextTick(bool noAccum, bool inhibitLowLat) {
if (chan[i].volSpeed!=0) {
chan[i].volume=(chan[i].volume&0xff)|(dispatchCmd(DivCommand(DIV_CMD_GET_VOLUME,i))<<8);
chan[i].volume+=chan[i].volSpeed;
if (chan[i].volSpeedTarget!=-1) {
bool atTarget=false;
if (chan[i].volSpeed>0) {
atTarget=(chan[i].volume>=chan[i].volSpeedTarget);
} else if (chan[i].volSpeed<0) {
atTarget=(chan[i].volume<=chan[i].volSpeedTarget);
} else {
atTarget=true;
chan[i].volSpeedTarget=chan[i].volume;
}
if (atTarget) {
chan[i].volume=chan[i].volSpeedTarget;
chan[i].volSpeed=0;
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,0));
}
}
if (chan[i].volume>chan[i].volMax) {
chan[i].volume=chan[i].volMax;
chan[i].volSpeed=0;
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_HINT_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_HINT_VOL_SLIDE,i,0));
@ -1604,6 +1668,7 @@ bool DivEngine::nextTick(bool noAccum, bool inhibitLowLat) {
} else {
chan[i].volume=0;
}
chan[i].volSpeedTarget=-1;
dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
dispatchCmd(DivCommand(DIV_CMD_HINT_VOLUME,i,chan[i].volume>>8));
} else {
@ -1647,26 +1712,34 @@ bool DivEngine::nextTick(bool noAccum, bool inhibitLowLat) {
chan[i].panPos+=chan[i].panRate;
chan[i].panPos&=255;
// calculate...
// calculate inverted...
switch (chan[i].panPos&0xc0) {
case 0: // center -> right
chan[i].panL=0xff-((chan[i].panPos&0x3f)<<2);
chan[i].panR=0xff;
chan[i].panL=((chan[i].panPos&0x3f)<<2);
chan[i].panR=0;
break;
case 0x40: // right -> center
chan[i].panL=(chan[i].panPos&0x3f)<<2;
chan[i].panR=0xff;
chan[i].panL=0xff-((chan[i].panPos&0x3f)<<2);
chan[i].panR=0;
break;
case 0x80: // center -> left
chan[i].panL=0xff;
chan[i].panR=0xff-((chan[i].panPos&0x3f)<<2);
chan[i].panL=0;
chan[i].panR=((chan[i].panPos&0x3f)<<2);
break;
case 0xc0: // left -> center
chan[i].panL=0xff;
chan[i].panR=(chan[i].panPos&0x3f)<<2;
chan[i].panL=0;
chan[i].panR=0xff-((chan[i].panPos&0x3f)<<2);
break;
}
// multiply by depth
chan[i].panL=(chan[i].panL*chan[i].panDepth)/15;
chan[i].panR=(chan[i].panR*chan[i].panDepth)/15;
// then invert it to get final panning
chan[i].panL^=0xff;
chan[i].panR^=0xff;
dispatchCmd(DivCommand(DIV_CMD_PANNING,i,chan[i].panL,chan[i].panR));
}

View file

@ -19,6 +19,7 @@
#include "sfWrapper.h"
#include "../fileutils.h"
#include "../ta-log.h"
#include "sndfile.h"
sf_count_t _vioGetSize(void* user) {
@ -80,6 +81,7 @@ SNDFILE* SFWrapper::doOpen(const char* path, int mode, SF_INFO* sfinfo) {
vio.seek=_vioSeek;
vio.tell=_vioTell;
vio.write=_vioWrite;
logV("SFWrapper: opening %s",path);
const char* modeC="rb";
if (mode==SFM_WRITE) {
@ -91,10 +93,12 @@ SNDFILE* SFWrapper::doOpen(const char* path, int mode, SF_INFO* sfinfo) {
f=ps_fopen(path,modeC);
if (f==NULL) {
logE("SFWrapper: failed to open (%s)",strerror(errno));
return NULL;
}
if (fseek(f,0,SEEK_END)==-1) {
logE("SFWrapper: failed to seek to end (%s)",strerror(errno));
fclose(f);
f=NULL;
return NULL;
@ -102,6 +106,7 @@ SNDFILE* SFWrapper::doOpen(const char* path, int mode, SF_INFO* sfinfo) {
len=ftell(f);
if (len==(SIZE_MAX>>1)) {
logE("SFWrapper: failed to tell (%s)",strerror(errno));
len=0;
fclose(f);
f=NULL;
@ -109,6 +114,7 @@ SNDFILE* SFWrapper::doOpen(const char* path, int mode, SF_INFO* sfinfo) {
}
if (fseek(f,0,SEEK_SET)==-1) {
logE("SFWrapper: failed to seek to beginning (%s)",strerror(errno));
len=0;
fclose(f);
f=NULL;
@ -117,5 +123,8 @@ SNDFILE* SFWrapper::doOpen(const char* path, int mode, SF_INFO* sfinfo) {
sf=sf_open_virtual(&vio,mode,sfinfo,this);
if (sf!=NULL) fileMode=mode;
if (sf==NULL) {
logE("SFWrapper: WHY IS IT NULL?!");
}
return sf;
}
}

View file

@ -102,6 +102,173 @@ bool DivSubSong::walk(int& loopOrder, int& loopRow, int& loopEnd, int chans, int
return false;
}
double calcRowLenInSeconds(const DivGroovePattern& speeds, float hz, int vN, int vD, int timeBaseFromSong) {
double hl=1; //count for 1 row
if (hl<=0.0) hl=4.0;
double timeBase=timeBaseFromSong+1;
double speedSum=0;
for (int i=0; i<MIN(16,speeds.len); i++) {
speedSum+=speeds.val[i];
}
speedSum/=MAX(1,speeds.len);
if (timeBase<1.0) timeBase=1.0;
if (speedSum<1.0) speedSum=1.0;
if (vD<1) vD=1;
return 1.0/((60.0*hz/(timeBase*hl*speedSum))*(double)vN/(double)vD/60.0);
}
void DivSubSong::findLength(int loopOrder, int loopRow, double fadeoutLen, int& rowsForFadeout, bool& hasFFxx, std::vector<int>& orders_vec, std::vector<DivGroovePattern>& grooves, int& length, int chans, int jumpTreatment, int ignoreJumpAtEnd, int firstPat) {
length=0;
hasFFxx=false;
rowsForFadeout=0;
float secondsPerThisRow=0.0f;
DivGroovePattern curSpeeds=speeds; //simulate that we are playing the song, track all speed/BPM/tempo/engine rate changes
short curVirtualTempoN=virtualTempoN;
short curVirtualTempoD=virtualTempoD;
float curHz=hz;
double curDivider=(double)timeBase;
double curLen=0.0; //how many seconds passed since the start of song
int nextOrder=-1;
int nextRow=0;
int effectVal=0;
int lastSuspectedLoopEnd=-1;
DivPattern* subPat[DIV_MAX_CHANS];
unsigned char wsWalked[8192];
memset(wsWalked,0,8192);
if (firstPat>0) {
memset(wsWalked,255,32*firstPat);
}
for (int i=firstPat; i<ordersLen; i++) {
bool jumped=false;
for (int j=0; j<chans; j++) {
subPat[j]=pat[j].getPattern(orders.ord[j][i],false);
}
if (i>lastSuspectedLoopEnd) {
lastSuspectedLoopEnd=i;
}
for (int j=nextRow; j<patLen; j++) {
nextRow=0;
bool changingOrder=false;
bool jumpingOrder=false;
if (wsWalked[((i<<5)+(j>>3))&8191]&(1<<(j&7))) {
return;
}
for (int k=0; k<chans; k++) {
for (int l=0; l<pat[k].effectCols; l++) {
effectVal=subPat[k]->data[j][5+(l<<1)];
if (effectVal<0) effectVal=0;
if (subPat[k]->data[j][4+(l<<1)]==0xff) {
hasFFxx=true;
// FFxx makes YOU SHALL NOT PASS!!! move
orders_vec.push_back(j+1); // order len
length+=j+1; // add length of order to song length
return;
}
switch (subPat[k]->data[j][4+(l<<1)]) {
case 0x09: { // select groove pattern/speed 1
if (grooves.empty()) {
if (effectVal>0) curSpeeds.val[0]=effectVal;
} else {
if (effectVal<(short)grooves.size()) {
curSpeeds=grooves[effectVal];
//curSpeed=0;
}
}
break;
}
case 0x0f: { // speed 1/speed 2
if (curSpeeds.len==2 && grooves.empty()) {
if (effectVal>0) curSpeeds.val[1]=effectVal;
} else {
if (effectVal>0) curSpeeds.val[0]=effectVal;
}
break;
}
case 0xfd: { // virtual tempo num
if (effectVal>0) curVirtualTempoN=effectVal;
break;
}
case 0xfe: { // virtual tempo den
if (effectVal>0) curVirtualTempoD=effectVal;
break;
}
case 0xf0: { // set Hz by tempo (set bpm)
curDivider=(double)effectVal*2.0/5.0;
if (curDivider<1) curDivider=1;
break;
}
}
if (subPat[k]->data[j][4+(l<<1)]==0x0d) {
if (jumpTreatment==2) {
if ((i<ordersLen-1 || !ignoreJumpAtEnd)) {
nextOrder=i+1;
nextRow=effectVal;
jumpingOrder=true;
}
} else if (jumpTreatment==1) {
if (nextOrder==-1 && (i<ordersLen-1 || !ignoreJumpAtEnd)) {
nextOrder=i+1;
nextRow=effectVal;
jumpingOrder=true;
}
} else {
if ((i<ordersLen-1 || !ignoreJumpAtEnd)) {
if (!changingOrder) {
nextOrder=i+1;
}
jumpingOrder=true;
nextRow=effectVal;
}
}
} else if (subPat[k]->data[j][4+(l<<1)]==0x0b) {
if (nextOrder==-1 || jumpTreatment==0) {
nextOrder=effectVal;
if (jumpTreatment==1 || jumpTreatment==2 || !jumpingOrder) {
nextRow=0;
}
changingOrder=true;
}
}
}
}
if (i>loopOrder || (i==loopOrder && j>loopRow)) {
// we count each row fadeout lasts. When our time is greater than fadeout length we successfully counted the number of fadeout rows
if (curLen<=fadeoutLen && fadeoutLen>0.0) {
secondsPerThisRow=calcRowLenInSeconds(speeds,curHz,curVirtualTempoN,curVirtualTempoD,curDivider);
curLen+=secondsPerThisRow;
rowsForFadeout++;
}
}
wsWalked[((i<<5)+(j>>3))&8191]|=1<<(j&7);
if (nextOrder!=-1) {
i=nextOrder-1;
orders_vec.push_back(j+1); // order len
length+=j+1; // add length of order to song length
jumped=true;
nextOrder=-1;
break;
}
}
if (!jumped) { // if no jump occured we add full pattern length
orders_vec.push_back(patLen); // order len
length+=patLen; // add length of order to song length
}
}
}
void DivSubSong::clearData() {
for (int i=0; i<DIV_MAX_CHANS; i++) {
pat[i].wipePatterns();

View file

@ -187,6 +187,11 @@ struct DivSubSong {
*/
bool walk(int& loopOrder, int& loopRow, int& loopEnd, int chans, int jumpTreatment, int ignoreJumpAtEnd, int firstPat=0);
/**
* find song length in rows (up to specified loop point).
*/
void findLength(int loopOrder, int loopRow, double fadeoutLen, int& rowsForFadeout, bool& hasFFxx, std::vector<int>& orders, std::vector<DivGroovePattern>& grooves, int& length, int chans, int jumpTreatment, int ignoreJumpAtEnd, int firstPat=0);
void clearData();
void optimizePatterns();
void rearrangePatterns();

View file

@ -433,6 +433,7 @@ void DivEngine::registerSystems() {
{0x25, {DIV_CMD_AY_ENVELOPE_SLIDE, _("25xx: Envelope slide up"), negEffectVal}},
{0x26, {DIV_CMD_AY_ENVELOPE_SLIDE, _("26xx: Envelope slide down")}},
{0x29, {DIV_CMD_AY_AUTO_ENVELOPE, _("29xy: Set auto-envelope (x: numerator; y: denominator)")}},
{0x2c, {DIV_CMD_AY_AUTO_PWM, _("2Cxx: Set timer period offset (bit 7: sign)")}},
{0x2e, {DIV_CMD_AY_IO_WRITE, _("2Exx: Write to I/O port A"), constVal<0>, effectVal}},
{0x2f, {DIV_CMD_AY_IO_WRITE, _("2Fxx: Write to I/O port B"), constVal<1>, effectVal}},
};
@ -503,6 +504,7 @@ void DivEngine::registerSystems() {
{0x5d, {DIV_CMD_FM_D2R, _("5Dxx: Set decay 2 of operator 2 (0 to 1F)"), constVal<1>, effectValAnd<31>}},
{0x5e, {DIV_CMD_FM_D2R, _("5Exx: Set decay 2 of operator 3 (0 to 1F)"), constVal<2>, effectValAnd<31>}},
{0x5f, {DIV_CMD_FM_D2R, _("5Fxx: Set decay 2 of operator 4 (0 to 1F)"), constVal<3>, effectValAnd<31>}},
{0x60, {DIV_CMD_FM_OPMASK, _("60xx: Set operator mask (bits 0-3)")}},
};
EffectHandlerMap fmOPMPostEffectHandlerMap(fmOPNPostEffectHandlerMap);
@ -513,6 +515,7 @@ void DivEngine::registerSystems() {
{0x1e, {DIV_CMD_FM_AM_DEPTH, _("1Exx: Set AM depth (0 to 7F)"), effectValAnd<127>}},
{0x1f, {DIV_CMD_FM_PM_DEPTH, _("1Fxx: Set PM depth (0 to 7F)"), effectValAnd<127>}},
{0x55, {DIV_CMD_FM_SSG, _("55xy: Set detune 2 (x: operator from 1 to 4 (0 for all ops); y: detune from 0 to 3)"), effectOpVal<4>, effectValAnd<3>}},
{0x60, {DIV_CMD_FM_OPMASK, _("60xx: Set operator mask (bits 0-3)")}},
});
EffectHandlerMap fmOPZPostEffectHandlerMap(fmOPMPostEffectHandlerMap);

View file

@ -24,7 +24,9 @@
constexpr int MASTER_CLOCK_PREC=(sizeof(void*)==8)?8:0;
void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write, int streamOff, double* loopTimer, double* loopFreq, int* loopSample, bool* sampleDir, bool isSecond, int* pendingFreq, int* playingSample, int* setPos, unsigned int* sampleOff8, unsigned int* sampleLen8, size_t bankOffset, bool directStream) {
// this function is so long
// may as well make it something else
void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write, int streamOff, double* loopTimer, double* loopFreq, int* loopSample, bool* sampleDir, bool isSecond, int* pendingFreq, int* playingSample, int* setPos, unsigned int* sampleOff8, unsigned int* sampleLen8, size_t bankOffset, bool directStream, bool* sampleStoppable) {
unsigned char baseAddr1=isSecond?0xa0:0x50;
unsigned char baseAddr2=isSecond?0x80:0;
unsigned short baseAddr2S=isSecond?0x8000:0;
@ -647,6 +649,7 @@ void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write
logD("writing stream command %x:%x with stream ID %d",write.addr,write.val,streamID);
switch (write.addr&0xff) {
case 0: // play sample
sampleStoppable[streamID]=true;
if (write.val<(unsigned int)song.sampleLen) {
if (playingSample[streamID]!=(int)write.val) {
pendingFreq[streamID]=write.val;
@ -685,6 +688,7 @@ void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write
}
break;
case 1: { // set sample freq
sampleStoppable[streamID]=true;
int realFreq=write.val;
if (realFreq<0) realFreq=0;
if (realFreq>44100) realFreq=44100;
@ -728,11 +732,14 @@ void DivEngine::performVGMWrite(SafeWriter* w, DivSystem sys, DivRegWrite& write
break;
}
case 2: // stop sample
w->writeC(0x94);
w->writeC(streamID);
loopSample[streamID]=-1;
playingSample[streamID]=-1;
pendingFreq[streamID]=-1;
if (sampleStoppable[streamID]) {
w->writeC(0x94);
w->writeC(streamID);
loopSample[streamID]=-1;
playingSample[streamID]=-1;
pendingFreq[streamID]=-1;
sampleStoppable[streamID]=false;
}
break;
case 3: // set sample direction
sampleDir[streamID]=write.val;
@ -1224,6 +1231,7 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p
bool sampleDir[DIV_MAX_CHANS];
int pendingFreq[DIV_MAX_CHANS];
int playingSample[DIV_MAX_CHANS];
bool sampleStoppable[DIV_MAX_CHANS];
int setPos[DIV_MAX_CHANS];
std::vector<unsigned int> chipVol;
std::vector<DivDelayedWrite> delayedWrites[DIV_MAX_CHIPS];
@ -1246,6 +1254,7 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p
playingSample[i]=-1;
setPos[i]=0;
sampleDir[i]=false;
sampleStoppable[i]=true;
}
bool writeDACSamples=false;
@ -2160,8 +2169,8 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p
w->writeI(0);
w->write(writeADPCM_Y8950[i]->getSampleMem(0),writeADPCM_Y8950[i]->getSampleMemUsage(0));
}
if (writeQSound[i]!=NULL && writeQSound[i]->getSampleMemUsage()>0) {
unsigned int blockSize=(writeQSound[i]->getSampleMemUsage()+0xffff)&(~0xffff);
if (writeQSound[i]!=NULL && writeQSound[i]->getSampleMemUsage(1)>0) {
unsigned int blockSize=(writeQSound[i]->getSampleMemUsage(1)+0xffff)&(~0xffff);
if (blockSize > 0x1000000) {
blockSize = 0x1000000;
}
@ -2169,7 +2178,7 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p
w->writeC(0x66);
w->writeC(0x8F);
w->writeI((blockSize+8)|(i*0x80000000));
w->writeI(writeQSound[i]->getSampleMemCapacity());
w->writeI(writeQSound[i]->getSampleMemCapacity(1));
w->writeI(0);
w->write(writeQSound[i]->getSampleMem(),blockSize);
}
@ -2514,7 +2523,7 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p
for (int i=0; i<song.systemLen; i++) {
std::vector<DivRegWrite>& writes=disCont[i].dispatch->getRegisterWrites();
for (DivRegWrite& j: writes) {
performVGMWrite(w,song.system[i],j,streamIDs[i],loopTimer,loopFreq,loopSample,sampleDir,isSecond[i],pendingFreq,playingSample,setPos,sampleOff8,sampleLen8,bankOffset[i],directStream);
performVGMWrite(w,song.system[i],j,streamIDs[i],loopTimer,loopFreq,loopSample,sampleDir,isSecond[i],pendingFreq,playingSample,setPos,sampleOff8,sampleLen8,bankOffset[i],directStream,sampleStoppable);
writeCount++;
}
writes.clear();
@ -2554,7 +2563,7 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p
lastOne=i.second.time;
}
// write write
performVGMWrite(w,song.system[i.first],i.second.write,streamIDs[i.first],loopTimer,loopFreq,loopSample,sampleDir,isSecond[i.first],pendingFreq,playingSample,setPos,sampleOff8,sampleLen8,bankOffset[i.first],directStream);
performVGMWrite(w,song.system[i.first],i.second.write,streamIDs[i.first],loopTimer,loopFreq,loopSample,sampleDir,isSecond[i.first],pendingFreq,playingSample,setPos,sampleOff8,sampleLen8,bankOffset[i.first],directStream,sampleStoppable);
// handle global Furnace commands
writeCount++;
@ -2670,7 +2679,8 @@ SafeWriter* DivEngine::saveVGM(bool* sysToExport, bool loop, int version, bool p
w->writeWString(ws,false); // japanese author name
w->writeS(0); // date
w->writeWString(L"Furnace (chiptune tracker)",false); // ripper
w->writeS(0); // notes
ws=utf8To16(song.notes.c_str());
w->writeWString(ws,false); // notes
int gd3Len=w->tell()-gd3Off-12;

View file

@ -33,11 +33,78 @@ bool DivEngine::isExporting() {
return exporting;
}
void DivEngine::getLoopsLeft(int &loops) {
if (totalLoops<0 || exportLoopCount==0) {
loops=0;
return;
}
loops=exportLoopCount-1-totalLoops;
}
void DivEngine::getTotalLoops(int &loops) {
loops=exportLoopCount-1;
}
void DivEngine::getCurSongPos(int &row, int &order) {
row=curRow;
order=curOrder;
}
void DivEngine::getTotalAudioFiles(int &files) {
files=0;
switch (exportMode) {
case DIV_EXPORT_MODE_ONE: {
files=1;
break;
}
case DIV_EXPORT_MODE_MANY_SYS: {
files=1; // there actually are several files but they are processed in the same loop, so to correctly draw progress we think of them as one file
break;
}
case DIV_EXPORT_MODE_MANY_CHAN: {
for (int i=0; i<chans; i++) {
if (exportChannelMask[i]) {
files++;
}
}
break;
}
default:
break;
}
}
void DivEngine::getCurFileIndex(int &file) {
file=0;
switch (exportMode) {
case DIV_EXPORT_MODE_ONE: {
file=0;
break;
}
case DIV_EXPORT_MODE_MANY_SYS: {
file=0; // there actually are several files but they are processed in the same loop, so to correctly draw progress we think of them as one file
break;
}
case DIV_EXPORT_MODE_MANY_CHAN: {
file=curExportChan;
break;
}
default:
break;
}
}
bool DivEngine::getIsFadingOut() {
return isFadingOut;
}
#ifdef HAVE_SNDFILE
void DivEngine::runExportThread() {
size_t fadeOutSamples=got.rate*exportFadeOut;
size_t curFadeOutSample=0;
bool isFadingOut=false;
isFadingOut=false;
switch (exportMode) {
case DIV_EXPORT_MODE_ONE: {
@ -140,7 +207,11 @@ void DivEngine::runExportThread() {
sf[i]=NULL;
si[i].samplerate=got.rate;
si[i].channels=disCont[i].dispatch->getOutputCount();
si[i].format=SF_FORMAT_WAV|SF_FORMAT_PCM_16;
if (exportFormat==DIV_EXPORT_FORMAT_S16) {
si[i].format=SF_FORMAT_WAV|SF_FORMAT_PCM_16;
} else {
si[i].format=SF_FORMAT_WAV|SF_FORMAT_FLOAT;
}
}
for (int i=0; i<song.systemLen; i++) {
@ -247,6 +318,8 @@ void DivEngine::runExportThread() {
// take control of audio output
deinitAudioBackend();
curExportChan=0;
float* outBuf[DIV_MAX_OUTPUTS];
float* outBufFinal;
for (int i=0; i<exportOutputs; i++) {
@ -258,6 +331,7 @@ void DivEngine::runExportThread() {
for (int i=0; i<chans; i++) {
if (!exportChannelMask[i]) continue;
SNDFILE* sf;
SF_INFO si;
SFWrapper sfWrap;
@ -338,6 +412,8 @@ void DivEngine::runExportThread() {
}
}
curExportChan++;
if (sfWrap.doClose()!=0) {
logE("could not close audio file!");
}
@ -378,6 +454,7 @@ void DivEngine::runExportThread() {
}
logI("done!");
exporting=false;
curExportChan=0;
break;
}
}

View file

@ -1,92 +0,0 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef _ZSM_H
#define _ZSM_H
//#include "engine.h"
#include "safeWriter.h"
#include "dispatch.h"
#include <stdlib.h>
#define ZSM_HEADER_SIZE 16
#define ZSM_VERSION 1
#define ZSM_YM_CMD 0x40
#define ZSM_DELAY_CMD 0x80
#define ZSM_YM_MAX_WRITES 63
#define ZSM_SYNC_MAX_WRITES 31
#define ZSM_DELAY_MAX 127
#define ZSM_EOF ZSM_DELAY_CMD
#define ZSM_EXT ZSM_YM_CMD
#define ZSM_EXT_PCM 0x00
#define ZSM_EXT_CHIP 0x40
#define ZSM_EXT_SYNC 0x80
#define ZSM_EXT_CUSTOM 0xC0
enum YM_STATE { ym_PREV, ym_NEW, ym_STATES };
enum PSG_STATE { psg_PREV, psg_NEW, psg_STATES };
class DivZSM {
private:
struct S_pcmInst {
int geometry;
unsigned int offset, length, loopPoint;
bool isLooped;
};
SafeWriter* w;
int ymState[ym_STATES][256];
int psgState[psg_STATES][64];
int pcmRateCache;
int pcmCtrlRVCache;
int pcmCtrlDCCache;
unsigned int pcmLoopPointCache;
bool pcmIsLooped;
std::vector<DivRegWrite> ymwrites;
std::vector<DivRegWrite> pcmMeta;
std::vector<unsigned char> pcmData;
std::vector<unsigned char> pcmCache;
std::vector<S_pcmInst> pcmInsts;
std::vector<DivRegWrite> syncCache;
int loopOffset;
int numWrites;
int ticks;
int tickRate;
int ymMask;
int psgMask;
bool optimize;
public:
DivZSM();
~DivZSM();
void init(unsigned int rate = 60);
int getoffset();
void writeYM(unsigned char a, unsigned char v);
void writePSG(unsigned char a, unsigned char v);
void writePCM(unsigned char a, unsigned char v);
void writeSync(unsigned char a, unsigned char v);
void setOptimize(bool o);
void tick(int numticks = 1);
void setLoopPoint();
SafeWriter* finish();
private:
void flushWrites();
void flushTicks();
};
#endif

View file

@ -1,208 +0,0 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2024 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "engine.h"
#include "../ta-log.h"
#include "../utfutils.h"
#include "song.h"
#include "zsm.h"
constexpr int MASTER_CLOCK_PREC=(sizeof(void*)==8)?8:0;
constexpr int MASTER_CLOCK_MASK=(sizeof(void*)==8)?0xff:0;
SafeWriter* DivEngine::saveZSM(unsigned int zsmrate, bool loop, bool optimize) {
int VERA=-1;
int YM=-1;
int IGNORED=0;
// find indexes for YM and VERA. Ignore other systems.
for (int i=0; i<song.systemLen; i++) {
switch (song.system[i]) {
case DIV_SYSTEM_VERA:
if (VERA>=0) {
IGNORED++;
break;
}
VERA=i;
logD("VERA detected as chip id %d",i);
break;
case DIV_SYSTEM_YM2151:
if (YM>=0) {
IGNORED++;
break;
}
YM=i;
logD("YM detected as chip id %d",i);
break;
default:
IGNORED++;
logD("Ignoring chip %d systemID %d",i,(int)song.system[i]);
break;
}
}
if (VERA<0 && YM<0) {
logE("No supported systems for ZSM");
return NULL;
}
if (IGNORED>0) {
logW("ZSM export ignoring %d unsupported system%c",IGNORED,IGNORED>1?'s':' ');
}
stop();
repeatPattern=false;
setOrder(0);
BUSY_BEGIN_SOFT;
double origRate=got.rate;
got.rate=zsmrate&0xffff;
// determine loop point
int loopOrder=0;
int loopRow=0;
int loopEnd=0;
walkSong(loopOrder,loopRow,loopEnd);
logI("loop point: %d %d",loopOrder,loopRow);
warnings="";
DivZSM zsm;
zsm.init(zsmrate);
// reset the playback state
curOrder=0;
freelance=false;
playing=false;
extValuePresent=false;
remainingLoops=-1;
// Prepare to write song data
playSub(false);
//size_t tickCount=0;
bool done=false;
bool loopNow=false;
int loopPos=-1;
int fracWait=0; // accumulates fractional ticks
if (VERA>=0) disCont[VERA].dispatch->toggleRegisterDump(true);
if (YM>=0) {
disCont[YM].dispatch->toggleRegisterDump(true);
// emit LFO initialization commands
zsm.writeYM(0x18,0); // freq=0
zsm.writeYM(0x19,0x7F); // AMD =7F
zsm.writeYM(0x19,0xFF); // PMD =7F
// TODO: incorporate the Furnace meta-command for init data and filter
// out writes to otherwise-unused channels.
}
// Indicate the song's tuning as a sync meta-event
// specified in terms of how many 1/256th semitones
// the song is offset from standard A-440 tuning.
// This is mainly to benefit visualizations in players
// for non-standard tunings so that they can avoid
// displaying the entire song held in pitch bend.
// Tunings offsets that exceed a half semitone
// will simply be represented in a different key
// by nature of overflowing the signed char value
signed char tuningoffset=(signed char)(round(3072*(log(song.tuning/440.0)/log(2))))&0xff;
zsm.writeSync(0x01,tuningoffset);
// Set optimize flag, which mainly buffers PSG writes
// whenever the channel is silent
zsm.setOptimize(optimize);
while (!done) {
if (loopPos==-1) {
if (loopOrder==curOrder && loopRow==curRow && loop)
loopNow=true;
if (loopNow) {
// If Virtual Tempo is in use, our exact loop point
// might be skipped due to quantization error.
// If this happens, the tick immediately following is our loop point.
if (ticks==1 || !(loopOrder==curOrder && loopRow==curRow)) {
loopPos=zsm.getoffset();
zsm.setLoopPoint();
loopNow=false;
}
}
}
if (nextTick() || !playing) {
done=true;
if (!loop) {
for (int i=0; i<song.systemLen; i++) {
disCont[i].dispatch->getRegisterWrites().clear();
}
break;
}
if (!playing) {
loopPos=-1;
}
}
// get register dumps
for (int j=0; j<2; j++) {
int i=0;
// dump YM writes first
if (j==0) {
if (YM<0) {
continue;
} else {
i=YM;
}
}
// dump VERA writes second
if (j==1) {
if (VERA<0) {
continue;
} else {
i=VERA;
}
}
std::vector<DivRegWrite>& writes=disCont[i].dispatch->getRegisterWrites();
if (writes.size()>0)
logD("zsmOps: Writing %d messages to chip %d",writes.size(),i);
for (DivRegWrite& write: writes) {
if (i==YM) zsm.writeYM(write.addr&0xff,write.val);
if (i==VERA) {
if (done && write.addr>=64) continue; // don't process any PCM or sync events on the loop lookahead
zsm.writePSG(write.addr&0xff,write.val);
}
}
writes.clear();
}
// write wait
int totalWait=cycles>>MASTER_CLOCK_PREC;
fracWait+=cycles&MASTER_CLOCK_MASK;
totalWait+=fracWait>>MASTER_CLOCK_PREC;
fracWait&=MASTER_CLOCK_MASK;
if (totalWait>0 && !done) {
zsm.tick(totalWait);
//tickCount+=totalWait;
}
}
// end of song
// done - close out.
got.rate=origRate;
if (VERA>=0) disCont[VERA].dispatch->toggleRegisterDump(false);
if (YM>=0) disCont[YM].dispatch->toggleRegisterDump(false);
remainingLoops=-1;
playing=false;
freelance=false;
extValuePresent=false;
BUSY_END;
return zsm.finish();
}