home *** CD-ROM | disk | FTP | other *** search
/ messroms.de / 2007-01-13_www.messroms.de.zip / JUPITER / TOOLS / TAPECONV.ZIP / TAPH.C next >
Text File  |  1997-10-30  |  3KB  |  123 lines

  1. /*
  2.  
  3.     Based on the theory behind TAPH.PAS by TechnoMancer.
  4.  
  5.     Requires a 22khz Mono Sample in .WAV format
  6.  
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. FILE *f,*o;
  13.  
  14. void WaveOpen(char *FName)
  15. {
  16. f = fopen(FName,"rb");                    /* Open input file */
  17. fseek(f,44L,SEEK_SET);                    /* Skip the header */
  18. o = fopen("out.tap","wb");                /* Open the output file */
  19. }
  20.  
  21.  
  22. static long last;                        /* Last Edge offset */
  23.  
  24. int flip = 0;                            /* 0 = last went up,1 = last down */
  25.  
  26. int GetEdgeSize(void)
  27. {
  28. int n1,n2,n3;
  29. long res,curr;
  30.  
  31. n1 = n2 = n3 = 0;                        /* Initial values */
  32. if (flip == 0)
  33.         n1 = n2 = n3 = 255;
  34.  
  35. while (1)
  36.     {
  37.     n3 = fgetc(f);                        /* Get new one */
  38.     n3 = n3 & 0xFF;
  39.                                         /* When in order and zero-x */
  40.     if ( (n1 < n2 && n2 < n3 && n1 < 128 && n3 > 128) ||
  41.          (n1 > n2 && n2 > n3 && n1 > 128 && n3 < 128))
  42.         {
  43.         flip = (n1 < n2) ? 0 : 1;        /* Which way are we going ? */
  44.         curr = ftell(f);                /* Work out offset to last */
  45.         res = curr-last;
  46.         last = curr;                    /* update last */
  47.         return((int)res);                /* return distance */
  48.         }
  49.     n1 = n2;n2 = n3;                    /* Otherwise rotate next in */
  50.     }
  51. return(0);
  52. }
  53.  
  54. struct _Block                            /* Structure of ACE Header */
  55. {
  56. unsigned char Type;
  57. char Name[10];
  58. unsigned int Size;
  59. char Padding[32];
  60. } Block;
  61.  
  62. void ReadBlock(int Size,unsigned char *Block)
  63. {
  64. int i,b,n,e1,e2;
  65. while (i = GetEdgeSize(),                /* Skip leader */
  66.                 i >= 11 && i < 17) { }
  67.  
  68. Size++;
  69.  
  70. for (i = 0;i < 8*2+2;i++)                /* Skip miscellaneous stuff... */
  71.                         GetEdgeSize();
  72.  
  73. fputc(Size & 0xFF,o);                    /* Header to output file */
  74. fputc(Size >> 8,o);
  75.  
  76. for (i = 0;i < Size;i++)                /* Copy bytes */
  77.     {
  78.     b = 0;
  79.     for (n = 0;n < 8;n++)                /* Read in a bit at a time */
  80.         {
  81.         b = b * 2;
  82.         e1 = GetEdgeSize();
  83.         e2 = GetEdgeSize();                /* One complete wave per bit */
  84. /*        printf("%d:%d ",e1,e2); */
  85.         if (e1 > 8) b++;                /* If leading edge > 8 its a '1' */
  86.         }
  87. /*    printf("%02x %c ",b,b & 0x7F); */
  88.     fputc(b,o);                            /* Write to .TAP file */
  89.     if (Block != NULL) *(Block+i) = b;    /* Store in header if applicable */
  90.     }
  91. printf("Read block %d bytes.\n",Size);
  92. for (i = 0;i < 40;i++) printf("%d ",GetEdgeSize());
  93. }
  94.  
  95. void main(int argc,char *argv[])
  96. {
  97. int i;
  98. if (argc != 2) exit(printf("TAPH <wave file>\n"));
  99. WaveOpen(argv[1]);
  100. if (f == NULL) exit(printf("Couldn't open....\n"));
  101.  
  102. for (i = 0;i < 14;i++) GetEdgeSize();    /* Skip any junk */
  103.  
  104. ReadBlock(25,(unsigned char *)&Block);    /* Read header */
  105.  
  106. printf("Block Type : %d\n",Block.Type);    /* Print information */
  107. printf("Block Size : %d\n",Block.Size);
  108. printf("Block Name : ");
  109. for (i = 0;i < 10;i++) printf("%c",Block.Name[i] & 0x7F);
  110. printf("\n");
  111.  
  112. ReadBlock(Block.Size,NULL);                /* Read body */
  113. fclose(f);
  114. fclose(o);
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.