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-07-22 04:36:42 +00:00
|
|
|
#ifndef _SAMPLE_H
|
|
|
|
#define _SAMPLE_H
|
|
|
|
|
2021-12-15 19:15:44 +00:00
|
|
|
#include "../ta-utils.h"
|
2022-12-09 05:30:27 +00:00
|
|
|
#include "defines.h"
|
2022-11-21 00:19:24 +00:00
|
|
|
#include "safeWriter.h"
|
|
|
|
#include "dataErrors.h"
|
2022-03-22 23:36:29 +00:00
|
|
|
#include <deque>
|
2021-12-15 19:15:44 +00:00
|
|
|
|
2022-08-11 13:21:54 +00:00
|
|
|
enum DivSampleLoopMode: unsigned char {
|
|
|
|
DIV_SAMPLE_LOOP_FORWARD=0,
|
|
|
|
DIV_SAMPLE_LOOP_BACKWARD,
|
|
|
|
DIV_SAMPLE_LOOP_PINGPONG,
|
|
|
|
DIV_SAMPLE_LOOP_MAX // boundary for loop mode
|
|
|
|
};
|
|
|
|
|
2022-07-22 04:36:42 +00:00
|
|
|
enum DivSampleDepth: unsigned char {
|
|
|
|
DIV_SAMPLE_DEPTH_1BIT=0,
|
|
|
|
DIV_SAMPLE_DEPTH_1BIT_DPCM=1,
|
|
|
|
DIV_SAMPLE_DEPTH_YMZ_ADPCM=3,
|
|
|
|
DIV_SAMPLE_DEPTH_QSOUND_ADPCM=4,
|
|
|
|
DIV_SAMPLE_DEPTH_ADPCM_A=5,
|
|
|
|
DIV_SAMPLE_DEPTH_ADPCM_B=6,
|
|
|
|
DIV_SAMPLE_DEPTH_8BIT=8,
|
|
|
|
DIV_SAMPLE_DEPTH_BRR=9,
|
|
|
|
DIV_SAMPLE_DEPTH_VOX=10,
|
|
|
|
DIV_SAMPLE_DEPTH_16BIT=16,
|
|
|
|
DIV_SAMPLE_DEPTH_MAX // boundary for sample depth
|
|
|
|
};
|
|
|
|
|
2022-03-20 08:14:00 +00:00
|
|
|
enum DivResampleFilters {
|
|
|
|
DIV_RESAMPLE_NONE=0,
|
|
|
|
DIV_RESAMPLE_LINEAR,
|
|
|
|
DIV_RESAMPLE_CUBIC,
|
|
|
|
DIV_RESAMPLE_BLEP,
|
|
|
|
DIV_RESAMPLE_SINC,
|
|
|
|
DIV_RESAMPLE_BEST
|
|
|
|
};
|
|
|
|
|
2022-03-22 23:36:29 +00:00
|
|
|
struct DivSampleHistory {
|
|
|
|
unsigned char* data;
|
|
|
|
unsigned int length, samples;
|
2022-07-22 04:36:42 +00:00
|
|
|
DivSampleDepth depth;
|
|
|
|
int rate, centerRate, loopStart, loopEnd;
|
2023-06-05 09:28:07 +00:00
|
|
|
bool loop, brrEmphasis, dither;
|
2022-08-11 13:21:54 +00:00
|
|
|
DivSampleLoopMode loopMode;
|
2022-03-22 23:36:29 +00:00
|
|
|
bool hasSample;
|
2023-06-05 09:28:07 +00:00
|
|
|
DivSampleHistory(void* d, unsigned int l, unsigned int s, DivSampleDepth de, int r, int cr, int ls, int le, bool lp, bool be, bool di, DivSampleLoopMode lm):
|
2022-03-22 23:36:29 +00:00
|
|
|
data((unsigned char*)d),
|
|
|
|
length(l),
|
|
|
|
samples(s),
|
|
|
|
depth(de),
|
|
|
|
rate(r),
|
|
|
|
centerRate(cr),
|
|
|
|
loopStart(ls),
|
2022-07-22 04:36:42 +00:00
|
|
|
loopEnd(le),
|
2022-08-11 13:21:54 +00:00
|
|
|
loop(lp),
|
2022-12-07 07:57:19 +00:00
|
|
|
brrEmphasis(be),
|
2023-06-05 09:28:07 +00:00
|
|
|
dither(di),
|
2022-08-11 13:21:54 +00:00
|
|
|
loopMode(lm),
|
2022-03-22 23:36:29 +00:00
|
|
|
hasSample(true) {}
|
2023-06-05 09:28:07 +00:00
|
|
|
DivSampleHistory(DivSampleDepth de, int r, int cr, int ls, int le, bool lp, bool be, bool di, DivSampleLoopMode lm):
|
2022-03-22 23:36:29 +00:00
|
|
|
data(NULL),
|
|
|
|
length(0),
|
|
|
|
samples(0),
|
|
|
|
depth(de),
|
|
|
|
rate(r),
|
|
|
|
centerRate(cr),
|
|
|
|
loopStart(ls),
|
2022-07-22 04:36:42 +00:00
|
|
|
loopEnd(le),
|
2022-08-11 13:21:54 +00:00
|
|
|
loop(lp),
|
2022-12-07 07:57:19 +00:00
|
|
|
brrEmphasis(be),
|
2023-06-05 09:28:07 +00:00
|
|
|
dither(di),
|
2022-08-11 13:21:54 +00:00
|
|
|
loopMode(lm),
|
2022-03-22 23:36:29 +00:00
|
|
|
hasSample(false) {}
|
|
|
|
~DivSampleHistory();
|
|
|
|
};
|
|
|
|
|
2021-05-11 20:08:08 +00:00
|
|
|
struct DivSample {
|
|
|
|
String name;
|
2023-02-10 07:01:23 +00:00
|
|
|
int rate, centerRate, loopStart, loopEnd;
|
2022-02-03 04:17:16 +00:00
|
|
|
// valid values are:
|
2022-02-24 03:00:12 +00:00
|
|
|
// - 0: ZX Spectrum overlay drum (1-bit)
|
|
|
|
// - 1: 1-bit NES DPCM (1-bit)
|
2022-05-14 21:59:07 +00:00
|
|
|
// - 3: YMZ ADPCM
|
2022-02-24 08:57:45 +00:00
|
|
|
// - 4: QSound ADPCM
|
2022-02-24 03:00:12 +00:00
|
|
|
// - 5: ADPCM-A
|
|
|
|
// - 6: ADPCM-B
|
2022-02-03 04:17:16 +00:00
|
|
|
// - 8: 8-bit PCM
|
2022-02-24 03:00:12 +00:00
|
|
|
// - 9: BRR (SNES)
|
2022-05-19 09:35:39 +00:00
|
|
|
// - 10: VOX ADPCM
|
2022-02-03 04:17:16 +00:00
|
|
|
// - 16: 16-bit PCM
|
2022-07-22 04:36:42 +00:00
|
|
|
DivSampleDepth depth;
|
2023-06-05 09:28:07 +00:00
|
|
|
bool loop, brrEmphasis, dither;
|
2022-08-11 13:21:54 +00:00
|
|
|
// valid values are:
|
2022-11-07 22:22:13 +00:00
|
|
|
// - 0: Forward loop
|
|
|
|
// - 1: Backward loop
|
|
|
|
// - 2: Pingpong loop
|
2022-08-11 13:21:54 +00:00
|
|
|
DivSampleLoopMode loopMode;
|
2022-02-24 03:00:12 +00:00
|
|
|
|
2022-12-09 05:30:27 +00:00
|
|
|
bool renderOn[DIV_MAX_SAMPLE_TYPE][DIV_MAX_CHIPS];
|
2022-11-26 09:13:11 +00:00
|
|
|
|
2022-02-24 03:00:12 +00:00
|
|
|
// these are the new data structures.
|
2022-02-24 08:57:45 +00:00
|
|
|
signed char* data8; // 8
|
|
|
|
short* data16; // 16
|
|
|
|
unsigned char* data1; // 0
|
|
|
|
unsigned char* dataDPCM; // 1
|
2022-05-14 21:59:07 +00:00
|
|
|
unsigned char* dataZ; // 3
|
2022-02-24 08:57:45 +00:00
|
|
|
unsigned char* dataQSoundA; // 4
|
|
|
|
unsigned char* dataA; // 5
|
|
|
|
unsigned char* dataB; // 6
|
|
|
|
unsigned char* dataBRR; // 9
|
|
|
|
unsigned char* dataVOX; // 10
|
2022-02-24 03:00:12 +00:00
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
unsigned int length8, length16, length1, lengthDPCM, lengthZ, lengthQSoundA, lengthA, lengthB, lengthBRR, lengthVOX;
|
2022-02-24 03:00:12 +00:00
|
|
|
|
2022-02-24 08:57:45 +00:00
|
|
|
unsigned int samples;
|
2021-05-13 07:39:26 +00:00
|
|
|
|
2022-03-22 23:36:29 +00:00
|
|
|
std::deque<DivSampleHistory*> undoHist;
|
|
|
|
std::deque<DivSampleHistory*> redoHist;
|
|
|
|
|
2022-11-21 00:19:24 +00:00
|
|
|
/**
|
|
|
|
* put sample data.
|
|
|
|
* @param w a SafeWriter.
|
|
|
|
*/
|
|
|
|
void putSampleData(SafeWriter* w);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read sample data.
|
|
|
|
* @param reader the reader.
|
|
|
|
* @param version the format version.
|
|
|
|
* @return a DivDataErrors.
|
|
|
|
*/
|
|
|
|
DivDataErrors readSampleData(SafeReader& reader, short version);
|
|
|
|
|
2022-07-22 04:36:42 +00:00
|
|
|
/**
|
|
|
|
* check if sample is loopable.
|
|
|
|
* @return whether it is loopable.
|
|
|
|
*/
|
|
|
|
bool isLoopable();
|
|
|
|
|
2022-08-11 13:21:54 +00:00
|
|
|
/**
|
|
|
|
* get sample start position
|
|
|
|
* @return the samples start position.
|
|
|
|
*/
|
|
|
|
int getLoopStartPosition(DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get sample loop end position
|
|
|
|
* @return the samples loop end position.
|
|
|
|
*/
|
|
|
|
int getLoopEndPosition(DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX);
|
|
|
|
|
2022-07-22 04:36:42 +00:00
|
|
|
/**
|
|
|
|
* get sample end position
|
|
|
|
* @return the samples end position.
|
|
|
|
*/
|
2022-08-11 13:21:54 +00:00
|
|
|
int getEndPosition(DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get sample offset
|
|
|
|
* @return the sample offset.
|
|
|
|
*/
|
|
|
|
int getSampleOffset(int offset, int length, DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX);
|
2022-07-22 04:36:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @warning DO NOT USE - internal functions
|
|
|
|
*/
|
|
|
|
void setSampleCount(unsigned int count);
|
|
|
|
|
2022-03-20 08:14:00 +00:00
|
|
|
/**
|
|
|
|
* @warning DO NOT USE - internal functions
|
|
|
|
*/
|
2023-05-11 22:46:22 +00:00
|
|
|
bool resampleNone(double sRate, double tRate);
|
|
|
|
bool resampleLinear(double sRate, double tRate);
|
|
|
|
bool resampleCubic(double sRate, double tRate);
|
|
|
|
bool resampleBlep(double sRate, double tRate);
|
|
|
|
bool resampleSinc(double sRate, double tRate);
|
2022-03-20 08:14:00 +00:00
|
|
|
|
2022-03-16 03:05:55 +00:00
|
|
|
/**
|
|
|
|
* save this sample to a file.
|
|
|
|
* @param path a path.
|
|
|
|
* @return whether saving succeeded or not.
|
|
|
|
*/
|
2021-12-18 06:03:59 +00:00
|
|
|
bool save(const char* path);
|
2022-03-16 03:05:55 +00:00
|
|
|
|
2023-05-02 08:57:25 +00:00
|
|
|
/**
|
|
|
|
* save this sample to a file (raw).
|
|
|
|
* @param path a path.
|
|
|
|
* @return whether saving succeeded or not.
|
|
|
|
*/
|
|
|
|
bool saveRaw(const char* path);
|
|
|
|
|
2022-03-16 03:05:55 +00:00
|
|
|
/**
|
|
|
|
* @warning DO NOT USE - internal function
|
|
|
|
* initialize sample data.
|
|
|
|
* @param d sample type.
|
|
|
|
* @param count number of samples.
|
|
|
|
* @return whether it was successful.
|
|
|
|
*/
|
2022-07-22 04:36:42 +00:00
|
|
|
bool initInternal(DivSampleDepth d, int count);
|
2022-03-16 03:05:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize sample data. make sure you have set `depth` before doing so.
|
|
|
|
* @param count number of samples.
|
|
|
|
* @return whether it was successful.
|
|
|
|
*/
|
2022-02-24 08:57:45 +00:00
|
|
|
bool init(unsigned int count);
|
2022-03-16 03:05:55 +00:00
|
|
|
|
2022-03-19 08:42:44 +00:00
|
|
|
/**
|
|
|
|
* resize sample data. make sure the sample has been initialized before doing so.
|
|
|
|
* @warning do not attempt to resize a sample outside of a synchronized block!
|
|
|
|
* @param count number of samples.
|
|
|
|
* @return whether it was successful.
|
|
|
|
*/
|
|
|
|
bool resize(unsigned int count);
|
|
|
|
|
2022-03-21 07:43:52 +00:00
|
|
|
/**
|
|
|
|
* remove part of the sample data.
|
|
|
|
* @warning do not attempt to strip a sample outside of a synchronized block!
|
|
|
|
* @param start the beginning.
|
|
|
|
* @param end the end.
|
|
|
|
* @return whether it was successful.
|
|
|
|
*/
|
|
|
|
bool strip(unsigned int begin, unsigned int end);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clip the sample data to specified boundaries.
|
|
|
|
* @warning do not attempt to trim a sample outside of a synchronized block!
|
|
|
|
* @param start the beginning.
|
|
|
|
* @param end the end.
|
|
|
|
* @return whether it was successful.
|
|
|
|
*/
|
|
|
|
bool trim(unsigned int begin, unsigned int end);
|
|
|
|
|
2022-03-22 09:54:01 +00:00
|
|
|
/**
|
|
|
|
* insert silence at specified position.
|
|
|
|
* @warning do not attempt to do this outside of a synchronized block!
|
|
|
|
* @param pos the beginning.
|
|
|
|
* @param length how many samples to insert.
|
|
|
|
* @return whether it was successful.
|
|
|
|
*/
|
|
|
|
bool insert(unsigned int pos, unsigned int length);
|
|
|
|
|
2022-03-20 08:14:00 +00:00
|
|
|
/**
|
|
|
|
* change the sample rate.
|
|
|
|
* @warning do not attempt to resample outside of a synchronized block!
|
2023-05-11 22:46:22 +00:00
|
|
|
* @param sRate source rate.
|
|
|
|
* @param tRate target rate.
|
2022-03-20 08:14:00 +00:00
|
|
|
* @param filter the interpolation filter.
|
|
|
|
* @return whether it was successful.
|
|
|
|
*/
|
2023-05-11 22:46:22 +00:00
|
|
|
bool resample(double sRate, double tRate, int filter);
|
2022-03-20 08:14:00 +00:00
|
|
|
|
2023-05-24 07:54:36 +00:00
|
|
|
/**
|
|
|
|
* convert sample depth.
|
|
|
|
* @warning do not attempt to do this outside of a synchronized block!
|
|
|
|
* @param newDepth the new depth.
|
|
|
|
*/
|
|
|
|
void convert(DivSampleDepth newDepth);
|
|
|
|
|
2022-03-16 03:05:55 +00:00
|
|
|
/**
|
|
|
|
* initialize the rest of sample formats for this sample.
|
|
|
|
*/
|
2022-09-25 22:18:04 +00:00
|
|
|
void render(unsigned int formatMask=0xffffffff);
|
2022-03-16 03:05:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the sample data for the current depth.
|
|
|
|
* @return the sample data, or NULL if not created.
|
|
|
|
*/
|
2022-02-24 08:57:45 +00:00
|
|
|
void* getCurBuf();
|
2022-03-16 03:05:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the sample data length for the current depth.
|
|
|
|
* @return the sample data length.
|
|
|
|
*/
|
2022-02-24 08:57:45 +00:00
|
|
|
unsigned int getCurBufLen();
|
2022-03-22 23:36:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* prepare an undo step for this sample.
|
|
|
|
* @param data whether to include sample data.
|
2022-03-23 05:42:59 +00:00
|
|
|
* @param doNotPush if this is true, don't push the DivSampleHistory to the undo history.
|
2022-03-22 23:36:29 +00:00
|
|
|
* @return the undo step.
|
|
|
|
*/
|
2022-03-23 05:42:59 +00:00
|
|
|
DivSampleHistory* prepareUndo(bool data, bool doNotPush=false);
|
2022-03-22 23:36:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* undo. you may need to call DivEngine::renderSamples afterwards.
|
|
|
|
* @warning do not attempt to undo outside of a synchronized block!
|
|
|
|
* @return 0 on failure; 1 on success and 2 on success (data changed).
|
|
|
|
*/
|
|
|
|
int undo();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* redo. you may need to call DivEngine::renderSamples afterwards.
|
|
|
|
* @warning do not attempt to redo outside of a synchronized block!
|
|
|
|
* @return 0 on failure; 1 on success and 2 on success (data changed).
|
|
|
|
*/
|
|
|
|
int redo();
|
2021-05-13 07:39:26 +00:00
|
|
|
DivSample():
|
|
|
|
name(""),
|
2021-12-23 23:04:44 +00:00
|
|
|
rate(32000),
|
2022-01-27 21:52:06 +00:00
|
|
|
centerRate(8363),
|
2022-01-15 22:54:21 +00:00
|
|
|
loopStart(-1),
|
2022-07-22 04:36:42 +00:00
|
|
|
loopEnd(-1),
|
|
|
|
depth(DIV_SAMPLE_DEPTH_16BIT),
|
2022-08-11 13:21:54 +00:00
|
|
|
loop(false),
|
2022-12-07 07:57:19 +00:00
|
|
|
brrEmphasis(true),
|
2023-06-05 09:28:07 +00:00
|
|
|
dither(false),
|
2022-08-11 13:21:54 +00:00
|
|
|
loopMode(DIV_SAMPLE_LOOP_FORWARD),
|
2022-02-24 08:57:45 +00:00
|
|
|
data8(NULL),
|
|
|
|
data16(NULL),
|
|
|
|
data1(NULL),
|
|
|
|
dataDPCM(NULL),
|
2022-05-14 21:59:07 +00:00
|
|
|
dataZ(NULL),
|
2022-02-24 08:57:45 +00:00
|
|
|
dataQSoundA(NULL),
|
|
|
|
dataA(NULL),
|
|
|
|
dataB(NULL),
|
|
|
|
dataBRR(NULL),
|
|
|
|
dataVOX(NULL),
|
|
|
|
length8(0),
|
|
|
|
length16(0),
|
|
|
|
length1(0),
|
|
|
|
lengthDPCM(0),
|
2022-05-14 21:59:07 +00:00
|
|
|
lengthZ(0),
|
2022-02-24 08:57:45 +00:00
|
|
|
lengthQSoundA(0),
|
|
|
|
lengthA(0),
|
|
|
|
lengthB(0),
|
|
|
|
lengthBRR(0),
|
|
|
|
lengthVOX(0),
|
2022-11-26 09:13:11 +00:00
|
|
|
samples(0) {
|
2022-12-09 05:30:27 +00:00
|
|
|
for (int i=0; i<DIV_MAX_CHIPS; i++) {
|
|
|
|
for (int j=0; j<DIV_MAX_SAMPLE_TYPE; j++) {
|
|
|
|
renderOn[j][i]=true;
|
|
|
|
}
|
2022-11-26 09:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 19:15:44 +00:00
|
|
|
~DivSample();
|
2021-05-11 20:08:08 +00:00
|
|
|
};
|
2022-07-22 04:36:42 +00:00
|
|
|
|
|
|
|
#endif
|