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.
|
|
|
|
*/
|
|
|
|
|
2022-01-21 23:17:05 +00:00
|
|
|
#include "dataErrors.h"
|
2022-01-19 10:10:06 +00:00
|
|
|
#include "engine.h"
|
2022-01-19 09:32:40 +00:00
|
|
|
#include "wavetable.h"
|
2022-01-19 10:10:06 +00:00
|
|
|
#include "../ta-log.h"
|
2022-01-20 10:04:03 +00:00
|
|
|
#include "../fileutils.h"
|
2022-01-19 09:32:40 +00:00
|
|
|
|
2022-01-19 10:10:06 +00:00
|
|
|
void DivWavetable::putWaveData(SafeWriter* w) {
|
|
|
|
w->write("WAVE",4);
|
|
|
|
w->writeI(0);
|
|
|
|
|
|
|
|
w->writeC(0); // name
|
|
|
|
w->writeI(len);
|
|
|
|
w->writeI(min);
|
|
|
|
w->writeI(max);
|
|
|
|
for (int j=0; j<len; j++) {
|
|
|
|
w->writeI(data[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 23:17:05 +00:00
|
|
|
DivDataErrors DivWavetable::readWaveData(SafeReader& reader, short version) {
|
|
|
|
char magic[4];
|
|
|
|
reader.read(magic,4);
|
|
|
|
if (memcmp(magic,"WAVE",4)!=0) {
|
|
|
|
return DIV_DATA_INVALID_HEADER;
|
|
|
|
}
|
|
|
|
reader.readI(); // reserved
|
2022-01-21 22:59:48 +00:00
|
|
|
|
|
|
|
reader.readString(); // ignored for now
|
|
|
|
len=reader.readI();
|
|
|
|
min=reader.readI();
|
|
|
|
max=reader.readI();
|
2022-01-21 23:17:05 +00:00
|
|
|
|
|
|
|
if (len>256 || min!=0 || max>255) return DIV_DATA_INVALID_DATA;
|
|
|
|
|
2022-01-21 22:59:48 +00:00
|
|
|
reader.read(data,4*len);
|
2022-01-21 23:17:05 +00:00
|
|
|
|
|
|
|
return DIV_DATA_SUCCESS;
|
2022-01-21 22:59:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:10:06 +00:00
|
|
|
bool DivWavetable::save(const char* path) {
|
|
|
|
SafeWriter* w=new SafeWriter();
|
|
|
|
w->init();
|
|
|
|
|
|
|
|
// write magic
|
|
|
|
w->write("-Furnace waveta-",16);
|
|
|
|
|
|
|
|
// write version
|
|
|
|
w->writeS(DIV_ENGINE_VERSION);
|
|
|
|
|
|
|
|
// reserved
|
|
|
|
w->writeS(0);
|
|
|
|
|
|
|
|
putWaveData(w);
|
|
|
|
|
2022-01-20 10:04:03 +00:00
|
|
|
FILE* outFile=ps_fopen(path,"wb");
|
2022-01-19 10:10:06 +00:00
|
|
|
if (outFile==NULL) {
|
|
|
|
logE("could not save wavetable: %s!\n",strerror(errno));
|
|
|
|
w->finish();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fwrite(w->getFinalBuf(),1,w->size(),outFile)!=w->size()) {
|
|
|
|
logW("did not write entire wavetable!\n");
|
|
|
|
}
|
|
|
|
fclose(outFile);
|
|
|
|
w->finish();
|
|
|
|
return true;
|
|
|
|
}
|