2022-02-15 03:12:20 +00:00
|
|
|
/**
|
|
|
|
* Furnace Tracker - multi-system chiptune tracker
|
2023-01-20 00:18:40 +00:00
|
|
|
* Copyright (C) 2021-2023 tildearrow and contributors
|
2022-02-15 03:12:20 +00:00
|
|
|
*
|
|
|
|
* 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) {
|
2022-05-27 19:40:26 +00:00
|
|
|
size_t blockStartSeek, blockEndSeek;
|
|
|
|
|
2022-01-19 10:10:06 +00:00
|
|
|
w->write("WAVE",4);
|
2022-05-27 19:40:26 +00:00
|
|
|
blockStartSeek=w->tell();
|
2022-01-19 10:10:06 +00:00
|
|
|
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-05-27 19:40:26 +00:00
|
|
|
|
|
|
|
blockEndSeek=w->tell();
|
|
|
|
w->seek(blockStartSeek,SEEK_SET);
|
|
|
|
w->writeI(blockEndSeek-blockStartSeek-4);
|
|
|
|
w->seek(0,SEEK_END);
|
2022-01-19 10:10:06 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-11-21 07:38:17 +00:00
|
|
|
logV("header is invalid: %c%c%c%c",magic[0],magic[1],magic[2],magic[3]);
|
2022-01-21 23:17:05 +00:00
|
|
|
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
|
|
|
|
2022-12-08 05:39:29 +00:00
|
|
|
if (len>256) {
|
|
|
|
logE("invalid len: %d",len);
|
2022-11-21 07:38:17 +00:00
|
|
|
return DIV_DATA_INVALID_DATA;
|
|
|
|
}
|
2022-01-21 23:17:05 +00:00
|
|
|
|
2022-12-08 05:39:29 +00:00
|
|
|
if (min!=0) {
|
|
|
|
logW("invalid min %d",min);
|
|
|
|
min=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max>255) {
|
|
|
|
logW("invalid max %d",max);
|
|
|
|
max=255;
|
|
|
|
}
|
|
|
|
|
2022-08-20 03:37:54 +00:00
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
data[i]=reader.readI();
|
|
|
|
}
|
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) {
|
2022-04-11 03:12:02 +00:00
|
|
|
logE("could not save wavetable: %s!",strerror(errno));
|
2022-01-19 10:10:06 +00:00
|
|
|
w->finish();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fwrite(w->getFinalBuf(),1,w->size(),outFile)!=w->size()) {
|
2022-04-11 03:12:02 +00:00
|
|
|
logW("did not write entire wavetable!");
|
2022-01-19 10:10:06 +00:00
|
|
|
}
|
|
|
|
fclose(outFile);
|
|
|
|
w->finish();
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-13 20:43:13 +00:00
|
|
|
|
|
|
|
bool DivWavetable::saveDMW(const char* path) {
|
|
|
|
SafeWriter* w=new SafeWriter();
|
|
|
|
w->init();
|
|
|
|
|
|
|
|
// write width
|
|
|
|
w->writeI(len);
|
|
|
|
|
|
|
|
// check height
|
|
|
|
w->writeC(max);
|
|
|
|
if (max==255) {
|
|
|
|
// write as new format (because 0xff means that)
|
|
|
|
w->writeC(1); // format version
|
|
|
|
w->writeC(max); // actual height
|
|
|
|
|
|
|
|
// waveform data
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
w->writeI(data[i]&0xff);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// write as old format
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
w->writeC(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* outFile=ps_fopen(path,"wb");
|
|
|
|
if (outFile==NULL) {
|
|
|
|
logE("could not save wavetable: %s!",strerror(errno));
|
|
|
|
w->finish();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fwrite(w->getFinalBuf(),1,w->size(),outFile)!=w->size()) {
|
|
|
|
logW("did not write entire wavetable!");
|
|
|
|
}
|
|
|
|
fclose(outFile);
|
|
|
|
w->finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DivWavetable::saveRaw(const char* path) {
|
|
|
|
SafeWriter* w=new SafeWriter();
|
|
|
|
w->init();
|
|
|
|
|
|
|
|
// waveform data
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
w->writeC(data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* outFile=ps_fopen(path,"wb");
|
|
|
|
if (outFile==NULL) {
|
|
|
|
logE("could not save wavetable: %s!",strerror(errno));
|
|
|
|
w->finish();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fwrite(w->getFinalBuf(),1,w->size(),outFile)!=w->size()) {
|
|
|
|
logW("did not write entire wavetable!");
|
|
|
|
}
|
|
|
|
fclose(outFile);
|
|
|
|
w->finish();
|
|
|
|
return true;
|
|
|
|
}
|