home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / beos / PPBeDevKit.ZIP / PLAYERPR.TAR / PlayerPRO / Source / Import-Export / 669.c next >
Encoding:
C/C++ Source or Header  |  1998-12-26  |  10.8 KB  |  433 lines

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 5.0 - DRIVER SOURCE CODE -
  4. //
  5. //    Library Version 5.0
  6. //
  7. //    To use with MAD Library for Mac: Symantec, CodeWarrior and MPW
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //
  14. //    COPYRIGHT ANTOINE ROSSET 1996, 1997, 1998
  15. //
  16. //    Thank you for your interest in PlayerPRO !
  17. //
  18. //    FAX:                (+41 22) 346 11 97
  19. //    PHONE:             (+41 79) 203 74 62
  20. //    Internet:     RossetAntoine@bluewin.ch
  21. //
  22. /********************                        ***********************/
  23.  
  24. #include "669.h"
  25. #include "MAD.h"
  26. #include "RDriver.h"
  27.  
  28. #if defined(powerc) || defined(__powerc)
  29. enum {
  30.         PlayerPROPlug = kCStackBased
  31.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  32.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  33.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr)))
  34.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADMusic*)))
  35.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
  36.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( MADDriverSettings*)))
  37. };
  38.  
  39. ProcInfoType __procinfo = PlayerPROPlug;
  40. #else
  41. #include <A4Stuff.h>
  42. #endif
  43.  
  44. short Tdecode16( void *msg_buf)
  45. {
  46.   unsigned char *buf = msg_buf;
  47.   
  48.   return ( (short) buf[1] << 8) | ( (short) buf[0]);
  49. }
  50.  
  51. Ptr MADPlugNewPtr( long size, MADDriverSettings* init)
  52. {
  53.     if( init->sysMemory) return NewPtrSys( size);
  54.     else return NewPtr( size);
  55. }
  56.  
  57. Ptr MADPlugNewPtrClear( long size, MADDriverSettings* init)
  58. {
  59.     if( init->sysMemory) return NewPtrSysClear( size);
  60.     else return NewPtrClear( size);
  61. }
  62.  
  63. Cmd* GetMADCommand( register short PosX, register short    TrackIdX, register PatData*    tempMusicPat)
  64. {
  65.     if( PosX < 0) PosX = 0;
  66.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  67.         
  68.     return( & (tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  69. }
  70.  
  71. void pStrcpy(register unsigned char *s1, register unsigned char *s2)
  72. {
  73.     register short len, i;
  74.     
  75.     len = *s2;
  76.     for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
  77. }
  78. void mystrcpy( Ptr a, Ptr b)
  79. {
  80.     BlockMove( b + 1, a, b[ 0]);
  81. }
  82.  
  83. OSErr Convert6692Mad( Ptr    AlienFile, long MODSize, MADMusic    *theMAD, MADDriverSettings *init)
  84. {
  85.     SixSixNine             *the669;
  86.     short                 i, PatMax, x, z;
  87.     long                     sndSize, OffSetToSample, OldTicks, temp;
  88.     Ptr                    MaxPtr;
  89.     OSErr                theErr;
  90.     Ptr                    theInstrument[ 64], destPtr;
  91.     unsigned    short        tempS;
  92.     short                Note, Octave;
  93.     Byte                    *thePasByte;
  94.     
  95.     /**** Variables pour le MAD ****/
  96.     Cmd                    *aCmd;
  97.  
  98.     /**** Variables pour le MOD ****/
  99.     
  100.     struct PatSix        *PatInt;
  101.     struct PatCmd        *theCommand;
  102.     struct SampleInfo    *SInfo;
  103.     /********************************/
  104.  
  105.     the669 = (SixSixNine*) AlienFile;
  106.  
  107.     theMAD->header = (MADSpec*) MADPlugNewPtrClear( sizeof( MADSpec), init);    
  108.  
  109.     MaxPtr = (Ptr)((long) the669 + MODSize);
  110.  
  111.     OffSetToSample = 0x1f1L + (long)  the669->NOS * 25L + (long) the669->NOP * 0x600L;
  112.  
  113.     for( i = 0; i < the669->NOS ; i++)
  114.     {
  115.         temp = (long) the669;
  116.         temp += 0x1f1L + i*25L + 13L;
  117.         
  118.         SInfo = (SampleInfo*) temp;
  119.         
  120.         SInfo->length =    Tdecode16( &SInfo->length);
  121.         SInfo->loopStart = Tdecode16( &SInfo->loopStart);
  122.         SInfo->loopEnd = Tdecode16( &SInfo->loopEnd);
  123.                 
  124.         theInstrument[i] = (Ptr) ((long) the669 + (long) OffSetToSample);
  125.         OffSetToSample += SInfo->length;
  126.     }
  127.  
  128.     /******** Le 669 a ÄtÄ lu et analysÄ ***********/
  129.     /******** Copie des informations dans le MAD ***/
  130.  
  131.     theMAD->header->MAD = 'MADI';
  132.     for(i=0; i<32; i++) theMAD->header->name[i] = the669->message[i];
  133.     
  134.     mystrcpy( theMAD->header->infos, (Ptr) "\pConverted by PlayerPRO 669 Plug (⌐Antoine ROSSET <rossetantoine@bluewin.ch>)");
  135.     
  136.     theMAD->header->numPointers = 128;    //the669->loopOrder;
  137.     theMAD->header->tempo = 125;
  138.     theMAD->header->speed = 6;
  139.     theMAD->header->numPat = the669->NOP;
  140.     for(i=0; i<128; i++)
  141.     {
  142.         theMAD->header->oPointers[ i] = the669->orderList[ i];
  143.         if( theMAD->header->oPointers[ i] >= theMAD->header->numPat) theMAD->header->oPointers[ i] = theMAD->header->numPat-1;
  144.     }
  145.     theMAD->header->numChn = 8;
  146.     
  147.     for( i = 0; i < MAXTRACK; i++)
  148. {
  149.     if( i % 2 == 0) theMAD->header->chanPan[ i] = MAX_PANNING/4;
  150.     else theMAD->header->chanPan[ i] = MAX_PANNING - MAX_PANNING/4;
  151.     
  152.     theMAD->header->chanVol[ i] = MAX_VOLUME;
  153. }
  154.  
  155.     theMAD->header->generalVol        = 64;
  156.     theMAD->header->generalSpeed    = 80;
  157.     theMAD->header->generalPitch    = 80;
  158.  
  159.     theMAD->fid = ( InstrData*) MADPlugNewPtrClear( sizeof( InstrData) * (long) MAXINSTRU, init);
  160.     if( !theMAD->fid) return MADNeedMemory;
  161.     
  162.     theMAD->sample = ( sData**) MADPlugNewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE, init);
  163.     if( !theMAD->sample) return MADNeedMemory;
  164.     
  165.     for( i = 0; i < MAXINSTRU; i++) theMAD->fid[ i].firstSample = i * MAXSAMPLE;
  166.     
  167.     for(i=0; i<the669->NOS; i++)
  168.     {
  169.         temp = (long) the669;
  170.         temp += 0x1f1L + i*25L + 13L;
  171.         
  172.         SInfo = (SampleInfo*) temp;
  173.     
  174.         if( SInfo->length > 0)
  175.         {
  176.             sData    *curData;
  177.             
  178.             theMAD->fid[ i].numSamples = 1;
  179.             theMAD->fid[i].volFade = DEFAULT_VOLFADE;
  180.             
  181.             curData = theMAD->sample[ i*MAXSAMPLE + 0] = (sData*) MADPlugNewPtrClear( sizeof( sData), init);
  182.             
  183.             curData->size        = SInfo->length;
  184.             curData->loopBeg     = 0;
  185.             curData->loopSize     = 0;
  186.             curData->vol        = 64;
  187.             curData->c2spd        = NOFINETUNE;
  188.             curData->loopType    = 0;
  189.             curData->amp        = 8;
  190.             
  191.             curData->relNote    = 0;
  192.         //    for( x = 0; x < 22; x++) curData->name[x] = instru[i]->name[x];
  193.             
  194.             curData->data         = MADPlugNewPtr( curData->size, init);
  195.             if( curData->data == 0L) DebugStr("\pInstruments: I NEED MEMORY !!! NOW !");
  196.             
  197.             BlockMove( theInstrument[i], curData->data, curData->size);
  198.             
  199.             destPtr = curData->data;
  200.             for( temp = 0; temp < curData->size; temp++) *(destPtr + temp) -= 0x80;
  201.         }
  202.         else theMAD->fid[ i].numSamples = 0;
  203.     }
  204.     
  205.     PatInt = ( struct PatSix*) the669 + 0x1f1 + the669->NOS * 0x19;
  206.     
  207.     temp = (long) the669;
  208.     temp += 0x1f1L + (long) the669->NOS * 0x19L;
  209.     
  210.     PatInt = ( struct PatSix*) temp;
  211.     
  212.     /***** TEMPORAIRE ********/
  213.     
  214.     theMAD->header->numChn = 8;
  215. //    theMAD->header->PatMax = 1;
  216.  
  217.     for( i = 0; i < theMAD->header->numPat; i++)
  218.     {
  219.         theMAD->partition[ i] = (PatData*) MADPlugNewPtrClear( sizeof( PatHeader) + theMAD->header->numChn * 64L * sizeof( Cmd), init);
  220.         theMAD->partition[ i]->header.size = 64L;
  221.         theMAD->partition[ i]->header.compMode = 'NONE';
  222.         for( x = 0; x < 20; x++) theMAD->partition[ i]->header.name[ x] = 0;
  223.         theMAD->partition[ i]->header.patBytes = 0L;
  224.         theMAD->partition[ i]->header.unused2 = 0L;
  225.     
  226.         for( x = 0 ; x < 64; x++)
  227.         {
  228.             for( z = 0; z<theMAD->header->numChn; z++)
  229.             {
  230.                 aCmd = GetMADCommand( x, z, theMAD->partition[ i]);
  231.                 
  232.                 theCommand = &PatInt[ i].Cmds[ x][ z];
  233.                 if( (Ptr) theCommand >= MaxPtr) Debugger();
  234.                 
  235.                 thePasByte = ( Byte*) theCommand;
  236.                 
  237.                 if( thePasByte[0] == 0xFF)
  238.                 {
  239.                     aCmd->cmd     = 0;
  240.                     aCmd->arg     = 0;
  241.                     aCmd->ins     = 0;
  242.                     aCmd->note    = 0xFF;
  243.                     aCmd->vol    = 0xFF;
  244.                 }
  245.                 else if( thePasByte[0] == 0xFE)
  246.                 {
  247.                     aCmd->ins     = 0;
  248.                     aCmd->note     = 0xFF;
  249.                     aCmd->cmd     = 0;
  250.                     aCmd->arg     = 0;
  251.                     
  252.                     aCmd->vol    = theCommand->Volume;
  253.                     aCmd->vol    = (aCmd->vol * 64) / 15;
  254.                 }
  255.                 else
  256.                 {
  257.                     aCmd->ins = theCommand->Instru + 1;
  258.                     aCmd->note = theCommand->AmigaPeriod;
  259.                     
  260.                     Note = (aCmd->note & 0xF0) >> 4;
  261.                     Octave = (aCmd->note & 0x0F);
  262.                     
  263.                     aCmd->note = Note*12 + Octave;
  264.                     
  265.                     if( aCmd->note == 0) aCmd->note = 0xFF;
  266.                     else aCmd->note += 24;
  267.                     
  268.                     aCmd->vol    = theCommand->Volume;
  269.                     aCmd->vol    = (aCmd->vol * 64) / 15;
  270.                 }
  271.                 
  272.                 if( thePasByte[2] == 0xFF)
  273.                 {
  274.                     aCmd->cmd = 0;
  275.                     aCmd->arg = 0;
  276.                 }
  277.                 else
  278.                 {
  279.                     aCmd->cmd = 0;
  280.                     aCmd->arg = 0;
  281.                 }
  282.             }
  283.         }
  284.     }
  285.     
  286.     return noErr;
  287. }
  288.  
  289. OSErr Extract669Info( PPInfoRec *info, Ptr AlienFile)
  290. {
  291.     SixSixNine    *the669 = (SixSixNine*) AlienFile;
  292.     long            PatternSize;
  293.     short        i;
  294.     short        maxInstru;
  295.     short        tracksNo;
  296.     
  297.     /*** Signature ***/
  298.     
  299.     info->signature = '669 ';
  300.     
  301.     /*** Internal name ***/
  302.     
  303.     the669->message[ 30] = '\0';
  304.     MADstrcpy( info->internalFileName, ( the669->message));
  305.  
  306.     /*** Total Patterns ***/
  307.     
  308.     info->totalPatterns = 0;
  309.     
  310.     /*** Partition Length ***/
  311.     
  312.     info->partitionLength = 0;
  313.     
  314.     /*** Total Instruments ***/
  315.     
  316.     info->totalInstruments = 0;
  317.     
  318.     MADstrcpy( info->formatDescription, "669 Plug");
  319.  
  320.     return noErr;
  321. }
  322.  
  323. OSErr Test669File( Ptr AlienFile)
  324. {
  325. SixSixNine    *the669 = (SixSixNine*) AlienFile;
  326.  
  327. if( the669->marker == 0x6669 || the669->marker == 0x6966) return   noErr;
  328. else return  MADFileNotSupportedByThisPlug;
  329. }
  330.  
  331. OSErr  main( OSType order, char *AlienFileName, MADMusic *MadFile, PPInfoRec *info, MADDriverSettings *init)
  332. {
  333.     OSErr    myErr;
  334.     Ptr        AlienFile;
  335.     short    vRefNum, iFileRefI;
  336.     long    dirID, sndSize;
  337.     
  338. #ifndef powerc
  339.     long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  340. #endif
  341.  
  342.     c2pstr( AlienFileName);
  343.     
  344.     myErr = noErr;
  345.  
  346.     switch( order)
  347.     {
  348.         case 'IMPL':
  349.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  350.             if( myErr == noErr)
  351.             {
  352.                 GetEOF( iFileRefI, &sndSize);
  353.             
  354.                 // ** MEMORY Test Start
  355.                 AlienFile = MADPlugNewPtr( sndSize * 2L, init);
  356.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  357.                 // ** MEMORY Test End
  358.                 
  359.                 else
  360.                 {
  361.                     DisposePtr( AlienFile);
  362.                     
  363.                     AlienFile = MADPlugNewPtr( sndSize, init);
  364.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  365.                     if( myErr == noErr)
  366.                     {
  367.                         myErr = Test669File( AlienFile);
  368.                         if( myErr == noErr)
  369.                         {
  370.                             myErr = Convert6692Mad( AlienFile,  GetPtrSize( AlienFile), MadFile, init);
  371.                         }
  372.                     }
  373.                     DisposePtr( AlienFile);    AlienFile = 0L;
  374.                 }
  375.                 FSClose( iFileRefI);
  376.             }
  377.         break;
  378.         
  379.         case 'TEST':
  380.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  381.             if( myErr == noErr)
  382.             {
  383.                 sndSize = 1024L;
  384.                 
  385.                 AlienFile = MADPlugNewPtr( sndSize, init);
  386.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  387.                 else
  388.                 {
  389.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  390.                     
  391.                     myErr = Test669File( AlienFile);
  392.                     
  393.                     DisposePtr( AlienFile);    AlienFile = 0L;
  394.                 }
  395.                 FSClose( iFileRefI);
  396.             }
  397.         break;
  398.  
  399.         case 'INFO':
  400.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  401.             if( myErr == noErr)
  402.             {
  403.                 GetEOF( iFileRefI, &info->fileSize);
  404.             
  405.                 sndSize = 5000L;    // Read only 5000 first bytes for optimisation
  406.                 
  407.                 AlienFile = MADPlugNewPtr( sndSize, init);
  408.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  409.                 else
  410.                 {
  411.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  412.                     if( myErr == noErr)
  413.                     {
  414.                         myErr = Extract669Info( info, AlienFile);
  415.                     }
  416.                     DisposePtr( AlienFile);    AlienFile = 0L;
  417.                 }
  418.                 FSClose( iFileRefI);
  419.             }
  420.         break;
  421.         
  422.         default:
  423.             myErr = MADOrderNotImplemented;
  424.         break;
  425.     }
  426.  
  427.     p2cstr( (unsigned char*) AlienFileName);
  428.  
  429.     #ifndef powerc
  430.         SetA4( oldA4);
  431.     #endif
  432.     return myErr;
  433. }