comparison minijss/jloadxm.c @ 1157:e13269e17422

Clean up the sample data loading a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 05 Mar 2015 06:59:45 +0200
parents f5edb39a8db1
children 31bc984b7cd4
comparison
equal deleted inserted replaced
1156:622a469932b1 1157:e13269e17422
402 ninst, nsample, inst->loopE, inst->size); 402 ninst, nsample, inst->loopE, inst->size);
403 JSFUNSET(inst->flags, jsfLooped); 403 JSFUNSET(inst->flags, jsfLooped);
404 } 404 }
405 } 405 }
406 406
407 // Allocate memory for sample data
408 if (inst->flags & jsf16bit)
409 inst->data = dmCalloc(inst->size, sizeof(Uint16));
410 else
411 inst->data = dmCalloc(inst->size, sizeof(Uint8));
412
413 if (inst->data == NULL)
414 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
415 "Could not allocate sample data #%d/%d.\n", ninst, nsample);
416
417 return DMERR_OK; 407 return DMERR_OK;
418 } 408 }
419 409
420 410
421 static int jssXMLoadInstrumentData(DMResource *inFile, JSSInstrument *inst, int ninst, int nsample) 411 static int jssXMLoadInstrumentData(DMResource *inFile, JSSInstrument *inst, int ninst, int nsample)
422 { 412 {
423 int ret; 413 int ret;
414 size_t bsize;
424 415
425 JSSDEBUG("desc....: '%s'\n" 416 JSSDEBUG("desc....: '%s'\n"
426 "size....: %d\n" 417 "size....: %d\n"
427 "loopS...: %d\n" 418 "loopS...: %d\n"
428 "loopE...: %d\n" 419 "loopE...: %d\n"
430 "flags...: %x\n", 421 "flags...: %x\n",
431 inst->desc, 422 inst->desc,
432 inst->size, inst->loopS, inst->loopE, 423 inst->size, inst->loopS, inst->loopE,
433 inst->volume, inst->flags); 424 inst->volume, inst->flags);
434 425
426 // Allocate memory for sample data
427 bsize = (inst->flags & jsf16bit) ? sizeof(Uint16) : sizeof(Uint8);
428 bsize *= inst->size;
429
430 if ((inst->data = dmMalloc(bsize)) == NULL)
431 {
432 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
433 "Could not allocate %d bytes of sample data for instrument/sample #%d/%d.\n",
434 bsize, ninst, nsample);
435 }
436
437 // Read sampledata
438 if (dmfread(inst->data, sizeof(Uint8), bsize, inFile) != bsize)
439 {
440 JSSERROR(DMERR_FREAD, DMERR_FREAD,
441 "Error reading sample data for instrument #%d/%d, %d bytes.\n",
442 ninst, nsample, bsize);
443 }
444
445 // Convert the sample data
435 if (inst->flags & jsf16bit) 446 if (inst->flags & jsf16bit)
436 { 447 {
437 // Read sampledata 448 ret = jssDecodeSample16(
438 if (dmfread(inst->data, sizeof(Uint16), inst->size, inFile) != (size_t) inst->size) 449 (Uint16 *) inst->data, inst->size,
439 JSSERROR(DMERR_FREAD, DMERR_FREAD,
440 "Error reading sampledata for instrument #%d/%d, %d words.\n",
441 ninst, nsample, inst->size);
442
443 // Convert data
444 if ((ret = jssDecodeSample16((Uint16 *) inst->data, inst->size,
445 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) 450 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
446 (jsampDelta | jsampSwapEndianess) 451 (jsampDelta | jsampSwapEndianess)
447 #else 452 #else
448 (jsampDelta) 453 (jsampDelta)
449 #endif 454 #endif
450 )) != DMERR_OK) 455 );
451 return ret;
452 } 456 }
453 else 457 else
454 { 458 {
455 // Read sampledata 459 ret = jssDecodeSample8(
456 if (dmfread(inst->data, sizeof(Uint8), inst->size, inFile) != (size_t) inst->size) 460 (Uint8 *) inst->data, inst->size,
457 JSSERROR(DMERR_FREAD, DMERR_FREAD, 461 (jsampDelta | jsampFlipSign));
458 "Error reading sampledata for instrument #%d/%d, %d bytes.\n", 462 }
459 ninst, nsample, inst->size); 463 return ret;
460
461 // Convert data
462 if ((ret = jssDecodeSample8((Uint8 *) inst->data, inst->size,
463 (jsampDelta | jsampFlipSign))) != DMERR_OK)
464 return ret;
465 }
466 return DMERR_OK;
467 } 464 }
468 465
469 466
470 static BOOL jssXMLoadEnvelopePoints(DMResource *inFile, XMEnvelope *env) 467 static BOOL jssXMLoadEnvelopePoints(DMResource *inFile, XMEnvelope *env)
471 { 468 {