furnace/src/engine/engine.cpp

2878 lines
75 KiB
C++
Raw Normal View History

2022-02-15 03:12:20 +00:00
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2022 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 "dataErrors.h"
2022-01-29 06:22:32 +00:00
#include "song.h"
2022-03-19 08:42:44 +00:00
#include <functional>
2022-01-17 23:18:28 +00:00
#define _USE_MATH_DEFINES
#include "engine.h"
#include "instrument.h"
#include "safeReader.h"
#include "../ta-log.h"
#include "../fileutils.h"
#include "../audio/sdl.h"
2021-12-19 21:52:04 +00:00
#include <stdexcept>
2021-12-19 08:16:24 +00:00
#ifndef _WIN32
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>
#endif
2021-06-09 08:33:03 +00:00
#ifdef HAVE_JACK
#include "../audio/jack.h"
#endif
#include <math.h>
2021-12-07 17:21:23 +00:00
#include <sndfile.h>
#include <fmt/printf.h>
void process(void* u, float** in, float** out, int inChans, int outChans, unsigned int size) {
((DivEngine*)u)->nextBuf(in,out,inChans,outChans,size);
}
2022-01-20 18:48:20 +00:00
const char* DivEngine::getEffectDesc(unsigned char effect, int chan) {
switch (effect) {
case 0x00:
return "00xy: Arpeggio";
case 0x01:
return "01xx: Pitch slide up";
case 0x02:
return "02xx: Pitch slide down";
case 0x03:
return "03xx: Portamento";
case 0x04:
2022-02-15 06:46:03 +00:00
return "04xy: Vibrato (x: speed; y: depth)";
2022-03-14 14:50:52 +00:00
case 0x07:
return "07xy: Tremolo (x: speed; y: depth)";
2022-01-20 18:48:20 +00:00
case 0x08:
2022-02-15 06:46:03 +00:00
return "08xy: Set panning (x: left; y: right)";
2022-01-20 18:48:20 +00:00
case 0x09:
return "09xx: Set speed 1";
case 0x0a:
2022-02-15 06:46:03 +00:00
return "0Axy: Volume slide (0y: down; x0: up)";
2022-01-20 18:48:20 +00:00
case 0x0b:
return "0Bxx: Jump to pattern";
case 0x0c:
return "0Cxx: Retrigger";
case 0x0d:
return "0Dxx: Jump to next pattern";
case 0x0f:
return "0Fxx: Set speed 2";
2022-02-15 06:46:03 +00:00
case 0xc0: case 0xc1: case 0xc2: case 0xc3:
return "Cxxx: Set tick rate (hz)";
2022-01-20 18:48:20 +00:00
case 0xe0:
return "E0xx: Set arp speed";
case 0xe1:
2022-02-15 06:46:03 +00:00
return "E1xy: Note slide up (x: speed; y: semitones)";
2022-01-20 18:48:20 +00:00
case 0xe2:
2022-02-15 06:46:03 +00:00
return "E2xy: Note slide down (x: speed; y: semitones)";
2022-01-20 18:48:20 +00:00
case 0xe3:
2022-02-15 06:46:03 +00:00
return "E3xx: Set vibrato shape (0: up/down; 1: up only; 2: down only)";
2022-01-20 18:48:20 +00:00
case 0xe4:
return "E4xx: Set vibrato range";
case 0xe5:
2022-02-15 06:46:03 +00:00
return "E5xx: Set pitch (80: center)";
2022-01-20 18:48:20 +00:00
case 0xea:
return "EAxx: Legato";
case 0xeb:
return "EBxx: Set sample bank";
case 0xec:
return "ECxx: Note cut";
case 0xed:
return "EDxx: Note delay";
case 0xee:
return "EExx: Send external command";
case 0xef:
2022-02-15 06:46:03 +00:00
return "EFxx: Set global tuning (quirky!)";
case 0xf0:
return "F0xx: Set tick rate (bpm)";
2022-03-14 14:50:52 +00:00
case 0xf1:
return "F1xx: Single tick note slide up";
2022-03-14 14:50:52 +00:00
case 0xf2:
return "F2xx: Single tick note slide down";
2022-03-14 14:50:52 +00:00
case 0xf8:
return "F8xx: Single tick volume slide up";
2022-03-14 14:50:52 +00:00
case 0xf9:
return "F9xx: Single tick volume slide down";
2022-03-14 14:50:52 +00:00
case 0xfa:
return "FAxx: Fast volume slide (0y: down; x0: up)";
2022-02-04 19:43:57 +00:00
case 0xff:
2022-02-17 08:15:51 +00:00
return "FFxx: Stop song";
2022-01-20 18:48:20 +00:00
default:
2022-03-14 14:50:52 +00:00
if ((effect&0xf0)==0x90) {
return "9xxx: Set sample offset*256";
}
else if (chan>=0 && chan<chans) {
2022-01-20 18:48:20 +00:00
const char* ret=disCont[dispatchOfChan[chan]].dispatch->getEffectName(effect);
if (ret!=NULL) return ret;
}
break;
}
return "Invalid effect";
}
2022-01-27 06:04:26 +00:00
void DivEngine::walkSong(int& loopOrder, int& loopRow, int& loopEnd) {
2022-01-25 20:06:29 +00:00
loopOrder=0;
loopRow=0;
2022-01-27 06:04:26 +00:00
loopEnd=-1;
2022-01-25 20:06:29 +00:00
int nextOrder=-1;
int nextRow=0;
int effectVal=0;
DivPattern* pat[DIV_MAX_CHANS];
for (int i=0; i<song.ordersLen; i++) {
for (int j=0; j<chans; j++) {
pat[j]=song.pat[j].getPattern(song.orders.ord[j][i],false);
2022-01-25 20:06:29 +00:00
}
for (int j=nextRow; j<song.patLen; j++) {
nextRow=0;
for (int k=0; k<chans; k++) {
for (int l=0; l<song.pat[k].effectRows; l++) {
effectVal=pat[k]->data[j][5+(l<<1)];
if (effectVal<0) effectVal=0;
if (pat[k]->data[j][4+(l<<1)]==0x0d) {
2022-01-27 06:04:26 +00:00
if (nextOrder==-1 && i<song.ordersLen-1) {
nextOrder=i+1;
nextRow=effectVal;
}
2022-01-25 20:06:29 +00:00
} else if (pat[k]->data[j][4+(l<<1)]==0x0b) {
2022-01-27 06:04:26 +00:00
if (nextOrder==-1) {
nextOrder=effectVal;
nextRow=0;
}
2022-01-25 20:06:29 +00:00
}
}
}
if (nextOrder!=-1) {
if (nextOrder<=i) {
loopOrder=nextOrder;
loopRow=nextRow;
2022-01-27 06:04:26 +00:00
loopEnd=i;
2022-01-25 20:06:29 +00:00
return;
}
i=nextOrder-1;
nextOrder=-1;
break;
}
}
}
}
void _runExportThread(DivEngine* caller) {
caller->runExportThread();
}
bool DivEngine::isExporting() {
return exporting;
}
#define EXPORT_BUFSIZE 2048
void DivEngine::runExportThread() {
switch (exportMode) {
case DIV_EXPORT_MODE_ONE: {
SNDFILE* sf;
SF_INFO si;
si.samplerate=got.rate;
si.channels=2;
si.format=SF_FORMAT_WAV|SF_FORMAT_PCM_16;
sf=sf_open(exportPath.c_str(),SFM_WRITE,&si);
if (sf==NULL) {
2022-01-23 04:50:49 +00:00
logE("could not open file for writing! (%s)\n",sf_strerror(NULL));
exporting=false;
return;
}
float* outBuf[3];
outBuf[0]=new float[EXPORT_BUFSIZE];
outBuf[1]=new float[EXPORT_BUFSIZE];
outBuf[2]=new float[EXPORT_BUFSIZE*2];
// take control of audio output
deinitAudioBackend();
playSub(false);
2022-01-23 04:50:49 +00:00
logI("rendering to file...\n");
while (playing) {
nextBuf(NULL,outBuf,0,2,EXPORT_BUFSIZE);
for (int i=0; i<EXPORT_BUFSIZE; i++) {
outBuf[2][i<<1]=MAX(-1.0f,MIN(1.0f,outBuf[0][i]));
outBuf[2][1+(i<<1)]=MAX(-1.0f,MIN(1.0f,outBuf[1][i]));
}
if (totalProcessed>EXPORT_BUFSIZE) {
logE("error: total processed is bigger than export bufsize! %d>%d\n",totalProcessed,EXPORT_BUFSIZE);
}
if (sf_writef_float(sf,outBuf[2],totalProcessed)!=(int)totalProcessed) {
logE("error: failed to write entire buffer!\n");
break;
}
}
2022-01-18 07:04:03 +00:00
delete[] outBuf[0];
delete[] outBuf[1];
delete[] outBuf[2];
if (sf_close(sf)!=0) {
logE("could not close audio file!\n");
}
exporting=false;
if (initAudioBackend()) {
for (int i=0; i<song.systemLen; i++) {
disCont[i].setRates(got.rate);
disCont[i].setQuality(lowQuality);
}
if (!output->setRun(true)) {
logE("error while activating audio!\n");
}
}
2022-01-23 04:50:49 +00:00
logI("done!\n");
break;
}
case DIV_EXPORT_MODE_MANY_SYS: {
SNDFILE* sf[32];
SF_INFO si[32];
String fname[32];
for (int i=0; i<song.systemLen; i++) {
sf[i]=NULL;
si[i].samplerate=got.rate;
if (disCont[i].dispatch->isStereo()) {
si[i].channels=2;
} else {
si[i].channels=1;
}
si[i].format=SF_FORMAT_WAV|SF_FORMAT_PCM_16;
}
for (int i=0; i<song.systemLen; i++) {
fname[i]=fmt::sprintf("%s_s%02d.wav",exportPath,i+1);
2022-01-23 04:50:49 +00:00
logI("- %s\n",fname[i].c_str());
sf[i]=sf_open(fname[i].c_str(),SFM_WRITE,&si[i]);
if (sf[i]==NULL) {
2022-01-23 04:50:49 +00:00
logE("could not open file for writing! (%s)\n",sf_strerror(NULL));
for (int j=0; j<i; j++) {
sf_close(sf[i]);
}
return;
}
}
float* outBuf[2];
outBuf[0]=new float[EXPORT_BUFSIZE];
outBuf[1]=new float[EXPORT_BUFSIZE];
short* sysBuf=new short[EXPORT_BUFSIZE*2];
// take control of audio output
deinitAudioBackend();
playSub(false);
2022-01-23 04:50:49 +00:00
logI("rendering to files...\n");
while (playing) {
nextBuf(NULL,outBuf,0,2,EXPORT_BUFSIZE);
for (int i=0; i<song.systemLen; i++) {
for (int j=0; j<EXPORT_BUFSIZE; j++) {
if (!disCont[i].dispatch->isStereo()) {
sysBuf[j]=disCont[i].bbOut[0][j];
} else {
sysBuf[j<<1]=disCont[i].bbOut[0][j];
sysBuf[1+(j<<1)]=disCont[i].bbOut[1][j];
}
}
if (totalProcessed>EXPORT_BUFSIZE) {
logE("error: total processed is bigger than export bufsize! (%d) %d>%d\n",i,totalProcessed,EXPORT_BUFSIZE);
}
if (sf_writef_short(sf[i],sysBuf,totalProcessed)!=(int)totalProcessed) {
logE("error: failed to write entire buffer! (%d)\n",i);
break;
}
}
}
2022-01-18 07:04:03 +00:00
delete[] outBuf[0];
delete[] outBuf[1];
delete[] sysBuf;
for (int i=0; i<song.systemLen; i++) {
if (sf_close(sf[i])!=0) {
logE("could not close audio file!\n");
}
}
exporting=false;
if (initAudioBackend()) {
for (int i=0; i<song.systemLen; i++) {
disCont[i].setRates(got.rate);
disCont[i].setQuality(lowQuality);
}
if (!output->setRun(true)) {
logE("error while activating audio!\n");
}
}
2022-01-23 04:50:49 +00:00
logI("done!\n");
break;
}
case DIV_EXPORT_MODE_MANY_CHAN: {
2022-01-18 07:04:03 +00:00
// take control of audio output
deinitAudioBackend();
float* outBuf[3];
outBuf[0]=new float[EXPORT_BUFSIZE];
outBuf[1]=new float[EXPORT_BUFSIZE];
outBuf[2]=new float[EXPORT_BUFSIZE*2];
int loopCount=remainingLoops;
2022-01-23 04:50:49 +00:00
logI("rendering to files...\n");
2022-01-18 07:04:03 +00:00
for (int i=0; i<chans; i++) {
SNDFILE* sf;
SF_INFO si;
String fname=fmt::sprintf("%s_c%02d.wav",exportPath,i+1);
2022-01-23 04:50:49 +00:00
logI("- %s\n",fname.c_str());
2022-01-18 07:04:03 +00:00
si.samplerate=got.rate;
si.channels=2;
si.format=SF_FORMAT_WAV|SF_FORMAT_PCM_16;
sf=sf_open(fname.c_str(),SFM_WRITE,&si);
if (sf==NULL) {
2022-01-23 04:50:49 +00:00
logE("could not open file for writing! (%s)\n",sf_strerror(NULL));
2022-01-18 07:04:03 +00:00
break;
}
for (int j=0; j<chans; j++) {
bool mute=(j!=i);
isMuted[j]=mute;
if (disCont[dispatchOfChan[j]].dispatch!=NULL) {
disCont[dispatchOfChan[j]].dispatch->muteChannel(dispatchChanOfChan[j],isMuted[j]);
}
}
curOrder=0;
remainingLoops=loopCount;
playSub(false);
while (playing) {
nextBuf(NULL,outBuf,0,2,EXPORT_BUFSIZE);
for (int j=0; j<EXPORT_BUFSIZE; j++) {
outBuf[2][j<<1]=MAX(-1.0f,MIN(1.0f,outBuf[0][j]));
outBuf[2][1+(j<<1)]=MAX(-1.0f,MIN(1.0f,outBuf[1][j]));
}
if (totalProcessed>EXPORT_BUFSIZE) {
logE("error: total processed is bigger than export bufsize! %d>%d\n",totalProcessed,EXPORT_BUFSIZE);
}
if (sf_writef_float(sf,outBuf[2],totalProcessed)!=(int)totalProcessed) {
logE("error: failed to write entire buffer!\n");
break;
}
}
if (sf_close(sf)!=0) {
logE("could not close audio file!\n");
}
}
exporting=false;
delete[] outBuf[0];
delete[] outBuf[1];
delete[] outBuf[2];
for (int i=0; i<chans; i++) {
isMuted[i]=false;
if (disCont[dispatchOfChan[i]].dispatch!=NULL) {
disCont[dispatchOfChan[i]].dispatch->muteChannel(dispatchChanOfChan[i],false);
}
}
if (initAudioBackend()) {
for (int i=0; i<song.systemLen; i++) {
disCont[i].setRates(got.rate);
disCont[i].setQuality(lowQuality);
}
if (!output->setRun(true)) {
logE("error while activating audio!\n");
}
}
2022-01-23 04:50:49 +00:00
logI("done!\n");
break;
}
}
}
bool DivEngine::saveAudio(const char* path, int loops, DivAudioExportModes mode) {
exportPath=path;
exportMode=mode;
exporting=true;
stop();
repeatPattern=false;
setOrder(0);
remainingLoops=loops;
exportThread=new std::thread(_runExportThread,this);
return true;
}
void DivEngine::waitAudioFile() {
if (exportThread!=NULL) {
exportThread->join();
}
}
bool DivEngine::haltAudioFile() {
stop();
return true;
}
2022-01-18 04:59:52 +00:00
void DivEngine::notifyInsChange(int ins) {
BUSY_BEGIN;
2022-01-18 04:59:52 +00:00
for (int i=0; i<song.systemLen; i++) {
disCont[i].dispatch->notifyInsChange(ins);
}
BUSY_END;
2022-01-18 04:59:52 +00:00
}
2022-01-18 05:25:10 +00:00
void DivEngine::notifyWaveChange(int wave) {
BUSY_BEGIN;
2022-01-18 05:25:10 +00:00
for (int i=0; i<song.systemLen; i++) {
disCont[i].dispatch->notifyWaveChange(wave);
}
BUSY_END;
2022-01-18 05:25:10 +00:00
}
void DivEngine::renderSamplesP() {
BUSY_BEGIN;
renderSamples();
BUSY_END;
}
void DivEngine::renderSamples() {
2021-12-21 21:02:31 +00:00
sPreview.sample=-1;
sPreview.pos=0;
2021-12-10 09:22:13 +00:00
2022-02-24 21:16:02 +00:00
// step 1: render samples
for (int i=0; i<song.sampleLen; i++) {
song.sample[i]->render();
}
2021-12-11 04:41:00 +00:00
2022-02-24 21:16:02 +00:00
// step 2: allocate ADPCM-A samples
if (adpcmAMem==NULL) adpcmAMem=new unsigned char[16777216];
2021-12-11 04:41:00 +00:00
size_t memPos=0;
for (int i=0; i<song.sampleLen; i++) {
DivSample* s=song.sample[i];
2022-02-24 21:16:02 +00:00
int paddedLen=(s->lengthA+255)&(~0xff);
if ((memPos&0xf00000)!=((memPos+paddedLen)&0xf00000)) {
memPos=(memPos+0xfffff)&0xf00000;
2021-12-11 04:41:00 +00:00
}
2022-01-24 17:47:18 +00:00
if (memPos>=16777216) {
2022-02-25 05:11:27 +00:00
logW("out of ADPCM-A memory for sample %d!\n",i);
2022-01-24 17:47:18 +00:00
break;
}
2022-02-24 21:16:02 +00:00
if (memPos+paddedLen>=16777216) {
memcpy(adpcmAMem+memPos,s->dataA,16777216-memPos);
2022-02-25 05:11:27 +00:00
logW("out of ADPCM-A memory for sample %d!\n",i);
2022-01-24 17:47:18 +00:00
} else {
2022-02-24 21:16:02 +00:00
memcpy(adpcmAMem+memPos,s->dataA,paddedLen);
2022-01-24 17:47:18 +00:00
}
2022-02-24 21:16:02 +00:00
s->offA=memPos;
memPos+=paddedLen;
2021-12-11 04:41:00 +00:00
}
2022-02-24 21:16:02 +00:00
adpcmAMemLen=memPos+256;
2022-02-22 09:01:57 +00:00
2022-02-25 05:11:27 +00:00
// step 2: allocate ADPCM-B samples
if (adpcmBMem==NULL) adpcmBMem=new unsigned char[16777216];
memPos=0;
for (int i=0; i<song.sampleLen; i++) {
DivSample* s=song.sample[i];
int paddedLen=(s->lengthB+255)&(~0xff);
if ((memPos&0xf00000)!=((memPos+paddedLen)&0xf00000)) {
memPos=(memPos+0xfffff)&0xf00000;
}
if (memPos>=16777216) {
logW("out of ADPCM-B memory for sample %d!\n",i);
break;
}
if (memPos+paddedLen>=16777216) {
memcpy(adpcmBMem+memPos,s->dataB,16777216-memPos);
logW("out of ADPCM-B memory for sample %d!\n",i);
} else {
memcpy(adpcmBMem+memPos,s->dataB,paddedLen);
}
s->offB=memPos;
memPos+=paddedLen;
}
adpcmBMemLen=memPos+256;
2022-02-22 09:01:57 +00:00
// step 4: allocate qsound pcm samples
if (qsoundMem==NULL) qsoundMem=new unsigned char[16777216];
memset(qsoundMem,0,16777216);
2022-02-22 09:01:57 +00:00
memPos=0;
for (int i=0; i<song.sampleLen; i++) {
DivSample* s=song.sample[i];
2022-02-24 21:16:02 +00:00
int length=s->length8;
if (length>65536-16) {
length=65536-16;
2022-02-23 03:13:17 +00:00
}
2022-02-22 09:01:57 +00:00
if ((memPos&0xff0000)!=((memPos+length)&0xff0000)) {
memPos=(memPos+0xffff)&0xff0000;
}
if (memPos>=16777216) {
logW("out of QSound PCM memory for sample %d!\n",i);
break;
}
if (memPos+length>=16777216) {
2022-02-23 03:13:17 +00:00
for (unsigned int i=0; i<16777216-(memPos+length); i++) {
2022-02-24 21:16:02 +00:00
qsoundMem[(memPos+i)^0x8000]=s->data8[i];
2022-02-23 03:13:17 +00:00
}
2022-02-22 09:01:57 +00:00
logW("out of QSound PCM memory for sample %d!\n",i);
} else {
2022-02-23 03:13:17 +00:00
for (int i=0; i<length; i++) {
2022-02-24 21:16:02 +00:00
qsoundMem[(memPos+i)^0x8000]=s->data8[i];
2022-02-23 03:13:17 +00:00
}
2022-02-22 09:01:57 +00:00
}
2022-02-24 21:16:02 +00:00
s->offQSound=memPos^0x8000;
2022-02-22 09:01:57 +00:00
memPos+=length+16;
}
qsoundMemLen=memPos+256;
// step 4: allocate x1-010 pcm samples
if (x1_010Mem==NULL) x1_010Mem=new unsigned char[1048576];
memset(x1_010Mem,0,1048576);
memPos=0;
for (int i=0; i<song.sampleLen; i++) {
DivSample* s=song.sample[i];
int paddedLen=(s->length8+4095)&(~0xfff);
2022-03-07 15:15:21 +00:00
// fit sample bank size to 128KB for Seta 2 external bankswitching logic (not emulated yet!)
if (paddedLen>131072) {
paddedLen=131072;
}
if ((memPos&0xfe0000)!=((memPos+paddedLen)&0xfe0000)) {
memPos=(memPos+0x1ffff)&0xfe0000;
}
if (memPos>=1048576) {
logW("out of X1-010 memory for sample %d!\n",i);
break;
}
if (memPos+paddedLen>=1048576) {
memcpy(x1_010Mem+memPos,s->data8,1048576-memPos);
logW("out of X1-010 memory for sample %d!\n",i);
} else {
memcpy(x1_010Mem+memPos,s->data8,paddedLen);
}
s->offX1_010=memPos;
memPos+=paddedLen;
}
x1_010MemLen=memPos+256;
}
2022-03-02 07:22:51 +00:00
void DivEngine::createNew(const int* description) {
2021-12-24 23:23:01 +00:00
quitDispatch();
BUSY_BEGIN;
saveLock.lock();
2021-12-24 23:23:01 +00:00
song.unload();
song=DivSong();
2022-03-01 22:19:52 +00:00
if (description!=NULL) {
if (description[0]!=0) {
int index=0;
for (int i=0; description[i]; i+=4) {
song.system[index]=(DivSystem)description[i];
song.systemVol[index]=description[i+1];
song.systemPan[index]=description[i+2];
song.systemFlags[index]=description[i+3];
index++;
if (index>=32) break;
}
song.systemLen=index;
}
}
2022-01-08 21:03:32 +00:00
recalcChans();
2021-12-24 23:23:01 +00:00
renderSamples();
saveLock.unlock();
BUSY_END;
2021-12-24 23:23:01 +00:00
initDispatch();
BUSY_BEGIN;
2021-12-24 23:23:01 +00:00
reset();
BUSY_END;
2021-12-24 23:23:01 +00:00
}
2022-01-08 21:03:32 +00:00
void DivEngine::changeSystem(int index, DivSystem which) {
2021-12-18 03:14:41 +00:00
quitDispatch();
BUSY_BEGIN;
2022-03-21 22:56:48 +00:00
saveLock.lock();
2022-01-08 21:03:32 +00:00
song.system[index]=which;
song.systemFlags[index]=0;
2022-01-08 21:03:32 +00:00
recalcChans();
2022-03-21 22:56:48 +00:00
saveLock.unlock();
BUSY_END;
2021-12-18 03:14:41 +00:00
initDispatch();
BUSY_BEGIN;
2021-12-18 03:14:41 +00:00
renderSamples();
reset();
BUSY_END;
2021-12-18 03:14:41 +00:00
}
2022-01-08 23:18:23 +00:00
bool DivEngine::addSystem(DivSystem which) {
if (song.systemLen>32) {
2022-03-15 23:36:24 +00:00
lastError="max number of systems is 32";
2022-01-08 23:18:23 +00:00
return false;
}
// this was DIV_MAX_CHANS but I am setting it to 63 for now due to an ImGui limitation
if (chans+getChannelCount(which)>63) {
lastError="max number of total channels is 63";
2022-01-08 23:18:23 +00:00
return false;
}
quitDispatch();
BUSY_BEGIN;
2022-03-21 22:56:48 +00:00
saveLock.lock();
song.system[song.systemLen]=which;
song.systemVol[song.systemLen]=64;
song.systemPan[song.systemLen]=0;
song.systemFlags[song.systemLen++]=0;
2022-01-08 23:18:23 +00:00
recalcChans();
2022-03-21 22:56:48 +00:00
saveLock.unlock();
BUSY_END;
2022-01-08 23:18:23 +00:00
initDispatch();
BUSY_BEGIN;
2022-01-08 23:18:23 +00:00
renderSamples();
reset();
BUSY_END;
2022-01-08 23:18:23 +00:00
return true;
}
2022-01-09 21:36:47 +00:00
bool DivEngine::removeSystem(int index) {
if (song.systemLen<=1) {
lastError="cannot remove the last one";
return false;
}
if (index<0 || index>=song.systemLen) {
lastError="invalid index";
return false;
}
quitDispatch();
BUSY_BEGIN;
2022-03-21 22:56:48 +00:00
saveLock.lock();
2022-01-09 21:36:47 +00:00
song.system[index]=DIV_SYSTEM_NULL;
song.systemLen--;
for (int i=index; i<song.systemLen; i++) {
2022-01-19 03:02:04 +00:00
song.system[i]=song.system[i+1];
2022-01-09 21:36:47 +00:00
}
recalcChans();
2022-03-21 22:56:48 +00:00
saveLock.unlock();
BUSY_END;
2022-01-09 21:36:47 +00:00
initDispatch();
BUSY_BEGIN;
2022-01-09 21:36:47 +00:00
renderSamples();
reset();
BUSY_END;
2022-01-09 21:36:47 +00:00
return true;
}
2022-02-01 23:08:19 +00:00
void DivEngine::poke(int sys, unsigned int addr, unsigned short val) {
if (sys<0 || sys>=song.systemLen) return;
BUSY_BEGIN;
2022-02-01 23:08:19 +00:00
disCont[sys].dispatch->poke(addr,val);
BUSY_END;
2022-02-01 23:08:19 +00:00
}
void DivEngine::poke(int sys, std::vector<DivRegWrite>& wlist) {
if (sys<0 || sys>=song.systemLen) return;
BUSY_BEGIN;
2022-02-01 23:08:19 +00:00
disCont[sys].dispatch->poke(wlist);
BUSY_END;
2022-02-01 23:08:19 +00:00
}
String DivEngine::getLastError() {
return lastError;
}
2022-01-29 06:22:32 +00:00
String DivEngine::getWarnings() {
return warnings;
}
DivInstrument* DivEngine::getIns(int index) {
if (index<0 || index>=song.insLen) return &song.nullIns;
return song.ins[index];
}
DivWavetable* DivEngine::getWave(int index) {
if (index<0 || index>=song.waveLen) {
if (song.waveLen>0) {
return song.wave[0];
} else {
return &song.nullWave;
}
}
return song.wave[index];
}
2022-02-25 03:52:20 +00:00
DivSample* DivEngine::getSample(int index) {
if (index<0 || index>=song.sampleLen) return &song.nullSample;
return song.sample[index];
}
void DivEngine::setLoops(int loops) {
remainingLoops=loops;
}
2022-01-27 05:29:16 +00:00
DivChannelState* DivEngine::getChanState(int ch) {
if (ch<0 || ch>=chans) return NULL;
return &chan[ch];
}
void* DivEngine::getDispatchChanState(int ch) {
if (ch<0 || ch>=chans) return NULL;
return disCont[dispatchOfChan[ch]].dispatch->getChanState(dispatchChanOfChan[ch]);
}
2022-02-22 09:01:57 +00:00
unsigned char* DivEngine::getRegisterPool(int sys, int& size, int& depth) {
if (sys<0 || sys>=song.systemLen) return NULL;
if (disCont[sys].dispatch==NULL) return NULL;
size=disCont[sys].dispatch->getRegisterPoolSize();
2022-02-22 09:01:57 +00:00
depth=disCont[sys].dispatch->getRegisterPoolDepth();
return disCont[sys].dispatch->getRegisterPool();
}
void DivEngine::enableCommandStream(bool enable) {
cmdStreamEnabled=enable;
}
void DivEngine::getCommandStream(std::vector<DivCommand>& where) {
BUSY_BEGIN;
where.clear();
for (DivCommand& i: cmdStream) {
where.push_back(i);
}
cmdStream.clear();
BUSY_END;
}
void DivEngine::playSub(bool preserveDrift, int goalRow) {
for (int i=0; i<song.systemLen; i++) disCont[i].dispatch->setSkipRegisterWrites(false);
2021-12-11 21:51:34 +00:00
reset();
2021-12-21 07:02:25 +00:00
if (preserveDrift && curOrder==0) return;
bool oldRepeatPattern=repeatPattern;
repeatPattern=false;
int goal=curOrder;
curOrder=0;
2021-12-11 08:34:43 +00:00
curRow=0;
2022-02-06 05:42:07 +00:00
stepPlay=0;
int prevDrift;
prevDrift=clockDrift;
clockDrift=0;
cycles=0;
2021-12-21 07:02:25 +00:00
if (preserveDrift) {
endOfSong=false;
} else {
ticks=1;
2021-12-21 07:30:09 +00:00
totalTicks=0;
totalSeconds=0;
totalTicksR=0;
2021-12-21 07:02:25 +00:00
}
speedAB=false;
2021-12-11 21:51:34 +00:00
playing=true;
2022-01-08 21:03:32 +00:00
for (int i=0; i<song.systemLen; i++) disCont[i].dispatch->setSkipRegisterWrites(true);
2022-02-17 08:20:08 +00:00
while (playing && curOrder<goal) {
if (nextTick(preserveDrift)) return;
}
int oldOrder=curOrder;
2022-02-17 08:20:08 +00:00
while (playing && curRow<goalRow) {
if (nextTick(preserveDrift)) return;
if (oldOrder!=curOrder) break;
}
2022-01-08 21:03:32 +00:00
for (int i=0; i<song.systemLen; i++) disCont[i].dispatch->setSkipRegisterWrites(false);
if (goal>0 || goalRow>0) {
2022-01-08 21:03:32 +00:00
for (int i=0; i<song.systemLen; i++) disCont[i].dispatch->forceIns();
2021-12-21 21:02:31 +00:00
}
for (int i=0; i<chans; i++) {
chan[i].cut=-1;
}
repeatPattern=oldRepeatPattern;
if (preserveDrift) {
clockDrift=prevDrift;
} else {
clockDrift=0;
cycles=0;
2021-12-21 07:02:25 +00:00
}
if (!preserveDrift) {
ticks=1;
}
cmdStream.clear();
}
int DivEngine::calcBaseFreq(double clock, double divider, int note, bool period) {
double base=(period?(song.tuning*0.0625):song.tuning)*pow(2.0,(float)(note+3)/12.0);
return period?
round((clock/base)/divider):
base*(divider/clock);
}
2022-02-03 07:24:11 +00:00
int DivEngine::calcFreq(int base, int pitch, bool period, int octave) {
if (song.linearPitch) {
return period?
base*pow(2,-(double)pitch/(12.0*128.0))/(98.0+globalPitch*6.0)*98.0:
(base*pow(2,(double)pitch/(12.0*128.0))*(98+globalPitch*6))/98;
}
return period?
base-pitch:
2022-02-03 07:24:11 +00:00
base+((pitch*octave)>>1);
}
void DivEngine::play() {
BUSY_BEGIN_SOFT;
2022-02-06 21:29:30 +00:00
sPreview.sample=-1;
sPreview.wave=-1;
sPreview.pos=0;
2022-02-06 05:42:07 +00:00
if (stepPlay==0) {
freelance=false;
playSub(false);
} else {
stepPlay=0;
}
2022-02-10 08:15:39 +00:00
for (int i=0; i<DIV_MAX_CHANS; i++) {
keyHit[i]=false;
}
BUSY_END;
2021-12-11 08:34:43 +00:00
}
void DivEngine::playToRow(int row) {
BUSY_BEGIN_SOFT;
2022-02-06 21:29:30 +00:00
sPreview.sample=-1;
sPreview.wave=-1;
sPreview.pos=0;
freelance=false;
playSub(false,row);
2022-02-10 08:15:39 +00:00
for (int i=0; i<DIV_MAX_CHANS; i++) {
keyHit[i]=false;
}
BUSY_END;
}
2022-02-06 05:42:07 +00:00
void DivEngine::stepOne(int row) {
if (!isPlaying()) {
BUSY_BEGIN_SOFT;
2022-02-06 05:42:07 +00:00
freelance=false;
playSub(false,row);
2022-02-10 08:15:39 +00:00
for (int i=0; i<DIV_MAX_CHANS; i++) {
keyHit[i]=false;
}
} else {
BUSY_BEGIN;
2022-02-06 05:42:07 +00:00
}
stepPlay=2;
ticks=1;
BUSY_END;
2022-02-06 05:42:07 +00:00
}
2021-12-11 08:34:43 +00:00
void DivEngine::stop() {
BUSY_BEGIN;
2021-12-28 23:23:57 +00:00
freelance=false;
2021-12-11 08:34:43 +00:00
playing=false;
extValuePresent=false;
2022-02-06 05:42:07 +00:00
stepPlay=0;
remainingLoops=-1;
2022-02-06 21:29:30 +00:00
sPreview.sample=-1;
sPreview.wave=-1;
sPreview.pos=0;
for (int i=0; i<song.systemLen; i++) {
disCont[i].dispatch->notifyPlaybackStop();
}
BUSY_END;
2021-12-11 08:34:43 +00:00
}
2022-02-03 23:38:57 +00:00
void DivEngine::halt() {
BUSY_BEGIN;
2022-02-03 23:38:57 +00:00
halted=true;
BUSY_END;
2022-02-03 23:38:57 +00:00
}
void DivEngine::resume() {
BUSY_BEGIN;
2022-02-03 23:38:57 +00:00
halted=false;
haltOn=DIV_HALT_NONE;
BUSY_END;
2022-02-03 23:38:57 +00:00
}
void DivEngine::haltWhen(DivHaltPositions when) {
BUSY_BEGIN;
2022-02-03 23:38:57 +00:00
halted=false;
haltOn=when;
BUSY_END;
2022-02-03 23:38:57 +00:00
}
bool DivEngine::isHalted() {
return halted;
}
const char** DivEngine::getRegisterSheet(int sys) {
if (sys<0 || sys>=song.systemLen) return NULL;
return disCont[sys].dispatch->getRegisterSheet();
}
2022-01-08 21:03:32 +00:00
void DivEngine::recalcChans() {
chans=0;
int chanIndex=0;
for (int i=0; i<song.systemLen; i++) {
int chanCount=getChannelCount(song.system[i]);
chans+=chanCount;
for (int j=0; j<chanCount; j++) {
sysOfChan[chanIndex]=song.system[i];
dispatchOfChan[chanIndex]=i;
dispatchChanOfChan[chanIndex]=j;
chanIndex++;
}
}
}
2021-12-11 21:51:34 +00:00
void DivEngine::reset() {
2022-01-08 06:57:37 +00:00
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-11 21:51:34 +00:00
chan[i]=DivChannelState();
2022-01-08 21:03:32 +00:00
if (i<chans) chan[i].volMax=(disCont[dispatchOfChan[i]].dispatch->dispatch(DivCommand(DIV_CMD_GET_VOLMAX,dispatchChanOfChan[i]))<<8)|0xff;
2021-12-11 21:51:34 +00:00
chan[i].volume=chan[i].volMax;
}
extValue=0;
extValuePresent=0;
2021-12-21 07:30:09 +00:00
speed1=song.speed1;
speed2=song.speed2;
nextSpeed=speed1;
divider=60;
if (song.customTempo) {
divider=song.hz;
} else {
if (song.pal) {
divider=60;
} else {
divider=50;
}
}
2021-12-27 20:22:57 +00:00
globalPitch=0;
2022-01-08 21:03:32 +00:00
for (int i=0; i<song.systemLen; i++) {
disCont[i].dispatch->reset();
2022-01-20 02:04:51 +00:00
disCont[i].clear();
2022-01-08 21:03:32 +00:00
}
2021-12-11 21:51:34 +00:00
}
void DivEngine::syncReset() {
BUSY_BEGIN;
reset();
BUSY_END;
}
const int sampleRates[6]={
4000, 8000, 11025, 16000, 22050, 32000
};
int DivEngine::fileToDivRate(int frate) {
if (frate<0) frate=0;
if (frate>5) frate=5;
return sampleRates[frate];
}
int DivEngine::divToFileRate(int drate) {
if (drate>26000) {
return 5;
} else if (drate>18000) {
return 4;
} else if (drate>14000) {
return 3;
} else if (drate>9500) {
return 2;
} else if (drate>6000) {
return 1;
} else {
return 0;
}
return 4;
}
int DivEngine::getEffectiveSampleRate(int rate) {
if (rate<1) return 0;
switch (song.system[0]) {
case DIV_SYSTEM_YMU759:
return 8000;
case DIV_SYSTEM_YM2612: case DIV_SYSTEM_YM2612_EXT:
return 1278409/(1280000/rate);
case DIV_SYSTEM_PCE:
return 1789773/(1789773/rate);
case DIV_SYSTEM_SEGAPCM: case DIV_SYSTEM_SEGAPCM_COMPAT:
return (31250*MIN(255,(rate*255/31250)))/255;
2022-03-07 15:15:21 +00:00
case DIV_SYSTEM_QSOUND:
return (24038*MIN(65535,(rate*4096/24038)))/4096;
case DIV_SYSTEM_YM2610: case DIV_SYSTEM_YM2610_EXT: case DIV_SYSTEM_YM2610_FULL: case DIV_SYSTEM_YM2610_FULL_EXT: case DIV_SYSTEM_YM2610B: case DIV_SYSTEM_YM2610B_EXT:
return 18518;
2022-03-04 11:13:49 +00:00
case DIV_SYSTEM_VERA:
return (48828*MIN(128,(rate*128/48828)))/128;
case DIV_SYSTEM_X1_010:
return (31250*MIN(255,(rate*16/31250)))/16; // TODO: support variable clock case
default:
break;
}
return rate;
}
2022-01-20 21:51:31 +00:00
void DivEngine::previewSample(int sample, int note) {
BUSY_BEGIN;
2021-12-29 04:10:13 +00:00
if (sample<0 || sample>=(int)song.sample.size()) {
sPreview.sample=-1;
sPreview.pos=0;
BUSY_END;
return;
}
2022-01-08 21:03:32 +00:00
blip_clear(samp_bb);
2022-01-20 21:51:31 +00:00
double rate=song.sample[sample]->rate;
if (note>=0) {
2022-02-04 08:29:40 +00:00
rate=(song.tuning*pow(2.0,(double)(note+3)/12.0)*((double)song.sample[sample]->centerRate/8363.0));
2022-01-20 21:51:31 +00:00
if (rate<=0) rate=song.sample[sample]->rate;
}
if (rate<100) rate=100;
2022-01-20 21:51:31 +00:00
blip_set_rates(samp_bb,rate,got.rate);
2022-01-08 21:03:32 +00:00
samp_prevSample=0;
sPreview.pos=0;
sPreview.sample=sample;
sPreview.wave=-1;
BUSY_END;
}
2022-01-20 21:51:31 +00:00
void DivEngine::stopSamplePreview() {
BUSY_BEGIN;
2022-01-20 21:51:31 +00:00
sPreview.sample=-1;
sPreview.pos=0;
BUSY_END;
2022-01-20 21:51:31 +00:00
}
void DivEngine::previewWave(int wave, int note) {
BUSY_BEGIN;
if (wave<0 || wave>=(int)song.wave.size()) {
sPreview.wave=-1;
sPreview.pos=0;
BUSY_END;
return;
}
2022-01-20 05:43:08 +00:00
if (song.wave[wave]->len<=0) {
BUSY_END;
2022-01-20 05:43:08 +00:00
return;
}
blip_clear(samp_bb);
double rate=song.wave[wave]->len*((song.tuning*0.0625)*pow(2.0,(double)(note+3)/12.0));
if (rate<100) rate=100;
blip_set_rates(samp_bb,rate,got.rate);
samp_prevSample=0;
sPreview.pos=0;
sPreview.sample=-1;
sPreview.wave=wave;
BUSY_END;
}
void DivEngine::stopWavePreview() {
BUSY_BEGIN;
sPreview.wave=-1;
sPreview.pos=0;
BUSY_END;
}
2021-12-19 08:16:24 +00:00
String DivEngine::getConfigPath() {
return configPath;
}
2021-12-13 22:09:46 +00:00
int DivEngine::getMaxVolumeChan(int ch) {
return chan[ch].volMax>>8;
}
2021-12-11 08:34:43 +00:00
unsigned char DivEngine::getOrder() {
return curOrder;
}
2021-12-13 22:09:46 +00:00
int DivEngine::getRow() {
return curRow;
}
2021-12-21 07:30:09 +00:00
unsigned char DivEngine::getSpeed1() {
return speed1;
}
unsigned char DivEngine::getSpeed2() {
return speed2;
}
2022-03-16 04:30:15 +00:00
float DivEngine::getHz() {
2021-12-21 07:30:09 +00:00
if (song.customTempo) {
return song.hz;
} else if (song.pal) {
2022-03-16 04:30:15 +00:00
return 60.0;
2021-12-21 07:30:09 +00:00
} else {
2022-03-16 04:30:15 +00:00
return 50.0;
2021-12-21 07:30:09 +00:00
}
2022-03-16 04:30:15 +00:00
return 60.0;
2021-12-21 07:30:09 +00:00
}
2022-03-16 04:30:15 +00:00
float DivEngine::getCurHz() {
return divider;
}
int DivEngine::getTotalSeconds() {
return totalSeconds;
}
2021-12-21 07:30:09 +00:00
int DivEngine::getTotalTicks() {
return totalTicks;
}
bool DivEngine::getRepeatPattern() {
return repeatPattern;
}
void DivEngine::setRepeatPattern(bool value) {
BUSY_BEGIN;
repeatPattern=value;
BUSY_END;
}
bool DivEngine::hasExtValue() {
return extValuePresent;
}
unsigned char DivEngine::getExtValue() {
return extValue;
}
2021-12-13 22:09:46 +00:00
bool DivEngine::isPlaying() {
2021-12-28 23:23:57 +00:00
return (playing && !freelance);
2021-12-13 22:09:46 +00:00
}
2022-02-06 05:42:07 +00:00
bool DivEngine::isStepping() {
return !(stepPlay==0);
}
2021-12-18 08:25:42 +00:00
bool DivEngine::isChannelMuted(int chan) {
return isMuted[chan];
}
void DivEngine::toggleMute(int chan) {
muteChannel(chan,!isMuted[chan]);
}
void DivEngine::toggleSolo(int chan) {
bool solo=false;
for (int i=0; i<chans; i++) {
if (i==chan) {
solo=true;
continue;
} else {
if (!isMuted[i]) {
solo=false;
break;
}
}
}
BUSY_BEGIN;
2021-12-18 08:25:42 +00:00
if (!solo) {
for (int i=0; i<chans; i++) {
isMuted[i]=(i!=chan);
2022-01-08 21:03:32 +00:00
if (disCont[dispatchOfChan[i]].dispatch!=NULL) {
disCont[dispatchOfChan[i]].dispatch->muteChannel(dispatchChanOfChan[i],isMuted[i]);
2021-12-18 08:25:42 +00:00
}
}
} else {
for (int i=0; i<chans; i++) {
isMuted[i]=false;
2022-01-08 21:03:32 +00:00
if (disCont[dispatchOfChan[i]].dispatch!=NULL) {
disCont[dispatchOfChan[i]].dispatch->muteChannel(dispatchChanOfChan[i],isMuted[i]);
2021-12-18 08:25:42 +00:00
}
}
}
BUSY_END;
2021-12-18 08:25:42 +00:00
}
void DivEngine::muteChannel(int chan, bool mute) {
BUSY_BEGIN;
2021-12-18 08:25:42 +00:00
isMuted[chan]=mute;
2022-01-08 21:03:32 +00:00
if (disCont[dispatchOfChan[chan]].dispatch!=NULL) {
disCont[dispatchOfChan[chan]].dispatch->muteChannel(dispatchChanOfChan[chan],isMuted[chan]);
2021-12-18 08:25:42 +00:00
}
BUSY_END;
2021-12-18 08:25:42 +00:00
}
void DivEngine::unmuteAll() {
BUSY_BEGIN;
for (int i=0; i<chans; i++) {
isMuted[i]=false;
if (disCont[dispatchOfChan[i]].dispatch!=NULL) {
disCont[dispatchOfChan[i]].dispatch->muteChannel(dispatchChanOfChan[i],isMuted[i]);
}
}
BUSY_END;
}
2022-01-10 04:50:26 +00:00
int DivEngine::addInstrument(int refChan) {
BUSY_BEGIN;
DivInstrument* ins=new DivInstrument;
int insCount=(int)song.ins.size();
ins->name=fmt::sprintf("Instrument %d",insCount);
2022-01-16 03:11:40 +00:00
ins->type=getPreferInsType(refChan);
saveLock.lock();
song.ins.push_back(ins);
song.insLen=insCount+1;
saveLock.unlock();
BUSY_END;
return insCount;
}
enum DivInsFormats {
DIV_INSFORMAT_DMP,
DIV_INSFORMAT_TFI,
DIV_INSFORMAT_VGI,
DIV_INSFORMAT_FTI,
DIV_INSFORMAT_BTI,
DIV_INSFORMAT_S3I,
DIV_INSFORMAT_SBI,
};
// TODO: re-organize this function to:
// - support replacing instruments
// - support instrument formats which contain multiple instruments
bool DivEngine::addInstrumentFromFile(const char* path) {
2022-01-29 06:22:32 +00:00
warnings="";
const char* pathRedux=strrchr(path,DIR_SEPARATOR);
if (pathRedux==NULL) {
pathRedux=path;
} else {
pathRedux++;
}
String stripPath;
const char* pathReduxEnd=strrchr(pathRedux,'.');
if (pathReduxEnd==NULL) {
stripPath=pathRedux;
} else {
2022-02-26 10:04:31 +00:00
for (const char* i=pathRedux; i!=pathReduxEnd && (*i); i++) {
stripPath+=*i;
}
}
FILE* f=ps_fopen(path,"rb");
if (f==NULL) {
2022-01-29 09:25:55 +00:00
lastError=strerror(errno);
return false;
}
unsigned char* buf;
ssize_t len;
if (fseek(f,0,SEEK_END)!=0) {
2022-01-29 09:25:55 +00:00
lastError=strerror(errno);
fclose(f);
return false;
}
len=ftell(f);
if (len<0) {
2022-01-29 09:25:55 +00:00
lastError=strerror(errno);
fclose(f);
return false;
}
if (len==0) {
2022-01-29 09:25:55 +00:00
lastError=strerror(errno);
fclose(f);
return false;
}
if (fseek(f,0,SEEK_SET)!=0) {
2022-01-29 09:25:55 +00:00
lastError=strerror(errno);
fclose(f);
return false;
}
buf=new unsigned char[len];
if (fread(buf,1,len,f)!=(size_t)len) {
logW("did not read entire instrument file buffer!\n");
2022-01-29 09:25:55 +00:00
lastError="did not read entire instrument file!";
delete[] buf;
return false;
}
fclose(f);
SafeReader reader=SafeReader(buf,len);
unsigned char magic[16];
bool isFurnaceInstr=false;
try {
reader.read(magic,16);
if (memcmp("-Furnace instr.-",magic,16)==0) {
isFurnaceInstr=true;
}
} catch (EndOfFileException& e) {
reader.seek(0,SEEK_SET);
}
DivInstrument* ins=new DivInstrument;
if (isFurnaceInstr) {
try {
short version=reader.readS();
reader.readS(); // reserved
2022-01-29 06:22:32 +00:00
if (version>DIV_ENGINE_VERSION) {
warnings="this instrument is made with a more recent version of Furnace!";
}
unsigned int dataPtr=reader.readI();
reader.seek(dataPtr,SEEK_SET);
if (ins->readInsData(reader,version)!=DIV_DATA_SUCCESS) {
lastError="invalid instrument header/data!";
delete ins;
delete[] buf;
return false;
}
} catch (EndOfFileException& e) {
lastError="premature end of file";
logE("premature end of file!\n");
delete ins;
delete[] buf;
return false;
}
} else { // read as a different format
const char* ext=strrchr(path,'.');
DivInsFormats format=DIV_INSFORMAT_DMP;
if (ext!=NULL) {
String extS;
for (; *ext; ext++) {
char i=*ext;
if (i>='A' && i<='Z') {
i+='a'-'A';
}
extS+=i;
}
if (extS==String(".dmp")) {
format=DIV_INSFORMAT_DMP;
} else if (extS==String(".tfi")) {
format=DIV_INSFORMAT_TFI;
} else if (extS==String(".vgi")) {
format=DIV_INSFORMAT_VGI;
} else if (extS==String(".fti")) {
format=DIV_INSFORMAT_FTI;
} else if (extS==String(".bti")) {
format=DIV_INSFORMAT_BTI;
} else if (extS==String(".s3i")) {
format = DIV_INSFORMAT_S3I;
} else if (extS==String(".sbi")) {
format = DIV_INSFORMAT_SBI;
}
2022-01-31 17:55:51 +00:00
}
// TDOO these really should be re-organized to separate functions per instrument file type.
switch (format) {
case DIV_INSFORMAT_DMP: {
// this is a ridiculous mess
unsigned char version=0;
unsigned char sys=0;
try {
reader.seek(0,SEEK_SET);
version=reader.readC();
} catch (EndOfFileException& e) {
lastError="premature end of file";
logE("premature end of file!\n");
delete ins;
delete[] buf;
return false;
}
2022-01-31 17:55:51 +00:00
if (version>11) {
lastError="unknown instrument version!";
delete ins;
delete[] buf;
return false;
}
ins->name=stripPath;
if (version>=11) { // 1.0
try {
sys=reader.readC();
switch (sys) {
case 1: // YMU759
ins->type=DIV_INS_FM;
break;
case 2: // Genesis
ins->type=DIV_INS_FM;
break;
case 3: // SMS
ins->type=DIV_INS_STD;
break;
case 4: // Game Boy
ins->type=DIV_INS_GB;
break;
case 5: // PC Engine
ins->type=DIV_INS_PCE;
break;
case 6: // NES
ins->type=DIV_INS_STD;
break;
case 7: case 0x17: // C64
ins->type=DIV_INS_C64;
break;
case 8: // Arcade
ins->type=DIV_INS_FM;
break;
default:
lastError="unknown instrument type!";
delete ins;
delete[] buf;
return false;
break;
}
} catch (EndOfFileException& e) {
lastError="premature end of file";
logE("premature end of file!\n");
2022-01-31 17:55:51 +00:00
delete ins;
delete[] buf;
return false;
}
}
try {
bool mode=true;
2022-02-01 06:21:51 +00:00
if (version>1) {
mode=reader.readC();
if (mode==0) {
if (version<11) {
ins->type=DIV_INS_STD;
}
} else {
ins->type=DIV_INS_FM;
}
} else {
ins->type=DIV_INS_FM;
}
if (mode) { // FM
if (version<10) {
if (version>1) {
ins->fm.ops=reader.readC()?4:2;
} else {
ins->fm.ops=reader.readC()?2:4;
}
}
if (version>1) { // HELP! in which version of the format did we start storing FMS!
ins->fm.fms=reader.readC();
}
ins->fm.fb=reader.readC();
ins->fm.alg=reader.readC();
// DITTO
if (sys!=1) ins->fm.ams=reader.readC();
for (int j=0; j<ins->fm.ops; j++) {
ins->fm.op[j].mult=reader.readC();
ins->fm.op[j].tl=reader.readC();
ins->fm.op[j].ar=reader.readC();
ins->fm.op[j].dr=reader.readC();
ins->fm.op[j].sl=reader.readC();
ins->fm.op[j].rr=reader.readC();
ins->fm.op[j].am=reader.readC();
// what the hell how do I tell!
if (sys==1) { // YMU759
ins->fm.op[j].ws=reader.readC();
ins->fm.op[j].ksl=reader.readC();
ins->fm.op[j].vib=reader.readC();
ins->fm.op[j].egt=reader.readC();
ins->fm.op[j].sus=reader.readC();
ins->fm.op[j].ksr=reader.readC();
ins->fm.op[j].dvb=reader.readC();
ins->fm.op[j].dam=reader.readC();
} else {
ins->fm.op[j].rs=reader.readC();
ins->fm.op[j].dt=reader.readC();
ins->fm.op[j].dt2=ins->fm.op[j].dt>>4;
ins->fm.op[j].dt&=15;
ins->fm.op[j].d2r=reader.readC();
ins->fm.op[j].ssgEnv=reader.readC();
}
}
} else { // STD
if (ins->type!=DIV_INS_GB) {
ins->std.volMacroLen=reader.readC();
if (version>5) {
for (int i=0; i<ins->std.volMacroLen; i++) {
ins->std.volMacro[i]=reader.readI();
}
} else {
for (int i=0; i<ins->std.volMacroLen; i++) {
ins->std.volMacro[i]=reader.readC();
}
}
if (version<11) for (int i=0; i<ins->std.volMacroLen; i++) {
if (ins->std.volMacro[i]>15 && ins->type==DIV_INS_STD) ins->type=DIV_INS_PCE;
}
if (ins->std.volMacroLen>0) {
ins->std.volMacroOpen=true;
ins->std.volMacroLoop=reader.readC();
} else {
ins->std.volMacroOpen=false;
}
}
ins->std.arpMacroLen=reader.readC();
if (version>5) {
for (int i=0; i<ins->std.arpMacroLen; i++) {
ins->std.arpMacro[i]=reader.readI();
}
} else {
for (int i=0; i<ins->std.arpMacroLen; i++) {
ins->std.arpMacro[i]=reader.readC();
}
}
if (ins->std.arpMacroLen>0) {
ins->std.arpMacroOpen=true;
ins->std.arpMacroLoop=reader.readC();
} else {
ins->std.arpMacroOpen=false;
}
if (version>8) { // TODO: when?
ins->std.arpMacroMode=reader.readC();
}
ins->std.dutyMacroLen=reader.readC();
if (version>5) {
for (int i=0; i<ins->std.dutyMacroLen; i++) {
ins->std.dutyMacro[i]=reader.readI();
}
} else {
for (int i=0; i<ins->std.dutyMacroLen; i++) {
ins->std.dutyMacro[i]=reader.readC();
}
}
if (ins->std.dutyMacroLen>0) {
ins->std.dutyMacroOpen=true;
ins->std.dutyMacroLoop=reader.readC();
} else {
ins->std.dutyMacroOpen=false;
}
ins->std.waveMacroLen=reader.readC();
if (version>5) {
for (int i=0; i<ins->std.waveMacroLen; i++) {
ins->std.waveMacro[i]=reader.readI();
}
} else {
for (int i=0; i<ins->std.waveMacroLen; i++) {
ins->std.waveMacro[i]=reader.readC();
}
}
if (ins->std.waveMacroLen>0) {
ins->std.waveMacroOpen=true;
ins->std.waveMacroLoop=reader.readC();
} else {
ins->std.waveMacroOpen=false;
}
if (ins->type==DIV_INS_C64) {
ins->c64.triOn=reader.readC();
ins->c64.sawOn=reader.readC();
ins->c64.pulseOn=reader.readC();
ins->c64.noiseOn=reader.readC();
ins->c64.a=reader.readC();
ins->c64.d=reader.readC();
ins->c64.s=reader.readC();
ins->c64.r=reader.readC();
ins->c64.duty=(reader.readC()*4095)/100;
ins->c64.ringMod=reader.readC();
ins->c64.oscSync=reader.readC();
ins->c64.toFilter=reader.readC();
if (version<0x07) { // TODO: UNSURE
ins->c64.volIsCutoff=reader.readI();
} else {
ins->c64.volIsCutoff=reader.readC();
}
ins->c64.initFilter=reader.readC();
ins->c64.res=reader.readC();
ins->c64.cut=(reader.readC()*2047)/100;
ins->c64.hp=reader.readC();
ins->c64.bp=reader.readC();
ins->c64.lp=reader.readC();
ins->c64.ch3off=reader.readC();
}
if (ins->type==DIV_INS_GB) {
ins->gb.envVol=reader.readC();
ins->gb.envDir=reader.readC();
ins->gb.envLen=reader.readC();
ins->gb.soundLen=reader.readC();
}
}
} catch (EndOfFileException& e) {
lastError="premature end of file";
logE("premature end of file!\n");
delete ins;
delete[] buf;
return false;
}
break;
}
case DIV_INSFORMAT_TFI:
try {
reader.seek(0,SEEK_SET);
ins->type=DIV_INS_FM;
ins->name=stripPath;
ins->fm.alg=reader.readC();
ins->fm.fb=reader.readC();
for (int i=0; i<4; i++) {
DivInstrumentFM::Operator& op=ins->fm.op[i];
op.mult=reader.readC();
op.dt=reader.readC();
op.tl=reader.readC();
op.rs=reader.readC();
op.ar=reader.readC();
op.dr=reader.readC();
op.d2r=reader.readC();
op.rr=reader.readC();
op.sl=reader.readC();
op.ssgEnv=reader.readC();
}
} catch (EndOfFileException& e) {
lastError="premature end of file";
logE("premature end of file!\n");
delete ins;
delete[] buf;
return false;
}
break;
case DIV_INSFORMAT_VGI:
try {
reader.seek(0,SEEK_SET);
ins->type=DIV_INS_FM;
ins->name=stripPath;
ins->fm.alg=reader.readC();
ins->fm.fb=reader.readC();
unsigned char fmsams=reader.readC();
ins->fm.fms=fmsams&7;
ins->fm.ams=fmsams>>4;
for (int i=0; i<4; i++) {
DivInstrumentFM::Operator& op=ins->fm.op[i];
op.mult=reader.readC();
op.dt=reader.readC();
op.tl=reader.readC();
op.rs=reader.readC();
op.ar=reader.readC();
op.dr=reader.readC();
if (op.dr&0x80) {
op.am=1;
op.dr&=0x7f;
}
op.d2r=reader.readC();
op.rr=reader.readC();
op.sl=reader.readC();
op.ssgEnv=reader.readC();
}
} catch (EndOfFileException& e) {
lastError="premature end of file";
logE("premature end of file!\n");
delete ins;
delete[] buf;
return false;
}
break;
case DIV_INSFORMAT_FTI:
break;
case DIV_INSFORMAT_BTI:
break;
case DIV_INSFORMAT_S3I:
try {
reader.seek(0, SEEK_SET);
uint8_t s3i_type = reader.readC();
if (s3i_type >= 2) {
ins->type = DIV_INS_OPL;
// skip internal filename - we'll use the long name description
reader.seek(12, SEEK_CUR);
// skip reserved bytes
reader.seek(3, SEEK_CUR);
// 12-byte opl value
uint8_t s3i_Mcharacteristics = reader.readC();
uint8_t s3i_Ccharacteristics = reader.readC();
uint8_t s3i_Mscaling_output = reader.readC();
uint8_t s3i_Cscaling_output = reader.readC();
uint8_t s3i_Meg_AD = reader.readC();
uint8_t s3i_Ceg_AD = reader.readC();
uint8_t s3i_Meg_SR = reader.readC();
uint8_t s3i_Ceg_SR = reader.readC();
uint8_t s3i_Mwave = reader.readC();
uint8_t s3i_Cwave = reader.readC();
uint8_t s3i_FeedConnect = reader.readC();
DivInstrumentFM::Operator& opM = ins->fm.op[0];
DivInstrumentFM::Operator& opC = ins->fm.op[1];
ins->fm.ops = 2;
opM.mult = s3i_Mcharacteristics & 0xF;
opM.ksr = ((s3i_Mcharacteristics >> 4) & 0x1);
opM.sus = ((s3i_Mcharacteristics >> 5) & 0x1);
opM.vib = ((s3i_Mcharacteristics >> 6) & 0x1);
opM.am = ((s3i_Mcharacteristics >> 7) & 0x1);
opM.tl = s3i_Mscaling_output & 0x3F;
opM.ksl = ((s3i_Mscaling_output >> 6) & 0x3);
opM.ar = ((s3i_Meg_AD >> 4) & 0xF);
opM.dr = (s3i_Meg_AD & 0xF);
opM.rr = (s3i_Meg_SR & 0xF);
opM.sl = ((s3i_Meg_SR >> 4) & 0xF);
opM.ws = s3i_Mwave;
ins->fm.alg = (s3i_FeedConnect & 0x1);
ins->fm.fb = ((s3i_FeedConnect >> 1) & 0x7);
opC.mult = s3i_Ccharacteristics & 0xF;
opC.ksr = ((s3i_Ccharacteristics >> 4) & 0x1);
opC.sus = ((s3i_Ccharacteristics >> 5) & 0x1);
opC.vib = ((s3i_Ccharacteristics >> 6) & 0x1);
opC.am = ((s3i_Ccharacteristics >> 7) & 0x1);
opC.tl = s3i_Cscaling_output & 0x3F;
opC.ksl = ((s3i_Cscaling_output >> 6) & 0x3);
opC.ar = ((s3i_Ceg_AD >> 4) & 0xF);
opC.dr = (s3i_Ceg_AD & 0xF);
opC.rr = (s3i_Ceg_SR & 0xF);
opC.sl = ((s3i_Ceg_SR >> 4) & 0xF);
opC.ws = s3i_Cwave;
// Skip more stuff we don't need
reader.seek(21, SEEK_CUR);
} else {
logE("S3I PCM samples currently not supported.");
}
ins->name = reader.readString(28);
int s3i_signature = reader.readI();
if (s3i_signature != 0x49524353) {
logW("S3I signature invalid.");
};
}
catch (EndOfFileException& e) {
lastError = "premature end of file";
logE("premature end of file!\n");
delete ins;
delete[] buf;
return false;
}
break;
case DIV_INSFORMAT_SBI:
try {
reader.seek(0, SEEK_SET);
ins->type = DIV_INS_OPL;
int sbi_header = reader.readI();
// SBI header determines format
bool is_2op = (sbi_header == 0x1A494253); // SBI\x1A
bool is_4op = (sbi_header == 0x1A504F34); // 4OP\x1A
bool is_6op = (sbi_header == 0x1A504F36); // 6OP\x1A - Freq Monster 801-specific
// 32-byte null terminated instrument name
ins->name = reader.readString(32);
// 2op SBI
uint8_t sbi_Mcharacteristics = reader.readC();
uint8_t sbi_Ccharacteristics = reader.readC();
uint8_t sbi_Mscaling_output = reader.readC();
uint8_t sbi_Cscaling_output = reader.readC();
uint8_t sbi_Meg_AD = reader.readC();
uint8_t sbi_Ceg_AD = reader.readC();
uint8_t sbi_Meg_SR = reader.readC();
uint8_t sbi_Ceg_SR = reader.readC();
uint8_t sbi_Mwave = reader.readC();
uint8_t sbi_Cwave = reader.readC();
uint8_t sbi_FeedConnect = reader.readC();
// 4op SBI
uint8_t sbi_M4characteristics;
uint8_t sbi_C4characteristics;
uint8_t sbi_M4scaling_output;
uint8_t sbi_C4scaling_output;
uint8_t sbi_M4eg_AD;
uint8_t sbi_C4eg_AD;
uint8_t sbi_M4eg_SR;
uint8_t sbi_C4eg_SR;
uint8_t sbi_M4wave;
uint8_t sbi_C4wave;
uint8_t sbi_4opConnect;
if (is_2op) {
DivInstrumentFM::Operator& opM = ins->fm.op[0];
DivInstrumentFM::Operator& opC = ins->fm.op[1];
ins->fm.ops = 2;
opM.mult = sbi_Mcharacteristics & 0xF;
opM.ksr = ((sbi_Mcharacteristics >> 4) & 0x1);
opM.sus = ((sbi_Mcharacteristics >> 5) & 0x1);
opM.vib = ((sbi_Mcharacteristics >> 6) & 0x1);
opM.am = ((sbi_Mcharacteristics >> 7) & 0x1);
opM.tl = sbi_Mscaling_output & 0x3F;
opM.ksl = ((sbi_Mscaling_output >> 6) & 0x3);
opM.ar = ((sbi_Meg_AD >> 4) & 0xF);
opM.dr = (sbi_Meg_AD & 0xF);
opM.rr = (sbi_Meg_SR & 0xF);
opM.sl = ((sbi_Meg_SR >> 4) & 0xF);
opM.ws = sbi_Mwave;
ins->fm.alg = (sbi_FeedConnect & 0x1);
ins->fm.fb = ((sbi_FeedConnect >> 1) & 0x7);
opC.mult = sbi_Ccharacteristics & 0xF;
opC.ksr = ((sbi_Ccharacteristics >> 4) & 0x1);
opC.sus = ((sbi_Ccharacteristics >> 5) & 0x1);
opC.vib = ((sbi_Ccharacteristics >> 6) & 0x1);
opC.am = ((sbi_Ccharacteristics >> 7) & 0x1);
opC.tl = sbi_Cscaling_output & 0x3F;
opC.ksl = ((sbi_Cscaling_output >> 6) & 0x3);
opC.ar = ((sbi_Ceg_AD >> 4) & 0xF);
opC.dr = (sbi_Ceg_AD & 0xF);
opC.rr = (sbi_Ceg_SR & 0xF);
opC.sl = ((sbi_Ceg_SR >> 4) & 0xF);
opC.ws = sbi_Cwave;
// Ignore rest of file - rest is 'reserved padding'.
2022-03-19 18:50:22 +00:00
reader.seek(0, SEEK_END);
}
if (is_4op || is_6op) {
// Operator placement is different so need to place in correct registers.
// Note: 6op is an unofficial extension of 4op SBIs by Darron Broad (Freq Monster 801).
// We'll only use the 4op portion here for pure OPL3.
DivInstrumentFM::Operator& opM = ins->fm.op[0];
DivInstrumentFM::Operator& opC = ins->fm.op[2];
DivInstrumentFM::Operator& opM4 = ins->fm.op[1];
DivInstrumentFM::Operator& opC4 = ins->fm.op[3];
ins->fm.ops = 4;
sbi_M4characteristics = reader.readC();
sbi_C4characteristics = reader.readC();
sbi_M4scaling_output = reader.readC();
sbi_C4scaling_output = reader.readC();
sbi_M4eg_AD = reader.readC();
sbi_C4eg_AD = reader.readC();
sbi_M4eg_SR = reader.readC();
sbi_C4eg_SR = reader.readC();
sbi_M4wave = reader.readC();
sbi_C4wave = reader.readC();
sbi_4opConnect = reader.readC();
ins->fm.alg = (sbi_FeedConnect & 0x1) | ((sbi_4opConnect & 0x1) << 1);
2022-03-19 14:04:38 +00:00
ins->fm.fb = ((sbi_FeedConnect >> 1) & 0x7);
opM.mult = sbi_Mcharacteristics & 0xF;
opM.ksr = ((sbi_Mcharacteristics >> 4) & 0x1);
opM.sus = ((sbi_Mcharacteristics >> 5) & 0x1);
opM.vib = ((sbi_Mcharacteristics >> 6) & 0x1);
opM.am = ((sbi_Mcharacteristics >> 7) & 0x1);
opM.tl = sbi_Mscaling_output & 0x3F;
opM.ksl = ((sbi_Mscaling_output >> 6) & 0x3);
opM.ar = ((sbi_Meg_AD >> 4) & 0xF);
opM.dr = (sbi_Meg_AD & 0xF);
opM.rr = (sbi_Meg_SR & 0xF);
opM.sl = ((sbi_Meg_SR >> 4) & 0xF);
opM.ws = sbi_Mwave;
opC.mult = sbi_Ccharacteristics & 0xF;
opC.ksr = ((sbi_Ccharacteristics >> 4) & 0x1);
opC.sus = ((sbi_Ccharacteristics >> 5) & 0x1);
opC.vib = ((sbi_Ccharacteristics >> 6) & 0x1);
opC.am = ((sbi_Ccharacteristics >> 7) & 0x1);
opC.tl = sbi_Cscaling_output & 0x3F;
opC.ksl = ((sbi_Cscaling_output >> 6) & 0x3);
opC.ar = ((sbi_Ceg_AD >> 4) & 0xF);
opC.dr = (sbi_Ceg_AD & 0xF);
opC.rr = (sbi_Ceg_SR & 0xF);
opC.sl = ((sbi_Ceg_SR >> 4) & 0xF);
opC.ws = sbi_Cwave;
opM4.mult = sbi_M4characteristics & 0xF;
opM4.ksr = ((sbi_M4characteristics >> 4) & 0x1);
opM4.sus = ((sbi_M4characteristics >> 5) & 0x1);
opM4.vib = ((sbi_M4characteristics >> 6) & 0x1);
opM4.am = ((sbi_M4characteristics >> 7) & 0x1);
opM4.tl = sbi_M4scaling_output & 0x3F;
opM4.ksl = ((sbi_M4scaling_output >> 6) & 0x3);
opM4.ar = ((sbi_M4eg_AD >> 4) & 0xF);
opM4.dr = (sbi_M4eg_AD & 0xF);
opM4.rr = (sbi_M4eg_SR & 0xF);
opM4.sl = ((sbi_M4eg_SR >> 4) & 0xF);
opM4.ws = sbi_M4wave;
opC4.mult = sbi_C4characteristics & 0xF;
opC4.ksr = ((sbi_C4characteristics >> 4) & 0x1);
opC4.sus = ((sbi_C4characteristics >> 5) & 0x1);
opC4.vib = ((sbi_C4characteristics >> 6) & 0x1);
opC4.am = ((sbi_C4characteristics >> 7) & 0x1);
opC4.tl = sbi_C4scaling_output & 0x3F;
opC4.ksl = ((sbi_C4scaling_output >> 6) & 0x3);
opC4.ar = ((sbi_C4eg_AD >> 4) & 0xF);
opC4.dr = (sbi_C4eg_AD & 0xF);
opC4.rr = (sbi_C4eg_SR & 0xF);
opC4.sl = ((sbi_C4eg_SR >> 4) & 0xF);
opC4.ws = sbi_C4wave;
// Ignore rest of file once we've read in all we need.
// Note: Freq Monster 801 adds a ton of other additional fields irrelevant to chip registers.
reader.seek(0, SEEK_END);
}
} catch (EndOfFileException& e) {
lastError = "premature end of file";
logE("premature end of file!\n");
delete ins;
delete[] buf;
return false;
}
break;
}
2022-02-01 06:21:51 +00:00
if (reader.tell()<reader.size()) {
addWarning("https://github.com/tildearrow/furnace/issues/84");
addWarning("there is more data at the end of the file! what happened here!");
addWarning(fmt::sprintf("exactly %d bytes, if you are curious",reader.size()-reader.tell()));
}
}
BUSY_BEGIN;
saveLock.lock();
int insCount=(int)song.ins.size();
song.ins.push_back(ins);
song.insLen=insCount+1;
saveLock.unlock();
BUSY_END;
return true;
}
void DivEngine::delInstrument(int index) {
BUSY_BEGIN;
saveLock.lock();
if (index>=0 && index<(int)song.ins.size()) {
2022-01-13 22:40:29 +00:00
for (int i=0; i<song.systemLen; i++) {
disCont[i].dispatch->notifyInsDeletion(song.ins[index]);
}
delete song.ins[index];
song.ins.erase(song.ins.begin()+index);
song.insLen=song.ins.size();
for (int i=0; i<chans; i++) {
for (int j=0; j<128; j++) {
if (song.pat[i].data[j]==NULL) continue;
for (int k=0; k<song.patLen; k++) {
if (song.pat[i].data[j]->data[k][2]>index) {
song.pat[i].data[j]->data[k][2]--;
}
}
}
}
}
saveLock.unlock();
BUSY_END;
}
int DivEngine::addWave() {
BUSY_BEGIN;
saveLock.lock();
DivWavetable* wave=new DivWavetable;
int waveCount=(int)song.wave.size();
song.wave.push_back(wave);
song.waveLen=waveCount+1;
saveLock.unlock();
BUSY_END;
return waveCount;
}
2022-01-11 21:25:55 +00:00
bool DivEngine::addWaveFromFile(const char* path) {
2022-01-21 22:59:48 +00:00
FILE* f=ps_fopen(path,"rb");
if (f==NULL) {
return false;
}
unsigned char* buf;
ssize_t len;
if (fseek(f,0,SEEK_END)!=0) {
fclose(f);
return false;
}
len=ftell(f);
if (len<0) {
fclose(f);
return false;
}
if (len==0) {
fclose(f);
return false;
}
if (fseek(f,0,SEEK_SET)!=0) {
fclose(f);
return false;
}
buf=new unsigned char[len];
if (fread(buf,1,len,f)!=(size_t)len) {
logW("did not read entire wavetable file buffer!\n");
delete[] buf;
return false;
}
fclose(f);
SafeReader reader=SafeReader(buf,len);
unsigned char magic[16];
bool isFurnaceTable=false;
try {
reader.read(magic,16);
if (memcmp("-Furnace waveta-",magic,16)==0) {
isFurnaceTable=true;
}
} catch (EndOfFileException& e) {
2022-01-21 22:59:48 +00:00
reader.seek(0,SEEK_SET);
}
DivWavetable* wave=new DivWavetable;
try {
if (isFurnaceTable) {
reader.seek(16,SEEK_SET);
short version=reader.readS();
reader.readS(); // reserved
reader.seek(20,SEEK_SET);
if (wave->readWaveData(reader,version)!=DIV_DATA_SUCCESS) {
lastError="invalid wavetable header/data!";
delete wave;
2022-01-21 22:59:48 +00:00
delete[] buf;
return false;
}
} else {
try {
// read as .dmw
reader.seek(0,SEEK_SET);
int len=reader.readI();
wave->max=(unsigned char)reader.readC();
2022-02-21 03:16:43 +00:00
if (wave->max==255) { // new wavetable format
unsigned char waveVersion=reader.readC();
logI("reading modern .dmw...\n");
logD("wave version %d\n",waveVersion);
wave->max=reader.readC();
for (int i=0; i<len; i++) {
wave->data[i]=reader.readI();
}
} else if (reader.size()==(size_t)(len+5)) {
2022-01-21 22:59:48 +00:00
// read as .dmw
logI("reading .dmw...\n");
if (len>256) len=256;
for (int i=0; i<len; i++) {
wave->data[i]=(unsigned char)reader.readC();
}
} else {
// read as binary
logI("reading binary...\n");
len=reader.size();
if (len>256) len=256;
reader.seek(0,SEEK_SET);
for (int i=0; i<len; i++) {
wave->data[i]=(unsigned char)reader.readC();
if (wave->max<wave->data[i]) wave->max=wave->data[i];
}
wave->len=len;
}
} catch (EndOfFileException& e) {
2022-01-21 22:59:48 +00:00
// read as binary
len=reader.size();
logI("reading binary for being too small...\n");
if (len>256) len=256;
reader.seek(0,SEEK_SET);
for (int i=0; i<len; i++) {
wave->data[i]=(unsigned char)reader.readC();
if (wave->max<wave->data[i]) wave->max=wave->data[i];
}
wave->len=len;
}
}
} catch (EndOfFileException& e) {
2022-01-21 22:59:48 +00:00
delete wave;
delete[] buf;
return false;
}
BUSY_BEGIN;
saveLock.lock();
2022-01-21 22:59:48 +00:00
int waveCount=(int)song.wave.size();
song.wave.push_back(wave);
song.waveLen=waveCount+1;
saveLock.unlock();
BUSY_END;
2022-01-11 21:25:55 +00:00
return true;
}
void DivEngine::delWave(int index) {
BUSY_BEGIN;
saveLock.lock();
if (index>=0 && index<(int)song.wave.size()) {
delete song.wave[index];
song.wave.erase(song.wave.begin()+index);
song.waveLen=song.wave.size();
}
saveLock.unlock();
BUSY_END;
}
int DivEngine::addSample() {
BUSY_BEGIN;
saveLock.lock();
DivSample* sample=new DivSample;
int sampleCount=(int)song.sample.size();
sample->name=fmt::sprintf("Sample %d",sampleCount);
song.sample.push_back(sample);
song.sampleLen=sampleCount+1;
saveLock.unlock();
renderSamples();
BUSY_END;
return sampleCount;
}
2022-03-23 21:39:08 +00:00
int DivEngine::addSampleFromFile(const char* path) {
BUSY_BEGIN;
SF_INFO si;
SNDFILE* f=sf_open(path,SFM_READ,&si);
if (f==NULL) {
BUSY_END;
2022-03-23 22:00:40 +00:00
int err=sf_error(NULL);
if (err==SF_ERR_SYSTEM) {
lastError=fmt::sprintf("could not open file! (%s %s)",sf_error_number(err),strerror(errno));
} else {
lastError=fmt::sprintf("could not open file! (%s)",sf_error_number(err));
}
2022-03-23 21:39:08 +00:00
return -1;
}
2022-03-23 21:39:08 +00:00
if (si.frames>16777215) {
lastError="this sample is too big! max sample size is 16777215.";
sf_close(f);
BUSY_END;
2022-03-23 21:39:08 +00:00
return -1;
}
short* buf=new short[si.channels*si.frames];
if (sf_readf_short(f,buf,si.frames)!=si.frames) {
logW("sample read size mismatch!\n");
}
DivSample* sample=new DivSample;
int sampleCount=(int)song.sample.size();
const char* sName=strrchr(path,DIR_SEPARATOR);
if (sName==NULL) {
sName=path;
} else {
sName++;
}
sample->name=sName;
int index=0;
if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) {
sample->depth=8;
} else {
sample->depth=16;
}
sample->init(si.frames);
for (int i=0; i<si.frames*si.channels; i+=si.channels) {
int averaged=0;
for (int j=0; j<si.channels; j++) {
averaged+=buf[i+j];
}
averaged/=si.channels;
if (((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8)) {
sample->data8[index++]=averaged>>8;
} else {
sample->data16[index++]=averaged;
}
}
delete[] buf;
sample->rate=si.samplerate;
if (sample->rate<4000) sample->rate=4000;
2022-02-22 09:01:57 +00:00
if (sample->rate>96000) sample->rate=96000;
sample->centerRate=si.samplerate;
SF_INSTRUMENT inst;
if (sf_command(f, SFC_GET_INSTRUMENT, &inst, sizeof(inst)) == SF_TRUE)
{
// There's no documentation on libsndfile detune range, but the code
// implies -50..50. Yet when loading a file you can get a >50 value.
if(inst.detune > 50)
inst.detune = inst.detune - 100;
short pitch = ((0x3c-inst.basenote)*100) + inst.detune;
sample->centerRate=si.samplerate*pow(2.0,pitch/(12.0 * 100.0));
if(inst.loop_count && inst.loops[0].mode == SF_LOOP_FORWARD)
{
sample->loopStart=inst.loops[0].start;
if(inst.loops[0].end < (unsigned int)sampleCount)
sampleCount=inst.loops[0].end;
}
}
if (sample->centerRate<4000) sample->centerRate=4000;
if (sample->centerRate>64000) sample->centerRate=64000;
sf_close(f);
saveLock.lock();
song.sample.push_back(sample);
song.sampleLen=sampleCount+1;
saveLock.unlock();
renderSamples();
BUSY_END;
return sampleCount;
}
void DivEngine::delSample(int index) {
BUSY_BEGIN;
saveLock.lock();
if (index>=0 && index<(int)song.sample.size()) {
delete song.sample[index];
song.sample.erase(song.sample.begin()+index);
song.sampleLen=song.sample.size();
renderSamples();
}
saveLock.unlock();
BUSY_END;
}
2021-12-22 22:39:16 +00:00
void DivEngine::addOrder(bool duplicate, bool where) {
2022-01-10 03:17:03 +00:00
unsigned char order[DIV_MAX_CHANS];
2021-12-22 22:39:16 +00:00
if (song.ordersLen>=0x7e) return;
BUSY_BEGIN_SOFT;
2021-12-22 22:39:16 +00:00
if (duplicate) {
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-22 22:39:16 +00:00
order[i]=song.orders.ord[i][curOrder];
}
} else {
bool used[256];
for (int i=0; i<chans; i++) {
memset(used,0,sizeof(bool)*256);
for (int j=0; j<song.ordersLen; j++) {
used[song.orders.ord[i][j]]=true;
}
order[i]=0x7e;
for (int j=0; j<256; j++) {
if (!used[j]) {
order[i]=j;
break;
}
}
}
}
if (where) { // at the end
saveLock.lock();
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-22 22:39:16 +00:00
song.orders.ord[i][song.ordersLen]=order[i];
}
song.ordersLen++;
saveLock.unlock();
2021-12-22 22:39:16 +00:00
} else { // after current order
saveLock.lock();
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-22 22:39:16 +00:00
for (int j=song.ordersLen; j>curOrder; j--) {
song.orders.ord[i][j]=song.orders.ord[i][j-1];
}
song.orders.ord[i][curOrder+1]=order[i];
}
song.ordersLen++;
saveLock.unlock();
2021-12-22 22:39:16 +00:00
curOrder++;
2021-12-28 23:23:57 +00:00
if (playing && !freelance) {
2021-12-22 22:39:16 +00:00
playSub(false);
}
}
BUSY_END;
2021-12-22 22:39:16 +00:00
}
2022-02-12 08:59:05 +00:00
void DivEngine::deepCloneOrder(bool where) {
unsigned char order[DIV_MAX_CHANS];
if (song.ordersLen>=0x7e) return;
2022-02-12 23:02:33 +00:00
warnings="";
BUSY_BEGIN_SOFT;
2022-02-12 08:59:05 +00:00
for (int i=0; i<chans; i++) {
2022-02-21 04:07:46 +00:00
bool didNotFind=true;
logD("channel %d\n",i);
2022-02-12 08:59:05 +00:00
order[i]=song.orders.ord[i][curOrder];
// find free slot
for (int j=0; j<128; j++) {
2022-02-21 04:07:46 +00:00
logD("finding free slot in %d...\n",j);
2022-02-12 08:59:05 +00:00
if (song.pat[i].data[j]==NULL) {
int origOrd=order[i];
order[i]=j;
DivPattern* oldPat=song.pat[i].getPattern(origOrd,false);
DivPattern* pat=song.pat[i].getPattern(j,true);
memcpy(pat->data,oldPat->data,256*32*sizeof(short));
2022-02-21 04:07:46 +00:00
logD("found at %d\n",j);
didNotFind=false;
2022-02-12 08:59:05 +00:00
break;
}
}
2022-02-21 04:07:46 +00:00
if (didNotFind) {
2022-02-12 23:02:33 +00:00
addWarning(fmt::sprintf("no free patterns in channel %d!",i));
}
2022-02-12 08:59:05 +00:00
}
if (where) { // at the end
saveLock.lock();
2022-02-12 08:59:05 +00:00
for (int i=0; i<chans; i++) {
song.orders.ord[i][song.ordersLen]=order[i];
}
song.ordersLen++;
saveLock.unlock();
2022-02-12 08:59:05 +00:00
} else { // after current order
saveLock.lock();
2022-02-12 08:59:05 +00:00
for (int i=0; i<chans; i++) {
for (int j=song.ordersLen; j>curOrder; j--) {
song.orders.ord[i][j]=song.orders.ord[i][j-1];
}
song.orders.ord[i][curOrder+1]=order[i];
}
song.ordersLen++;
saveLock.unlock();
2022-02-12 08:59:05 +00:00
curOrder++;
if (playing && !freelance) {
playSub(false);
}
}
BUSY_END;
2022-02-12 08:59:05 +00:00
}
2021-12-22 22:39:16 +00:00
void DivEngine::deleteOrder() {
if (song.ordersLen<=1) return;
BUSY_BEGIN_SOFT;
saveLock.lock();
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-22 22:39:16 +00:00
for (int j=curOrder; j<song.ordersLen; j++) {
song.orders.ord[i][j]=song.orders.ord[i][j+1];
}
}
song.ordersLen--;
saveLock.unlock();
2021-12-22 22:39:16 +00:00
if (curOrder>=song.ordersLen) curOrder=song.ordersLen-1;
2021-12-28 23:23:57 +00:00
if (playing && !freelance) {
2021-12-22 22:39:16 +00:00
playSub(false);
}
BUSY_END;
2021-12-22 22:39:16 +00:00
}
void DivEngine::moveOrderUp() {
BUSY_BEGIN_SOFT;
2021-12-22 22:39:16 +00:00
if (curOrder<1) {
BUSY_END;
2021-12-22 22:39:16 +00:00
return;
}
saveLock.lock();
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-22 22:39:16 +00:00
song.orders.ord[i][curOrder]^=song.orders.ord[i][curOrder-1];
song.orders.ord[i][curOrder-1]^=song.orders.ord[i][curOrder];
song.orders.ord[i][curOrder]^=song.orders.ord[i][curOrder-1];
}
saveLock.unlock();
2021-12-22 22:39:16 +00:00
curOrder--;
2021-12-28 23:23:57 +00:00
if (playing && !freelance) {
2021-12-22 22:39:16 +00:00
playSub(false);
}
BUSY_END;
2021-12-22 22:39:16 +00:00
}
void DivEngine::moveOrderDown() {
BUSY_BEGIN_SOFT;
2021-12-22 22:39:16 +00:00
if (curOrder>=song.ordersLen-1) {
BUSY_END;
2021-12-22 22:39:16 +00:00
return;
}
saveLock.lock();
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-22 22:39:16 +00:00
song.orders.ord[i][curOrder]^=song.orders.ord[i][curOrder+1];
song.orders.ord[i][curOrder+1]^=song.orders.ord[i][curOrder];
song.orders.ord[i][curOrder]^=song.orders.ord[i][curOrder+1];
}
saveLock.unlock();
2021-12-22 22:39:16 +00:00
curOrder++;
2021-12-28 23:23:57 +00:00
if (playing && !freelance) {
2021-12-22 22:39:16 +00:00
playSub(false);
}
BUSY_END;
2021-12-22 22:39:16 +00:00
}
void DivEngine::exchangeIns(int one, int two) {
for (int i=0; i<chans; i++) {
for (int j=0; j<128; j++) {
if (song.pat[i].data[j]==NULL) continue;
for (int k=0; k<song.patLen; k++) {
if (song.pat[i].data[j]->data[k][2]==one) {
song.pat[i].data[j]->data[k][2]=two;
} else if (song.pat[i].data[j]->data[k][2]==two) {
song.pat[i].data[j]->data[k][2]=one;
}
}
}
}
}
bool DivEngine::moveInsUp(int which) {
if (which<1 || which>=(int)song.ins.size()) return false;
BUSY_BEGIN;
DivInstrument* prev=song.ins[which];
saveLock.lock();
song.ins[which]=song.ins[which-1];
song.ins[which-1]=prev;
exchangeIns(which,which-1);
saveLock.unlock();
BUSY_END;
return true;
}
bool DivEngine::moveWaveUp(int which) {
if (which<1 || which>=(int)song.wave.size()) return false;
BUSY_BEGIN;
DivWavetable* prev=song.wave[which];
saveLock.lock();
song.wave[which]=song.wave[which-1];
song.wave[which-1]=prev;
saveLock.unlock();
BUSY_END;
return true;
}
bool DivEngine::moveSampleUp(int which) {
if (which<1 || which>=(int)song.sample.size()) return false;
BUSY_BEGIN;
DivSample* prev=song.sample[which];
saveLock.lock();
song.sample[which]=song.sample[which-1];
song.sample[which-1]=prev;
saveLock.unlock();
BUSY_END;
return true;
}
bool DivEngine::moveInsDown(int which) {
if (which<0 || which>=((int)song.ins.size())-1) return false;
BUSY_BEGIN;
DivInstrument* prev=song.ins[which];
saveLock.lock();
song.ins[which]=song.ins[which+1];
song.ins[which+1]=prev;
exchangeIns(which,which+1);
saveLock.unlock();
BUSY_END;
return true;
}
bool DivEngine::moveWaveDown(int which) {
if (which<0 || which>=((int)song.wave.size())-1) return false;
BUSY_BEGIN;
DivWavetable* prev=song.wave[which];
saveLock.lock();
song.wave[which]=song.wave[which+1];
song.wave[which+1]=prev;
saveLock.unlock();
BUSY_END;
return true;
}
bool DivEngine::moveSampleDown(int which) {
if (which<0 || which>=((int)song.sample.size())-1) return false;
BUSY_BEGIN;
DivSample* prev=song.sample[which];
saveLock.lock();
song.sample[which]=song.sample[which+1];
song.sample[which+1]=prev;
saveLock.unlock();
BUSY_END;
return true;
}
2021-12-28 23:23:57 +00:00
void DivEngine::noteOn(int chan, int ins, int note, int vol) {
2022-01-24 22:13:47 +00:00
if (chan<0 || chan>=chans) return;
BUSY_BEGIN;
2021-12-28 23:23:57 +00:00
pendingNotes.push(DivNoteEvent(chan,ins,note,vol,true));
if (!playing) {
reset();
freelance=true;
playing=true;
}
BUSY_END;
2021-12-28 23:23:57 +00:00
}
void DivEngine::noteOff(int chan) {
2022-01-24 22:13:47 +00:00
if (chan<0 || chan>=chans) return;
BUSY_BEGIN;
2021-12-28 23:23:57 +00:00
pendingNotes.push(DivNoteEvent(chan,-1,-1,-1,false));
if (!playing) {
reset();
freelance=true;
playing=true;
}
BUSY_END;
2021-12-28 23:23:57 +00:00
}
2021-12-11 08:34:43 +00:00
void DivEngine::setOrder(unsigned char order) {
BUSY_BEGIN_SOFT;
2021-12-11 08:34:43 +00:00
curOrder=order;
if (order>=song.ordersLen) curOrder=0;
2021-12-28 23:23:57 +00:00
if (playing && !freelance) {
2021-12-21 07:02:25 +00:00
playSub(false);
2021-12-11 08:34:43 +00:00
}
BUSY_END;
}
void DivEngine::setSysFlags(int system, unsigned int flags, bool restart) {
BUSY_BEGIN_SOFT;
saveLock.lock();
2022-01-28 23:12:56 +00:00
song.systemFlags[system]=flags;
saveLock.unlock();
2022-01-28 23:12:56 +00:00
disCont[system].dispatch->setFlags(song.systemFlags[system]);
disCont[system].setRates(got.rate);
if (restart && isPlaying()) {
playSub(false);
}
BUSY_END;
2022-01-28 23:12:56 +00:00
}
2022-03-16 04:30:15 +00:00
void DivEngine::setSongRate(float hz, bool pal) {
BUSY_BEGIN;
saveLock.lock();
2021-12-15 22:32:08 +00:00
song.pal=!pal;
song.hz=hz;
2022-03-16 04:30:15 +00:00
// what?
song.customTempo=true;
divider=60;
if (song.customTempo) {
divider=song.hz;
} else {
if (song.pal) {
divider=60;
} else {
divider=50;
}
2022-01-08 21:03:32 +00:00
}
saveLock.unlock();
BUSY_END;
2021-12-15 22:32:08 +00:00
}
2021-06-09 08:33:03 +00:00
void DivEngine::setAudio(DivAudioEngines which) {
audioEngine=which;
}
void DivEngine::setView(DivStatusView which) {
view=which;
}
2022-01-04 05:02:41 +00:00
bool DivEngine::getMetronome() {
return metronome;
}
void DivEngine::setMetronome(bool enable) {
metronome=enable;
metroAmp=0;
}
void DivEngine::setConsoleMode(bool enable) {
consoleMode=enable;
}
2022-02-06 04:48:56 +00:00
bool DivEngine::switchMaster() {
2022-01-17 06:42:26 +00:00
deinitAudioBackend();
quitDispatch();
initDispatch();
2022-01-17 06:42:26 +00:00
if (initAudioBackend()) {
for (int i=0; i<song.systemLen; i++) {
disCont[i].setRates(got.rate);
disCont[i].setQuality(lowQuality);
}
if (!output->setRun(true)) {
logE("error while activating audio!\n");
2022-02-06 04:48:56 +00:00
return false;
2022-01-17 06:42:26 +00:00
}
2022-02-06 04:48:56 +00:00
} else {
return false;
2022-01-17 06:42:26 +00:00
}
2022-02-06 04:48:56 +00:00
return true;
2022-01-17 06:42:26 +00:00
}
2022-03-19 08:42:44 +00:00
void DivEngine::synchronized(const std::function<void()>& what) {
BUSY_BEGIN;
2022-03-19 08:42:44 +00:00
what();
BUSY_END;
2022-03-19 08:42:44 +00:00
}
void DivEngine::lockSave(const std::function<void()>& what) {
saveLock.lock();
what();
saveLock.unlock();
}
void DivEngine::lockEngine(const std::function<void()>& what) {
BUSY_BEGIN;
saveLock.lock();
what();
saveLock.unlock();
BUSY_END;
}
2022-02-06 02:26:24 +00:00
TAAudioDesc& DivEngine::getAudioDescWant() {
return want;
}
TAAudioDesc& DivEngine::getAudioDescGot() {
return got;
}
2022-02-14 02:42:57 +00:00
std::vector<String>& DivEngine::getAudioDevices() {
return audioDevs;
}
void DivEngine::rescanAudioDevices() {
audioDevs.clear();
if (output!=NULL) {
audioDevs=output->listAudioDevices();
}
}
void DivEngine::initDispatch() {
BUSY_BEGIN;
2022-01-08 21:03:32 +00:00
for (int i=0; i<song.systemLen; i++) {
2022-01-28 23:12:56 +00:00
disCont[i].init(song.system[i],this,getChannelCount(song.system[i]),got.rate,song.systemFlags[i]);
2022-01-08 21:03:32 +00:00
disCont[i].setRates(got.rate);
2022-01-17 06:42:26 +00:00
disCont[i].setQuality(lowQuality);
}
2022-01-08 21:03:32 +00:00
recalcChans();
BUSY_END;
}
void DivEngine::quitDispatch() {
BUSY_BEGIN;
2022-01-08 21:03:32 +00:00
for (int i=0; i<song.systemLen; i++) {
disCont[i].quit();
}
cycles=0;
clockDrift=0;
chans=0;
playing=false;
speedAB=false;
endOfSong=false;
ticks=0;
curRow=0;
curOrder=0;
nextSpeed=3;
changeOrd=-1;
changePos=0;
totalTicks=0;
totalSeconds=0;
totalTicksR=0;
totalCmds=0;
lastCmds=0;
cmdsPerSecond=0;
2022-01-08 06:57:37 +00:00
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-18 08:25:42 +00:00
isMuted[i]=0;
}
BUSY_END;
}
#define CHECK_CONFIG_DIR_MAC() \
configPath+="/Library/Application Support/Furnace"; \
if (stat(configPath.c_str(),&st)<0) { \
logI("creating config dir...\n"); \
if (mkdir(configPath.c_str(),0755)<0) { \
logW("could not make config dir! (%s)\n",strerror(errno)); \
configPath="."; \
} \
}
2021-12-19 08:16:24 +00:00
#define CHECK_CONFIG_DIR() \
configPath+="/.config"; \
if (stat(configPath.c_str(),&st)<0) { \
logI("creating user config dir...\n"); \
if (mkdir(configPath.c_str(),0755)<0) { \
logW("could not make user config dir! (%s)\n",strerror(errno)); \
configPath="."; \
} \
} \
if (configPath!=".") { \
configPath+="/furnace"; \
if (stat(configPath.c_str(),&st)<0) { \
logI("creating config dir...\n"); \
if (mkdir(configPath.c_str(),0755)<0) { \
logW("could not make config dir! (%s)\n",strerror(errno)); \
configPath="."; \
} \
} \
}
bool DivEngine::initAudioBackend() {
2022-01-17 06:42:26 +00:00
// load values
2022-01-23 04:50:49 +00:00
if (audioEngine==DIV_AUDIO_NULL) {
if (getConfString("audioEngine","SDL")=="JACK") {
audioEngine=DIV_AUDIO_JACK;
} else {
audioEngine=DIV_AUDIO_SDL;
}
2022-01-17 06:42:26 +00:00
}
lowQuality=getConfInt("audioQuality",0);
forceMono=getConfInt("forceMono",0);
2022-01-17 06:42:26 +00:00
switch (audioEngine) {
case DIV_AUDIO_JACK:
#ifndef HAVE_JACK
logE("Furnace was not compiled with JACK support!\n");
setConf("audioEngine","SDL");
saveConf();
output=new TAAudioSDL;
#else
output=new TAAudioJACK;
#endif
break;
case DIV_AUDIO_SDL:
output=new TAAudioSDL;
break;
2022-01-23 04:50:49 +00:00
case DIV_AUDIO_DUMMY:
output=new TAAudio;
break;
default:
logE("invalid audio engine!\n");
return false;
}
2022-02-14 02:42:57 +00:00
audioDevs=output->listAudioDevices();
want.deviceName=getConfString("audioDevice","");
want.bufsize=getConfInt("audioBufSize",1024);
want.rate=getConfInt("audioRate",44100);
want.fragments=2;
want.inChans=0;
want.outChans=2;
want.outFormat=TA_AUDIO_FORMAT_F32;
want.name="Furnace";
output->setCallback(process,this);
if (!output->init(want,got)) {
logE("error while initializing audio!\n");
2022-01-17 06:42:26 +00:00
delete output;
output=NULL;
audioEngine=DIV_AUDIO_NULL;
return false;
}
2022-01-27 22:49:00 +00:00
return true;
}
bool DivEngine::deinitAudioBackend() {
if (output!=NULL) {
output->quit();
delete output;
output=NULL;
audioEngine=DIV_AUDIO_NULL;
}
return true;
}
2021-12-19 08:16:24 +00:00
#ifdef _WIN32
#include "winStuff.h"
#endif
bool DivEngine::init() {
2021-12-19 08:16:24 +00:00
// init config
#ifdef _WIN32
configPath=getWinConfigPath();
#else
struct stat st;
char* home=getenv("HOME");
if (home==NULL) {
int uid=getuid();
struct passwd* entry=getpwuid(uid);
if (entry==NULL) {
logW("unable to determine config directory! (%s)\n",strerror(errno));
configPath=".";
} else {
configPath=entry->pw_dir;
#ifdef __APPLE__
CHECK_CONFIG_DIR_MAC();
#else
2021-12-19 08:16:24 +00:00
CHECK_CONFIG_DIR();
#endif
2021-12-19 08:16:24 +00:00
}
} else {
configPath=home;
#ifdef __APPLE__
CHECK_CONFIG_DIR_MAC();
#else
2021-12-19 08:16:24 +00:00
CHECK_CONFIG_DIR();
#endif
2021-12-19 08:16:24 +00:00
}
#endif
logD("config path: %s\n",configPath.c_str());
2021-12-19 21:52:04 +00:00
loadConf();
2021-12-19 08:16:24 +00:00
// init the rest of engine
2022-02-06 04:48:56 +00:00
bool haveAudio=false;
if (!initAudioBackend()) {
logE("no audio output available!\n");
} else {
haveAudio=true;
}
2022-01-08 21:03:32 +00:00
samp_bb=blip_new(32768);
if (samp_bb==NULL) {
logE("not enough memory!\n");
return false;
}
2022-02-06 04:48:56 +00:00
samp_bbOut=new short[32768];
2022-01-08 21:03:32 +00:00
samp_bbIn=new short[32768];
samp_bbInLen=32768;
2022-01-08 21:03:32 +00:00
blip_set_rates(samp_bb,44100,got.rate);
for (int i=0; i<64; i++) {
vibTable[i]=127*sin(((double)i/64.0)*(2*M_PI));
}
2022-01-08 06:57:37 +00:00
for (int i=0; i<DIV_MAX_CHANS; i++) {
2021-12-18 08:25:42 +00:00
isMuted[i]=0;
2022-02-10 08:15:39 +00:00
keyHit[i]=false;
2021-12-18 08:25:42 +00:00
}
2022-01-27 22:49:00 +00:00
oscBuf[0]=new float[32768];
oscBuf[1]=new float[32768];
initDispatch();
2021-12-11 21:51:34 +00:00
reset();
active=true;
2022-02-06 04:48:56 +00:00
if (!haveAudio) {
return false;
2022-02-06 04:48:56 +00:00
} else {
if (!output->setRun(true)) {
logE("error while activating!\n");
return false;
}
}
return true;
}
2021-12-13 22:09:46 +00:00
bool DivEngine::quit() {
deinitAudioBackend();
2021-12-13 22:09:46 +00:00
quitDispatch();
2021-12-19 21:52:04 +00:00
logI("saving config.\n");
saveConf();
active=false;
2022-01-27 22:49:00 +00:00
delete[] oscBuf[0];
delete[] oscBuf[1];
2021-12-13 22:09:46 +00:00
return true;
2021-12-14 17:33:26 +00:00
}