| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef _SAMPLE_H |
| 21 |
#define _SAMPLE_H |
| 22 |
|
| 23 |
#include "../ta-utils.h" |
| 24 |
#include "defines.h" |
| 25 |
#include "safeWriter.h" |
| 26 |
#include "dataErrors.h" |
| 27 |
#include "../fixedQueue.h" |
| 28 |
|
| 29 |
enum DivSampleLoopMode: unsigned char { |
| 30 |
DIV_SAMPLE_LOOP_FORWARD=0, |
| 31 |
DIV_SAMPLE_LOOP_BACKWARD, |
| 32 |
DIV_SAMPLE_LOOP_PINGPONG, |
| 33 |
DIV_SAMPLE_LOOP_MAX // boundary for loop mode |
| 34 |
}; |
| 35 |
|
| 36 |
enum DivSampleDepth: unsigned char { |
| 37 |
DIV_SAMPLE_DEPTH_1BIT=0, |
| 38 |
DIV_SAMPLE_DEPTH_1BIT_DPCM=1, |
| 39 |
DIV_SAMPLE_DEPTH_YMZ_ADPCM=3, |
| 40 |
DIV_SAMPLE_DEPTH_QSOUND_ADPCM=4, |
| 41 |
DIV_SAMPLE_DEPTH_ADPCM_A=5, |
| 42 |
DIV_SAMPLE_DEPTH_ADPCM_B=6, |
| 43 |
DIV_SAMPLE_DEPTH_ADPCM_K=7, |
| 44 |
DIV_SAMPLE_DEPTH_8BIT=8, |
| 45 |
DIV_SAMPLE_DEPTH_BRR=9, |
| 46 |
DIV_SAMPLE_DEPTH_VOX=10, |
| 47 |
DIV_SAMPLE_DEPTH_MULAW=11, |
| 48 |
DIV_SAMPLE_DEPTH_C219=12, |
| 49 |
DIV_SAMPLE_DEPTH_IMA_ADPCM=13, |
| 50 |
DIV_SAMPLE_DEPTH_12BIT=14, |
| 51 |
DIV_SAMPLE_DEPTH_4BIT=15, |
| 52 |
DIV_SAMPLE_DEPTH_16BIT=16, |
| 53 |
DIV_SAMPLE_DEPTH_MAX // boundary for sample depth |
| 54 |
}; |
| 55 |
|
| 56 |
enum DivResampleFilters { |
| 57 |
DIV_RESAMPLE_NONE=0, |
| 58 |
DIV_RESAMPLE_LINEAR, |
| 59 |
DIV_RESAMPLE_CUBIC, |
| 60 |
DIV_RESAMPLE_BLEP, |
| 61 |
DIV_RESAMPLE_SINC, |
| 62 |
DIV_RESAMPLE_BEST |
| 63 |
}; |
| 64 |
|
| 65 |
struct DivSampleHistory { |
| 66 |
unsigned char* data; |
| 67 |
unsigned int length, samples; |
| 68 |
DivSampleDepth depth; |
| 69 |
int rate, centerRate, loopStart, loopEnd; |
| 70 |
bool loop, brrEmphasis, brrNoFilter, dither; |
| 71 |
DivSampleLoopMode loopMode; |
| 72 |
bool hasSample; |
| 73 |
DivSampleHistory(void* d, unsigned int l, unsigned int s, DivSampleDepth de, int r, int cr, int ls, int le, bool lp, bool be, bool bf, bool di, DivSampleLoopMode lm): |
| 74 |
data((unsigned char*)d), |
| 75 |
length(l), |
| 76 |
samples(s), |
| 77 |
depth(de), |
| 78 |
rate(r), |
| 79 |
centerRate(cr), |
| 80 |
loopStart(ls), |
| 81 |
loopEnd(le), |
| 82 |
loop(lp), |
| 83 |
brrEmphasis(be), |
| 84 |
brrNoFilter(bf), |
| 85 |
dither(di), |
| 86 |
loopMode(lm), |
| 87 |
hasSample(true) {} |
| 88 |
DivSampleHistory(DivSampleDepth de, int r, int cr, int ls, int le, bool lp, bool be, bool bf, bool di, DivSampleLoopMode lm): |
| 89 |
data(NULL), |
| 90 |
length(0), |
| 91 |
samples(0), |
| 92 |
depth(de), |
| 93 |
rate(r), |
| 94 |
centerRate(cr), |
| 95 |
loopStart(ls), |
| 96 |
loopEnd(le), |
| 97 |
loop(lp), |
| 98 |
brrEmphasis(be), |
| 99 |
brrNoFilter(bf), |
| 100 |
dither(di), |
| 101 |
loopMode(lm), |
| 102 |
hasSample(false) {} |
| 103 |
~DivSampleHistory(); |
| 104 |
}; |
| 105 |
|
| 106 |
struct DivSample { |
| 107 |
String name; |
| 108 |
int rate, centerRate, loopStart, loopEnd; |
| 109 |
// valid values are: |
| 110 |
// - 0: ZX Spectrum overlay drum (1-bit) |
| 111 |
// - 1: 1-bit NES DPCM (1-bit) |
| 112 |
// - 3: YMZ ADPCM |
| 113 |
// - 4: QSound ADPCM |
| 114 |
// - 5: ADPCM-A |
| 115 |
// - 6: ADPCM-B |
| 116 |
// - 7: K053260 4-bit simple ADPCM |
| 117 |
// - 8: 8-bit PCM |
| 118 |
// - 9: BRR (SNES) |
| 119 |
// - 10: VOX ADPCM |
| 120 |
// - 11: 8-bit µ-law PCM |
| 121 |
// - 12: C219 "µ-law" PCM |
| 122 |
// - 13: IMA ADPCM |
| 123 |
// - 14: 12-bit PCM (MultiPCM) |
| 124 |
// - 16: 16-bit PCM |
| 125 |
DivSampleDepth depth; |
| 126 |
bool loop, brrEmphasis, brrNoFilter, dither; |
| 127 |
// valid values are: |
| 128 |
// - 0: Forward loop |
| 129 |
// - 1: Backward loop |
| 130 |
// - 2: Pingpong loop |
| 131 |
DivSampleLoopMode loopMode; |
| 132 |
|
| 133 |
bool renderOn[DIV_MAX_SAMPLE_TYPE][DIV_MAX_CHIPS]; |
| 134 |
|
| 135 |
// these are the new data structures. |
| 136 |
signed char* data8; // 8 |
| 137 |
short* data16; // 16 |
| 138 |
unsigned char* data1; // 0 |
| 139 |
unsigned char* dataDPCM; // 1 |
| 140 |
unsigned char* dataZ; // 3 |
| 141 |
unsigned char* dataQSoundA; // 4 |
| 142 |
unsigned char* dataA; // 5 |
| 143 |
unsigned char* dataB; // 6 |
| 144 |
unsigned char* dataK; // 7 |
| 145 |
unsigned char* dataBRR; // 9 |
| 146 |
unsigned char* dataVOX; // 10 |
| 147 |
unsigned char* dataMuLaw; // 11 |
| 148 |
unsigned char* dataC219; // 12 |
| 149 |
unsigned char* dataIMA; // 13 |
| 150 |
unsigned char* data12; // 14 |
| 151 |
unsigned char* data4; // 15 |
| 152 |
|
| 153 |
unsigned int length8, length16, length1, lengthDPCM, lengthZ, lengthQSoundA, lengthA, lengthB, lengthK, lengthBRR, lengthVOX, lengthMuLaw, lengthC219, lengthIMA, length12, length4; |
| 154 |
|
| 155 |
unsigned int samples; |
| 156 |
|
| 157 |
FixedQueue<DivSampleHistory*,128> undoHist; |
| 158 |
FixedQueue<DivSampleHistory*,128> redoHist; |
| 159 |
|
| 160 |
/** |
| 161 |
* put sample data. |
| 162 |
* @param w a SafeWriter. |
| 163 |
*/ |
| 164 |
void putSampleData(SafeWriter* w); |
| 165 |
|
| 166 |
/** |
| 167 |
* read sample data. |
| 168 |
* @param reader the reader. |
| 169 |
* @param version the format version. |
| 170 |
* @return a DivDataErrors. |
| 171 |
*/ |
| 172 |
DivDataErrors readSampleData(SafeReader& reader, short version); |
| 173 |
|
| 174 |
/** |
| 175 |
* check if sample is loopable. |
| 176 |
* @return whether it is loopable. |
| 177 |
*/ |
| 178 |
bool isLoopable(); |
| 179 |
|
| 180 |
/** |
| 181 |
* get sample start position |
| 182 |
* @return the samples start position. |
| 183 |
*/ |
| 184 |
int getLoopStartPosition(DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX); |
| 185 |
|
| 186 |
/** |
| 187 |
* get sample loop end position |
| 188 |
* @return the samples loop end position. |
| 189 |
*/ |
| 190 |
int getLoopEndPosition(DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX); |
| 191 |
|
| 192 |
/** |
| 193 |
* get sample end position |
| 194 |
* @return the samples end position. |
| 195 |
*/ |
| 196 |
int getEndPosition(DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX); |
| 197 |
|
| 198 |
/** |
| 199 |
* get sample offset |
| 200 |
* @return the sample offset. |
| 201 |
*/ |
| 202 |
int getSampleOffset(int offset, int length, DivSampleDepth depth=DIV_SAMPLE_DEPTH_MAX); |
| 203 |
|
| 204 |
/** |
| 205 |
* @warning DO NOT USE - internal functions |
| 206 |
*/ |
| 207 |
void setSampleCount(unsigned int count); |
| 208 |
|
| 209 |
/** |
| 210 |
* @warning DO NOT USE - internal functions |
| 211 |
*/ |
| 212 |
bool resampleNone(double sRate, double tRate); |
| 213 |
bool resampleLinear(double sRate, double tRate); |
| 214 |
bool resampleCubic(double sRate, double tRate); |
| 215 |
bool resampleBlep(double sRate, double tRate); |
| 216 |
bool resampleSinc(double sRate, double tRate); |
| 217 |
|
| 218 |
/** |
| 219 |
* save this sample to a file. |
| 220 |
* @param path a path. |
| 221 |
* @return whether saving succeeded or not. |
| 222 |
*/ |
| 223 |
bool save(const char* path); |
| 224 |
|
| 225 |
/** |
| 226 |
* save this sample to a file (raw). |
| 227 |
* @param path a path. |
| 228 |
* @return whether saving succeeded or not. |
| 229 |
*/ |
| 230 |
bool saveRaw(const char* path); |
| 231 |
|
| 232 |
/** |
| 233 |
* @warning DO NOT USE - internal function |
| 234 |
* initialize sample data. |
| 235 |
* @param d sample type. |
| 236 |
* @param count number of samples. |
| 237 |
* @return whether it was successful. |
| 238 |
*/ |
| 239 |
bool initInternal(DivSampleDepth d, int count); |
| 240 |
|
| 241 |
/** |
| 242 |
* initialize sample data. make sure you have set `depth` before doing so. |
| 243 |
* @param count number of samples. |
| 244 |
* @return whether it was successful. |
| 245 |
*/ |
| 246 |
bool init(unsigned int count); |
| 247 |
|
| 248 |
/** |
| 249 |
* resize sample data. make sure the sample has been initialized before doing so. |
| 250 |
* @warning do not attempt to resize a sample outside of a synchronized block! |
| 251 |
* @param count number of samples. |
| 252 |
* @return whether it was successful. |
| 253 |
*/ |
| 254 |
bool resize(unsigned int count); |
| 255 |
|
| 256 |
/** |
| 257 |
* remove part of the sample data. |
| 258 |
* @warning do not attempt to strip a sample outside of a synchronized block! |
| 259 |
* @param start the beginning. |
| 260 |
* @param end the end. |
| 261 |
* @return whether it was successful. |
| 262 |
*/ |
| 263 |
bool strip(unsigned int begin, unsigned int end); |
| 264 |
|
| 265 |
/** |
| 266 |
* clip the sample data to specified boundaries. |
| 267 |
* @warning do not attempt to trim a sample outside of a synchronized block! |
| 268 |
* @param start the beginning. |
| 269 |
* @param end the end. |
| 270 |
* @return whether it was successful. |
| 271 |
*/ |
| 272 |
bool trim(unsigned int begin, unsigned int end); |
| 273 |
|
| 274 |
/** |
| 275 |
* insert silence at specified position. |
| 276 |
* @warning do not attempt to do this outside of a synchronized block! |
| 277 |
* @param pos the beginning. |
| 278 |
* @param length how many samples to insert. |
| 279 |
* @return whether it was successful. |
| 280 |
*/ |
| 281 |
bool insert(unsigned int pos, unsigned int length); |
| 282 |
|
| 283 |
/** |
| 284 |
* change the sample rate. |
| 285 |
* @warning do not attempt to resample outside of a synchronized block! |
| 286 |
* @param sRate source rate. |
| 287 |
* @param tRate target rate. |
| 288 |
* @param filter the interpolation filter. |
| 289 |
* @return whether it was successful. |
| 290 |
*/ |
| 291 |
bool resample(double sRate, double tRate, int filter); |
| 292 |
|
| 293 |
/** |
| 294 |
* convert sample depth. |
| 295 |
* @warning do not attempt to do this outside of a synchronized block! |
| 296 |
* @param newDepth the new depth. |
| 297 |
*/ |
| 298 |
void convert(DivSampleDepth newDepth, unsigned int formatMask=0xffffffff); |
| 299 |
|
| 300 |
/** |
| 301 |
* initialize the rest of sample formats for this sample. |
| 302 |
*/ |
| 303 |
void render(unsigned int formatMask=0xffffffff); |
| 304 |
|
| 305 |
/** |
| 306 |
* get the sample data for the current depth. |
| 307 |
* @return the sample data, or NULL if not created. |
| 308 |
*/ |
| 309 |
void* getCurBuf(); |
| 310 |
|
| 311 |
/** |
| 312 |
* get the sample data length for the current depth. |
| 313 |
* @return the sample data length. |
| 314 |
*/ |
| 315 |
unsigned int getCurBufLen(); |
| 316 |
|
| 317 |
/** |
| 318 |
* prepare an undo step for this sample. |
| 319 |
* @param data whether to include sample data. |
| 320 |
* @param doNotPush if this is true, don't push the DivSampleHistory to the undo history. |
| 321 |
* @return the undo step. |
| 322 |
*/ |
| 323 |
DivSampleHistory* prepareUndo(bool data, bool doNotPush=false); |
| 324 |
|
| 325 |
/** |
| 326 |
* undo. you may need to call DivEngine::renderSamples afterwards. |
| 327 |
* @warning do not attempt to undo outside of a synchronized block! |
| 328 |
* @return 0 on failure; 1 on success and 2 on success (data changed). |
| 329 |
*/ |
| 330 |
int undo(); |
| 331 |
|
| 332 |
/** |
| 333 |
* redo. you may need to call DivEngine::renderSamples afterwards. |
| 334 |
* @warning do not attempt to redo outside of a synchronized block! |
| 335 |
* @return 0 on failure; 1 on success and 2 on success (data changed). |
| 336 |
*/ |
| 337 |
int redo(); |
| 338 |
DivSample(): |
| 339 |
name(""), |
| 340 |
rate(32000), |
| 341 |
centerRate(8363), |
| 342 |
loopStart(-1), |
| 343 |
loopEnd(-1), |
| 344 |
depth(DIV_SAMPLE_DEPTH_16BIT), |
| 345 |
loop(false), |
| 346 |
brrEmphasis(true), |
| 347 |
brrNoFilter(false), |
| 348 |
dither(false), |
| 349 |
loopMode(DIV_SAMPLE_LOOP_FORWARD), |
| 350 |
data8(NULL), |
| 351 |
data16(NULL), |
| 352 |
data1(NULL), |
| 353 |
dataDPCM(NULL), |
| 354 |
dataZ(NULL), |
| 355 |
dataQSoundA(NULL), |
| 356 |
dataA(NULL), |
| 357 |
dataB(NULL), |
| 358 |
dataK(NULL), |
| 359 |
dataBRR(NULL), |
| 360 |
dataVOX(NULL), |
| 361 |
dataMuLaw(NULL), |
| 362 |
dataC219(NULL), |
| 363 |
dataIMA(NULL), |
| 364 |
data12(NULL), |
| 365 |
data4(NULL), |
| 366 |
length8(0), |
| 367 |
length16(0), |
| 368 |
length1(0), |
| 369 |
lengthDPCM(0), |
| 370 |
lengthZ(0), |
| 371 |
lengthQSoundA(0), |
| 372 |
lengthA(0), |
| 373 |
lengthB(0), |
| 374 |
lengthK(0), |
| 375 |
lengthBRR(0), |
| 376 |
lengthVOX(0), |
| 377 |
lengthMuLaw(0), |
| 378 |
lengthC219(0), |
| 379 |
lengthIMA(0), |
| 380 |
length12(0), |
| 381 |
length4(0), |
| 382 |
samples(0) { |
| 383 |
for (int i=0; i<DIV_MAX_CHIPS; i++) { |
| 384 |
for (int j=0; j<DIV_MAX_SAMPLE_TYPE; j++) { |
| 385 |
renderOn[j][i]=true; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
~DivSample(); |
| 390 |
}; |
| 391 |
|
| 392 |
#endif |