| 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 "engine.h" |
| 21 |
#include "../ta-log.h" |
| 22 |
#include "../fileutils.h" |
| 23 |
#ifdef HAVE_SNDFILE |
| 24 |
#include "sfWrapper.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
std::vector<DivSample*> DivEngine::sampleFromFile(const char* path) { |
| 28 |
std::vector<DivSample*> ret; |
| 29 |
|
| 30 |
if (song.sample.size()>=32768) { |
| 31 |
lastError="too many samples!"; |
| 32 |
return ret; |
| 33 |
} |
| 34 |
BUSY_BEGIN; |
| 35 |
warnings=""; |
| 36 |
|
| 37 |
const char* pathRedux=strrchr(path,DIR_SEPARATOR); |
| 38 |
if (pathRedux==NULL) { |
| 39 |
pathRedux=path; |
| 40 |
} else { |
| 41 |
pathRedux++; |
| 42 |
} |
| 43 |
String stripPath; |
| 44 |
const char* pathReduxEnd=strrchr(pathRedux,'.'); |
| 45 |
if (pathReduxEnd==NULL) { |
| 46 |
stripPath=pathRedux; |
| 47 |
} else { |
| 48 |
for (const char* i=pathRedux; i!=pathReduxEnd && (*i); i++) { |
| 49 |
stripPath+=*i; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
const char* ext=strrchr(path,'.'); |
| 54 |
if (ext!=NULL) { |
| 55 |
String extS; |
| 56 |
for (; *ext; ext++) { |
| 57 |
char i=*ext; |
| 58 |
if (i>='A' && i<='Z') { |
| 59 |
i+='a'-'A'; |
| 60 |
} |
| 61 |
extS+=i; |
| 62 |
} |
| 63 |
|
| 64 |
if(extS == ".pps" || extS == ".ppc" || extS == ".pvi" || |
| 65 |
extS == ".pdx" || extS == ".pzi" || extS == ".p86" || |
| 66 |
extS == ".p") //sample banks! |
| 67 |
{ |
| 68 |
String stripPath; |
| 69 |
const char* pathReduxEnd=strrchr(pathRedux,'.'); |
| 70 |
if (pathReduxEnd==NULL) { |
| 71 |
stripPath=pathRedux; |
| 72 |
} else { |
| 73 |
for (const char* i=pathRedux; i!=pathReduxEnd && (*i); i++) { |
| 74 |
stripPath+=*i; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
FILE* f=ps_fopen(path,"rb"); |
| 79 |
if (f==NULL) { |
| 80 |
lastError=strerror(errno); |
| 81 |
return ret; |
| 82 |
} |
| 83 |
unsigned char* buf; |
| 84 |
ssize_t len; |
| 85 |
if (fseek(f,0,SEEK_END)!=0) { |
| 86 |
lastError=strerror(errno); |
| 87 |
fclose(f); |
| 88 |
return ret; |
| 89 |
} |
| 90 |
len=ftell(f); |
| 91 |
if (len<0) { |
| 92 |
lastError=strerror(errno); |
| 93 |
fclose(f); |
| 94 |
return ret; |
| 95 |
} |
| 96 |
if (len==(SIZE_MAX>>1)) { |
| 97 |
lastError=strerror(errno); |
| 98 |
fclose(f); |
| 99 |
return ret; |
| 100 |
} |
| 101 |
if (len==0) { |
| 102 |
lastError=strerror(errno); |
| 103 |
fclose(f); |
| 104 |
return ret; |
| 105 |
} |
| 106 |
if (fseek(f,0,SEEK_SET)!=0) { |
| 107 |
lastError=strerror(errno); |
| 108 |
fclose(f); |
| 109 |
return ret; |
| 110 |
} |
| 111 |
buf=new unsigned char[len]; |
| 112 |
if (fread(buf,1,len,f)!=(size_t)len) { |
| 113 |
logW("did not read entire sample bank file buffer!"); |
| 114 |
lastError=_("did not read entire sample bank file!"); |
| 115 |
delete[] buf; |
| 116 |
fclose(f); |
| 117 |
return ret; |
| 118 |
} |
| 119 |
fclose(f); |
| 120 |
|
| 121 |
SafeReader reader = SafeReader(buf,len); |
| 122 |
|
| 123 |
if(extS == ".pps") |
| 124 |
{ |
| 125 |
loadPPS(reader,ret,stripPath); |
| 126 |
} |
| 127 |
if(extS == ".ppc") |
| 128 |
{ |
| 129 |
loadPPC(reader,ret,stripPath); |
| 130 |
} |
| 131 |
if(extS == ".pvi") |
| 132 |
{ |
| 133 |
loadPVI(reader,ret,stripPath); |
| 134 |
} |
| 135 |
if(extS == ".pdx") |
| 136 |
{ |
| 137 |
loadPDX(reader,ret,stripPath); |
| 138 |
} |
| 139 |
if(extS == ".pzi") |
| 140 |
{ |
| 141 |
loadPZI(reader,ret,stripPath); |
| 142 |
} |
| 143 |
if(extS == ".p86") |
| 144 |
{ |
| 145 |
loadP86(reader,ret,stripPath); |
| 146 |
} |
| 147 |
if(extS == ".p") |
| 148 |
{ |
| 149 |
loadP(reader,ret,stripPath); |
| 150 |
} |
| 151 |
|
| 152 |
if((int)ret.size() > 0) |
| 153 |
{ |
| 154 |
int counter = 0; |
| 155 |
|
| 156 |
for(DivSample* s: ret) |
| 157 |
{ |
| 158 |
s->name = fmt::sprintf("%s sample %d", stripPath, counter); |
| 159 |
counter++; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
delete[] buf; //done with buffer |
| 164 |
BUSY_END; |
| 165 |
return ret; |
| 166 |
} |
| 167 |
|
| 168 |
if (extS==".dmc" || extS==".brr") { // read as .dmc or .brr |
| 169 |
size_t len=0; |
| 170 |
DivSample* sample=new DivSample; |
| 171 |
sample->name=stripPath; |
| 172 |
|
| 173 |
FILE* f=ps_fopen(path,"rb"); |
| 174 |
if (f==NULL) { |
| 175 |
BUSY_END; |
| 176 |
lastError=fmt::sprintf("could not open file! (%s)",strerror(errno)); |
| 177 |
delete sample; |
| 178 |
return ret; |
| 179 |
} |
| 180 |
|
| 181 |
if (fseek(f,0,SEEK_END)<0) { |
| 182 |
fclose(f); |
| 183 |
BUSY_END; |
| 184 |
lastError=fmt::sprintf("could not get file length! (%s)",strerror(errno)); |
| 185 |
delete sample; |
| 186 |
return ret; |
| 187 |
} |
| 188 |
|
| 189 |
len=ftell(f); |
| 190 |
|
| 191 |
if (len==0) { |
| 192 |
fclose(f); |
| 193 |
BUSY_END; |
| 194 |
lastError="file is empty!"; |
| 195 |
delete sample; |
| 196 |
return ret; |
| 197 |
} |
| 198 |
|
| 199 |
if (len==(SIZE_MAX>>1)) { |
| 200 |
fclose(f); |
| 201 |
BUSY_END; |
| 202 |
lastError="file is invalid!"; |
| 203 |
delete sample; |
| 204 |
return ret; |
| 205 |
} |
| 206 |
|
| 207 |
if (fseek(f,0,SEEK_SET)<0) { |
| 208 |
fclose(f); |
| 209 |
BUSY_END; |
| 210 |
lastError=fmt::sprintf("could not seek to beginning of file! (%s)",strerror(errno)); |
| 211 |
delete sample; |
| 212 |
return ret; |
| 213 |
} |
| 214 |
|
| 215 |
if (extS==".dmc") { |
| 216 |
sample->rate=33144; |
| 217 |
sample->centerRate=33144; |
| 218 |
sample->depth=DIV_SAMPLE_DEPTH_1BIT_DPCM; |
| 219 |
sample->init(len*8); |
| 220 |
} else if (extS==".brr") { |
| 221 |
sample->rate=32000; |
| 222 |
sample->centerRate=32000; |
| 223 |
sample->depth=DIV_SAMPLE_DEPTH_BRR; |
| 224 |
sample->init(16*(len/9)); |
| 225 |
} else { |
| 226 |
fclose(f); |
| 227 |
BUSY_END; |
| 228 |
lastError="wait... is that right? no I don't think so..."; |
| 229 |
delete sample; |
| 230 |
return ret; |
| 231 |
} |
| 232 |
|
| 233 |
unsigned char* dataBuf=sample->dataDPCM; |
| 234 |
if (extS==".brr") { |
| 235 |
dataBuf=sample->dataBRR; |
| 236 |
if ((len%9)==2) { |
| 237 |
// read loop position |
| 238 |
unsigned short loopPos=0; |
| 239 |
logD("BRR file has loop position"); |
| 240 |
if (fread(&loopPos,1,2,f)!=2) { |
| 241 |
logW("could not read loop position! %s",strerror(errno)); |
| 242 |
} else { |
| 243 |
#ifdef TA_BIG_ENDIAN |
| 244 |
loopPos=(loopPos>>8)|(loopPos<<8); |
| 245 |
#endif |
| 246 |
sample->loopStart=16*(loopPos/9); |
| 247 |
sample->loopEnd=sample->samples; |
| 248 |
sample->loop=true; |
| 249 |
sample->loopMode=DIV_SAMPLE_LOOP_FORWARD; |
| 250 |
} |
| 251 |
len-=2; |
| 252 |
if (len==0) { |
| 253 |
fclose(f); |
| 254 |
BUSY_END; |
| 255 |
lastError="BRR sample is empty!"; |
| 256 |
delete sample; |
| 257 |
return ret; |
| 258 |
} |
| 259 |
} else if ((len%9)!=0) { |
| 260 |
fclose(f); |
| 261 |
BUSY_END; |
| 262 |
lastError="possibly corrupt BRR sample!"; |
| 263 |
delete sample; |
| 264 |
return ret; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if (fread(dataBuf,1,len,f)==0) { |
| 269 |
fclose(f); |
| 270 |
BUSY_END; |
| 271 |
lastError=fmt::sprintf("could not read file! (%s)",strerror(errno)); |
| 272 |
delete sample; |
| 273 |
return ret; |
| 274 |
} |
| 275 |
fclose(f); |
| 276 |
BUSY_END; |
| 277 |
ret.push_back(sample); |
| 278 |
return ret; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
#ifndef HAVE_SNDFILE |
| 283 |
lastError="Furnace was not compiled with libsndfile!"; |
| 284 |
return ret; |
| 285 |
#else |
| 286 |
SF_INFO si; |
| 287 |
SFWrapper sfWrap; |
| 288 |
memset(&si,0,sizeof(SF_INFO)); |
| 289 |
SNDFILE* f=sfWrap.doOpen(path,SFM_READ,&si); |
| 290 |
if (f==NULL) { |
| 291 |
BUSY_END; |
| 292 |
int err=sf_error(NULL); |
| 293 |
if (err==SF_ERR_SYSTEM) { |
| 294 |
lastError=fmt::sprintf("could not open file! (%s %s)",sf_error_number(err),strerror(errno)); |
| 295 |
} else { |
| 296 |
lastError=fmt::sprintf("could not open file! (%s)\nif this is raw sample data, you may import it by right-clicking the Load Sample icon and selecting \"import raw\".",sf_error_number(err)); |
| 297 |
} |
| 298 |
return ret; |
| 299 |
} |
| 300 |
if (si.frames>16777215) { |
| 301 |
lastError="this sample is too big! max sample size is 16777215."; |
| 302 |
sfWrap.doClose(); |
| 303 |
BUSY_END; |
| 304 |
return ret; |
| 305 |
} |
| 306 |
void* buf=NULL; |
| 307 |
sf_count_t sampleLen=sizeof(short); |
| 308 |
if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) { |
| 309 |
logD("sample is 8-bit unsigned"); |
| 310 |
buf=new unsigned char[si.channels*si.frames]; |
| 311 |
sampleLen=sizeof(unsigned char); |
| 312 |
} else if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_FLOAT) { |
| 313 |
logD("sample is 32-bit float"); |
| 314 |
buf=new float[si.channels*si.frames]; |
| 315 |
sampleLen=sizeof(float); |
| 316 |
} else if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_DOUBLE) { |
| 317 |
logD("sample is 64-bit float"); |
| 318 |
buf=new float[si.channels*si.frames]; |
| 319 |
sampleLen=sizeof(double); |
| 320 |
} else { |
| 321 |
logD("sample is 16-bit signed"); |
| 322 |
buf=new short[si.channels*si.frames]; |
| 323 |
sampleLen=sizeof(short); |
| 324 |
} |
| 325 |
if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8 || |
| 326 |
(si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_FLOAT || |
| 327 |
(si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_DOUBLE) { |
| 328 |
if (sf_read_raw(f,buf,si.frames*si.channels*sampleLen)!=(si.frames*si.channels*sampleLen)) { |
| 329 |
logW("sample read size mismatch!"); |
| 330 |
} |
| 331 |
} else { |
| 332 |
if (sf_read_short(f,(short*)buf,si.frames*si.channels)!=(si.frames*si.channels)) { |
| 333 |
logW("sample read size mismatch!"); |
| 334 |
} |
| 335 |
} |
| 336 |
DivSample* sample=new DivSample; |
| 337 |
int sampleCount=(int)song.sample.size(); |
| 338 |
sample->name=stripPath; |
| 339 |
|
| 340 |
int index=0; |
| 341 |
if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) { |
| 342 |
sample->depth=DIV_SAMPLE_DEPTH_8BIT; |
| 343 |
} else { |
| 344 |
sample->depth=DIV_SAMPLE_DEPTH_16BIT; |
| 345 |
} |
| 346 |
sample->init(si.frames); |
| 347 |
if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) { |
| 348 |
for (int i=0; i<si.frames*si.channels; i+=si.channels) { |
| 349 |
int averaged=0; |
| 350 |
for (int j=0; j<si.channels; j++) { |
| 351 |
averaged+=((int)((unsigned char*)buf)[i+j])-128; |
| 352 |
} |
| 353 |
averaged/=si.channels; |
| 354 |
sample->data8[index++]=averaged; |
| 355 |
} |
| 356 |
delete[] (unsigned char*)buf; |
| 357 |
} else if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_FLOAT) { |
| 358 |
for (int i=0; i<si.frames*si.channels; i+=si.channels) { |
| 359 |
float averaged=0.0f; |
| 360 |
for (int j=0; j<si.channels; j++) { |
| 361 |
averaged+=((float*)buf)[i+j]; |
| 362 |
} |
| 363 |
averaged/=si.channels; |
| 364 |
averaged*=32767.0; |
| 365 |
if (averaged<-32768.0) averaged=-32768.0; |
| 366 |
if (averaged>32767.0) averaged=32767.0; |
| 367 |
sample->data16[index++]=averaged; |
| 368 |
} |
| 369 |
delete[] (float*)buf; |
| 370 |
} else if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_DOUBLE) { |
| 371 |
for (int i=0; i<si.frames*si.channels; i+=si.channels) { |
| 372 |
double averaged=0.0f; |
| 373 |
for (int j=0; j<si.channels; j++) { |
| 374 |
averaged+=((double*)buf)[i+j]; |
| 375 |
} |
| 376 |
averaged/=si.channels; |
| 377 |
averaged*=32767.0; |
| 378 |
if (averaged<-32768.0) averaged=-32768.0; |
| 379 |
if (averaged>32767.0) averaged=32767.0; |
| 380 |
sample->data16[index++]=averaged; |
| 381 |
} |
| 382 |
delete[] (double*)buf; |
| 383 |
} else { |
| 384 |
for (int i=0; i<si.frames*si.channels; i+=si.channels) { |
| 385 |
int averaged=0; |
| 386 |
for (int j=0; j<si.channels; j++) { |
| 387 |
averaged+=((short*)buf)[i+j]; |
| 388 |
} |
| 389 |
averaged/=si.channels; |
| 390 |
sample->data16[index++]=averaged; |
| 391 |
} |
| 392 |
delete[] (short*)buf; |
| 393 |
} |
| 394 |
|
| 395 |
sample->rate=si.samplerate; |
| 396 |
if (sample->rate<4000) sample->rate=4000; |
| 397 |
if (sample->rate>96000) sample->rate=96000; |
| 398 |
sample->centerRate=si.samplerate; |
| 399 |
|
| 400 |
SF_INSTRUMENT inst; |
| 401 |
if (sf_command(f, SFC_GET_INSTRUMENT, &inst, sizeof(inst)) == SF_TRUE) |
| 402 |
{ |
| 403 |
// There's no documentation on libsndfile detune range, but the code |
| 404 |
// implies -50..50. Yet when loading a file you can get a >50 value. |
| 405 |
// disabled for now |
| 406 |
/* |
| 407 |
if(inst.detune > 50) |
| 408 |
inst.detune = inst.detune - 100; |
| 409 |
short pitch = ((0x3c-inst.basenote)*100) + inst.detune; |
| 410 |
sample->centerRate=si.samplerate*pow(2.0,pitch/(12.0 * 100.0)); |
| 411 |
*/ |
| 412 |
if(inst.loop_count && inst.loops[0].mode >= SF_LOOP_FORWARD) |
| 413 |
{ |
| 414 |
sample->loop=true; |
| 415 |
sample->loopMode=(DivSampleLoopMode)(inst.loops[0].mode-SF_LOOP_FORWARD); |
| 416 |
sample->loopStart=inst.loops[0].start; |
| 417 |
sample->loopEnd=inst.loops[0].end; |
| 418 |
if(inst.loops[0].end < (unsigned int)sampleCount) |
| 419 |
sampleCount=inst.loops[0].end; |
| 420 |
} |
| 421 |
else |
| 422 |
sample->loop=false; |
| 423 |
} |
| 424 |
|
| 425 |
if (sample->centerRate<100) sample->centerRate=100; |
| 426 |
if (sample->centerRate>384000) sample->centerRate=384000; |
| 427 |
sfWrap.doClose(); |
| 428 |
BUSY_END; |
| 429 |
ret.push_back(sample); |
| 430 |
return ret; |
| 431 |
#endif |
| 432 |
} |
| 433 |
|
| 434 |
DivSample* DivEngine::sampleFromFileRaw(const char* path, DivSampleDepth depth, int channels, bool bigEndian, bool unsign, bool swapNibbles, int rate) { |
| 435 |
if (song.sample.size()>=32768) { |
| 436 |
lastError="too many samples!"; |
| 437 |
return NULL; |
| 438 |
} |
| 439 |
if (channels<1) { |
| 440 |
channels=1; |
| 441 |
} |
| 442 |
if (depth!=DIV_SAMPLE_DEPTH_8BIT && depth!=DIV_SAMPLE_DEPTH_16BIT) { |
| 443 |
if (channels!=1) { |
| 444 |
channels=1; |
| 445 |
} |
| 446 |
} |
| 447 |
BUSY_BEGIN; |
| 448 |
warnings=""; |
| 449 |
|
| 450 |
const char* pathRedux=strrchr(path,DIR_SEPARATOR); |
| 451 |
if (pathRedux==NULL) { |
| 452 |
pathRedux=path; |
| 453 |
} else { |
| 454 |
pathRedux++; |
| 455 |
} |
| 456 |
String stripPath; |
| 457 |
const char* pathReduxEnd=strrchr(pathRedux,'.'); |
| 458 |
if (pathReduxEnd==NULL) { |
| 459 |
stripPath=pathRedux; |
| 460 |
} else { |
| 461 |
for (const char* i=pathRedux; i!=pathReduxEnd && (*i); i++) { |
| 462 |
stripPath+=*i; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
size_t len=0; |
| 467 |
size_t lenDivided=0; |
| 468 |
DivSample* sample=new DivSample; |
| 469 |
sample->name=stripPath; |
| 470 |
|
| 471 |
FILE* f=ps_fopen(path,"rb"); |
| 472 |
if (f==NULL) { |
| 473 |
BUSY_END; |
| 474 |
lastError=fmt::sprintf("could not open file! (%s)",strerror(errno)); |
| 475 |
delete sample; |
| 476 |
return NULL; |
| 477 |
} |
| 478 |
|
| 479 |
if (fseek(f,0,SEEK_END)<0) { |
| 480 |
fclose(f); |
| 481 |
BUSY_END; |
| 482 |
lastError=fmt::sprintf("could not get file length! (%s)",strerror(errno)); |
| 483 |
delete sample; |
| 484 |
return NULL; |
| 485 |
} |
| 486 |
|
| 487 |
len=ftell(f); |
| 488 |
|
| 489 |
if (len==0) { |
| 490 |
fclose(f); |
| 491 |
BUSY_END; |
| 492 |
lastError="file is empty!"; |
| 493 |
delete sample; |
| 494 |
return NULL; |
| 495 |
} |
| 496 |
|
| 497 |
if (len==(SIZE_MAX>>1)) { |
| 498 |
fclose(f); |
| 499 |
BUSY_END; |
| 500 |
lastError="file is invalid!"; |
| 501 |
delete sample; |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
if (fseek(f,0,SEEK_SET)<0) { |
| 506 |
fclose(f); |
| 507 |
BUSY_END; |
| 508 |
lastError=fmt::sprintf("could not seek to beginning of file! (%s)",strerror(errno)); |
| 509 |
delete sample; |
| 510 |
return NULL; |
| 511 |
} |
| 512 |
|
| 513 |
lenDivided=len/channels; |
| 514 |
|
| 515 |
unsigned int samples=lenDivided; |
| 516 |
switch (depth) { |
| 517 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 518 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 519 |
samples=lenDivided*8; |
| 520 |
break; |
| 521 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 522 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: |
| 523 |
case DIV_SAMPLE_DEPTH_ADPCM_A: |
| 524 |
case DIV_SAMPLE_DEPTH_ADPCM_B: |
| 525 |
case DIV_SAMPLE_DEPTH_ADPCM_K: |
| 526 |
case DIV_SAMPLE_DEPTH_VOX: |
| 527 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 528 |
samples=lenDivided*2; |
| 529 |
break; |
| 530 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 531 |
samples=(lenDivided-4)*2; |
| 532 |
break; |
| 533 |
case DIV_SAMPLE_DEPTH_8BIT: |
| 534 |
case DIV_SAMPLE_DEPTH_MULAW: |
| 535 |
case DIV_SAMPLE_DEPTH_C219: |
| 536 |
samples=lenDivided; |
| 537 |
break; |
| 538 |
case DIV_SAMPLE_DEPTH_BRR: |
| 539 |
samples=16*((lenDivided+8)/9); |
| 540 |
break; |
| 541 |
case DIV_SAMPLE_DEPTH_12BIT: |
| 542 |
samples=(2+(lenDivided*2))/3; |
| 543 |
break; |
| 544 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 545 |
samples=(lenDivided+1)/2; |
| 546 |
break; |
| 547 |
default: |
| 548 |
break; |
| 549 |
} |
| 550 |
|
| 551 |
if (samples>16777215) { |
| 552 |
fclose(f); |
| 553 |
BUSY_END; |
| 554 |
lastError="this sample is too big! max sample size is 16777215."; |
| 555 |
delete sample; |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
|
| 559 |
sample->rate=rate; |
| 560 |
sample->centerRate=rate; |
| 561 |
sample->depth=depth; |
| 562 |
sample->init(samples); |
| 563 |
|
| 564 |
unsigned char* buf=new unsigned char[len]; |
| 565 |
if (fread(buf,1,len,f)==0) { |
| 566 |
fclose(f); |
| 567 |
BUSY_END; |
| 568 |
lastError=fmt::sprintf("could not read file! (%s)",strerror(errno)); |
| 569 |
delete[] buf; |
| 570 |
delete sample; |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
|
| 574 |
fclose(f); |
| 575 |
|
| 576 |
// import sample |
| 577 |
size_t pos=0; |
| 578 |
if (depth==DIV_SAMPLE_DEPTH_16BIT) { |
| 579 |
for (unsigned int i=0; i<samples; i++) { |
| 580 |
int accum=0; |
| 581 |
for (int j=0; j<channels; j++) { |
| 582 |
if (pos+1>=len) break; |
| 583 |
if (bigEndian) { |
| 584 |
accum+=(short)(((short)((buf[pos]<<8)|buf[pos+1]))^(unsign?0x8000:0)); |
| 585 |
} else { |
| 586 |
accum+=(short)(((short)(buf[pos]|(buf[pos+1]<<8)))^(unsign?0x8000:0)); |
| 587 |
} |
| 588 |
pos+=2; |
| 589 |
} |
| 590 |
accum/=channels; |
| 591 |
sample->data16[i]=accum; |
| 592 |
} |
| 593 |
} else if (depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 594 |
for (unsigned int i=0; i<samples; i++) { |
| 595 |
int accum=0; |
| 596 |
for (int j=0; j<channels; j++) { |
| 597 |
if (pos>=len) break; |
| 598 |
accum+=(signed char)(buf[pos++]^(unsign?0x80:0)); |
| 599 |
} |
| 600 |
accum/=channels; |
| 601 |
sample->data8[i]=accum; |
| 602 |
} |
| 603 |
if (bigEndian) { |
| 604 |
for (unsigned int i=0; (i+1)<samples; i+=2) { |
| 605 |
sample->data8[i]^=sample->data8[i^1]; |
| 606 |
sample->data8[i^1]^=sample->data8[i]; |
| 607 |
sample->data8[i]^=sample->data8[i^1]; |
| 608 |
} |
| 609 |
} |
| 610 |
} else { |
| 611 |
memcpy(sample->getCurBuf(),buf,len); |
| 612 |
} |
| 613 |
delete[] buf; |
| 614 |
|
| 615 |
if (swapNibbles) { |
| 616 |
unsigned char* b=(unsigned char*)sample->getCurBuf(); |
| 617 |
switch (depth) { |
| 618 |
case DIV_SAMPLE_DEPTH_1BIT: |
| 619 |
case DIV_SAMPLE_DEPTH_1BIT_DPCM: |
| 620 |
// reverse bit order |
| 621 |
for (unsigned int i=0; i<sample->getCurBufLen(); i++) { |
| 622 |
b[i]=( |
| 623 |
((b[i]&128)?1:0)| |
| 624 |
((b[i]&64)?2:0)| |
| 625 |
((b[i]&32)?4:0)| |
| 626 |
((b[i]&16)?8:0)| |
| 627 |
((b[i]&8)?16:0)| |
| 628 |
((b[i]&4)?32:0)| |
| 629 |
((b[i]&2)?64:0)| |
| 630 |
((b[i]&1)?128:0) |
| 631 |
); |
| 632 |
} |
| 633 |
break; |
| 634 |
case DIV_SAMPLE_DEPTH_YMZ_ADPCM: |
| 635 |
case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: |
| 636 |
case DIV_SAMPLE_DEPTH_ADPCM_A: |
| 637 |
case DIV_SAMPLE_DEPTH_ADPCM_B: |
| 638 |
case DIV_SAMPLE_DEPTH_ADPCM_K: |
| 639 |
case DIV_SAMPLE_DEPTH_VOX: |
| 640 |
case DIV_SAMPLE_DEPTH_4BIT: |
| 641 |
// swap nibbles |
| 642 |
for (unsigned int i=0; i<sample->getCurBufLen(); i++) { |
| 643 |
b[i]=(b[i]<<4)|(b[i]>>4); |
| 644 |
} |
| 645 |
break; |
| 646 |
case DIV_SAMPLE_DEPTH_MULAW: |
| 647 |
// Namco to G.711 |
| 648 |
// Namco: smmmmxxx |
| 649 |
// G.711: sxxxmmmm (^0xff) |
| 650 |
for (unsigned int i=0; i<sample->getCurBufLen(); i++) { |
| 651 |
b[i]=(((b[i]&7)<<4)|(((b[i]>>3)&15)^((b[i]&0x80)?15:0))|(b[i]&0x80))^0xff; |
| 652 |
} |
| 653 |
break; |
| 654 |
default: |
| 655 |
break; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
BUSY_END; |
| 660 |
return sample; |
| 661 |
} |