| 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 |
#include "sample.h" |
| 21 |
#include "../ta-log.h" |
| 22 |
#include "../fileutils.h" |
| 23 |
#include <math.h> |
| 24 |
#include <string.h> |
| 25 |
#ifdef HAVE_SNDFILE |
| 26 |
#include "sfWrapper.h" |
| 27 |
#endif |
| 28 |
#include "filter.h" |
| 29 |
#include "bsr.h" |
| 30 |
|
| 31 |
extern "C" { |
| 32 |
#include "../../extern/adpcm/bs_codec.h" |
| 33 |
#include "../../extern/adpcm/oki_codec.h" |
| 34 |
#include "../../extern/adpcm/yma_codec.h" |
| 35 |
#include "../../extern/adpcm/ymb_codec.h" |
| 36 |
#include "../../extern/adpcm/ymz_codec.h" |
| 37 |
} |
| 38 |
#include "../../extern/adpcm-xq-s/adpcm-lib.h" |
| 39 |
#include "brrUtils.h" |
| 40 |
|
| 41 |
DivSampleHistory::~DivSampleHistory() { |
| 42 |
if (data!=NULL) delete[] data; |
| 43 |
} |
| 44 |
|
| 45 |
void DivSample::putSampleData(SafeWriter* w) { |
| 46 |
size_t blockStartSeek, blockEndSeek; |
| 47 |
|
| 48 |
w->write("SMP2",4); |
| 49 |
blockStartSeek=w->tell(); |
| 50 |
w->writeI(0); |
| 51 |
|
| 52 |
w->writeString(name,false); |
| 53 |
w->writeI(samples); |
| 54 |
w->writeI(rate); |
| 55 |
w->writeI(centerRate); |
| 56 |
w->writeC(depth); |
| 57 |
w->writeC(loopMode); |
| 58 |
w->writeC(brrEmphasis); |
| 59 |
w->writeC((dither?1:0)|(brrNoFilter?2:0)); |
| 60 |
w->writeI(loop?loopStart:-1); |
| 61 |
w->writeI(loop?loopEnd:-1); |
| 62 |
|
| 63 |
for (int i=0; i<DIV_MAX_SAMPLE_TYPE; i++) { |
| 64 |
unsigned int out=0; |
| 65 |
for (int j=0; j<DIV_MAX_CHIPS; j++) { |
| 66 |
if (renderOn[i][j]) out|=1<<j; |
| 67 |
} |
| 68 |
w->writeI(out); |
| 69 |
} |
| 70 |
|
| 71 |
#ifdef TA_BIG_ENDIAN |
| 72 |
// store 16-bit samples as little-endian |
| 73 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 74 |
unsigned char* sampleBuf=(unsigned char*)getCurBuf(); |
| 75 |
size_t bufLen=getCurBufLen(); |
| 76 |
for (size_t i=0; i<bufLen; i+=2) { |
| 77 |
w->writeC(sampleBuf[i+1]); |
| 78 |
w->writeC(sampleBuf[i]); |
| 79 |
} |
| 80 |
} else { |
| 81 |
w->write(getCurBuf(),getCurBufLen()); |
| 82 |
} |
| 83 |
#else |
| 84 |
w->write(getCurBuf(),getCurBufLen()); |
| 85 |
#endif |
| 86 |
|
| 87 |
blockEndSeek=w->tell(); |
| 88 |
w->seek(blockStartSeek,SEEK_SET); |
| 89 |
w->writeI(blockEndSeek-blockStartSeek-4); |
| 90 |
w->seek(0,SEEK_END); |
| 91 |
} |
| 92 |
|
| 93 |
// Delek why |
| 94 |
static double samplePitchesSD[11]={ |
| 95 |
0.1666666666, 0.2, 0.25, 0.333333333, 0.5, |
| 96 |
1, |
| 97 |
2, 3, 4, 5, 6 |
| 98 |
}; |
| 99 |
|
| 100 |
DivDataErrors DivSample::readSampleData(SafeReader& reader, short version) { |
| 101 |
int vol=0; |
| 102 |
int pitch=0; |
| 103 |
char magic[4]; |
| 104 |
|
| 105 |
reader.read(magic,4); |
| 106 |
if (memcmp(magic,"SMPL",4)!=0 && memcmp(magic,"SMP2",4)!=0) { |
| 107 |
logV("header is invalid: %c%c%c%c",magic[0],magic[1],magic[2],magic[3]); |
| 108 |
return DIV_DATA_INVALID_HEADER; |
| 109 |
} |
| 110 |
bool isNewSample=(memcmp(magic,"SMP2",4)==0); |
| 111 |
reader.readI(); |
| 112 |
if (!isNewSample) logV("(old sample)"); |
| 113 |
|
| 114 |
name=reader.readString(); |
| 115 |
samples=reader.readI(); |
| 116 |
if (!isNewSample) { |
| 117 |
loopEnd=samples; |
| 118 |
} |
| 119 |
rate=reader.readI(); |
| 120 |
|
| 121 |
if (isNewSample) { |
| 122 |
centerRate=reader.readI(); |
| 123 |
depth=(DivSampleDepth)reader.readC(); |
| 124 |
if (version>=123) { |
| 125 |
loopMode=(DivSampleLoopMode)reader.readC(); |
| 126 |
} else { |
| 127 |
loopMode=DIV_SAMPLE_LOOP_FORWARD; |
| 128 |
reader.readC(); |
| 129 |
} |
| 130 |
|
| 131 |
if (version>=129) { |
| 132 |
brrEmphasis=reader.readC(); |
| 133 |
} else { |
| 134 |
reader.readC(); |
| 135 |
} |
| 136 |
if (version>=159) { |
| 137 |
signed char c=reader.readC(); |
| 138 |
dither=c&1; |
| 139 |
if (version>=213) brrNoFilter=c&2; |
| 140 |
} else { |
| 141 |
reader.readC(); |
| 142 |
} |
| 143 |
|
| 144 |
loopStart=reader.readI(); |
| 145 |
loopEnd=reader.readI(); |
| 146 |
loop=(loopStart>=0)&&(loopEnd>=0); |
| 147 |
|
| 148 |
for (int i=0; i<DIV_MAX_SAMPLE_TYPE; i++) { |
| 149 |
unsigned int outMask=(unsigned int)reader.readI(); |
| 150 |
for (int j=0; j<DIV_MAX_CHIPS; j++) { |
| 151 |
renderOn[i][j]=outMask&(1<<j); |
| 152 |
} |
| 153 |
} |
| 154 |
} else { |
| 155 |
if (version<58) { |
| 156 |
vol=reader.readS(); |
| 157 |
pitch=reader.readS(); |
| 158 |
} else { |
| 159 |
reader.readI(); |
| 160 |
} |
| 161 |
depth=(DivSampleDepth)reader.readC(); |
| 162 |
|
| 163 |
// reserved |
| 164 |
reader.readC(); |
| 165 |
|
| 166 |
// while version 32 stored this value, it was unused. |
| 167 |
if (version>=38) { |
| 168 |
centerRate=(unsigned short)reader.readS(); |
| 169 |
} else { |
| 170 |
reader.readS(); |
| 171 |
} |
| 172 |
|
| 173 |
if (version>=19) { |
| 174 |
loopStart=reader.readI(); |
| 175 |
loop=(loopStart>=0)&&(loopEnd>=0); |
| 176 |
} else { |
| 177 |
reader.readI(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (version>=58) { // modern sample |
| 182 |
init(samples); |
| 183 |
reader.read(getCurBuf(),getCurBufLen()); |
| 184 |
#ifdef TA_BIG_ENDIAN |
| 185 |
// convert 16-bit samples to big-endian |
| 186 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 187 |
unsigned char* sampleBuf=(unsigned char*)getCurBuf(); |
| 188 |
size_t sampleBufLen=getCurBufLen(); |
| 189 |
for (size_t pos=0; pos<sampleBufLen; pos+=2) { |
| 190 |
sampleBuf[pos]^=sampleBuf[pos+1]; |
| 191 |
sampleBuf[pos+1]^=sampleBuf[pos]; |
| 192 |
sampleBuf[pos]^=sampleBuf[pos+1]; |
| 193 |
} |
| 194 |
} |
| 195 |
#endif |
| 196 |
} else { // legacy sample |
| 197 |
int length=samples; |
| 198 |
short* data=new short[length]; |
| 199 |
reader.read(data,2*length); |
| 200 |
|
| 201 |
#ifdef TA_BIG_ENDIAN |
| 202 |
// convert 16-bit samples to big-endian |
| 203 |
for (int pos=0; pos<length; pos++) { |
| 204 |
data[pos]=((unsigned short)data[pos]>>8)|((unsigned short)data[pos]<<8); |
| 205 |
} |
| 206 |
#endif |
| 207 |
|
| 208 |
if (pitch!=5) { |
| 209 |
logD("scaling from %d...",pitch); |
| 210 |
} |
| 211 |
|
| 212 |
// render data |
| 213 |
if (depth!=DIV_SAMPLE_DEPTH_8BIT && depth!=DIV_SAMPLE_DEPTH_16BIT) { |
| 214 |
logW("sample depth is wrong! (%d)",(int)depth); |
| 215 |
depth=DIV_SAMPLE_DEPTH_16BIT; |
| 216 |
} |
| 217 |
samples=(double)samples/samplePitchesSD[pitch]; |
| 218 |
init(samples); |
| 219 |
|
| 220 |
unsigned int k=0; |
| 221 |
float mult=(float)(vol)/50.0f; |
| 222 |
for (double j=0; j<length; j+=samplePitchesSD[pitch]) { |
| 223 |
if (k>=samples) { |
| 224 |
break; |
| 225 |
} |
| 226 |
if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 227 |
float next=(float)(data[(unsigned int)j]-0x80)*mult; |
| 228 |
data8[k++]=fmin(fmax(next,-128),127); |
| 229 |
} else { |
| 230 |
float next=(float)data[(unsigned int)j]*mult; |
| 231 |
data16[k++]=fmin(fmax(next,-32768),32767); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
delete[] data; |
| 236 |
} |
| 237 |
|
| 238 |
return DIV_DATA_SUCCESS; |
| 239 |
} |
| 240 |
|
| 241 |
bool DivSample::isLoopable() { |
| 242 |
return loop && ((loopStart>=0 && loopStart<loopEnd) && (loopEnd>loopStart && loopEnd<=(int)samples)); |
| 243 |
} |
| 244 |
|
| 245 |
int DivSample::getSampleOffset(int offset, int length, DivSampleDepth depth) { |
| 246 |
if ((length==0) || (offset==length)) { |
| 247 |
int off=offset; |
| 248 |
switch (depth) { |
| 249 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 250 |
off=(offset+7)/8; |
| 251 |
break; |
| 252 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 253 |
off=(offset+7)/8; |
| 254 |
break; |
| 255 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 256 |
off=(offset+1)/2; |
| 257 |
break; |
| 258 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: |
| 259 |
off=(offset+1)/2; |
| 260 |
break; |
| 261 |
case DIV_SAMPLE_DEPTH_ADPCM_A: |
| 262 |
off=(offset+1)/2; |
| 263 |
break; |
| 264 |
case DIV_SAMPLE_DEPTH_ADPCM_B: |
| 265 |
off=(offset+1)/2; |
| 266 |
break; |
| 267 |
case DIV_SAMPLE_DEPTH_ADPCM_K: |
| 268 |
off=(offset+1)/2; |
| 269 |
break; |
| 270 |
case DIV_SAMPLE_DEPTH_8BIT: |
| 271 |
off=offset; |
| 272 |
break; |
| 273 |
case DIV_SAMPLE_DEPTH_BRR: |
| 274 |
off=9*((offset+15)/16); |
| 275 |
break; |
| 276 |
case DIV_SAMPLE_DEPTH_VOX: |
| 277 |
off=(offset+1)/2; |
| 278 |
break; |
| 279 |
case DIV_SAMPLE_DEPTH_MULAW: |
| 280 |
off=offset; |
| 281 |
break; |
| 282 |
case DIV_SAMPLE_DEPTH_C219: |
| 283 |
off=offset; |
| 284 |
break; |
| 285 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 286 |
off=(offset+1)/2; |
| 287 |
break; |
| 288 |
case DIV_SAMPLE_DEPTH_12BIT: |
| 289 |
off=((offset*3)+1)/2; |
| 290 |
break; |
| 291 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 292 |
off=offset*2; |
| 293 |
break; |
| 294 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 295 |
off=(offset+1)/2; |
| 296 |
break; |
| 297 |
default: |
| 298 |
break; |
| 299 |
} |
| 300 |
return off; |
| 301 |
} else { |
| 302 |
int off=offset; |
| 303 |
int len=length; |
| 304 |
switch (depth) { |
| 305 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 306 |
off=(offset+7)/8; |
| 307 |
len=(length+7)/8; |
| 308 |
break; |
| 309 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 310 |
off=(offset+7)/8; |
| 311 |
len=(length+7)/8; |
| 312 |
break; |
| 313 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 314 |
off=(offset+1)/2; |
| 315 |
len=(length+1)/2; |
| 316 |
break; |
| 317 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: |
| 318 |
off=(offset+1)/2; |
| 319 |
len=(length+1)/2; |
| 320 |
break; |
| 321 |
case DIV_SAMPLE_DEPTH_ADPCM_A: |
| 322 |
off=(offset+1)/2; |
| 323 |
len=(length+1)/2; |
| 324 |
break; |
| 325 |
case DIV_SAMPLE_DEPTH_ADPCM_B: |
| 326 |
off=(offset+1)/2; |
| 327 |
len=(length+1)/2; |
| 328 |
break; |
| 329 |
case DIV_SAMPLE_DEPTH_ADPCM_K: |
| 330 |
off=(offset+1)/2; |
| 331 |
len=(length+1)/2; |
| 332 |
break; |
| 333 |
case DIV_SAMPLE_DEPTH_8BIT: |
| 334 |
off=offset; |
| 335 |
len=length; |
| 336 |
break; |
| 337 |
case DIV_SAMPLE_DEPTH_BRR: |
| 338 |
off=9*((offset+15)/16); |
| 339 |
len=9*((length+15)/16); |
| 340 |
break; |
| 341 |
case DIV_SAMPLE_DEPTH_VOX: |
| 342 |
off=(offset+1)/2; |
| 343 |
len=(length+1)/2; |
| 344 |
break; |
| 345 |
case DIV_SAMPLE_DEPTH_MULAW: |
| 346 |
off=offset; |
| 347 |
len=length; |
| 348 |
break; |
| 349 |
case DIV_SAMPLE_DEPTH_C219: |
| 350 |
off=offset; |
| 351 |
len=length; |
| 352 |
break; |
| 353 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 354 |
off=(offset+1)/2; |
| 355 |
len=(length+1)/2; |
| 356 |
break; |
| 357 |
case DIV_SAMPLE_DEPTH_12BIT: |
| 358 |
off=((offset*3)+1)/2; |
| 359 |
len=((length*3)+1)/2; |
| 360 |
break; |
| 361 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 362 |
off=(offset+1)/2; |
| 363 |
len=(length+1)/2; |
| 364 |
break; |
| 365 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 366 |
off=offset*2; |
| 367 |
len=length*2; |
| 368 |
break; |
| 369 |
default: |
| 370 |
break; |
| 371 |
} |
| 372 |
return isLoopable()?off:len; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
int DivSample::getLoopStartPosition(DivSampleDepth depth) { |
| 377 |
return getSampleOffset(loopStart,0,depth); |
| 378 |
} |
| 379 |
|
| 380 |
int DivSample::getLoopEndPosition(DivSampleDepth depth) { |
| 381 |
return getSampleOffset(loopEnd,samples,depth); |
| 382 |
} |
| 383 |
|
| 384 |
int DivSample::getEndPosition(DivSampleDepth depth) { |
| 385 |
int off=samples; |
| 386 |
switch (depth) { |
| 387 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 388 |
off=length1; |
| 389 |
break; |
| 390 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 391 |
off=lengthDPCM; |
| 392 |
break; |
| 393 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 394 |
off=lengthZ; |
| 395 |
break; |
| 396 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: |
| 397 |
off=lengthQSoundA; |
| 398 |
break; |
| 399 |
case DIV_SAMPLE_DEPTH_ADPCM_A: |
| 400 |
off=lengthA; |
| 401 |
break; |
| 402 |
case DIV_SAMPLE_DEPTH_ADPCM_B: |
| 403 |
off=lengthB; |
| 404 |
break; |
| 405 |
case DIV_SAMPLE_DEPTH_ADPCM_K: |
| 406 |
off=lengthK; |
| 407 |
break; |
| 408 |
case DIV_SAMPLE_DEPTH_8BIT: |
| 409 |
off=length8; |
| 410 |
break; |
| 411 |
case DIV_SAMPLE_DEPTH_BRR: |
| 412 |
off=lengthBRR; |
| 413 |
break; |
| 414 |
case DIV_SAMPLE_DEPTH_VOX: |
| 415 |
off=lengthVOX; |
| 416 |
break; |
| 417 |
case DIV_SAMPLE_DEPTH_MULAW: |
| 418 |
off=lengthMuLaw; |
| 419 |
break; |
| 420 |
case DIV_SAMPLE_DEPTH_C219: |
| 421 |
off=lengthC219; |
| 422 |
break; |
| 423 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 424 |
off=lengthIMA; |
| 425 |
break; |
| 426 |
case DIV_SAMPLE_DEPTH_12BIT: |
| 427 |
off=length12; |
| 428 |
break; |
| 429 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 430 |
off=length4; |
| 431 |
break; |
| 432 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 433 |
off=length16; |
| 434 |
break; |
| 435 |
default: |
| 436 |
break; |
| 437 |
} |
| 438 |
return off; |
| 439 |
} |
| 440 |
|
| 441 |
void DivSample::setSampleCount(unsigned int count) { |
| 442 |
samples=count; |
| 443 |
if ((!isLoopable()) || loopEnd<0 || loopEnd>(int)samples) loopEnd=samples; |
| 444 |
} |
| 445 |
|
| 446 |
bool DivSample::save(const char* path) { |
| 447 |
#ifndef HAVE_SNDFILE |
| 448 |
logE("Furnace was not compiled with libsndfile!"); |
| 449 |
return false; |
| 450 |
#else |
| 451 |
SNDFILE* f; |
| 452 |
SF_INFO si; |
| 453 |
SFWrapper sfWrap; |
| 454 |
memset(&si,0,sizeof(SF_INFO)); |
| 455 |
|
| 456 |
if (length16<1) return false; |
| 457 |
|
| 458 |
si.channels=1; |
| 459 |
si.samplerate=centerRate; |
| 460 |
switch (depth) { |
| 461 |
case DIV_SAMPLE_DEPTH_8BIT: // 8-bit |
| 462 |
si.format=SF_FORMAT_PCM_U8|SF_FORMAT_WAV; |
| 463 |
break; |
| 464 |
default: // 16-bit |
| 465 |
si.format=SF_FORMAT_PCM_16|SF_FORMAT_WAV; |
| 466 |
break; |
| 467 |
} |
| 468 |
|
| 469 |
f=sfWrap.doOpen(path,SFM_WRITE,&si); |
| 470 |
|
| 471 |
if (f==NULL) { |
| 472 |
logE("could not open wave file for saving! %s",sf_error_number(sf_error(f))); |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
SF_INSTRUMENT inst; |
| 477 |
memset(&inst, 0, sizeof(inst)); |
| 478 |
inst.gain = 1; |
| 479 |
// TODO: fix |
| 480 |
short pitch = (0x3c * 100) + 50 - (log2((double)centerRate/8363.0) * 12.0 * 100.0); |
| 481 |
inst.basenote = pitch / 100; |
| 482 |
inst.detune = 50 - (pitch % 100); |
| 483 |
inst.velocity_hi = 0x7f; |
| 484 |
inst.key_hi = 0x7f; |
| 485 |
if(isLoopable()) |
| 486 |
{ |
| 487 |
inst.loop_count = 1; |
| 488 |
inst.loops[0].mode = (int)loopMode+SF_LOOP_FORWARD; |
| 489 |
inst.loops[0].start = loopStart; |
| 490 |
inst.loops[0].end = loopEnd; |
| 491 |
} |
| 492 |
sf_command(f, SFC_SET_INSTRUMENT, &inst, sizeof(inst)); |
| 493 |
|
| 494 |
switch (depth) { |
| 495 |
case DIV_SAMPLE_DEPTH_8BIT: { |
| 496 |
// convert from signed to unsigned |
| 497 |
unsigned char* buf=new unsigned char[length8]; |
| 498 |
for (size_t i=0; i<length8; i++) { |
| 499 |
buf[i]=data8[i]^0x80; |
| 500 |
} |
| 501 |
sf_write_raw(f,buf,length8); |
| 502 |
delete[] buf; |
| 503 |
break; |
| 504 |
} |
| 505 |
default: |
| 506 |
sf_writef_short(f,data16,samples); |
| 507 |
break; |
| 508 |
} |
| 509 |
|
| 510 |
sfWrap.doClose(); |
| 511 |
|
| 512 |
return true; |
| 513 |
#endif |
| 514 |
} |
| 515 |
|
| 516 |
bool DivSample::saveRaw(const char* path) { |
| 517 |
if (samples<1) { |
| 518 |
logE("sample is empty though!"); |
| 519 |
return false; |
| 520 |
} |
| 521 |
|
| 522 |
FILE* f=ps_fopen(path,"wb"); |
| 523 |
if (f==NULL) { |
| 524 |
logE("could not save sample: %s!",strerror(errno)); |
| 525 |
return false; |
| 526 |
} |
| 527 |
if (depth==DIV_SAMPLE_DEPTH_BRR) { |
| 528 |
if (isLoopable()) { |
| 529 |
unsigned short loopPos=getLoopStartPosition(DIV_SAMPLE_DEPTH_BRR); |
| 530 |
fputc(loopPos&0xff,f); |
| 531 |
fputc(loopPos>>8,f); |
| 532 |
} else { |
| 533 |
fputc(0,f); |
| 534 |
fputc(0,f); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
if (fwrite(getCurBuf(),1,getCurBufLen(),f)!=getCurBufLen()) { |
| 539 |
logW("did not write entire instrument!"); |
| 540 |
} |
| 541 |
fclose(f); |
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
// 16-bit memory is padded to 512, to make things easier for ADPCM-A/B. |
| 546 |
bool DivSample::initInternal(DivSampleDepth d, int count) { |
| 547 |
logV("initInternal(%d,%d)",(int)d,count); |
| 548 |
switch (d) { |
| 549 |
case DIV_SAMPLE_DEPTH_1BIT: // 1-bit |
| 550 |
if (data1!=NULL) delete[] data1; |
| 551 |
length1=(count+7)/8; |
| 552 |
data1=new unsigned char[length1]; |
| 553 |
memset(data1,0,length1); |
| 554 |
break; |
| 555 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: // DPCM |
| 556 |
if (dataDPCM!=NULL) delete[] dataDPCM; |
| 557 |
lengthDPCM=1+((((count-1)/8)+15)&(~15)); |
| 558 |
dataDPCM=new unsigned char[lengthDPCM]; |
| 559 |
memset(dataDPCM,0xaa,lengthDPCM); |
| 560 |
break; |
| 561 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: // YMZ ADPCM |
| 562 |
if (dataZ!=NULL) delete[] dataZ; |
| 563 |
lengthZ=(count+1)/2; |
| 564 |
// for padding AICA sample |
| 565 |
dataZ=new unsigned char[(lengthZ+3)&(~0x03)]; |
| 566 |
memset(dataZ,0,(lengthZ+3)&(~0x03)); |
| 567 |
break; |
| 568 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: // QSound ADPCM |
| 569 |
if (dataQSoundA!=NULL) delete[] dataQSoundA; |
| 570 |
lengthQSoundA=(count+1)/2; |
| 571 |
dataQSoundA=new unsigned char[lengthQSoundA]; |
| 572 |
memset(dataQSoundA,0,lengthQSoundA); |
| 573 |
break; |
| 574 |
case DIV_SAMPLE_DEPTH_ADPCM_A: // ADPCM-A |
| 575 |
if (dataA!=NULL) delete[] dataA; |
| 576 |
lengthA=(count+1)/2; |
| 577 |
dataA=new unsigned char[(lengthA+255)&(~0xff)]; |
| 578 |
memset(dataA,0x80,(lengthA+255)&(~0xff)); |
| 579 |
break; |
| 580 |
case DIV_SAMPLE_DEPTH_ADPCM_B: // ADPCM-B |
| 581 |
if (dataB!=NULL) delete[] dataB; |
| 582 |
lengthB=(count+1)/2; |
| 583 |
dataB=new unsigned char[(lengthB+255)&(~0xff)]; |
| 584 |
memset(dataB,0x80,(lengthB+255)&(~0xff)); |
| 585 |
break; |
| 586 |
case DIV_SAMPLE_DEPTH_ADPCM_K: // K05 ADPCM |
| 587 |
if (dataK!=NULL) delete[] dataK; |
| 588 |
lengthK=(count+1)/2; |
| 589 |
dataK=new unsigned char[(lengthK+255)&(~0xff)]; |
| 590 |
memset(dataK,0,(lengthK+255)&(~0xff)); |
| 591 |
break; |
| 592 |
case DIV_SAMPLE_DEPTH_8BIT: // 8-bit |
| 593 |
if (data8!=NULL) delete[] data8; |
| 594 |
length8=count; |
| 595 |
// for padding X1-010 sample |
| 596 |
data8=new signed char[(count+4095)&(~0xfff)]; |
| 597 |
memset(data8,0,(count+4095)&(~0xfff)); |
| 598 |
break; |
| 599 |
case DIV_SAMPLE_DEPTH_BRR: // BRR |
| 600 |
if (dataBRR!=NULL) delete[] dataBRR; |
| 601 |
lengthBRR=9*((count+15)/16); |
| 602 |
dataBRR=new unsigned char[lengthBRR+9]; |
| 603 |
memset(dataBRR,0,lengthBRR+9); |
| 604 |
break; |
| 605 |
case DIV_SAMPLE_DEPTH_VOX: // VOX |
| 606 |
if (dataVOX!=NULL) delete[] dataVOX; |
| 607 |
lengthVOX=(count+1)/2; |
| 608 |
dataVOX=new unsigned char[lengthVOX]; |
| 609 |
memset(dataVOX,0,lengthVOX); |
| 610 |
break; |
| 611 |
case DIV_SAMPLE_DEPTH_MULAW: // 8-bit µ-law |
| 612 |
if (dataMuLaw!=NULL) delete[] dataMuLaw; |
| 613 |
lengthMuLaw=count; |
| 614 |
dataMuLaw=new unsigned char[(count+4095)&(~0xfff)]; |
| 615 |
memset(dataMuLaw,0,(count+4095)&(~0xfff)); |
| 616 |
break; |
| 617 |
case DIV_SAMPLE_DEPTH_C219: // 8-bit C219 "μ-law" |
| 618 |
if (dataC219!=NULL) delete[] dataC219; |
| 619 |
lengthC219=count; |
| 620 |
dataC219=new unsigned char[(count+4095)&(~0xfff)]; |
| 621 |
memset(dataC219,0,(count+4095)&(~0xfff)); |
| 622 |
break; |
| 623 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: // IMA ADPCM |
| 624 |
if (dataIMA!=NULL) delete[] dataIMA; |
| 625 |
lengthIMA=4+((count+1)/2); |
| 626 |
dataIMA=new unsigned char[lengthIMA]; |
| 627 |
memset(dataIMA,0,lengthIMA); |
| 628 |
break; |
| 629 |
case DIV_SAMPLE_DEPTH_12BIT: // 12-bit PCM (MultiPCM) |
| 630 |
if (data12!=NULL) delete[] data12; |
| 631 |
length12=((count*3)+1)/2; |
| 632 |
data12=new unsigned char[length12+8]; |
| 633 |
memset(data12,0,length12+8); |
| 634 |
break; |
| 635 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 636 |
if (data4!=NULL) delete[] data4; |
| 637 |
length4=(count+1)/2; |
| 638 |
data4=new unsigned char[length4]; |
| 639 |
memset(data4,0,length4); |
| 640 |
break; |
| 641 |
case DIV_SAMPLE_DEPTH_16BIT: // 16-bit |
| 642 |
if (data16!=NULL) delete[] data16; |
| 643 |
length16=count*2; |
| 644 |
data16=new short[(count+511)&(~0x1ff)]; |
| 645 |
memset(data16,0,((count+511)&(~0x1ff))*sizeof(short)); |
| 646 |
break; |
| 647 |
default: |
| 648 |
return false; |
| 649 |
} |
| 650 |
return true; |
| 651 |
} |
| 652 |
|
| 653 |
bool DivSample::init(unsigned int count) { |
| 654 |
if (!initInternal(depth,count)) return false; |
| 655 |
setSampleCount(count); |
| 656 |
return true; |
| 657 |
} |
| 658 |
|
| 659 |
bool DivSample::resize(unsigned int count) { |
| 660 |
if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 661 |
if (data8!=NULL) { |
| 662 |
signed char* oldData8=data8; |
| 663 |
data8=NULL; |
| 664 |
initInternal(DIV_SAMPLE_DEPTH_8BIT,count); |
| 665 |
memcpy(data8,oldData8,MIN(count,samples)); |
| 666 |
delete[] oldData8; |
| 667 |
} else { |
| 668 |
initInternal(DIV_SAMPLE_DEPTH_8BIT,count); |
| 669 |
} |
| 670 |
setSampleCount(count); |
| 671 |
return true; |
| 672 |
} else if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 673 |
if (data16!=NULL) { |
| 674 |
short* oldData16=data16; |
| 675 |
data16=NULL; |
| 676 |
initInternal(DIV_SAMPLE_DEPTH_16BIT,count); |
| 677 |
memcpy(data16,oldData16,sizeof(short)*MIN(count,samples)); |
| 678 |
delete[] oldData16; |
| 679 |
} else { |
| 680 |
initInternal(DIV_SAMPLE_DEPTH_16BIT,count); |
| 681 |
} |
| 682 |
setSampleCount(count); |
| 683 |
return true; |
| 684 |
} |
| 685 |
return false; |
| 686 |
} |
| 687 |
|
| 688 |
bool DivSample::strip(unsigned int begin, unsigned int end) { |
| 689 |
if (begin>samples) begin=samples; |
| 690 |
if (end>samples) end=samples; |
| 691 |
int count=samples-(end-begin); |
| 692 |
if (count<=0) { |
| 693 |
loopStart=-1; |
| 694 |
loopEnd=-1; |
| 695 |
loop=false; |
| 696 |
return resize(0); |
| 697 |
} |
| 698 |
if (loopStart>(int)begin && loopEnd<(int)end) { |
| 699 |
loopStart=-1; |
| 700 |
loopEnd=-1; |
| 701 |
loop=false; |
| 702 |
} else { |
| 703 |
if (loopStart<(int)end && loopStart>(int)begin) { |
| 704 |
loopStart=end; |
| 705 |
} |
| 706 |
if (loopStart>(int)begin && loopEnd>(int)begin) { |
| 707 |
loopStart-=end-begin; |
| 708 |
loopEnd-=end-begin; |
| 709 |
if (loopEnd<0) loopEnd=0; |
| 710 |
if (loopStart<0) loopStart=0; |
| 711 |
} else if (loopEnd>(int)begin) { |
| 712 |
loopEnd=begin; |
| 713 |
} |
| 714 |
} |
| 715 |
if (loopStart>loopEnd) { |
| 716 |
loopStart=-1; |
| 717 |
loopEnd=-1; |
| 718 |
loop=false; |
| 719 |
} |
| 720 |
if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 721 |
if (data8!=NULL) { |
| 722 |
signed char* oldData8=data8; |
| 723 |
data8=NULL; |
| 724 |
initInternal(DIV_SAMPLE_DEPTH_8BIT,count); |
| 725 |
if (begin>0) { |
| 726 |
memcpy(data8,oldData8,begin); |
| 727 |
} |
| 728 |
if (samples-end>0) { |
| 729 |
memcpy(data8+begin,oldData8+end,samples-end); |
| 730 |
} |
| 731 |
delete[] oldData8; |
| 732 |
} else { |
| 733 |
// do nothing |
| 734 |
return true; |
| 735 |
} |
| 736 |
setSampleCount(count); |
| 737 |
return true; |
| 738 |
} else if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 739 |
if (data16!=NULL) { |
| 740 |
short* oldData16=data16; |
| 741 |
data16=NULL; |
| 742 |
initInternal(DIV_SAMPLE_DEPTH_16BIT,count); |
| 743 |
if (begin>0) { |
| 744 |
memcpy(data16,oldData16,sizeof(short)*begin); |
| 745 |
} |
| 746 |
if (samples-end>0) { |
| 747 |
memcpy(&(data16[begin]),&(oldData16[end]),sizeof(short)*(samples-end)); |
| 748 |
} |
| 749 |
delete[] oldData16; |
| 750 |
} else { |
| 751 |
// do nothing |
| 752 |
return true; |
| 753 |
} |
| 754 |
setSampleCount(count); |
| 755 |
return true; |
| 756 |
} |
| 757 |
return false; |
| 758 |
} |
| 759 |
|
| 760 |
bool DivSample::trim(unsigned int begin, unsigned int end) { |
| 761 |
int count=end-begin; |
| 762 |
if (count==0) return true; |
| 763 |
if (begin==0 && end==samples) return true; |
| 764 |
if (((int)begin<loopStart && (int)end<loopStart) || ((int)begin>loopEnd && (int)end>loopEnd)) { |
| 765 |
loopStart=-1; |
| 766 |
loopEnd=-1; |
| 767 |
loop=false; |
| 768 |
} else { |
| 769 |
loopStart-=begin; |
| 770 |
loopEnd-=begin; |
| 771 |
if (loopStart<0) loopStart=0; |
| 772 |
if (loopEnd>count) loopEnd=count; |
| 773 |
} |
| 774 |
if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 775 |
if (data8!=NULL) { |
| 776 |
signed char* oldData8=data8; |
| 777 |
data8=NULL; |
| 778 |
initInternal(DIV_SAMPLE_DEPTH_8BIT,count); |
| 779 |
memcpy(data8,oldData8+begin,count); |
| 780 |
delete[] oldData8; |
| 781 |
} else { |
| 782 |
// do nothing |
| 783 |
return true; |
| 784 |
} |
| 785 |
setSampleCount(count); |
| 786 |
return true; |
| 787 |
} else if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 788 |
if (data16!=NULL) { |
| 789 |
short* oldData16=data16; |
| 790 |
data16=NULL; |
| 791 |
initInternal(DIV_SAMPLE_DEPTH_16BIT,count); |
| 792 |
memcpy(data16,&(oldData16[begin]),sizeof(short)*count); |
| 793 |
delete[] oldData16; |
| 794 |
} else { |
| 795 |
// do nothing |
| 796 |
return true; |
| 797 |
} |
| 798 |
setSampleCount(count); |
| 799 |
return true; |
| 800 |
} |
| 801 |
return false; |
| 802 |
} |
| 803 |
|
| 804 |
bool DivSample::insert(unsigned int pos, unsigned int length) { |
| 805 |
unsigned int count=samples+length; |
| 806 |
if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 807 |
if (data8!=NULL) { |
| 808 |
signed char* oldData8=data8; |
| 809 |
data8=NULL; |
| 810 |
initInternal(DIV_SAMPLE_DEPTH_8BIT,count); |
| 811 |
if (pos>0) { |
| 812 |
memcpy(data8,oldData8,pos); |
| 813 |
} |
| 814 |
if (count-pos-length>0) { |
| 815 |
memcpy(data8+pos+length,oldData8+pos,count-pos-length); |
| 816 |
} |
| 817 |
delete[] oldData8; |
| 818 |
} else { |
| 819 |
initInternal(DIV_SAMPLE_DEPTH_8BIT,count); |
| 820 |
} |
| 821 |
setSampleCount(count); |
| 822 |
return true; |
| 823 |
} else if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 824 |
if (data16!=NULL) { |
| 825 |
short* oldData16=data16; |
| 826 |
data16=NULL; |
| 827 |
initInternal(DIV_SAMPLE_DEPTH_16BIT,count); |
| 828 |
if (pos>0) { |
| 829 |
memcpy(data16,oldData16,sizeof(short)*pos); |
| 830 |
} |
| 831 |
if (count-pos-length>0) { |
| 832 |
memcpy(&(data16[pos+length]),&(oldData16[pos]),sizeof(short)*(count-pos-length)); |
| 833 |
} |
| 834 |
delete[] oldData16; |
| 835 |
} else { |
| 836 |
initInternal(DIV_SAMPLE_DEPTH_16BIT,count); |
| 837 |
} |
| 838 |
setSampleCount(count); |
| 839 |
return true; |
| 840 |
} |
| 841 |
return false; |
| 842 |
} |
| 843 |
|
| 844 |
void DivSample::convert(DivSampleDepth newDepth, unsigned int formatMask) { |
| 845 |
render(formatMask|(1U<<newDepth)); |
| 846 |
depth=newDepth; |
| 847 |
switch (depth) { |
| 848 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 849 |
setSampleCount((samples+7)&(~7)); |
| 850 |
break; |
| 851 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 852 |
if (samples) { |
| 853 |
setSampleCount((1+((((samples-1)/8)+15)&(~15)))<<3); |
| 854 |
} else { |
| 855 |
setSampleCount(8); |
| 856 |
} |
| 857 |
break; |
| 858 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 859 |
setSampleCount(((lengthZ+3)&(~0x03))*2); |
| 860 |
break; |
| 861 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: // QSound ADPCM |
| 862 |
setSampleCount((samples+1)&(~1)); |
| 863 |
break; |
| 864 |
case DIV_SAMPLE_DEPTH_ADPCM_A: // ADPCM-A |
| 865 |
setSampleCount((samples+1)&(~1)); |
| 866 |
break; |
| 867 |
case DIV_SAMPLE_DEPTH_ADPCM_B: // ADPCM-B |
| 868 |
setSampleCount((samples+1)&(~1)); |
| 869 |
break; |
| 870 |
case DIV_SAMPLE_DEPTH_ADPCM_K: // K05 ADPCM |
| 871 |
setSampleCount((samples+1)&(~1)); |
| 872 |
break; |
| 873 |
case DIV_SAMPLE_DEPTH_BRR: // BRR |
| 874 |
setSampleCount(16*(lengthBRR/9)); |
| 875 |
break; |
| 876 |
case DIV_SAMPLE_DEPTH_VOX: // VOX |
| 877 |
setSampleCount((samples+1)&(~1)); |
| 878 |
break; |
| 879 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 880 |
setSampleCount((samples+1)&(~1)); |
| 881 |
break; |
| 882 |
default: |
| 883 |
break; |
| 884 |
} |
| 885 |
render(formatMask|(1U<<newDepth)); |
| 886 |
} |
| 887 |
|
| 888 |
#define RESAMPLE_BEGIN \ |
| 889 |
if (samples<1) return true; \ |
| 890 |
int finalCount=(double)samples*(tRate/sRate); \ |
| 891 |
signed char* oldData8=data8; \ |
| 892 |
short* oldData16=data16; \ |
| 893 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { \ |
| 894 |
if (data16!=NULL) { \ |
| 895 |
data16=NULL; \ |
| 896 |
initInternal(DIV_SAMPLE_DEPTH_16BIT,finalCount); \ |
| 897 |
} \ |
| 898 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { \ |
| 899 |
if (data8!=NULL) { \ |
| 900 |
data8=NULL; \ |
| 901 |
initInternal(DIV_SAMPLE_DEPTH_8BIT,finalCount); \ |
| 902 |
} \ |
| 903 |
} else { \ |
| 904 |
return false; \ |
| 905 |
} |
| 906 |
|
| 907 |
#define RESAMPLE_END \ |
| 908 |
if (loopStart>=0) loopStart=(double)loopStart*(tRate/sRate); \ |
| 909 |
if (loopEnd>=0) loopEnd=(double)loopEnd*(tRate/sRate); \ |
| 910 |
centerRate=(int)((double)centerRate*(tRate/sRate)); \ |
| 911 |
rate=(int)((double)rate*(tRate/sRate)); \ |
| 912 |
samples=finalCount; \ |
| 913 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { \ |
| 914 |
delete[] oldData16; \ |
| 915 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { \ |
| 916 |
delete[] oldData8; \ |
| 917 |
} |
| 918 |
|
| 919 |
bool DivSample::resampleNone(double sRate, double tRate) { |
| 920 |
RESAMPLE_BEGIN; |
| 921 |
|
| 922 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 923 |
for (int i=0; i<finalCount; i++) { |
| 924 |
unsigned int pos=(unsigned int)((double)i*(sRate/tRate)); |
| 925 |
if (pos>=samples) { |
| 926 |
data16[i]=0; |
| 927 |
} else { |
| 928 |
data16[i]=oldData16[pos]; |
| 929 |
} |
| 930 |
} |
| 931 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 932 |
for (int i=0; i<finalCount; i++) { |
| 933 |
unsigned int pos=(unsigned int)((double)i*(sRate/tRate)); |
| 934 |
if (pos>=samples) { |
| 935 |
data8[i]=0; |
| 936 |
} else { |
| 937 |
data8[i]=oldData8[pos]; |
| 938 |
} |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
RESAMPLE_END; |
| 943 |
return true; |
| 944 |
} |
| 945 |
|
| 946 |
bool DivSample::resampleLinear(double sRate, double tRate) { |
| 947 |
RESAMPLE_BEGIN; |
| 948 |
|
| 949 |
double posFrac=0; |
| 950 |
unsigned int posInt=0; |
| 951 |
double factor=sRate/tRate; |
| 952 |
|
| 953 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 954 |
for (int i=0; i<finalCount; i++) { |
| 955 |
short s1=(posInt>=samples)?0:oldData16[posInt]; |
| 956 |
short s2=(posInt+1>=samples)?((loopStart>=0 && loopStart<(int)samples)?oldData16[loopStart]:0):oldData16[posInt+1]; |
| 957 |
|
| 958 |
data16[i]=s1+(float)(s2-s1)*posFrac; |
| 959 |
|
| 960 |
posFrac+=factor; |
| 961 |
while (posFrac>=1.0) { |
| 962 |
posFrac-=1.0; |
| 963 |
posInt++; |
| 964 |
} |
| 965 |
} |
| 966 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 967 |
for (int i=0; i<finalCount; i++) { |
| 968 |
short s1=(posInt>=samples)?0:oldData8[posInt]; |
| 969 |
short s2=(posInt+1>=samples)?((loopStart>=0 && loopStart<(int)samples)?oldData8[loopStart]:0):oldData8[posInt+1]; |
| 970 |
|
| 971 |
data8[i]=s1+(float)(s2-s1)*posFrac; |
| 972 |
|
| 973 |
posFrac+=factor; |
| 974 |
while (posFrac>=1.0) { |
| 975 |
posFrac-=1.0; |
| 976 |
posInt++; |
| 977 |
} |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
RESAMPLE_END; |
| 982 |
return true; |
| 983 |
} |
| 984 |
|
| 985 |
bool DivSample::resampleCubic(double sRate, double tRate) { |
| 986 |
RESAMPLE_BEGIN; |
| 987 |
|
| 988 |
double posFrac=0; |
| 989 |
unsigned int posInt=0; |
| 990 |
double factor=sRate/tRate; |
| 991 |
float* cubicTable=DivFilterTables::getCubicTable(); |
| 992 |
|
| 993 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 994 |
for (int i=0; i<finalCount; i++) { |
| 995 |
unsigned int n=((unsigned int)(posFrac*1024.0))&1023; |
| 996 |
float* t=&cubicTable[n<<2]; |
| 997 |
float s0=(posInt<1)?0:oldData16[posInt-1]; |
| 998 |
float s1=(posInt>=samples)?0:oldData16[posInt]; |
| 999 |
float s2=(posInt+1>=samples)?((loopStart>=0 && loopStart<(int)samples)?oldData16[loopStart]:0):oldData16[posInt+1]; |
| 1000 |
float s3=(posInt+2>=samples)?((loopStart>=0 && loopStart<(int)samples)?oldData16[loopStart]:0):oldData16[posInt+2]; |
| 1001 |
|
| 1002 |
float result=s0*t[0]+s1*t[1]+s2*t[2]+s3*t[3]; |
| 1003 |
if (result<-32768) result=-32768; |
| 1004 |
if (result>32767) result=32767; |
| 1005 |
data16[i]=result; |
| 1006 |
|
| 1007 |
posFrac+=factor; |
| 1008 |
while (posFrac>=1.0) { |
| 1009 |
posFrac-=1.0; |
| 1010 |
posInt++; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 1014 |
for (int i=0; i<finalCount; i++) { |
| 1015 |
unsigned int n=((unsigned int)(posFrac*1024.0))&1023; |
| 1016 |
float* t=&cubicTable[n<<2]; |
| 1017 |
float s0=(posInt<1)?0:oldData8[posInt-1]; |
| 1018 |
float s1=(posInt>=samples)?0:oldData8[posInt]; |
| 1019 |
float s2=(posInt+1>=samples)?((loopStart>=0 && loopStart<(int)samples)?oldData8[loopStart]:0):oldData8[posInt+1]; |
| 1020 |
float s3=(posInt+2>=samples)?((loopStart>=0 && loopStart<(int)samples)?oldData8[loopStart]:0):oldData8[posInt+2]; |
| 1021 |
|
| 1022 |
float result=s0*t[0]+s1*t[1]+s2*t[2]+s3*t[3]; |
| 1023 |
if (result<-128) result=-128; |
| 1024 |
if (result>127) result=127; |
| 1025 |
data8[i]=result; |
| 1026 |
|
| 1027 |
posFrac+=factor; |
| 1028 |
while (posFrac>=1.0) { |
| 1029 |
posFrac-=1.0; |
| 1030 |
posInt++; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
RESAMPLE_END; |
| 1036 |
return true; |
| 1037 |
} |
| 1038 |
|
| 1039 |
bool DivSample::resampleBlep(double sRate, double tRate) { |
| 1040 |
RESAMPLE_BEGIN; |
| 1041 |
|
| 1042 |
double posFrac=0; |
| 1043 |
unsigned int posInt=0; |
| 1044 |
double factor=tRate/sRate; |
| 1045 |
float* sincITable=DivFilterTables::getSincIntegralTable(); |
| 1046 |
|
| 1047 |
float* floatData=new float[finalCount]; |
| 1048 |
memset(floatData,0,finalCount*sizeof(float)); |
| 1049 |
|
| 1050 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 1051 |
memset(data16,0,finalCount*sizeof(short)); |
| 1052 |
for (int i=0; i<finalCount; i++) { |
| 1053 |
if (posInt<samples) { |
| 1054 |
data16[i]=oldData16[posInt]; |
| 1055 |
} |
| 1056 |
|
| 1057 |
posFrac+=1.0; |
| 1058 |
while (posFrac>=1.0) { |
| 1059 |
unsigned int n=((unsigned int)(posFrac*8192.0))&8191; |
| 1060 |
posFrac-=factor; |
| 1061 |
posInt++; |
| 1062 |
|
| 1063 |
float* t1=&sincITable[(8191-n)<<3]; |
| 1064 |
float* t2=&sincITable[n<<3]; |
| 1065 |
float delta=oldData16[posInt]-oldData16[posInt-1]; |
| 1066 |
|
| 1067 |
for (int j=0; j<8; j++) { |
| 1068 |
if (i-j>0) { |
| 1069 |
floatData[i-j]+=t1[j]*-delta; |
| 1070 |
} |
| 1071 |
if (i+j+1<finalCount) { |
| 1072 |
floatData[i+j+1]+=t2[j]*delta; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
for (int i=0; i<finalCount; i++) { |
| 1078 |
float result=floatData[i]+data16[i]; |
| 1079 |
if (result<-32768) result=-32768; |
| 1080 |
if (result>32767) result=32767; |
| 1081 |
data16[i]=round(result); |
| 1082 |
} |
| 1083 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 1084 |
memset(data8,0,finalCount); |
| 1085 |
for (int i=0; i<finalCount; i++) { |
| 1086 |
if (posInt<samples) { |
| 1087 |
data8[i]=oldData8[posInt]; |
| 1088 |
} |
| 1089 |
|
| 1090 |
posFrac+=1.0; |
| 1091 |
while (posFrac>=1.0) { |
| 1092 |
unsigned int n=((unsigned int)(posFrac*8192.0))&8191; |
| 1093 |
posFrac-=factor; |
| 1094 |
posInt++; |
| 1095 |
|
| 1096 |
float* t1=&sincITable[(8191-n)<<3]; |
| 1097 |
float* t2=&sincITable[n<<3]; |
| 1098 |
float delta=oldData8[posInt]-oldData8[posInt-1]; |
| 1099 |
|
| 1100 |
for (int j=0; j<8; j++) { |
| 1101 |
if (i-j>0) { |
| 1102 |
floatData[i-j]+=t1[j]*-delta; |
| 1103 |
} |
| 1104 |
if (i+j+1<finalCount) { |
| 1105 |
floatData[i+j+1]+=t2[j]*delta; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
for (int i=0; i<finalCount; i++) { |
| 1111 |
float result=floatData[i]+data8[i]; |
| 1112 |
if (result<-128) result=-128; |
| 1113 |
if (result>127) result=127; |
| 1114 |
data8[i]=round(result); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
delete[] floatData; |
| 1118 |
|
| 1119 |
RESAMPLE_END; |
| 1120 |
return true; |
| 1121 |
} |
| 1122 |
|
| 1123 |
bool DivSample::resampleSinc(double sRate, double tRate) { |
| 1124 |
RESAMPLE_BEGIN; |
| 1125 |
|
| 1126 |
double posFrac=0; |
| 1127 |
unsigned int posInt=0; |
| 1128 |
double factor=sRate/tRate; |
| 1129 |
float* sincTable=DivFilterTables::getSincTable(); |
| 1130 |
float s[16]; |
| 1131 |
|
| 1132 |
memset(s,0,16*sizeof(float)); |
| 1133 |
|
| 1134 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 1135 |
for (int i=0; i<finalCount+8; i++) { |
| 1136 |
unsigned int n=((unsigned int)(posFrac*8192.0))&8191; |
| 1137 |
float result=0; |
| 1138 |
float* t1=&sincTable[(8191-n)<<3]; |
| 1139 |
float* t2=&sincTable[n<<3]; |
| 1140 |
for (int j=0; j<8; j++) { |
| 1141 |
result+=s[j]*t2[7-j]; |
| 1142 |
result+=s[8+j]*t1[j]; |
| 1143 |
} |
| 1144 |
if (result<-32768) result=-32768; |
| 1145 |
if (result>32767) result=32767; |
| 1146 |
if (i>=8) { |
| 1147 |
data16[i-8]=result; |
| 1148 |
} |
| 1149 |
|
| 1150 |
posFrac+=factor; |
| 1151 |
while (posFrac>=1.0) { |
| 1152 |
posFrac-=1.0; |
| 1153 |
posInt++; |
| 1154 |
|
| 1155 |
for (int j=0; j<15; j++) s[j]=s[j+1]; |
| 1156 |
s[15]=(posInt>=samples)?0:oldData16[posInt]; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 1160 |
for (int i=0; i<finalCount+8; i++) { |
| 1161 |
unsigned int n=((unsigned int)(posFrac*8192.0))&8191; |
| 1162 |
float result=0; |
| 1163 |
float* t1=&sincTable[(8191-n)<<3]; |
| 1164 |
float* t2=&sincTable[n<<3]; |
| 1165 |
for (int j=0; j<8; j++) { |
| 1166 |
result+=s[j]*t2[7-j]; |
| 1167 |
result+=s[8+j]*t1[j]; |
| 1168 |
} |
| 1169 |
if (result<-128) result=-128; |
| 1170 |
if (result>127) result=127; |
| 1171 |
if (i>=8) { |
| 1172 |
data8[i-8]=result; |
| 1173 |
} |
| 1174 |
|
| 1175 |
posFrac+=factor; |
| 1176 |
while (posFrac>=1.0) { |
| 1177 |
posFrac-=1.0; |
| 1178 |
posInt++; |
| 1179 |
|
| 1180 |
for (int j=0; j<15; j++) s[j]=s[j+1]; |
| 1181 |
s[15]=(posInt>=samples)?0:oldData8[posInt]; |
| 1182 |
} |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
RESAMPLE_END; |
| 1187 |
return true; |
| 1188 |
} |
| 1189 |
|
| 1190 |
bool DivSample::resample(double sRate, double tRate, int filter) { |
| 1191 |
if (depth!=DIV_SAMPLE_DEPTH_8BIT && depth!=DIV_SAMPLE_DEPTH_16BIT) return false; |
| 1192 |
if (tRate<100) return false; |
| 1193 |
switch (filter) { |
| 1194 |
case DIV_RESAMPLE_NONE: |
| 1195 |
return resampleNone(sRate,tRate); |
| 1196 |
break; |
| 1197 |
case DIV_RESAMPLE_LINEAR: |
| 1198 |
return resampleLinear(sRate,tRate); |
| 1199 |
break; |
| 1200 |
case DIV_RESAMPLE_CUBIC: |
| 1201 |
return resampleCubic(sRate,tRate); |
| 1202 |
break; |
| 1203 |
case DIV_RESAMPLE_BLEP: |
| 1204 |
return resampleBlep(sRate,tRate); |
| 1205 |
break; |
| 1206 |
case DIV_RESAMPLE_SINC: |
| 1207 |
return resampleSinc(sRate,tRate); |
| 1208 |
break; |
| 1209 |
case DIV_RESAMPLE_BEST: |
| 1210 |
if (tRate>sRate) { |
| 1211 |
return resampleSinc(sRate,tRate); |
| 1212 |
} else { |
| 1213 |
return resampleBlep(sRate,tRate); |
| 1214 |
} |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
return false; |
| 1218 |
} |
| 1219 |
|
| 1220 |
#define NOT_IN_FORMAT(x) (depth!=x && formatMask&(1U<<(unsigned int)x)) |
| 1221 |
|
| 1222 |
union IntFloat { |
| 1223 |
unsigned int i; |
| 1224 |
float f; |
| 1225 |
}; |
| 1226 |
|
| 1227 |
const short c219Table[256]={ |
| 1228 |
0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480, |
| 1229 |
512, 576, 640, 704, 768, 832, 896, 960, 1024, 1152, 1280, 1408, 1536, 1664, 1792, 1920, |
| 1230 |
2048, 2176, 2304, 2432, 2560, 2688, 2816, 2944, 3072, 3200, 3328, 3456, 3584, 3712, 3840, 3968, |
| 1231 |
4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, |
| 1232 |
8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, |
| 1233 |
12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, |
| 1234 |
16384, 16640, 16896, 17152, 17408, 17920, 18432, 18944, 19456, 19968, 20480, 20992, 21504, 22016, 22528, 23040, |
| 1235 |
23552, 24064, 24576, 25088, 25600, 26112, 26624, 27136, 27648, 28160, 28672, 29184, 29696, 30208, 30720, 31232, |
| 1236 |
-32, -64, -96, -128, -160, -192, -224, -256, -288, -320, -352, -384, -416, -448, -480, -512, |
| 1237 |
-544, -608, -672, -736, -800, -864, -928, -992, -1056, -1184, -1312, -1440, -1568, -1696, -1824, -1952, |
| 1238 |
-2080, -2208, -2336, -2464, -2592, -2720, -2848, -2976, -3104, -3232, -3360, -3488, -3616, -3744, -3872, -4000, |
| 1239 |
-4128, -4384, -4640, -4896, -5152, -5408, -5664, -5920, -6176, -6432, -6688, -6944, -7200, -7456, -7712, -7968, |
| 1240 |
-8224, -8480, -8736, -8992, -9248, -9504, -9760, -10016, -10272, -10528, -10784, -11040, -11296, -11552, -11808, -12064, |
| 1241 |
-12320, -12576, -12832, -13088, -13344, -13600, -13856, -14112, -14368, -14624, -14880, -15136, -15392, -15648, -15904, -16160, |
| 1242 |
-16416, -16672, -16928, -17184, -17440, -17952, -18464, -18976, -19488, -20000, -20512, -21024, -21536, -22048, -22560, -23072, |
| 1243 |
-23584, -24096, -24608, -25120, -25632, -26144, -26656, -27168, -27680, -28192, -28704, -29216, -29728, -30240, -30752, -31264 |
| 1244 |
}; |
| 1245 |
|
| 1246 |
unsigned char c219HighBitPos[16]={ |
| 1247 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 24, 48, 48, 48, 48 |
| 1248 |
}; |
| 1249 |
|
| 1250 |
unsigned char c219ShiftToVal[16]={ |
| 1251 |
5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 8, 8, 8, 8 |
| 1252 |
}; |
| 1253 |
|
| 1254 |
signed char adpcmKTable[16]={ |
| 1255 |
0, 1, 2, 4, 8, 16, 32, 64, -128, -64, -32, -16, -8, -4, -2, -1 |
| 1256 |
}; |
| 1257 |
|
| 1258 |
void DivSample::render(unsigned int formatMask) { |
| 1259 |
// step 1: convert to 16-bit if needed |
| 1260 |
if (depth!=DIV_SAMPLE_DEPTH_16BIT) { |
| 1261 |
if (!initInternal(DIV_SAMPLE_DEPTH_16BIT,samples)) return; |
| 1262 |
switch (depth) { |
| 1263 |
case DIV_SAMPLE_DEPTH_1BIT: // 1-bit |
| 1264 |
for (unsigned int i=0; i<samples; i++) { |
| 1265 |
data16[i]=((data1[i>>3]>>(i&7))&1)?0x7fff:-0x7fff; |
| 1266 |
} |
| 1267 |
break; |
| 1268 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: { // DPCM |
| 1269 |
int accum=0; |
| 1270 |
for (unsigned int i=0; i<samples; i++) { |
| 1271 |
accum+=((dataDPCM[i>>3]>>(i&7))&1)?1:-1; |
| 1272 |
if (accum>63) accum=63; |
| 1273 |
if (accum<-64) accum=-64; |
| 1274 |
data16[i]=accum*512; |
| 1275 |
} |
| 1276 |
break; |
| 1277 |
} |
| 1278 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: // YMZ ADPCM |
| 1279 |
ymz_decode(dataZ,data16,samples); |
| 1280 |
break; |
| 1281 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: // QSound ADPCM |
| 1282 |
bs_decode(dataQSoundA,data16,samples); |
| 1283 |
break; |
| 1284 |
case DIV_SAMPLE_DEPTH_ADPCM_A: // ADPCM-A |
| 1285 |
yma_decode(dataA,data16,samples); |
| 1286 |
break; |
| 1287 |
case DIV_SAMPLE_DEPTH_ADPCM_B: // ADPCM-B |
| 1288 |
ymb_decode(dataB,data16,samples); |
| 1289 |
break; |
| 1290 |
case DIV_SAMPLE_DEPTH_ADPCM_K: { // K05 ADPCM |
| 1291 |
signed char s=0; |
| 1292 |
for (unsigned int i=0; i<samples; i++) { |
| 1293 |
unsigned char nibble=dataK[i>>1]; |
| 1294 |
if (i&1) { // TODO: is this right? |
| 1295 |
nibble>>=4; |
| 1296 |
} else { |
| 1297 |
nibble&=15; |
| 1298 |
} |
| 1299 |
s+=adpcmKTable[nibble]; |
| 1300 |
data16[i]=s<<8; |
| 1301 |
} |
| 1302 |
break; |
| 1303 |
} |
| 1304 |
case DIV_SAMPLE_DEPTH_8BIT: // 8-bit PCM |
| 1305 |
for (unsigned int i=0; i<samples; i++) { |
| 1306 |
data16[i]=data8[i]<<8; |
| 1307 |
} |
| 1308 |
break; |
| 1309 |
case DIV_SAMPLE_DEPTH_BRR: // BRR |
| 1310 |
brrDecode(dataBRR,data16,lengthBRR,brrEmphasis); |
| 1311 |
break; |
| 1312 |
case DIV_SAMPLE_DEPTH_VOX: // VOX |
| 1313 |
oki_decode(dataVOX,data16,samples); |
| 1314 |
break; |
| 1315 |
case DIV_SAMPLE_DEPTH_MULAW: // 8-bit µ-law PCM |
| 1316 |
for (unsigned int i=0; i<samples; i++) { |
| 1317 |
IntFloat s; |
| 1318 |
s.i=(dataMuLaw[i]^0xff); |
| 1319 |
s.i=0x3f800000+(((s.i<<24)&0x80000000)|((s.i&0x7f)<<19)); |
| 1320 |
data16[i]=(short)(s.f*128.0f); |
| 1321 |
} |
| 1322 |
break; |
| 1323 |
case DIV_SAMPLE_DEPTH_C219: // 8-bit C219 "μ-law" PCM |
| 1324 |
for (unsigned int i=0; i<samples; i++) { |
| 1325 |
data16[i]=c219Table[dataC219[i]&0x7f]; |
| 1326 |
if (dataC219[i]&0x80) data16[i]=-data16[i]; |
| 1327 |
} |
| 1328 |
break; |
| 1329 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: // IMA ADPCM |
| 1330 |
if (adpcm_decode_block(data16,dataIMA,lengthIMA,samples)==0) logE("oh crap!"); |
| 1331 |
break; |
| 1332 |
case DIV_SAMPLE_DEPTH_12BIT: // 12-bit PCM (MultiPCM) |
| 1333 |
for (unsigned int i=0, j=0; i<samples; i+=2, j+=3) { |
| 1334 |
data16[i+0]=(data12[j+0]<<8)|(data12[j+1]&0xf0); |
| 1335 |
if (i+1<samples) { |
| 1336 |
data16[i+1]=(data12[j+2]<<8)|((data12[j+1]<<4)&0xf0); |
| 1337 |
} |
| 1338 |
} |
| 1339 |
break; |
| 1340 |
case DIV_SAMPLE_DEPTH_4BIT: { |
| 1341 |
unsigned short nibble=0; |
| 1342 |
for (unsigned int i=0; i<samples; i++) { |
| 1343 |
if (i&1) { |
| 1344 |
nibble=data4[i>>1]&0xf; |
| 1345 |
} else { |
| 1346 |
nibble=data4[i>>1]>>4; |
| 1347 |
} |
| 1348 |
data16[i]=((nibble<<12)|(nibble<<8)|(nibble<<4)|nibble)^0x8000; |
| 1349 |
} |
| 1350 |
break; |
| 1351 |
} |
| 1352 |
default: |
| 1353 |
return; |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
// step 2: render to other formats |
| 1358 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_1BIT)) { // 1-bit |
| 1359 |
if (!initInternal(DIV_SAMPLE_DEPTH_1BIT,samples)) return; |
| 1360 |
for (unsigned int i=0; i<samples; i++) { |
| 1361 |
if (data16[i]>0) { |
| 1362 |
data1[i>>3]|=1<<(i&7); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_1BIT_DPCM)) { // DPCM |
| 1367 |
if (!initInternal(DIV_SAMPLE_DEPTH_1BIT_DPCM,samples)) return; |
| 1368 |
int accum=63; |
| 1369 |
int next=63; |
| 1370 |
|
| 1371 |
for (unsigned int i=0; (i<samples && (i>>3)<lengthDPCM); i++) { |
| 1372 |
next=((unsigned short)(data16[i]^0x8000))>>9; |
| 1373 |
if (next>accum) { |
| 1374 |
dataDPCM[i>>3]|=1<<(i&7); |
| 1375 |
accum++; |
| 1376 |
} else { |
| 1377 |
dataDPCM[i>>3]&=~(1<<(i&7)); |
| 1378 |
accum--; |
| 1379 |
} |
| 1380 |
if (accum<0) accum=0; |
| 1381 |
if (accum>127) accum=127; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_YMZ_ADPCM)) { // YMZ ADPCM |
| 1385 |
if (!initInternal(DIV_SAMPLE_DEPTH_YMZ_ADPCM,samples)) return; |
| 1386 |
ymz_encode(data16,dataZ,(samples+7)&(~0x7)); |
| 1387 |
} |
| 1388 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_QSOUND_ADPCM)) { // QSound ADPCM |
| 1389 |
if (!initInternal(DIV_SAMPLE_DEPTH_QSOUND_ADPCM,samples)) return; |
| 1390 |
bs_encode(data16,dataQSoundA,samples); |
| 1391 |
} |
| 1392 |
// TODO: pad to 256. |
| 1393 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_ADPCM_A)) { // ADPCM-A |
| 1394 |
if (!initInternal(DIV_SAMPLE_DEPTH_ADPCM_A,samples)) return; |
| 1395 |
yma_encode(data16,dataA,(samples+511)&(~0x1ff)); |
| 1396 |
} |
| 1397 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_ADPCM_B)) { // ADPCM-B |
| 1398 |
if (!initInternal(DIV_SAMPLE_DEPTH_ADPCM_B,samples)) return; |
| 1399 |
ymb_encode(data16,dataB,(samples+511)&(~0x1ff)); |
| 1400 |
} |
| 1401 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_ADPCM_K)) { // K05 ADPCM |
| 1402 |
if (!initInternal(DIV_SAMPLE_DEPTH_ADPCM_K,samples)) return; |
| 1403 |
signed char accum=0; |
| 1404 |
unsigned char out=0; |
| 1405 |
for (unsigned int i=0; i<samples; i++) { |
| 1406 |
signed char target=data16[i]>>8; |
| 1407 |
short delta=target-accum; |
| 1408 |
unsigned char next=0; |
| 1409 |
|
| 1410 |
if (delta!=0) { |
| 1411 |
int b=bsr((delta>=0)?delta:-delta); |
| 1412 |
if (delta>=0) { |
| 1413 |
if (b>7) b=7; |
| 1414 |
next=b&15; |
| 1415 |
|
| 1416 |
// test previous |
| 1417 |
if (next>1) { |
| 1418 |
const signed char t1=accum+adpcmKTable[next]; |
| 1419 |
const signed char t2=accum+adpcmKTable[next-1]; |
| 1420 |
const signed char d1=((t1-target)<0)?(target-t1):(t1-target); |
| 1421 |
const signed char d2=((t2-target)<0)?(target-t2):(t2-target); |
| 1422 |
|
| 1423 |
if (d2<d1) next--; |
| 1424 |
} |
| 1425 |
} else { |
| 1426 |
if (b>8) b=8; |
| 1427 |
next=(16-b)&15; |
| 1428 |
|
| 1429 |
// test next |
| 1430 |
if (next<15) { |
| 1431 |
const signed char t1=accum+adpcmKTable[next]; |
| 1432 |
const signed char t2=accum+adpcmKTable[next+1]; |
| 1433 |
const signed char d1=((t1-target)<0)?(target-t1):(t1-target); |
| 1434 |
const signed char d2=((t2-target)<0)?(target-t2):(t2-target); |
| 1435 |
|
| 1436 |
if (d2<d1) next++; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
/*if (accum+adpcmKTable[next]>=128 || accum+adpcmKTable[next]<-128) { |
| 1441 |
if (delta>=0) { |
| 1442 |
next--; |
| 1443 |
} else { |
| 1444 |
next++; |
| 1445 |
if (next>15) next=15; |
| 1446 |
} |
| 1447 |
}*/ |
| 1448 |
} |
| 1449 |
|
| 1450 |
out>>=4; |
| 1451 |
out|=next<<4; |
| 1452 |
accum+=adpcmKTable[next]; |
| 1453 |
|
| 1454 |
if (i&1) { |
| 1455 |
dataK[i>>1]=out; |
| 1456 |
out=0; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
} |
| 1460 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_8BIT)) { // 8-bit PCM |
| 1461 |
if (!initInternal(DIV_SAMPLE_DEPTH_8BIT,samples)) return; |
| 1462 |
if (dither) { |
| 1463 |
unsigned short lfsr=0x6438; |
| 1464 |
unsigned short lfsr1=0x1283; |
| 1465 |
signed char errorLast=0; |
| 1466 |
signed char errorCur=0; |
| 1467 |
for (unsigned int i=0; i<samples; i++) { |
| 1468 |
signed char val=CLAMP(data16[i]+128,-32768,32767)>>8; |
| 1469 |
errorLast=errorCur; |
| 1470 |
errorCur=(val<<8)-data16[i]; |
| 1471 |
data8[i]=CLAMP(val-((((errorLast+errorCur)>>1)+(lfsr&0xff))>>8),-128,127); |
| 1472 |
lfsr=(lfsr<<1)|(((lfsr>>1)^(lfsr>>2)^(lfsr>>4)^(lfsr>>15))&1); |
| 1473 |
lfsr1=(lfsr1<<1)|(((lfsr1>>1)^(lfsr1>>2)^(lfsr1>>4)^(lfsr1>>15))&1); |
| 1474 |
} |
| 1475 |
} else { |
| 1476 |
for (unsigned int i=0; i<samples; i++) { |
| 1477 |
data8[i]=data16[i]>>8; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
} |
| 1481 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_BRR)) { // BRR |
| 1482 |
int sampleCount=isLoopable()?loopEnd:samples; |
| 1483 |
if (sampleCount>(int)samples) sampleCount=samples; |
| 1484 |
if (!initInternal(DIV_SAMPLE_DEPTH_BRR,sampleCount)) return; |
| 1485 |
brrEncode(data16,dataBRR,sampleCount,loop?loopStart:-1,brrEmphasis,brrNoFilter); |
| 1486 |
} |
| 1487 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_VOX)) { // VOX |
| 1488 |
if (!initInternal(DIV_SAMPLE_DEPTH_VOX,samples)) return; |
| 1489 |
oki_encode(data16,dataVOX,samples); |
| 1490 |
} |
| 1491 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_MULAW)) { // µ-law |
| 1492 |
if (!initInternal(DIV_SAMPLE_DEPTH_MULAW,samples)) return; |
| 1493 |
for (unsigned int i=0; i<samples; i++) { |
| 1494 |
IntFloat s; |
| 1495 |
s.f=data16[i]; |
| 1496 |
s.i&=0x7fffffff; |
| 1497 |
if (s.f>32639.0f) s.f=32639.0f; |
| 1498 |
s.f/=128.0f; |
| 1499 |
s.f+=1.0f; |
| 1500 |
s.i-=0x3f800000; |
| 1501 |
dataMuLaw[i]=(((data16[i]<0)?0x80:0)|(s.i&0x03f80000)>>19)^0xff; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_C219)) { // C219 |
| 1505 |
if (!initInternal(DIV_SAMPLE_DEPTH_C219,samples)) return; |
| 1506 |
for (unsigned int i=0; i<samples; i++) { |
| 1507 |
short s=data16[i]; |
| 1508 |
unsigned char x=0; |
| 1509 |
bool negate=s&0x8000; |
| 1510 |
if (negate) { |
| 1511 |
s^=0xffff; |
| 1512 |
} |
| 1513 |
if (s==0) { |
| 1514 |
x=0; |
| 1515 |
} else if (s>17152) { // 100+ |
| 1516 |
x=((s-17152)>>9)+100; |
| 1517 |
} else { |
| 1518 |
int b=bsr(s)-1; |
| 1519 |
x=((s-(c219Table[c219HighBitPos[b]]))>>c219ShiftToVal[b])+c219HighBitPos[b]; |
| 1520 |
} |
| 1521 |
if (x>127) x=127; |
| 1522 |
dataC219[i]=x|(negate?0x80:0); |
| 1523 |
} |
| 1524 |
} |
| 1525 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_IMA_ADPCM)) { // IMA ADPCM |
| 1526 |
if (!initInternal(DIV_SAMPLE_DEPTH_IMA_ADPCM,samples)) return; |
| 1527 |
int delta[2]; |
| 1528 |
delta[0]=0; |
| 1529 |
delta[1]=0; |
| 1530 |
|
| 1531 |
void* codec=adpcm_create_context(1,1,NOISE_SHAPING_OFF,delta); |
| 1532 |
if (codec==NULL) { |
| 1533 |
logE("oh no IMA encoder could not be created!"); |
| 1534 |
} else { |
| 1535 |
size_t whyPointer=0; |
| 1536 |
adpcm_encode_block(codec,dataIMA,&whyPointer,data16,samples); |
| 1537 |
if (whyPointer!=lengthIMA) logW("IMA length mismatch! %d -> %d!=%d",(int)samples,(int)whyPointer,(int)lengthIMA); |
| 1538 |
|
| 1539 |
adpcm_free_context(codec); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_12BIT)) { // 12-bit PCM (MultiPCM) |
| 1543 |
if (!initInternal(DIV_SAMPLE_DEPTH_12BIT,samples)) return; |
| 1544 |
for (unsigned int i=0, j=0; i<samples; i+=2, j+=3) { |
| 1545 |
data12[j+0]=data16[i+0]>>8; |
| 1546 |
data12[j+1]=((data16[i+0]>>4)&0xf)|(i+1<samples?(data16[i+1]>>4)&0xf:0); |
| 1547 |
if (i+1<samples) { |
| 1548 |
data12[j+2]=data16[i+1]>>8; |
| 1549 |
} else { |
| 1550 |
data12[j+2]=0; |
| 1551 |
} |
| 1552 |
} |
| 1553 |
} |
| 1554 |
if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_4BIT)) { |
| 1555 |
if (!initInternal(DIV_SAMPLE_DEPTH_4BIT,samples)) return; |
| 1556 |
unsigned char _sample=0, sample4=0; |
| 1557 |
unsigned short* samplePtr = (unsigned short*)data16; |
| 1558 |
for (unsigned int i=0; i<samples; i+=2) { |
| 1559 |
_sample=(*samplePtr++^0x8000)>>12; |
| 1560 |
sample4=_sample<<4; |
| 1561 |
if (i+1<samples) { |
| 1562 |
_sample=(*samplePtr++^0x8000)>>12; |
| 1563 |
sample4|=_sample; |
| 1564 |
} |
| 1565 |
data4[i>>1]=sample4; |
| 1566 |
} |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
void* DivSample::getCurBuf() { |
| 1571 |
switch (depth) { |
| 1572 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 1573 |
return data1; |
| 1574 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 1575 |
return dataDPCM; |
| 1576 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 1577 |
return dataZ; |
| 1578 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: |
| 1579 |
return dataQSoundA; |
| 1580 |
case DIV_SAMPLE_DEPTH_ADPCM_A: |
| 1581 |
return dataA; |
| 1582 |
case DIV_SAMPLE_DEPTH_ADPCM_B: |
| 1583 |
return dataB; |
| 1584 |
case DIV_SAMPLE_DEPTH_ADPCM_K: |
| 1585 |
return dataK; |
| 1586 |
case DIV_SAMPLE_DEPTH_8BIT: |
| 1587 |
return data8; |
| 1588 |
case DIV_SAMPLE_DEPTH_BRR: |
| 1589 |
return dataBRR; |
| 1590 |
case DIV_SAMPLE_DEPTH_VOX: |
| 1591 |
return dataVOX; |
| 1592 |
case DIV_SAMPLE_DEPTH_MULAW: |
| 1593 |
return dataMuLaw; |
| 1594 |
case DIV_SAMPLE_DEPTH_C219: |
| 1595 |
return dataC219; |
| 1596 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 1597 |
return dataIMA; |
| 1598 |
case DIV_SAMPLE_DEPTH_12BIT: |
| 1599 |
return data12; |
| 1600 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 1601 |
return data4; |
| 1602 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 1603 |
return data16; |
| 1604 |
default: |
| 1605 |
return NULL; |
| 1606 |
} |
| 1607 |
return NULL; |
| 1608 |
} |
| 1609 |
|
| 1610 |
unsigned int DivSample::getCurBufLen() { |
| 1611 |
switch (depth) { |
| 1612 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 1613 |
return length1; |
| 1614 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 1615 |
return lengthDPCM; |
| 1616 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 1617 |
return lengthZ; |
| 1618 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: |
| 1619 |
return lengthQSoundA; |
| 1620 |
case DIV_SAMPLE_DEPTH_ADPCM_A: |
| 1621 |
return lengthA; |
| 1622 |
case DIV_SAMPLE_DEPTH_ADPCM_B: |
| 1623 |
return lengthB; |
| 1624 |
case DIV_SAMPLE_DEPTH_ADPCM_K: |
| 1625 |
return lengthK; |
| 1626 |
case DIV_SAMPLE_DEPTH_8BIT: |
| 1627 |
return length8; |
| 1628 |
case DIV_SAMPLE_DEPTH_BRR: |
| 1629 |
return lengthBRR; |
| 1630 |
case DIV_SAMPLE_DEPTH_VOX: |
| 1631 |
return lengthVOX; |
| 1632 |
case DIV_SAMPLE_DEPTH_MULAW: |
| 1633 |
return lengthMuLaw; |
| 1634 |
case DIV_SAMPLE_DEPTH_C219: |
| 1635 |
return lengthC219; |
| 1636 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 1637 |
return lengthIMA; |
| 1638 |
case DIV_SAMPLE_DEPTH_12BIT: |
| 1639 |
return length12; |
| 1640 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 1641 |
return length4; |
| 1642 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 1643 |
return length16; |
| 1644 |
default: |
| 1645 |
return 0; |
| 1646 |
} |
| 1647 |
return 0; |
| 1648 |
} |
| 1649 |
|
| 1650 |
DivSampleHistory* DivSample::prepareUndo(bool data, bool doNotPush) { |
| 1651 |
DivSampleHistory* h; |
| 1652 |
if (data) { |
| 1653 |
unsigned char* duplicate; |
| 1654 |
if (getCurBuf()==NULL) { |
| 1655 |
duplicate=NULL; |
| 1656 |
} else { |
| 1657 |
duplicate=new unsigned char[getCurBufLen()]; |
| 1658 |
memcpy(duplicate,getCurBuf(),getCurBufLen()); |
| 1659 |
} |
| 1660 |
h=new DivSampleHistory(duplicate,getCurBufLen(),samples,depth,rate,centerRate,loopStart,loopEnd,loop,brrEmphasis,brrNoFilter,dither,loopMode); |
| 1661 |
} else { |
| 1662 |
h=new DivSampleHistory(depth,rate,centerRate,loopStart,loopEnd,loop,brrEmphasis,brrNoFilter,dither,loopMode); |
| 1663 |
} |
| 1664 |
if (!doNotPush) { |
| 1665 |
while (!redoHist.empty()) { |
| 1666 |
DivSampleHistory* h=redoHist.back(); |
| 1667 |
delete h; |
| 1668 |
redoHist.pop_back(); |
| 1669 |
} |
| 1670 |
if (undoHist.size()>100) undoHist.pop_front(); |
| 1671 |
undoHist.push_back(h); |
| 1672 |
} |
| 1673 |
return h; |
| 1674 |
} |
| 1675 |
|
| 1676 |
#define applyHistory \ |
| 1677 |
depth=h->depth; \ |
| 1678 |
if (h->hasSample) { \ |
| 1679 |
initInternal(h->depth,h->samples); \ |
| 1680 |
samples=h->samples; \ |
| 1681 |
\ |
| 1682 |
if (h->length!=getCurBufLen()) logW("undo buffer length not equal to current buffer length! %d != %d",h->length,getCurBufLen()); \ |
| 1683 |
\ |
| 1684 |
void* buf=getCurBuf(); \ |
| 1685 |
\ |
| 1686 |
if (buf!=NULL && h->data!=NULL) { \ |
| 1687 |
memcpy(buf,h->data,h->length); \ |
| 1688 |
} \ |
| 1689 |
} \ |
| 1690 |
rate=h->rate; \ |
| 1691 |
centerRate=h->centerRate; \ |
| 1692 |
loopStart=h->loopStart; \ |
| 1693 |
loopEnd=h->loopEnd; \ |
| 1694 |
loop=h->loop; \ |
| 1695 |
brrEmphasis=h->brrEmphasis; \ |
| 1696 |
brrNoFilter=h->brrNoFilter; \ |
| 1697 |
dither=h->dither; \ |
| 1698 |
loopMode=h->loopMode; |
| 1699 |
|
| 1700 |
|
| 1701 |
int DivSample::undo() { |
| 1702 |
if (undoHist.empty()) return 0; |
| 1703 |
DivSampleHistory* h=undoHist.back(); |
| 1704 |
DivSampleHistory* redo=prepareUndo(h->hasSample,true); |
| 1705 |
|
| 1706 |
int ret=h->hasSample?2:1; |
| 1707 |
|
| 1708 |
applyHistory; |
| 1709 |
|
| 1710 |
redoHist.push_back(redo); |
| 1711 |
delete h; |
| 1712 |
undoHist.pop_back(); |
| 1713 |
return ret; |
| 1714 |
} |
| 1715 |
|
| 1716 |
int DivSample::redo() { |
| 1717 |
if (redoHist.empty()) return 0; |
| 1718 |
DivSampleHistory* h=redoHist.back(); |
| 1719 |
DivSampleHistory* undo=prepareUndo(h->hasSample,true); |
| 1720 |
|
| 1721 |
int ret=h->hasSample?2:1; |
| 1722 |
|
| 1723 |
applyHistory; |
| 1724 |
|
| 1725 |
undoHist.push_back(undo); |
| 1726 |
delete h; |
| 1727 |
redoHist.pop_back(); |
| 1728 |
return ret; |
| 1729 |
} |
| 1730 |
|
| 1731 |
DivSample::~DivSample() { |
| 1732 |
while (!undoHist.empty()) { |
| 1733 |
DivSampleHistory* h=undoHist.back(); |
| 1734 |
delete h; |
| 1735 |
undoHist.pop_back(); |
| 1736 |
} |
| 1737 |
while (!redoHist.empty()) { |
| 1738 |
DivSampleHistory* h=redoHist.back(); |
| 1739 |
delete h; |
| 1740 |
redoHist.pop_back(); |
| 1741 |
} |
| 1742 |
if (data8) delete[] data8; |
| 1743 |
if (data16) delete[] data16; |
| 1744 |
if (data1) delete[] data1; |
| 1745 |
if (dataDPCM) delete[] dataDPCM; |
| 1746 |
if (dataZ) delete[] dataZ; |
| 1747 |
if (dataQSoundA) delete[] dataQSoundA; |
| 1748 |
if (dataA) delete[] dataA; |
| 1749 |
if (dataB) delete[] dataB; |
| 1750 |
if (dataK) delete[] dataK; |
| 1751 |
if (dataBRR) delete[] dataBRR; |
| 1752 |
if (dataVOX) delete[] dataVOX; |
| 1753 |
if (dataMuLaw) delete[] dataMuLaw; |
| 1754 |
if (dataC219) delete[] dataC219; |
| 1755 |
if (dataIMA) delete[] dataIMA; |
| 1756 |
if (data12) delete[] data12; |
| 1757 |
if (data4) delete[] data4; |
| 1758 |
} |