comparison tools/xm2jss.c @ 1310:dce4730372c7

Add some blocks, cosmetics.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 20 Aug 2017 01:01:12 +0300
parents c44fcdc1c2e7
children eb3f883f4acb
comparison
equal deleted inserted replaced
1309:5ad7d780a09b 1310:dce4730372c7
461 size_t totalSize; 461 size_t totalSize;
462 int index; 462 int index;
463 463
464 // Check the module 464 // Check the module
465 if (module == NULL) 465 if (module == NULL)
466 {
466 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR, 467 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR,
467 "Module pointer was NULL\n"); 468 "Module pointer was NULL\n");
469 }
468 470
469 if (module->nchannels < 1 || module->npatterns < 1 || module->norders < 1 || 471 if (module->nchannels < 1 || module->npatterns < 1 || module->norders < 1 ||
470 module->nchannels > jsetMaxChannels || 472 module->nchannels > jsetMaxChannels ||
471 module->npatterns > jsetMaxPatterns || 473 module->npatterns > jsetMaxPatterns ||
472 module->norders > jsetMaxOrders) 474 module->norders > jsetMaxOrders)
475 {
473 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, 476 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS,
474 "Module had invalid values (nchannels=%d, npatterns=%d, norders=%d)\n", 477 "Module had invalid values (nchannels=%d, npatterns=%d, norders=%d)\n",
475 module->nchannels, module->npatterns, module->norders); 478 module->nchannels, module->npatterns, module->norders);
479 }
476 480
477 // Create the JSSMOD header 481 // Create the JSSMOD header
478 jssH.idMagic[0] = 'J'; 482 jssH.idMagic[0] = 'J';
479 jssH.idMagic[1] = 'M'; 483 jssH.idMagic[1] = 'M';
480 jssH.idVersion = JSSMOD_VERSION; 484 jssH.idVersion = JSSMOD_VERSION;
505 509
506 !dm_fwrite_byte(outFile, jssH.nchannels) || 510 !dm_fwrite_byte(outFile, jssH.nchannels) ||
507 !dm_fwrite_byte(outFile, jssH.defSpeed) || 511 !dm_fwrite_byte(outFile, jssH.defSpeed) ||
508 !dm_fwrite_byte(outFile, jssH.defTempo) || 512 !dm_fwrite_byte(outFile, jssH.defTempo) ||
509 !dm_fwrite_byte(outFile, jssH.patMode)) 513 !dm_fwrite_byte(outFile, jssH.patMode))
514 {
510 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, 515 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
511 "Error writing JSSMOD header!\n"); 516 "Error writing JSSMOD header!\n");
517 }
512 518
513 totalSize = sizeof(jssH); 519 totalSize = sizeof(jssH);
514 dmMsg(1," * JSSMOD-header 0x%04x, %d bytes.\n", JSSMOD_VERSION, totalSize); 520 dmMsg(1," * JSSMOD-header 0x%04x, %d bytes.\n", JSSMOD_VERSION, totalSize);
515 521
516 // Write orders list 522 // Write orders list
517 for (totalSize = index = 0; index < module->norders; index++) 523 for (totalSize = index = 0; index < module->norders; index++)
518 { 524 {
519 int tmp = module->orderList[index]; 525 int tmp = module->orderList[index];
520 if (tmp != jsetNotSet && tmp > module->npatterns) 526 if (tmp != jsetNotSet && tmp > module->npatterns)
527 {
521 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, 528 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
522 "Orderlist entry #%d has invalid value %d.\n", 529 "Orderlist entry #%d has invalid value %d.\n",
523 index, tmp); 530 index, tmp);
531 }
524 532
525 if (tmp == jsetNotSet) 533 if (tmp == jsetNotSet)
526 tmp = 0xffff; 534 tmp = 0xffff;
527 535
528 if (!dm_fwrite_le16(outFile, tmp)) 536 if (!dm_fwrite_le16(outFile, tmp))
536 dmMsg(1," * %d item orders list, %d bytes.\n", 544 dmMsg(1," * %d item orders list, %d bytes.\n",
537 module->norders, totalSize); 545 module->norders, totalSize);
538 546
539 // Allocate pattern compression buffer 547 // Allocate pattern compression buffer
540 if ((patBuf = dmMalloc(patBufSize)) == NULL) 548 if ((patBuf = dmMalloc(patBufSize)) == NULL)
549 {
541 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, 550 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
542 "Error allocating memory for pattern compression buffer.\n"); 551 "Error allocating memory for pattern compression buffer.\n");
552 }
543 553
544 // Convert and write patterns 554 // Convert and write patterns
545 for (totalSize = index = 0; index < module->npatterns; index++) 555 for (totalSize = index = 0; index < module->npatterns; index++)
546 if (module->patterns[index] != NULL) 556 if (module->patterns[index] != NULL)
547 { 557 {
548 JSSPattern *pattern = module->patterns[index]; 558 JSSPattern *pattern = module->patterns[index];
549 size_t dataSize = 0; 559 size_t dataSize = 0;
550 int ret; 560 int ret;
551 561
552 if (pattern->nrows > jsetMaxRows) 562 if (pattern->nrows > jsetMaxRows)
563 {
553 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, 564 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
554 "Pattern #%d has %d rows > %d max.\n", 565 "Pattern #%d has %d rows > %d max.\n",
555 index, pattern->nrows, jsetMaxRows); 566 index, pattern->nrows, jsetMaxRows);
567 }
556 568
557 switch (patMode) 569 switch (patMode)
558 { 570 {
559 case PATMODE_RAW_HORIZ: 571 case PATMODE_RAW_HORIZ:
560 ret = jssConvertPatternRawHoriz(patBuf, patBufSize, &dataSize, pattern); 572 ret = jssConvertPatternRawHoriz(patBuf, patBufSize, &dataSize, pattern);
576 "Unsupported pattern conversion mode %d for pattern #%d.\n", 588 "Unsupported pattern conversion mode %d for pattern #%d.\n",
577 patMode, index); 589 patMode, index);
578 } 590 }
579 591
580 if (ret != DMERR_OK) 592 if (ret != DMERR_OK)
593 {
581 JSSERROR(ret, ret, "Error converting pattern data #%d.\n", 594 JSSERROR(ret, ret, "Error converting pattern data #%d.\n",
582 pattern); 595 pattern);
596 }
583 597
584 dmMsg(3, " - Pattern %d size %d bytes\n", index, dataSize); 598 dmMsg(3, " - Pattern %d size %d bytes\n", index, dataSize);
585 totalSize += dataSize + sizeof(JSSMODPattern); 599 totalSize += dataSize + sizeof(JSSMODPattern);
586 600
587 if (!dm_fwrite_le32(outFile, dataSize) || 601 if (!dm_fwrite_le32(outFile, dataSize) ||
588 !dm_fwrite_le16(outFile, pattern->nrows) || 602 !dm_fwrite_le16(outFile, pattern->nrows) ||
589 !dm_fwrite_str(outFile, patBuf, dataSize)) 603 !dm_fwrite_str(outFile, patBuf, dataSize))
604 {
590 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, 605 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
591 "Error writing JSSMOD pattern #%d.\n", 606 "Error writing JSSMOD pattern #%d.\n",
592 index); 607 index);
593 } 608 }
594 else 609 }
610 else
611 {
595 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR, 612 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR,
596 "Pattern #%d was NULL.\n", index); 613 "Pattern #%d was NULL.\n", index);
614 }
597 615
598 dmFree(patBuf); 616 dmFree(patBuf);
599 dmMsg(1," * %d patterns, %d bytes.\n", 617 dmMsg(1," * %d patterns, %d bytes.\n",
600 module->npatterns, totalSize); 618 module->npatterns, totalSize);
601 619
622 } 640 }
623 641
624 if (!ok || 642 if (!ok ||
625 !jssMODWriteEnvelope(outFile, &einst->volumeEnv, "volume", index) || 643 !jssMODWriteEnvelope(outFile, &einst->volumeEnv, "volume", index) ||
626 !jssMODWriteEnvelope(outFile, &einst->panningEnv, "panning", index)) 644 !jssMODWriteEnvelope(outFile, &einst->panningEnv, "panning", index))
645 {
627 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, 646 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
628 "Error writing JSSMOD extended instrument #%d.\n", 647 "Error writing JSSMOD extended instrument #%d.\n",
629 index); 648 index);
649 }
630 650
631 totalSize += sizeof(JSSMODExtInstrument); 651 totalSize += sizeof(JSSMODExtInstrument);
632 } 652 }
633 else 653 else
654 {
634 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, 655 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR,
635 "Extended instrument #%d NULL!\n", 656 "Extended instrument #%d NULL!\n",
636 index); 657 index);
658 }
637 659
638 dmMsg(1," * %d Extended Instruments, %d bytes.\n", 660 dmMsg(1," * %d Extended Instruments, %d bytes.\n",
639 module->nextInstruments, totalSize); 661 module->nextInstruments, totalSize);
640 662
641 // Write sample instrument headers 663 // Write sample instrument headers
656 !dm_fwrite_le16(outFile, inst->ERelNote) || 678 !dm_fwrite_le16(outFile, inst->ERelNote) ||
657 !dm_fwrite_le16(outFile, inst->EFineTune) || 679 !dm_fwrite_le16(outFile, inst->EFineTune) ||
658 !dm_fwrite_le16(outFile, inst->EPanning) || 680 !dm_fwrite_le16(outFile, inst->EPanning) ||
659 !dm_fwrite_byte(outFile, inst->volume) || 681 !dm_fwrite_byte(outFile, inst->volume) ||
660 !dm_fwrite_byte(outFile, inst->convFlags)) 682 !dm_fwrite_byte(outFile, inst->convFlags))
683 {
661 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, 684 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
662 "Error writing JSSMOD instrument #%d.\n", 685 "Error writing JSSMOD instrument #%d.\n",
663 index); 686 index);
687 }
664 688
665 totalSize += sizeof(JSSMODInstrument); 689 totalSize += sizeof(JSSMODInstrument);
666 } 690 }
667 else 691 else
692 {
668 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, 693 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR,
669 "Instrument #%d NULL!\n", index); 694 "Instrument #%d NULL!\n", index);
695 }
670 696
671 dmMsg(1," * %d Instrument headers, %d bytes.\n", 697 dmMsg(1," * %d Instrument headers, %d bytes.\n",
672 module->ninstruments, totalSize); 698 module->ninstruments, totalSize);
673 699
674 // Write sample data 700 // Write sample data
688 bsize *= sizeof(Uint8); 714 bsize *= sizeof(Uint8);
689 jssEncodeSample8(inst->data, inst->size, flags8); 715 jssEncodeSample8(inst->data, inst->size, flags8);
690 } 716 }
691 717
692 if (!dm_fwrite_str(outFile, inst->data, bsize)) 718 if (!dm_fwrite_str(outFile, inst->data, bsize))
719 {
693 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, 720 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
694 "Error writing JSSMOD sample data for instrument #%d.\n", 721 "Error writing JSSMOD sample data for instrument #%d.\n",
695 index); 722 index);
723 }
696 724
697 totalSize += bsize; 725 totalSize += bsize;
698 } 726 }
699 } 727 }
700 728