1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
|
//#include "stdafx.h"
#include "TZXFile.h"
#include <stdlib.h>
#include <string.h>
TZXFile::TZXFile()
{
m_bDecodeSuccesful = false;
m_nFileOffset = 0;
m_pSourceFile = NULL;
m_nFileLength = 0;
m_nMajorRev = 0;
m_nMinorRev = 0;
m_nBlockCount = 0;
memset(m_pBlocks, 0, sizeof(TZXBlock *) * MAX_TZX_BLOCKS);
m_pAudioGenerator = new TZXAudioGenerator();
m_eFileType = FileTypeUndetermined;
}
TZXFile::~TZXFile()
{
for (int i = 0; i < m_nBlockCount; i++)
{
delete m_pBlocks[i];
}
delete m_pAudioGenerator;
}
bool TZXFile::AddToBlockList(TZXBlock *pBlock)
{
if (m_nBlockCount >= MAX_TZX_BLOCKS)
{
return false;
}
m_pBlocks[m_nBlockCount] = pBlock;
m_nBlockCount++;
return true;
}
int TZXFile::GetBlockCount()
{
return m_nBlockCount;
}
TZXBlock *TZXFile::GetBlockPtr(int nBlockNum)
{
return m_pBlocks[nBlockNum];
}
int TZXFile::GetAudioBufferLengthInSamples()
{
return m_pAudioGenerator->m_nAudioDataLengthinSamples;
}
bool TZXFile::ReadBytes(unsigned char *pDest, int nCount)
{
if(nCount < 0) return false;
if ((m_nFileOffset + nCount) > m_nFileLength) return false;
memcpy(pDest, m_pSourceFile+m_nFileOffset, nCount);
m_nFileOffset += nCount;
return true;
}
int TZXFile::DisplayError(int nError, void *param)
{
switch (nError)
{
case TZX_UNEXPECTED_EOF:
printf("Unexpected end of file, reading read beyond end of file.\n");
break;
case TZX_BAD_TZX_SIGNATURE:
printf("Bad TZX Signature in file, expected ZXTape!, but got %s\n", (char *)param);
break;
case TZX_UNSUPPORTED_REVISION:
printf("Unsupported TZX revision: %d.%d\n", m_nMajorRev, m_nMinorRev);
break;
case TZX_UNHANDLED_BLOCK_TYPE:
printf("The block type 0x%02x is not handled by this utility.\n", *(unsigned char *)param);
break;
case TZX_BLOCK_LIST_OVERFLOW:
printf("Block list overflow, the current build can only support a maximum of %d blocks in a TZX file.\n", MAX_TZX_BLOCKS);
break;
default:
printf("Unknown error code: %d\n", nError);
}
return nError;
}
int TZXFile::DecodeTextDescriptionBlock()
{
unsigned char nTextLength = 0;
if (!ReadBytes(&nTextLength, 1)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
TZXBlockTextDescription *tdb = new TZXBlockTextDescription();
tdb->m_szDescription = (unsigned char *)malloc(nTextLength + 1);
if (!ReadBytes(tdb->m_szDescription, nTextLength))
{
delete tdb;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
tdb->m_szDescription[nTextLength] = 0; // Add null termination
if (!AddToBlockList(tdb))
{
delete tdb;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeStandardSpeedDataBlock()
{
TZXBlockStandardSpeedData *ssd = new TZXBlockStandardSpeedData();
if (!ReadBytes((unsigned char *)&ssd->m_nPauseAfterBlockInMS, 2))
{
delete ssd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&ssd->m_nLengthOfData, 2))
{
delete ssd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
ssd->m_pData = (unsigned char *)malloc(ssd->m_nLengthOfData);
if (!ReadBytes(ssd->m_pData, ssd->m_nLengthOfData))
{
delete ssd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(ssd))
{
delete ssd;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeArchiveInfoBlock()
{
TZXBlockArchiveInfo *ai = new TZXBlockArchiveInfo();
unsigned short blockLength = 0;
if (!ReadBytes((unsigned char *)(&blockLength), 2))
{
delete ai;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes(&ai->m_nTextStringCount, 1))
{
delete ai;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
for (int i = 0; i < ai->m_nTextStringCount; i++)
{
if (!ReadBytes(&ai->m_nTextStringIdentificationByte[i], 1))
{
delete ai;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
unsigned char strLen = 0;
if (!ReadBytes(&strLen, 1))
{
delete ai;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
ai->m_szTextStrings[i] = (char *)malloc(strLen + 1);
if (!ReadBytes((unsigned char *)(ai->m_szTextStrings[i]), strLen))
{
delete ai;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
ai->m_szTextStrings[i][strLen] = 0;
}
if (!AddToBlockList(ai))
{
delete ai;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeGroupStartBlock()
{
unsigned char nTextLength = 0;
if (!ReadBytes(&nTextLength, 1)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
TZXBlockGroupStart *gs = new TZXBlockGroupStart();
gs->m_szName = (char *)malloc(nTextLength + 1);
if (!ReadBytes((unsigned char *)gs->m_szName, nTextLength))
{
delete gs;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
gs->m_szName[nTextLength] = 0; // Add null termination
if (!AddToBlockList(gs))
{
delete gs;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeTurboSpeedDataBlock()
{
TZXBlockTurboSpeedData *tsd = new TZXBlockTurboSpeedData();
if (!ReadBytes((unsigned char *)&tsd->m_nPilotPulse, 2))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&tsd->m_nFirstSyncPulse, 2))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&tsd->m_nSecondSyncPulse, 2))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&tsd->m_nZeroBitPulse, 2))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&tsd->m_nOneBitPulse, 2))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&tsd->m_nPilotCount, 2))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&tsd->m_nBitsInLastByte, 1))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&tsd->m_nDelayAfterBlock, 2))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
unsigned char temp[3];
if (!ReadBytes((unsigned char *)temp, 3))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
tsd->m_nDataLength = 0;
tsd->m_nDataLength = temp[0] | (temp[1] << 8) | (temp[2] << 16);
tsd->m_pData = (unsigned char *)malloc(tsd->m_nDataLength);
if (!ReadBytes((unsigned char *)tsd->m_pData, tsd->m_nDataLength))
{
delete tsd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(tsd))
{
delete tsd;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeLoopStartBlock()
{
TZXBlockLoopStart *ls = new TZXBlockLoopStart();
if (!ReadBytes((unsigned char *)&ls->m_nRepitions, 2))
{
delete ls;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(ls))
{
delete ls;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodePureToneBlock()
{
TZXBlockPureTone *pt = new TZXBlockPureTone();
if (!ReadBytes((unsigned char *)&pt->m_nPulseLengthInTStates, 2))
{
delete pt;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&pt->m_nNumberOfPulses, 2))
{
delete pt;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(pt))
{
delete pt;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodePulseSequenceBlock()
{
TZXBlockPulseSequence *ps = new TZXBlockPulseSequence();
if (!ReadBytes((unsigned char *)&ps->m_nPulseCount, 1))
{
delete ps;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
for (int i = 0; i < ps->m_nPulseCount; i++)
{
if (!ReadBytes((unsigned char *)&(ps->m_nPulseLength[i]), 2))
{
delete ps;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
}
if (!AddToBlockList(ps))
{
delete ps;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeLoopEndBlock()
{
TZXBlockLoopEnd *le = new TZXBlockLoopEnd();
if (!AddToBlockList(le))
{
delete le;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeGroupEndBlock()
{
TZXBlockGroupEnd *le = new TZXBlockGroupEnd();
if (!AddToBlockList(le))
{
delete le;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodePauseBlock()
{
TZXBlockPause *p = new TZXBlockPause();
if (!ReadBytes((unsigned char *)&p->m_nPauseInMS, 2))
{
delete p;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(p))
{
delete p;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodePureDataBlock()
{
TZXBlockPureData *pd = new TZXBlockPureData();
if (!ReadBytes((unsigned char *)&pd->m_nLengthOfZeroPulse, 2))
{
delete pd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&pd->m_nLengthOfOnePulse, 2))
{
delete pd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&pd->m_nBitsInLastByte,1))
{
delete pd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&pd->m_nPauseAfterBlock,2))
{
delete pd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
unsigned char temp[3];
if (!ReadBytes((unsigned char *)temp, 3))
{
delete pd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
pd->m_nDataLength = 0;
pd->m_nDataLength = temp[0] | (temp[1] << 8) | (temp[2] << 16);
pd->m_pData = (unsigned char *)malloc(pd->m_nDataLength);
if (!ReadBytes((unsigned char *)pd->m_pData, pd->m_nDataLength))
{
delete pd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(pd))
{
delete pd;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeHardwareTypeBlock()
{
TZXBlockHardwareType *ht = new TZXBlockHardwareType();
if (!ReadBytes((unsigned char *)&ht->m_nHwInfoCount, 1))
{
delete ht;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
ht->m_pHWInfo = (THWINFO *)malloc(sizeof(THWINFO) * ht->m_nHwInfoCount);
for (int i = 0; i < ht->m_nHwInfoCount; i++)
{
THWINFO *phwInfo = &(ht->m_pHWInfo[i]);
if (!ReadBytes((unsigned char *)&phwInfo->nHardwareType, 1))
{
delete ht;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&phwInfo->nHardwareID, 1))
{
delete ht;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&phwInfo->nHardwareInfo, 1))
{
delete ht;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
}
if (!AddToBlockList(ht))
{
delete ht;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeStopTheTape48KBlock()
{
int temp;
TZXBlockStopTheTape48K *st = new TZXBlockStopTheTape48K();
if (!ReadBytes((unsigned char *)&temp, 4))
{
delete st;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(st))
{
delete st;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeCustomInfoBlock()
{
TZXBlockCustomInfo *ci = new TZXBlockCustomInfo();
if (!ReadBytes((unsigned char *)&ci->m_szIDString[0], 16))
{
delete ci;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
ci->m_szIDString[16] = 0;
if (!ReadBytes((unsigned char *)&ci->m_nCustomInfoLength, 4))
{
delete ci;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
ci->m_pCustomData = (unsigned char *)malloc(ci->m_nCustomInfoLength);
if (!ReadBytes((unsigned char *)ci->m_pCustomData, ci->m_nCustomInfoLength))
{
delete ci;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!AddToBlockList(ci))
{
delete ci;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
int TZXFile::DecodeMessageBlock()
{
TZXBlockMessage *m = new TZXBlockMessage();
if (!ReadBytes((unsigned char *)&m->m_nDisplayTimeInSeconds, 1))
{
delete m;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)&m->m_nMessageLength, 1))
{
delete m;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
if (!ReadBytes((unsigned char *)m->m_szMessageString, m->m_nMessageLength))
{
delete m;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
m->m_szMessageString[m->m_nMessageLength] = 0;
if (!AddToBlockList(m))
{
delete m;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
return TZX_SUCCESS;
}
EFileType TZXFile::DecodeFile(unsigned char *pData, int nFileLength)
{
// Try to decode the file as a Tzx file and if that doesn't work try to decode it as a Tap File
int result = DecodeTzxFile(pData, nFileLength);
if(result == TZX_SUCCESS)
{
m_eFileType = FileTypeTzx;
return FileTypeTzx;
}
result = DecodeTapFileData(pData, nFileLength);
if(result == TZX_SUCCESS)
{
m_eFileType = FileTypeTap;
return FileTypeTap;
}
m_eFileType = FileTypeUndetermined;
return FileTypeUndetermined;
}
int TZXFile::DecodeTapFileData(unsigned char *pData, int nFileLength)
{
// Tap file support is kind of an after thought hack whereby the tap file will be loaded and converted into a TZX containing standard loading blocks using the data in the tap file
m_bDecodeSuccesful = false;
m_nFileOffset = 0;
m_nFileLength = nFileLength;
m_pSourceFile = pData;
while(m_nFileOffset < m_nFileLength)
{
// Decode the next chunk of the tap file
// Read 2 bytes, these are the length of the chunk
unsigned short nChunkLength;
if(!ReadBytes((unsigned char *)&nChunkLength, 2)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
unsigned char nFlag;
if(!ReadBytes(&nFlag,1))return DisplayError(TZX_UNEXPECTED_EOF, NULL);
if(nFlag == 0x00) // Header
{
TZXBlockStandardSpeedData *ssd = new TZXBlockStandardSpeedData();
ssd->m_nPauseAfterBlockInMS = 1000;
ssd->m_nLengthOfData = nChunkLength;
ssd->m_pData = (unsigned char *)malloc(ssd->m_nLengthOfData);
*ssd->m_pData = nFlag;
if (!ReadBytes((ssd->m_pData)+1, ssd->m_nLengthOfData-1))
{
delete ssd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
char name[11];
memcpy(name, ssd->m_pData+1, 10);
name[10] = 0;
printf("Header Name = %s.\n", name);
if (!AddToBlockList(ssd))
{
delete ssd;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
} else if(nFlag==0xFF){
TZXBlockStandardSpeedData *ssd = new TZXBlockStandardSpeedData();
ssd->m_nPauseAfterBlockInMS = 3000;
ssd->m_nLengthOfData = nChunkLength;
ssd->m_pData = (unsigned char *)malloc(ssd->m_nLengthOfData);
*ssd->m_pData = nFlag;
if (!ReadBytes(ssd->m_pData+1, ssd->m_nLengthOfData-1))
{
delete ssd;
return DisplayError(TZX_UNEXPECTED_EOF, NULL);
}
printf("Code Length = %d.\n", nChunkLength-2);
if (!AddToBlockList(ssd))
{
delete ssd;
return DisplayError(TZX_BLOCK_LIST_OVERFLOW, NULL);
}
} else {
return DisplayError(TZX_UNHANDLED_BLOCK_TYPE, NULL);
}
}
m_bDecodeSuccesful = true;
return TZX_SUCCESS;
}
int TZXFile::DecodeTzxFile(unsigned char *pData, int nFileLength)
{
m_bDecodeSuccesful = false;
m_nFileOffset = 0;
m_nFileLength = nFileLength;
m_pSourceFile = pData;
// Read the TZX Header
// First TZX Signature
unsigned char szTemp[1024];
if (!ReadBytes(szTemp, 7)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
szTemp[7] = 0;
if (strcmp((char *)szTemp, "ZXTape!") != 0) return DisplayError(TZX_BAD_TZX_SIGNATURE, szTemp);
// end of text marker
if (!ReadBytes(szTemp, 1)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
if (szTemp[0] != 0x1a) return DisplayError(TZX_BAD_TZX_SIGNATURE, szTemp);
// Major / minor revision
if (!ReadBytes(&m_nMajorRev, 1)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
if (!ReadBytes(&m_nMinorRev, 1)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
if (m_nMajorRev > 1) return DisplayError(TZX_UNSUPPORTED_REVISION, NULL);
if((m_nMajorRev == 1) && (m_nMinorRev > 20)) return DisplayError(TZX_UNSUPPORTED_REVISION, NULL);
// Read the blocks...
while (m_nFileOffset < m_nFileLength)
{
// Read the next block
// Read the block type identifier
unsigned char nBlockID = 0;
if (!ReadBytes(&nBlockID, 1)) return DisplayError(TZX_UNEXPECTED_EOF, NULL);
int result = 0;
switch (nBlockID)
{
case TZX_BLOCKID_STANDARD_SPEED_DATA: // 0x10
result = DecodeStandardSpeedDataBlock();
break;
case TZX_BLOCKID_TURBO_SPEED_DATA: // 0x11
result = DecodeTurboSpeedDataBlock();
break;
case TZX_BLOCKID_PURE_TONE: // 0x12
result = DecodePureToneBlock();
break;
case TZX_BLOCKID_PULSE_SEQUENCE: // 0x13
result = DecodePulseSequenceBlock();
break;
case TZX_BLOCKID_PURE_DATA: // 0x14
result = DecodePureDataBlock();
break;
case TZX_BLOCKID_PAUSE: // 0x20
result = DecodePauseBlock();
break;
case TZX_BLOCKID_GROUP_START: // 0x21
result = DecodeGroupStartBlock();
break;
case TZX_BLOCKID_GROUP_END: // 0x22
result = DecodeGroupEndBlock();
break;
case TZX_BLOCKID_LOOP_START: //0x24
result = DecodeLoopStartBlock();
break;
case TZX_BLOCKID_LOOP_END: // 0x25
result = DecodeLoopEndBlock();
break;
case TZX_BLOCKID_STOP_THE_TAPE_48K:
result = DecodeStopTheTape48KBlock();
break;
case TZX_BLOCKID_TEXT_DESCRIPTION: // 0x30
result = DecodeTextDescriptionBlock();
break;
case TZX_BLOCKID_MESSAGE_BLOCK: // 0x31
result = DecodeMessageBlock();
break;
case TZX_BLOCKID_ARCHIVE_INFO: // 0x32
result = DecodeArchiveInfoBlock();
break;
case TZX_BLOCKID_HARDWARE_TYPE: // 0x33
result = DecodeHardwareTypeBlock();
break;
case TZX_BLOCKID_CUSTOM_INFO: // 0x35
result = DecodeCustomInfoBlock();
break;
default:
return DisplayError(TZX_UNHANDLED_BLOCK_TYPE, &nBlockID);
}
if (result != TZX_SUCCESS) return result;
printf("Block %2d: %s\n", m_nBlockCount - 1, m_pBlocks[m_nBlockCount - 1]->GetDescription());
}
m_bDecodeSuccesful = true;
return TZX_SUCCESS;
}
char *TZXFile::GetAudioBufferPtr()
{
return m_pAudioGenerator->GetAudioBufferPtr();
}
int TZXFile::GetAudioBufferLength()
{
return m_pAudioGenerator->GetCurrentLength();
}
int TZXFile::GenerateAudioData()
{
// Clear any previously generated audio data in the buffer
m_pAudioGenerator->ResetBuffer();
// Go through each block and generate the audio
for (int i = 0; i < m_nBlockCount; i++)
{
TZXBlock *bl = m_pBlocks[i];
//printf("Generating audio for block %d - type %02x\n", i, bl->m_nBlockID);
bl->GenerateAudio(m_pAudioGenerator, this);
// Any additional special processing required for this block?
switch (bl->m_nBlockID)
{
case TZX_BLOCKID_LOOP_START:
m_nLoopRepitionsRemaining = ((TZXBlockLoopStart *)bl)->m_nRepitions;
m_nLoopFromBlockNum = i;
break;
case TZX_BLOCKID_LOOP_END:
m_nLoopRepitionsRemaining--;
if (m_nLoopRepitionsRemaining) i = m_nLoopFromBlockNum;
break;
default:
// No special processing
break;
}
}
return 0;
}
bool TZXFile::WriteAudioToUncompressedWavFile(const char *filename)
{
if (m_pAudioGenerator->GetAudioBufferPtr() == NULL) return false;
return m_pAudioGenerator->SaveAsUncompressedWav(filename);
}
|