home *** CD-ROM | disk | FTP | other *** search
/ Amiga Dream 59 / CDDream59.ISO / BeOs / Sound / Intel / PPBeDevKit.ZIP / PLAYERPR.TAR / PlayerPRO / Source / MainDriver.c < prev    next >
C/C++ Source or Header  |  1998-12-26  |  71KB  |  3,068 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 "RDriver.h"
  25. #include "RDriverInt.h"
  26. #include "FileUtils.h"
  27. #include "string.h"
  28.  
  29. #ifdef _MIDIHARDWARE_
  30. #include "OMS.h"
  31. #endif
  32.  
  33. #ifdef _MAC_H
  34. #include <Sound.h>
  35. #include <SoundInput.h>
  36. #include <SoundComponents.h>
  37. #endif
  38.  
  39. ///////////////////////////////
  40.  
  41. #define  NO_ASM
  42.  
  43. #define rate5khz    0x15BB9B5CUL
  44. #define rate16khz    0x4132DDF2UL
  45. #define rate48khz    0xBB800000UL
  46.  
  47. enum {
  48.     MADFileType = 1,
  49.     MADRsrcType = 2,
  50.     MADPtrType = 3
  51. };
  52.  
  53.  
  54. /*MADMusic* CreateFreeMADI()
  55. {
  56.     MADMusic    *music = (MADMusic*) NewPtrClear( sizeof( MADMusic));
  57.     MADSpec        *MADI;
  58.     short        i, x, z;
  59.     FSSpec        aSpec;
  60.     Cmd            *aCmd;
  61.     
  62.     // ******* HEADER *********
  63.     
  64.     MADI = (MADSpec*) MADNewPtrClear( sizeof( MADSpec), intDriver->lib);
  65.     
  66.     MADI->MAD                    = MADID;
  67.     MADI->numPat                = 1;
  68.     MADI->numPointers            = 1;
  69.     MADI->numInstru                = 0;
  70.     MADI->numChn                = 4;
  71.     MADI->speed                    = 6;
  72.     MADI->tempo                    = 125;
  73.     
  74.     music->header    = MADI;
  75.     
  76.     for( i = 0; i < MAXTRACK; i++)
  77.     {
  78.         if( i % 2 == 0) MADI->chanPan[ i] = MAX_PANNING/4;
  79.         else MADI->chanPan[ i] = MAX_PANNING - MAX_PANNING/4;
  80.         
  81.         MADI->chanVol[ i] = MAX_VOLUME;
  82.     }
  83.     
  84.     music->fid = (InstrData*) MADNewPtrClear( sizeof( InstrData) * (long) MAXINSTRU, intDriver->lib);
  85.     
  86.     music->sample = (sData**) MADNewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE, intDriver->lib);
  87.     
  88.     // ******* PARTITION *********
  89.     
  90.     for(i=0; i< MADI->numPat; i++)
  91.     {
  92.         music->partition[ i] = (PatData*) MADNewPtrClear( sizeof( PatHeader) + MADI->numChn * 64L * sizeof( Cmd), intDriver->lib);
  93.         
  94.         music->partition[ i]->header.size         = 64L;
  95.         music->partition[ i]->header.compMode     = 'NONE';
  96.         music->partition[ i]->header.patBytes     = 0L;
  97.         music->partition[ i]->header.unused2         = 0L;
  98.         
  99.         for( x = 0; x < 64; x++)
  100.         {
  101.             for( z = 0; z < music->header->numChn; z++)
  102.             {
  103.                 aCmd = GetMADCommand(  x,  z, music->partition[ i]);
  104.                 
  105.                 MADKillCmd( aCmd);
  106.             }
  107.         }
  108.     }
  109.     for( i = music->header->numPat; i < MAXPATTERN ; i++) music->partition[ i] = 0L;
  110.     
  111.     return music;
  112. }
  113.  
  114. void WriteMADHFile( FSSpec *newFile)
  115. {
  116.     short    fRefNum;
  117.     OSErr    iErr;
  118.     long    inOutCount;
  119.  
  120.     iErr = FSOpen( newFile->name, 0, &fRefNum);
  121.     
  122.     if( iErr != noErr)
  123.     {
  124.         iErr = Create( newFile->name, 0, 'SNPL', theType);
  125.         iErr = FSOpen( newFile->name, 0, &fRefNum);
  126.     }
  127.  
  128.     inOutCount = sizeof( MADSpec);
  129.     iErr = FSWrite( fRefNum, &inOutCount, MDriver->header);
  130.     
  131.     for( i = 0; i < MDriver->header->numPat ; i++)
  132.     {
  133.         if( thePrefs.MADCompression) MDriver->partition[ i]->header.compMode = 'MAD1';
  134.         
  135.         if( MDriver->partition[ i]->header.compMode == 'MAD1')
  136.         {
  137.             PatMAD = CompressPartitionMAD1( MDriver->partition[ i]);
  138.             inOutCount = PatMAD->header.PatBytes + sizeof( PatHeader);
  139.             
  140.             iErr = FSWrite( fRefNum, &inOutCount, PatMAD);
  141.             
  142.             MyDisposePtr( (Ptr*) &PatMAD);
  143.         }
  144.         else
  145.         {
  146.             inOutCount = sizeof( PatHeader);
  147.             inOutCount += MDriver->header->numChn * MDriver->partition[ i]->header.size * sizeof( Cmd);
  148.  
  149.             iErr = FSWrite( fRefNum, &inOutCount, MDriver->partition[ i]);
  150.         }
  151.     }
  152.     
  153.     for( i = 0; i < MDriver->header->numInstru ; i++)
  154.     {
  155.         inOutCount = MDriver->header->fid[i].size;
  156.         if( inOutCount > 0)
  157.         {
  158.             iErr = FSWrite( fRefNum, &inOutCount, MDriver->instrument[i]);
  159.         }
  160.     }
  161.     GetFPos( fRefNum, &tt);
  162.     SetEOF( fRefNum, tt);
  163.     FSClose( fRefNum);
  164. }
  165. */
  166.  
  167. long MADGetMusicSize( MADMusic    *music)
  168. {
  169.     short i, x;
  170.     long fileSize;
  171.     
  172.     if( music->header == 0L) return 0L;
  173.     
  174.     fileSize = sizeof( MADSpec);
  175.     fileSize += (long) music->header->numInstru * sizeof( InstrData);
  176.     for( i = 0; i < music->header->numPat; i++) fileSize += sizeof( PatHeader) + music->header->numChn * music->partition[ i]->header.size * sizeof( Cmd);
  177.     for( i = 0; i < music->header->numInstru ; i++)
  178.     {
  179.         for( x = 0; x < music->fid[ i].numSamples ; x++)
  180.         {
  181.             sData    *curData = music->sample[ i * MAXSAMPLE + x];
  182.             
  183.             fileSize += sizeof( sData);
  184.             fileSize += curData->size;
  185.         }
  186.     }
  187.     
  188.     return fileSize;
  189. }
  190.  
  191. #ifdef _MAC_H
  192. void ConvertTo64Rows( MADMusic *music)
  193. {
  194.     long        i, x, z;
  195.     Boolean        IsReading;
  196.     
  197.     if( music->header == 0L) return;
  198.     
  199.     IsReading = music->musicUnderModification;
  200.     music->musicUnderModification = true;
  201.     
  202.     for( i = 0; i < music->header->numPat; i++)
  203.     {
  204.         // Resize pattern to 64 rows and put a pattern break
  205.         
  206.         long        newSize;
  207.         PatData        *newPat;
  208.         
  209.         newSize = sizeof( PatHeader) + music->header->numChn * 64L * sizeof( Cmd);
  210.         
  211.         if( music->partition[ i]->header.size < 64)
  212.         {
  213.             Cmd        *srccmd, *dstcmd;
  214.             long    patsize;
  215.             
  216.             newPat = ( PatData*) NewPtrClear( newSize);
  217.             
  218.             newPat->header.size         = 64L;
  219.             newPat->header.compMode     = 'NONE';
  220.             newPat->header.patBytes     = 0L;
  221.             newPat->header.unused2         = 0;
  222.             
  223.             BlockMove( music->partition[ i]->header.name, newPat->header.name, 32);
  224.             
  225.             // Upgrade length to 64
  226.             
  227.             for( x = 0, patsize = 0; x < 64; x++, patsize++)
  228.             {
  229.                 for( z = 0; z < music->header->numChn; z++)
  230.                 {
  231.                     if( x < music->partition[ i]->header.size)
  232.                     {
  233.                         // src
  234.                         srccmd = GetMADCommand( patsize, z, music->partition[ i]);
  235.                         
  236.                         // dst
  237.                         dstcmd = GetMADCommand( x, z, newPat);
  238.                         
  239.                         *dstcmd = *srccmd;
  240.                     }
  241.                     else
  242.                     {
  243.                         dstcmd = GetMADCommand( x, z, newPat);
  244.                         
  245.                         MADKillCmd( dstcmd);
  246.                     }
  247.                 }
  248.             }
  249.             
  250.             // dst
  251.             dstcmd = GetMADCommand( music->partition[ i]->header.size-1, 0, newPat);        // Pose le pattern break
  252.             
  253.             MADKillCmd( dstcmd);
  254.             
  255.             dstcmd->cmd = skipE;
  256.             dstcmd->arg = 0;
  257.             
  258.             // Replace old pattern
  259.             
  260.             DisposePtr( ( Ptr) music->partition[ i]);
  261.             music->partition[ i] = 0L;
  262.             
  263.             music->partition[ i] = newPat;
  264.         }
  265.         else if( music->partition[ i]->header.size > 64)
  266.         {
  267.             long        patID = i;
  268.             long         patsize = 0;
  269.             PatData*    srcPat = music->partition[ i];
  270.             
  271.             while( patsize < srcPat->header.size)
  272.             {
  273.                 newPat = ( PatData*) NewPtrClear( newSize);
  274.                 
  275.                 newPat->header.size         = 64L;
  276.                 newPat->header.compMode     = 'NONE';
  277.                 newPat->header.patBytes     = 0L;
  278.                 newPat->header.unused2         = 0;
  279.                 
  280.                 BlockMove( srcPat->header.name, newPat->header.name, 32);
  281.                 
  282.                 for( x = 0; x < 64; x++, patsize++)
  283.                 {
  284.                     for( z = 0; z < music->header->numChn; z++)
  285.                     {
  286.                         Cmd        *srccmd, *dstcmd;
  287.                         
  288.                         if( patsize < srcPat->header.size)
  289.                         {
  290.                             // src
  291.                             srccmd = GetMADCommand( patsize, z, srcPat);
  292.                             
  293.                             // dst
  294.                             dstcmd = GetMADCommand( x, z, newPat);
  295.                             
  296.                             *dstcmd = *srccmd;
  297.                         }
  298.                         else
  299.                         {
  300.                             dstcmd = GetMADCommand( x, z, newPat);
  301.                             MADKillCmd( dstcmd);
  302.                         }
  303.                     }
  304.                 }
  305.                 
  306.                 // Update patterns list & partition
  307.                 
  308.                 if( patID != i)
  309.                 {
  310.                     for( x = music->header->numPat; x > patID ; x--)
  311.                     {
  312.                         music->partition[ x] = music->partition[ x - 1];
  313.                     }
  314.                     music->header->numPat++;
  315.                 }
  316.                 
  317.                 for( x = music->header->numPat; x < MAXPATTERN; x++) music->partition[ x] = 0L;
  318.                 
  319.                 music->partition[ patID] = newPat;
  320.                 
  321.                 patID++;
  322.             }
  323.             
  324.             // Do we need a pattern break ?
  325.             
  326.             if( (srcPat->header.size / 64) * 64 != srcPat->header.size)
  327.             {
  328.                 short    breakpos;
  329.                 Cmd        *dstcmd;
  330.                 
  331.                 breakpos = srcPat->header.size - (srcPat->header.size / 64) * 64;
  332.                 
  333.                 dstcmd = GetMADCommand( breakpos-1, 0, newPat);
  334.                 MADKillCmd( dstcmd);
  335.                 
  336.                 dstcmd->cmd = skipE;
  337.                 dstcmd->arg = 0;
  338.             }
  339.             
  340.             // Update la partition list
  341.             
  342.             for( x = 0; x < music->header->numPointers; x++)
  343.             {
  344.                 if( music->header->oPointers[ x] > patID) music->header->oPointers[ x] += patID - i;
  345.                 else if( music->header->oPointers[ x] == i)
  346.                 {
  347.                     // Avance le reste de la partition
  348.                     
  349.                     for( z = x; z < 256; z++)
  350.                     {
  351.                         if( x + patID - i - 1 < 256)
  352.                         {
  353.                             music->header->oPointers[ x + patID - i - 1] = music->header->oPointers[ x];
  354.                         }
  355.                     }
  356.                     
  357.                     for( z = i; z < patID; z++)
  358.                     {
  359.                         music->header->oPointers[ x] = z;
  360.                         
  361.                         x++;
  362.                     }
  363.                     x--;
  364.                     
  365.                     music->header->numPointers += patID - i - 1;
  366.                 }
  367.             }
  368.             
  369.             DisposePtr( ( Ptr) srcPat);
  370.         }
  371.     }
  372.     
  373.     music->musicUnderModification = IsReading;
  374. }
  375.  
  376. long MADMinimize( MADMusic *music)
  377. {
  378.     short         i, x, z;
  379.     Boolean        remove, IsReading;
  380.     Boolean        inst[ MAXINSTRU];
  381.      long        before, after;
  382.      
  383.      if( music->header == 0L) return 0L;
  384.      
  385.     IsReading = music->musicUnderModification;
  386.     music->musicUnderModification = true;
  387.      
  388.     // Check size before
  389.     
  390.     before = MADGetMusicSize( music);
  391.     
  392.     // Check for unused patterns
  393.     
  394.     if( music->header->numPat > 1)
  395.     {
  396.         for( i = 0; i < music->header->numPat; i++)
  397.         {
  398.             remove = true;
  399.             for( x = 0; x < music->header->numPointers; x++)
  400.             {
  401.                 if( music->header->oPointers[ x] == i)
  402.                 {
  403.                     remove = false;
  404.                     break;
  405.                 }
  406.             }
  407.             
  408.             if( remove)
  409.             {
  410.                 music->header->numPat--;
  411.                 
  412.                 DisposePtr( ( Ptr) music->partition[ i]);
  413.                 music->partition[ i] = 0L;
  414.                 
  415.                 for( x = i; x < music->header->numPat; x++)
  416.                 {
  417.                     music->partition[ x] = music->partition[ x + 1];
  418.                 }
  419.                 for( x = music->header->numPat; x < MAXPATTERN; x++) music->partition[ x] = 0L;
  420.                 
  421.                 /***** Update la partition list ******/
  422.                 
  423.                 for( x = 0; x < 128; x++)
  424.                 {
  425.                     if( music->header->oPointers[ x] > i) music->header->oPointers[ x]--;
  426.                     else if( music->header->oPointers[ x] == i) music->header->oPointers[ x] = 0;
  427.                 }
  428.                 
  429.                 if( i > 0) i--;
  430.             }
  431.         }
  432.     }
  433.     
  434.     // Check for unused instruments
  435.     for( i = 0; i < 32 ; i++) inst[ i] = false;
  436.     
  437.     for( i = 0; i < music->header->numPat; i++)
  438.     {
  439.         for( x = 0; x < music->partition[ i]->header.size; x++)
  440.         {
  441.             for( z = 0; z < music->header->numChn; z++)
  442.             {
  443.                 Cmd        *cmd;
  444.                 
  445.                 cmd = GetMADCommand( x, z, music->partition[ i]);
  446.                 
  447.                 if( cmd->ins > 0) inst[ cmd->ins-1] = true;
  448.             }
  449.         }
  450.     }
  451.     
  452.     for( i = 0; i < MAXINSTRU; i++)
  453.     {
  454.         if( inst[ i] == false) MADKillInstrument( music, i);
  455.     }
  456.     
  457.     // Check for after loop data
  458.     for( i = 0; i < MAXINSTRU; i++)
  459.     {
  460.         for( x = 0; x < music->fid[ i].numSamples; x++)
  461.         {
  462.             sData    *curData = music->sample[ i * MAXSAMPLE + x];
  463.             
  464.             if( curData->loopSize > 2)
  465.             {
  466.                 if( curData->size > curData->loopBeg + curData->loopSize)
  467.                 {
  468.                     SetPtrSize( curData->data, curData->loopBeg + curData->loopSize);
  469.                     
  470.                     curData->size = curData->loopBeg + curData->loopSize;
  471.                 }
  472.             }
  473.         }
  474.     }
  475.     
  476.     music->musicUnderModification = IsReading;
  477.     
  478.     after = MADGetMusicSize( music);
  479.     
  480.     return before - after;
  481. }
  482. #endif
  483.  
  484. Ptr MADNewPtr( long size, MADLibrary* init)
  485. {
  486.     if( size == 0) size = 1;    // Myriad, zeroptr on PC
  487.     
  488.     if( init->sysMemory) return NewPtrSys( size);
  489.     else return NewPtr( size);
  490. }
  491.  
  492. Ptr MADNewPtrClear( long size, MADLibrary* init)
  493. {
  494.     if( init->sysMemory) return NewPtrSysClear( size);
  495.     else return NewPtrClear( size);
  496. }
  497.  
  498. void MADGetBestDriver( MADDriverSettings    *Init)
  499. {
  500. #ifdef _MAC_H
  501. long                gestaltAnswer;
  502. short                myBit;
  503. NumVersion    nVers;
  504. Boolean            Stereo, StereoMixing, NewSoundManager, NewSoundManager31, hasASC, Audio16;
  505.  
  506. /***************                    ****************/
  507. /****** HARDWARE IDENTIFICATION AND CHECK **********/
  508. /***************                    ****************/
  509.  
  510. /****************/
  511. /** ASC CHIP ? **/
  512. /****************/
  513.  
  514.     Gestalt( kSoundOutputDeviceType, &gestaltAnswer);
  515.     if( gestaltAnswer == kASCSubType) hasASC = true;
  516.     else hasASC = false;
  517.  
  518. /**************/
  519. /** STEREO ? **/
  520. /**************/
  521.  
  522.     Gestalt( gestaltSoundAttr, &gestaltAnswer);
  523.     myBit = gestaltStereoCapability;
  524.     Stereo = BitTst( &gestaltAnswer, 31-myBit);
  525.  
  526. /******************************/
  527. /** INTERNAL STEREO MIXING ? **/
  528. /******************************/
  529.  
  530.     myBit = gestaltStereoMixing;
  531.     StereoMixing = BitTst( &gestaltAnswer, 31-myBit);
  532.  
  533. /***********************/
  534. /** 16 BITS SUPPORT ? **/
  535. /***********************/
  536.  
  537.     myBit = gestalt16BitSoundIO;
  538.     Audio16 = BitTst( &gestaltAnswer, 31-myBit);
  539.  
  540. /****************************/
  541. /** SOUND MANAGER >= 3.0 ? **/
  542. /****************************/
  543.  
  544.     nVers = SndSoundManagerVersion();
  545.     NewSoundManager31 = false;    NewSoundManager = false;
  546.     if( nVers.majorRev >= 3 && nVers.minorAndBugRev >= 0x10)
  547.     {
  548.         NewSoundManager31 = true;
  549.         NewSoundManager = true;
  550.     }
  551.     else if( nVers.majorRev >= 3) NewSoundManager = true;
  552.     
  553.     Init->numChn            = 4;
  554.     Init->surround            = false;
  555.     Init->repeatMusic        = true;
  556.     Init->sysMemory            = false;
  557.     Init->MicroDelaySize    = 25;
  558.     Init->Reverb            = false;
  559.     Init->ReverbSize        = 100;
  560.     Init->ReverbStrength    = 20;
  561.     
  562.     // STEREO OR MONO ?
  563.     
  564.     Init->outPutMode = DeluxeStereoOutPut;
  565.     
  566.     // RATE AND SIZE
  567.     
  568.     if( NewSoundManager == true)
  569.     {
  570.         Init->driverMode = SoundManagerDriver;
  571.         
  572.         ////////
  573.         
  574.         #if !TARGET_RT_MAC_CFM
  575.         if( NewSoundManager31)
  576.         {
  577.             OSErr    iErr;
  578.             
  579.             iErr = GetSoundOutputInfo( 0L, siSampleRate, &Init->outPutRate);            if(iErr) goto oldWay;
  580.             iErr = GetSoundOutputInfo( 0L, siSampleSize, (void*) &Init->outPutBits);    if(iErr) goto oldWay;
  581.         }
  582.         else
  583.         #endif
  584.         {
  585.              oldWay:;
  586.              
  587.             if( Audio16)            {    Init->outPutBits = 16;    Init->outPutRate = rate44khz;}
  588.             else                    {    Init->outPutBits = 8;    Init->outPutRate = rate22khz;}
  589.         }
  590.     }
  591.     else
  592.     {
  593.         Init->driverMode = SoundManagerDriver;
  594.         
  595.         Init->outPutBits = 8;
  596.         Init->outPutRate = rate22khz;
  597.     }
  598.     #else
  599.     
  600.     #endif
  601. }
  602.  
  603. OSErr MADCreateMicroDelay( MADDriverRec *intDriver)
  604. {
  605.     intDriver->MDelay = (intDriver->DriverSettings.MicroDelaySize * ( intDriver->DriverSettings.outPutRate >> 16)) / 1000;
  606.     
  607.     return noErr;
  608. }
  609.  
  610. void MADDisposeReverb( MADDriverRec *intDriver)
  611. {
  612.     if( intDriver->DriverSettings.Reverb) DisposePtr( intDriver->ReverbPtr);
  613.     intDriver->ReverbPtr = 0L;
  614. }
  615.  
  616. OSErr MADCreateReverb( MADDriverRec *intDriver)
  617. {
  618.     long i;
  619.     
  620.     if( intDriver->DriverSettings.Reverb)
  621.     {
  622.         intDriver->RDelay = (intDriver->DriverSettings.ReverbSize * ( intDriver->DriverSettings.outPutRate >> 16L)) / 1000L;
  623.         
  624.         switch( intDriver->DriverSettings.outPutBits)
  625.         {
  626.             case 8:
  627.                 intDriver->ReverbPtr = NewPtr( intDriver->RDelay * 2L);
  628.                 if( intDriver->ReverbPtr == 0L) return MADNeedMemory;
  629.                 for( i = 0; i < intDriver->RDelay * 2L; i++) intDriver->ReverbPtr[ i] = 0x80;
  630.             break;
  631.             
  632.             case 16:
  633.                 intDriver->ReverbPtr = NewPtrClear( intDriver->RDelay * 4L);
  634.                 if( intDriver->ReverbPtr == 0L) return MADNeedMemory;
  635.             break;
  636.         }
  637.     }
  638.     
  639.     return noErr;
  640. }
  641.  
  642. void MADDisposeDriverBuffer( MADDriverRec *intDriver)
  643. {
  644.     if( intDriver->IntDataPtr != 0L)    DisposePtr( intDriver->IntDataPtr);
  645.     intDriver->IntDataPtr = 0L;
  646. }
  647.  
  648. OSErr MADCreateDriverBuffer( MADDriverRec *intDriver)
  649. {
  650.     long BufSize = intDriver->ASCBUFFER;
  651.     
  652.     switch( intDriver->DriverSettings.outPutMode)
  653.     {
  654.     //    case MonoOutPut:                    BufSize = BufSize;            break;
  655.     //    case StereoOutPut:                BufSize = BufSize*2L;        break;
  656.         case DeluxeStereoOutPut:    BufSize = BufSize*2L;        break;
  657.     }
  658.     
  659.     switch( intDriver->DriverSettings.outPutBits)
  660.     {
  661.         case 16:                                    BufSize = BufSize*2L;        break;
  662.     }
  663.     
  664.     intDriver->IntDataPtr = MADNewPtr( BufSize, intDriver->lib);
  665.     if( intDriver->IntDataPtr == 0L) return MADNeedMemory;
  666.     
  667.     intDriver->BufSize                = BufSize;
  668.     intDriver->OscilloWavePtr    = intDriver->IntDataPtr;
  669.     
  670.     return noErr;
  671. }
  672.  
  673. OSErr MADCreateTiming( MADDriverRec *intDriver)
  674. {
  675.     /*************************/
  676.     /**     Rate table      **/
  677.     /*************************/
  678.     
  679.     intDriver->VSYNC    = ((intDriver->DriverSettings.outPutRate>>16) * 125L) / (50L);
  680.     intDriver->trackDiv = intDriver->DriverSettings.numChn;
  681. //    if( intDriver->DriverSettings.outPutMode == StereoOutPut) intDriver->trackDiv /= 2;
  682.  
  683.     return noErr;
  684. }
  685.  
  686.  
  687. OSErr MADChangeDriverSettings( MADDriverSettings    *DriverInitParam, MADDriverRec** returnDriver)
  688. {
  689.     MADMusic    *music;
  690.     Boolean        playing;
  691.     OSErr        err;
  692.     long        fullTime, curTime;
  693.     
  694.     music = (*returnDriver)->curMusic;
  695.     playing = (*returnDriver)->Reading;
  696.     
  697.     MADGetMusicStatus( *returnDriver, &fullTime, &curTime);
  698.     
  699.     if( (err = MADStopDriver( *returnDriver)) != noErr) return err;
  700.     if( (err = MADDisposeDriver( *returnDriver)) != noErr) return err;
  701.     
  702.     if( (err = MADCreateDriver( DriverInitParam, (*returnDriver)->lib, returnDriver)) != noErr) return err;
  703.     
  704.     if( (err = MADStartDriver( *returnDriver)) != noErr) return err;
  705.     
  706.     if( music)
  707.     {
  708.         MADAttachDriverToMusic( *returnDriver, music);
  709.         
  710.         MADSetMusicStatus( *returnDriver, 0, fullTime, curTime);
  711.         
  712.         if( playing) MADPlayMusic( *returnDriver);
  713.     }
  714.     
  715.     return noErr;
  716. }
  717.  
  718. OSErr MADCreateDriver( MADDriverSettings    *DriverInitParam, MADLibrary *lib, MADDriverRec** returnDriver)
  719. {
  720.     OSErr                     theErr;
  721.     long                    i;
  722.     MADDriverRec*            MDriver;
  723.     
  724.     *returnDriver = 0L;
  725.         
  726.     /*************************/
  727.     /** Paramaters checking **/
  728.     /*************************/
  729.     
  730.     theErr = noErr;
  731.     
  732.     if( DriverInitParam->numChn % 2 != 0)
  733.     {
  734.         DriverInitParam->numChn /= 2;
  735.         DriverInitParam->numChn *= 2;
  736.     //    DriverInitParam->numChn++;
  737.     }
  738.     if( DriverInitParam->numChn < 2) theErr = MADParametersErr;
  739.     if( DriverInitParam->numChn > MAXTRACK) theErr = MADParametersErr;
  740.     
  741.     if( DriverInitParam->outPutBits != 8 && DriverInitParam->outPutBits != 16) theErr = MADParametersErr;
  742.     
  743.     if( DriverInitParam->outPutRate < rate5khz) theErr = MADParametersErr;
  744.     if( DriverInitParam->outPutRate > rate48khz) theErr = MADParametersErr;
  745.     
  746.     if( DriverInitParam->outPutMode != DeluxeStereoOutPut &&
  747.         DriverInitParam->outPutMode != PolyPhonic) theErr = MADParametersErr;
  748.     
  749.     if( DriverInitParam->driverMode != SoundManagerDriver &&
  750.         DriverInitParam->driverMode != MIDISoundDriver &&
  751.         DriverInitParam->driverMode != BeOSSoundDriver &&
  752.         DriverInitParam->driverMode != DirectSound95NT &&
  753.         DriverInitParam->driverMode != Wave95NT &&
  754.         DriverInitParam->driverMode != NoHardwareDriver) theErr = MADParametersErr;
  755.     
  756.     if( DriverInitParam->MicroDelaySize < 0)         theErr = MADParametersErr;
  757.     if( DriverInitParam->MicroDelaySize > 1000)     theErr = MADParametersErr;
  758.     
  759.     if( DriverInitParam->Reverb)
  760.     {
  761.         if( DriverInitParam->ReverbSize < 25)             theErr = MADParametersErr;
  762.         if( DriverInitParam->ReverbSize > 1000)         theErr = MADParametersErr;
  763.         
  764.         if( DriverInitParam->ReverbStrength < 0)         theErr = MADParametersErr;
  765.         if( DriverInitParam->ReverbStrength > 70)         theErr = MADParametersErr;
  766.     }
  767.     
  768.     if( theErr != noErr)
  769.     {
  770.         return theErr;
  771.     }
  772.     
  773.     /*************************/
  774.     
  775.     if( lib->sysMemory) MDriver = (MADDriverRec*) NewPtrSysClear( sizeof( MADDriverRec));
  776.     else MDriver = (MADDriverRec*) NewPtrClear( sizeof( MADDriverRec));
  777.     
  778.     MDriver->lib = lib;
  779.     MDriver->curMusic = 0L;
  780.     
  781.     MDriver->Reading = false;
  782.     
  783.     theErr = MADStopDriver( MDriver);
  784.     if( theErr != noErr) return theErr;
  785.     
  786.     theErr = MADDisposeDriver( MDriver);
  787.     if( theErr != noErr) return theErr;
  788.     
  789.     theErr = MADCreateVibrato( MDriver);
  790.     if( theErr != noErr) return theErr;
  791.     
  792.     for( i = 0; i < MAXTRACK; i++) MDriver->Active[ i] = true;
  793.     
  794.     MDriver->DriverSettings        = *DriverInitParam;
  795.     MDriver->musicEnd                = false;
  796.     MDriver->Reading                = false;
  797.     MDriver->JumpToNextPattern    = true;
  798.     MDriver->smallcounter            = 128;            // Start immediately
  799.     MDriver->BufCounter            = 0;
  800.     MDriver->BytesToGenerate        = 0;
  801.     MDriver->speed                = 6;
  802.     MDriver->finespeed            = 125;
  803.     MDriver->VExt                    = 80;
  804.     MDriver->FreqExt                = 80;
  805.     MDriver->VolGlobal            = 64;
  806.     
  807.     MDriver->MIN_PITCH            = GetOldPeriod( NUMBER_NOTES-1, NOFINETUNE, MDriver);
  808.     MDriver->MAX_PITCH            = GetOldPeriod( 0, NOFINETUNE, MDriver);
  809.     
  810.     MADCleanDriver( MDriver);
  811.     
  812.     /*************************/
  813.     /**     Driver MODE        **/
  814.     /*************************/
  815.     
  816.     switch( MDriver->DriverSettings.driverMode)
  817.     {
  818.         #ifdef _MAC_H
  819.         case MIDISoundDriver:
  820.             MDriver->ASCBUFFER = 370;
  821.             MDriver->ASCBUFFER = (MDriver->ASCBUFFER * (MDriver->DriverSettings.outPutRate>>16L)) / 22254L;
  822.             MDriver->ASCBUFFER /= 2;
  823.             MDriver->ASCBUFFER *= 2;
  824.             if( MDriver->ASCBUFFER < 370) MDriver->ASCBUFFER = 370;
  825.         break;
  826.         #endif
  827.         
  828.         case SoundManagerDriver:
  829.             MDriver->ASCBUFFER = 370;
  830.             MDriver->ASCBUFFER = (MDriver->ASCBUFFER * (MDriver->DriverSettings.outPutRate>>16L)) / 22254L;
  831.             MDriver->ASCBUFFER /= 2;
  832.             MDriver->ASCBUFFER *= 2;
  833.             if( MDriver->ASCBUFFER < 370) MDriver->ASCBUFFER = 370;
  834.         break;
  835.         
  836.         case NoHardwareDriver:
  837.             MDriver->ASCBUFFER = 1024;
  838.         break;
  839.         
  840.         case BeOSSoundDriver:
  841.             MDriver->ASCBUFFER = 1024L;
  842.             
  843.             if( MDriver->DriverSettings.outPutBits == 8) MDriver->ASCBUFFER *= 2L;
  844.         break;
  845.         
  846.         case DirectSound95NT:
  847.             MDriver->ASCBUFFER = 7500L;
  848.         break;
  849.         
  850.         case Wave95NT:
  851.             MDriver->ASCBUFFER = 7500L;
  852.         break;
  853.         
  854.         default:
  855.             return MADParametersErr;
  856.         break;
  857.     }
  858.     
  859.     
  860.     theErr = MADCreateDriverBuffer( MDriver);        if( theErr != noErr) return theErr;
  861.     theErr = MADCreateTiming( MDriver);                    if( theErr != noErr) return theErr;
  862.     theErr = MADCreateReverb( MDriver);                    if( theErr != noErr) return theErr;
  863.     theErr = MADCreateVolumeTable(    MDriver);        if( theErr != noErr) return theErr;
  864.     
  865.     
  866.     /**********************************************/
  867.     /**    Interruption - Hardware Support       **/
  868.     /**********************************************/
  869.  
  870.     switch( MDriver->DriverSettings.driverMode)
  871.     {
  872.         #ifdef _MIDIHARDWARE_
  873.         case MIDISoundDriver:
  874.             MDriver->gOutNodeRefNum = -1;
  875.             
  876.             OMSAddPort( 'SNPL', 'out ', omsPortTypeOutput, NULL, 0L, &MDriver->MIDIPortRefNum);
  877.             
  878.             theErr = InitDBSoundManager( MDriver, initStereo);
  879.             if( theErr != noErr) return theErr;
  880.         break;
  881.         #endif
  882.         
  883.         #ifdef _MAC_H
  884.         case SoundManagerDriver:
  885.             theErr = InitDBSoundManager( MDriver, initStereo);
  886.             if( theErr != noErr) return theErr;
  887.         break;
  888.         #endif
  889.         
  890.         #ifdef _INTEL_H
  891.         case DirectSound95NT:
  892.             
  893.             if( !DirectSoundInit( MDriver)) theErr = MADUnknowErr;
  894.             if( theErr != noErr) return theErr;
  895.             
  896.         break;
  897.         
  898.         case Wave95NT:
  899.             if( !W95_Init( MDriver)) theErr = MADUnknowErr;
  900.             if( theErr != noErr) return theErr;
  901.         break;
  902.         #endif
  903.         
  904.         case BeOSSoundDriver:
  905.         
  906.         break;
  907.         
  908.         case NoHardwareDriver:
  909.         
  910.         break;
  911.     }
  912.     
  913.     *returnDriver = MDriver;
  914.     
  915.     return noErr;
  916. }
  917.  
  918. OSErr MADDisposeDriver( MADDriverRec* MDriver)
  919. {
  920.     if( MDriver->IntDataPtr == 0L) return noErr;
  921.     
  922.     MDriver->Reading = false;
  923.     
  924.     MADCleanDriver( MDriver);
  925.     
  926.     switch( MDriver->DriverSettings.driverMode)
  927.     {
  928.         #ifdef _MAC_H
  929.         case MIDISoundDriver:
  930.             AllNoteOff( MDriver);
  931.             DBSndClose( MDriver);
  932.         break;
  933.         
  934.         case SoundManagerDriver:
  935.             DBSndClose( MDriver);
  936.         break;
  937.         #endif
  938.         
  939.         #ifdef _INTEL_H
  940.         case DirectSound95NT:
  941.             DirectSoundClose( MDriver);
  942.         break;
  943.         
  944.         case Wave95NT:
  945.             W95_Exit( MDriver);
  946.         break;
  947.         #endif
  948.     }
  949.     
  950.     MADDisposeDriverBuffer( MDriver);            if( MemError()) return MADUnknowErr;
  951.     MADDisposeVolumeTable( MDriver);            if( MemError()) return MADUnknowErr;
  952.     MADDisposeReverb( MDriver);                    if( MemError()) return MADUnknowErr;
  953.     
  954.     DisposePtr( (Ptr) MDriver);
  955.     
  956.     return noErr;
  957. }
  958.  
  959. OSErr MADInitLibrary( char *PlugsFolderName, Boolean sysMemory, MADLibrary **lib)
  960. {
  961.     long     i, mytab[ 12] =
  962.     {
  963.         1712L*16L,1616L*16L,1524L*16L,1440L*16L,1356L*16L,1280L*16L,
  964.         1208L*16L,1140L*16L,1076L*16L,1016L*16L,960L*16L,907L*16L
  965.     };
  966.     
  967.     if( sysMemory) *lib = (MADLibrary*) NewPtrSysClear( sizeof( MADLibrary));
  968.     else *lib = (MADLibrary*) NewPtrClear( sizeof( MADLibrary));
  969.     
  970.     if( *lib == 0L) return MADNeedMemory;
  971.     
  972.     (*lib)->IDType = 'MADD';
  973.     (*lib)->sysMemory = sysMemory;
  974.     
  975.     for( i = 0; i < 12; i++)
  976.     {
  977.         (*lib)->mytab[ i] = mytab[ i];
  978.     }
  979.     
  980.     MInitImportPlug( *lib, PlugsFolderName);
  981.     
  982.     return noErr;
  983. }
  984.  
  985. OSErr MADDisposeLibrary( MADLibrary *MLibrary)
  986. {
  987.     if( MLibrary != 0L)
  988.     {
  989.         CloseImportPlug( MLibrary);
  990.         MLibrary->IDType = 'XXXX';
  991.         
  992.         DisposePtr( (Ptr) MLibrary);
  993.         MLibrary = 0L;
  994.     }
  995.     return noErr;
  996. }
  997.  
  998. OSErr MADAttachDriverToMusic( MADDriverRec *driver, MADMusic *music)
  999. {
  1000.     if( !driver) return -1;
  1001.     if( !music) return -1;
  1002.     
  1003.     driver->curMusic = music;
  1004.     driver->VolGlobal    = music->header->generalVol;        if( driver->VolGlobal <= 0) driver->VolGlobal = 64;
  1005.     driver->VExt        = music->header->generalSpeed;        if( driver->VExt <= 0) driver->VExt = 80;
  1006.     driver->FreqExt        = music->header->generalPitch;        if( driver->FreqExt <= 0) driver->FreqExt = 80;
  1007.  
  1008.     MADReset( driver);
  1009.     UpdateTracksNumber( driver);
  1010.     
  1011.     return noErr;
  1012. }
  1013.  
  1014. #ifdef _MAC_H
  1015. OSErr MADLoadMusicRsrc( MADMusic **music, OSType IDName, short    IDNo)
  1016. {
  1017.     Handle    theRsrc;
  1018.     OSErr    theErr;
  1019.     
  1020.     SetResLoad( false);
  1021.     theRsrc = GetResource( IDName, IDNo);
  1022.     if( theRsrc == 0L) return MADReadingErr;
  1023.     
  1024.     theErr = MADReadMAD( music, 0, MADRsrcType, theRsrc, 0L);
  1025.     if( theErr != noErr)
  1026.     {
  1027.         ReleaseResource( theRsrc);
  1028.         SetResLoad( true);
  1029.         
  1030.         MADDisposeMusic( music);
  1031.         return theErr;
  1032.     }
  1033.     
  1034.     ReleaseResource( theRsrc);
  1035.     SetResLoad( true);
  1036.     
  1037.     return noErr;
  1038. }
  1039. #endif
  1040.  
  1041. OSErr MADLoadMusicPtr( MADMusic **music, Ptr myPtr)
  1042. {
  1043.     OSErr    theErr;
  1044.     
  1045. //    MADDisposeMusic( music);
  1046.     
  1047.     theErr = MADReadMAD( music, 0, MADPtrType, 0L, myPtr);
  1048.     if( theErr != noErr) return theErr;
  1049.     
  1050.     return noErr;
  1051. }
  1052.  
  1053. OSErr MADLoadMADFileCString( MADMusic **music, Ptr fName)
  1054. {
  1055. OSErr        theErr;
  1056. UNFILE    srcFile;
  1057.  
  1058. //    MADDisposeMusic( music);
  1059.     
  1060.     srcFile = iFileOpen( fName);
  1061.     if( srcFile == 0L) return MADReadingErr;
  1062.     
  1063.     theErr = MADReadMAD( music, srcFile, MADFileType, 0L, 0L);
  1064.     if( theErr != noErr)
  1065.     {
  1066.         iClose( srcFile);
  1067.         MADDisposeMusic( music);
  1068.         return theErr;
  1069.     }
  1070.     
  1071.     iClose( srcFile);
  1072.     
  1073.     return( noErr);
  1074. }
  1075.  
  1076. #ifdef _MAC_H
  1077. OSErr MADSetHardwareVolume( long vol)
  1078. {
  1079. Point            tempL;
  1080. long            *tL;
  1081. NumVersion        nVers;
  1082. Boolean            NewSoundManager;
  1083.  
  1084.     if(vol > 64) vol = 64;
  1085.     if(vol < 0) vol = 0;
  1086.     
  1087.     nVers = SndSoundManagerVersion();
  1088. //    BlockMove( &tt, &nVers, 4);
  1089.     if( nVers.majorRev >= 3) NewSoundManager = true;
  1090.     else NewSoundManager = false;
  1091.     
  1092.     if( vol > 0)
  1093.     {
  1094.         if( NewSoundManager)
  1095.         {
  1096.             tempL.v = vol * 4;
  1097.             tempL.h = vol * 4;
  1098.             
  1099.             tL = (long*) &tempL;
  1100.             
  1101.             SetDefaultOutputVolume( *tL);
  1102.         }
  1103.         #if defined(powerc) || defined (__powerc)
  1104.         #else
  1105.         else
  1106.         {
  1107.         /*    #if GENERATINGCFM
  1108.             #else
  1109.             vol = (vol * 7L) / 64L;
  1110.             SetSoundVol( vol);
  1111.             #endif    */
  1112.         }
  1113.         #endif
  1114.     }
  1115.     
  1116.     return noErr;
  1117. }
  1118.  
  1119. long MADGetHardwareVolume()
  1120. {
  1121. Point            tempL;
  1122. NumVersion        nVers;
  1123. Boolean            NewSoundManager;
  1124. long            vol;
  1125.     
  1126.     nVers = SndSoundManagerVersion();
  1127. //    BlockMove( &tt, &nVers, 4);
  1128.     if( nVers.majorRev >= 3) NewSoundManager = true;
  1129.     else NewSoundManager = false;
  1130.     
  1131.     if( NewSoundManager)
  1132.     {
  1133.         GetDefaultOutputVolume( (long*) &tempL);
  1134.         
  1135.         vol = tempL.v / 4L;
  1136.     }
  1137.     #if defined(powerc) || defined (__powerc)
  1138.     #else
  1139.     else
  1140.     {
  1141.     /*    #if GENERATINGCFM
  1142.         #else
  1143.         short shortVol;
  1144.         
  1145.         GetSoundVol( &shortVol);
  1146.         
  1147.         vol = shortVol;
  1148.         vol = (64L * vol) / 7L;
  1149.         #endif*/
  1150.     }
  1151.     #endif
  1152.     
  1153.     if(vol > 64) vol = 64;
  1154.     if(vol < 0) vol = 0;
  1155.  
  1156.     return vol;
  1157. }
  1158.  
  1159. OSErr MADLoadMusicFSpFile( MADLibrary *lib, MADMusic **music, char *plugType, FSSpec *theSpec)
  1160. {
  1161. OSErr                        iErr;
  1162. FSSpec                    saved;
  1163.  
  1164.     HGetVol( 0L, &saved.vRefNum, &saved.parID);
  1165.     HSetVol( 0L, theSpec->vRefNum , theSpec->parID);
  1166.     p2cstr( theSpec->name);
  1167.     
  1168.     if( !MADstrcmp( "MADI", plugType))     iErr = MADLoadMADFileCString( music, (Ptr) theSpec->name);
  1169.     else                                iErr = MADLoadMusicFileCString( lib, music, plugType, (Ptr) theSpec->name);
  1170.     
  1171.     c2pstr( (Ptr) theSpec->name);
  1172.     HSetVol( 0L, saved.vRefNum , saved.parID);
  1173.     
  1174.     return iErr;
  1175. }
  1176.  
  1177. OSErr MADLoadMusicFilePString( MADLibrary *lib, MADMusic **music, char *plugType, Str255 fName)
  1178. {
  1179. OSErr                iErr;
  1180.  
  1181.     p2cstr( fName);
  1182.     
  1183.     if( !MADstrcmp( "MADI", plugType))     iErr = MADLoadMADFileCString(  music, (Ptr) fName);
  1184.     else                                iErr = MADLoadMusicFileCString( lib, music, plugType, (Ptr) fName);
  1185.     
  1186.     c2pstr( (Ptr) fName);
  1187.     
  1188.     return iErr;
  1189. }
  1190.  
  1191. /*
  1192. OSErr MADCopyCurrentPartition( MADMusic *aPartition)
  1193. {
  1194.     long    i, x;
  1195.     
  1196.     if( MDriver->header == 0L) return -1;
  1197.     if( aPartition == 0L) return -1;
  1198.     
  1199.     aPartition->header = (MADSpec*) MADNewPtr( sizeof( MADSpec), &MDriver->DriverSettings);
  1200.     if( aPartition->header == 0L) return -1;
  1201.     
  1202.     BlockMoveData( MDriver->header, aPartition->header, sizeof( MADSpec));
  1203.     
  1204.     for( i = 0; i < MDriver->header->numPat ; i++)
  1205.     {
  1206.         aPartition->partition[ i] = (PatData*) MADNewPtr( GetPtrSize( (Ptr) MDriver->partition[ i]), &MDriver->DriverSettings);
  1207.         if( aPartition->partition[ i] == 0L) return -1;
  1208.         
  1209.         BlockMoveData( MDriver->partition[ i], aPartition->partition[ i], GetPtrSize( (Ptr) MDriver->partition[ i]));
  1210.     }
  1211.     for( i = MDriver->header->numPat; i < MAXPATTERN ; i++) aPartition->partition[ i] = 0L;
  1212.     
  1213.     for( i = 0; i < MAXINSTRU ; i++)
  1214.     {
  1215.         for( x = 0; x < MDriver->header->fid[i].numSamples; x++)
  1216.         {
  1217.             sData    *dstData, *srcData = MDriver->sample[ i][ x];
  1218.             
  1219.             aPartition->sample[ i][ x] = dstData = (sData*) MADNewPtr( sizeof( sData), &MDriver->DriverSettings);
  1220.             if( dstData == 0L) return -1;
  1221.             
  1222.             BlockMoveData( srcData, dstData, sizeof( sData));
  1223.             
  1224.             dstData->data = MADNewPtr( srcData->size, &MDriver->DriverSettings);
  1225.             if( dstData->data == 0L) return -1;
  1226.             
  1227.             BlockMoveData( srcData->data, dstData->data, srcData->size);
  1228.         }
  1229.     }
  1230.     
  1231.     return noErr;
  1232. }*/
  1233.  
  1234. OSErr    MADMusicIdentifyFSp( MADLibrary *lib, char *type, FSSpec *theSpec)
  1235. {
  1236.     FSSpec    saved;
  1237.     OSErr        err;
  1238.     
  1239.     HGetVol( 0L, &saved.vRefNum, &saved.parID);
  1240.     HSetVol( 0L, theSpec->vRefNum, theSpec->parID);
  1241.     p2cstr( theSpec->name);
  1242.     
  1243.     err =  PPIdentifyFile( lib, type, (Ptr) theSpec->name);
  1244.     
  1245.     c2pstr( (Ptr) theSpec->name);
  1246.     HSetVol( 0L, saved.vRefNum, saved.parID);
  1247.     
  1248.     return err;
  1249. }
  1250.  
  1251. OSErr    MADMusicIdentifyPString( MADLibrary *lib, char *type, Str255 fName)
  1252. {
  1253.     OSErr    err;
  1254.     
  1255.     p2cstr( fName);
  1256.     err = PPIdentifyFile( lib, type, (Ptr) fName);
  1257.     c2pstr( (Ptr) fName);
  1258.     
  1259.     return err;
  1260. }
  1261. #endif
  1262.  
  1263. OSErr    MADMusicIdentifyCString( MADLibrary *lib, char *type, Ptr fName)
  1264. {
  1265.     OSErr    err;
  1266.     
  1267.     err = PPIdentifyFile( lib, type, fName);
  1268.     
  1269.     return err;
  1270. }
  1271.  
  1272. OSErr MADLoadMusicFileCString( MADLibrary *lib, MADMusic **music, char *plugType, Ptr fName)
  1273. {
  1274. OSErr            iErr;
  1275.  
  1276.     if( !MADstrcmp( "MADI", plugType)) iErr = MADLoadMADFileCString( music, fName);
  1277.     else
  1278.     {
  1279.         iErr = PPImportFile( lib, plugType, fName, music);
  1280.     }
  1281.     
  1282.     return iErr;
  1283. }
  1284.  
  1285. OSErr MADSetMusicStatus( MADDriverRec *MDriver, long minV, long maxV, long curV)
  1286. {
  1287.     short            i, x, y;
  1288.     Cmd                *aCmd;
  1289.  
  1290.     float            timeResult;
  1291.     long            time;
  1292.     long            speed, finespeed, fullTime, curTime;
  1293.     long            dstTime;
  1294.     
  1295.     if( MDriver == 0L)
  1296.     {
  1297.         return -1; 
  1298.     }
  1299.     
  1300.     if( MDriver->curMusic == 0L)
  1301.     {
  1302.         return -1; 
  1303.     }
  1304.     
  1305.     if( MDriver->curMusic->header == 0L)
  1306.     {
  1307.         return -1; 
  1308.     }
  1309.     
  1310.     MADGetMusicStatus( MDriver, &fullTime, &curTime);
  1311.     
  1312.     curV -= minV;
  1313.     maxV -= minV;
  1314.     
  1315.     dstTime            = (curV * fullTime)/maxV;
  1316.     timeResult        = 0;
  1317.     time            = 0;
  1318.     speed            = MDriver->curMusic->header->speed;
  1319.     finespeed        = MDriver->curMusic->header->tempo;
  1320.     
  1321.     for( i = 0; i < MDriver->curMusic->header->numPointers; i++)
  1322.     {
  1323.         for( x = 0; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1324.         {
  1325.             time ++;
  1326.             
  1327.             if( dstTime <= timeResult + ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed)))
  1328.             {
  1329.                 MDriver->PL = i;
  1330.                 MDriver->PartitionReader = x;
  1331.                 MDriver->Pat = MDriver->curMusic->header->oPointers[ i];
  1332.                 
  1333.                 MADCheckSpeed( MDriver->curMusic, MDriver);
  1334.                 
  1335.                 MADPurgeTrack( MDriver);
  1336.                 
  1337.                 return noErr;
  1338.             }
  1339.             
  1340.             for( y = 0; y <  MDriver->curMusic->header->numChn; y++)
  1341.             {
  1342.                 aCmd = GetMADCommand( x, y, MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]);
  1343.                                 
  1344.                 /** SpeedE **/
  1345.                 
  1346.                 if( aCmd->cmd == speedE)
  1347.                 {
  1348.                     /** Compute time for this interval **/
  1349.  
  1350.                     timeResult += ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1351.                     time = 0;
  1352.                     
  1353.                     /************************************/
  1354.                     
  1355.                     if( aCmd->arg < 32)
  1356.                     {
  1357.                         if( aCmd->arg != 0) speed = aCmd->arg;
  1358.                     }
  1359.                     else
  1360.                     {
  1361.                         if( aCmd->arg != 0) finespeed = aCmd->arg;
  1362.                     }
  1363.                 }
  1364.                 
  1365.                 /** SkipE **/
  1366.                 
  1367.                 if( aCmd->cmd == skipE)
  1368.                 {
  1369.                     for( ; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1370.                     {
  1371.                     }
  1372.                 }
  1373.             }
  1374.         }
  1375.     }
  1376.     
  1377.     return -1;
  1378. }
  1379.  
  1380. OSErr MADGetMusicStatus( MADDriverRec *MDriver, long *fullTime, long *curTime)
  1381. {
  1382.     short            i, x, y;
  1383.     Cmd                *aCmd;
  1384.  
  1385.     float            timeResult;
  1386.     long            time;
  1387.     long            speed, finespeed;
  1388.     
  1389.     if( MDriver == 0L)
  1390.     {
  1391.         *curTime = 0;
  1392.         *fullTime = 1;
  1393.         
  1394.         return -1; 
  1395.     }
  1396.     
  1397.     if( MDriver->curMusic == 0L)
  1398.     {
  1399.         *curTime = 0;
  1400.         *fullTime = 1;
  1401.         
  1402.         return -1; 
  1403.     }
  1404.     
  1405.     if( MDriver->curMusic->header == 0L)
  1406.     {
  1407.         *curTime = 0;
  1408.         *fullTime = 1;
  1409.         
  1410.         return -1; 
  1411.     }
  1412.     
  1413.     timeResult    = 0;
  1414.     time                = 0;
  1415.     speed                = MDriver->curMusic->header->speed;
  1416.     finespeed            = MDriver->curMusic->header->tempo;
  1417.     
  1418.     *curTime        = -1;
  1419.     
  1420.     for( i = 0; i < MDriver->curMusic->header->numPointers; i++)
  1421.     {
  1422.         for( x = 0; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1423.         {
  1424.             time ++;
  1425.             
  1426.             if( i == MDriver->PL    &&
  1427.                 x == MDriver->PartitionReader)
  1428.                 {
  1429.                     *curTime = timeResult + ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1430.                 }
  1431.             
  1432.             for( y = 0; y <  MDriver->curMusic->header->numChn; y++)
  1433.             {
  1434.                 aCmd = GetMADCommand( x, y, MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]);
  1435.                                 
  1436.                 /** SpeedE **/
  1437.                 
  1438.                 if( aCmd->cmd == speedE)
  1439.                 {
  1440.                     /** Compute time for this interval **/
  1441.  
  1442.                     timeResult += ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1443.                     time = 0;
  1444.                     
  1445.                     /************************************/
  1446.                     
  1447.                     if( aCmd->arg < 32)
  1448.                     {
  1449.                         if( aCmd->arg != 0) speed = aCmd->arg;
  1450.                     }
  1451.                     else
  1452.                     {
  1453.                         if( aCmd->arg != 0) finespeed = aCmd->arg;
  1454.                     }
  1455.                 }
  1456.                 
  1457.                 /** SkipE **/
  1458.                 
  1459.                 if( aCmd->cmd == skipE)
  1460.                 {
  1461.                     for( ; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1462.                     {
  1463.                         if( i == MDriver->PL    &&
  1464.                                 x == MDriver->PartitionReader)
  1465.                         {
  1466.                             *curTime = timeResult + ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1467.                         }
  1468.                     }
  1469.                 }
  1470.             }
  1471.         }
  1472.     }
  1473.     
  1474.     timeResult += ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1475.     
  1476.     *fullTime = timeResult;
  1477.     
  1478.     if( *curTime == -1) *curTime = *fullTime;
  1479.     
  1480.     return noErr;
  1481. }
  1482.  
  1483. OSErr MADReadMAD( MADMusic **music, UNFILE srcFile, short InPutType, Handle MADRsrc, Ptr MADPtr)
  1484. {
  1485. short                     i, x;
  1486. long                     inOutCount, OffSetToSample;
  1487. OSErr                    theErr = noErr;
  1488. PatHeader                tempPatHeader;
  1489. PatData*                tempPat;
  1490. MADMusic                 *MDriver;
  1491.  
  1492. *music = 0L;
  1493.  
  1494. MDriver = (MADMusic*) NewPtrClear( sizeof( MADMusic));
  1495. if( !MDriver) return -1L;
  1496.  
  1497. MDriver->musicUnderModification = false;
  1498.  
  1499. /**** HEADER ****/
  1500. inOutCount = sizeof( MADSpec);
  1501.  
  1502. MDriver->header = (MADSpec*) NewPtr( inOutCount);
  1503. if( MDriver->header == 0L)
  1504. {
  1505.     DisposePtr( (Ptr) MDriver);
  1506.     return MADNeedMemory;
  1507. }
  1508.  
  1509. switch( InPutType)
  1510. {
  1511.     #ifdef _MAC_H
  1512.     case MADRsrcType:
  1513.         OffSetToSample = 0L;
  1514.         ReadPartialResource( MADRsrc, 0L, MDriver->header, inOutCount);
  1515.         OffSetToSample += inOutCount;
  1516.     break;
  1517.     #endif
  1518.     
  1519.     case MADFileType:
  1520.         if( iRead( inOutCount, (Ptr) MDriver->header, srcFile)) theErr = MADIncompatibleFile;
  1521.     break;
  1522.     
  1523.     case MADPtrType:
  1524.         OffSetToSample = 0L;
  1525.         BlockMoveData( MADPtr, MDriver->header, inOutCount);
  1526.         OffSetToSample += inOutCount;
  1527.     break;
  1528. }
  1529.  
  1530. MOT32( &MDriver->header->MAD);
  1531. MOT16( &MDriver->header->speed);
  1532. MOT16( &MDriver->header->tempo);
  1533.  
  1534. if( MDriver->header->MAD != 'MADI' || MDriver->header->numInstru > MAXINSTRU)
  1535. {
  1536.     DisposePtr( (Ptr) MDriver->header);
  1537.     DisposePtr( (Ptr) MDriver);
  1538.     return MADIncompatibleFile;
  1539. }
  1540.  
  1541. /**** PARTITION ****/
  1542. for( i = MDriver->header->numPat; i < MAXPATTERN; i++) MDriver->partition[ i] = 0L;
  1543.  
  1544. for( i = 0; i < MDriver->header->numPat; i++)
  1545. {
  1546.     /** Lecture du header de la partition **/
  1547.     inOutCount = sizeof( PatHeader);
  1548.         
  1549.     switch( InPutType)
  1550.     {
  1551.         #ifdef _MAC_H
  1552.         case MADRsrcType:
  1553.             ReadPartialResource( MADRsrc, OffSetToSample, &tempPatHeader, inOutCount);
  1554.             //    OffSetToSample += inOutCount; ON LE RELIT APRES !!!!!!
  1555.         break;
  1556.         #endif
  1557.         
  1558.         case MADFileType:
  1559.             if( iRead( inOutCount, (Ptr) &tempPatHeader, srcFile)) theErr = MADIncompatibleFile;
  1560.             iSeekCur( -sizeof( PatHeader), srcFile);
  1561.         break;
  1562.         
  1563.         case MADPtrType:
  1564.             BlockMoveData( MADPtr + OffSetToSample, &tempPatHeader, inOutCount);
  1565.         break;
  1566.     }
  1567.     
  1568.     MOT32( &tempPatHeader.size);
  1569.     MOT32( &tempPatHeader.compMode);
  1570.     MOT32( &tempPatHeader.patBytes);
  1571.     MOT32( &tempPatHeader.unused2);
  1572.     
  1573.     /*************************************************/
  1574.     /** Lecture du header + contenu de la partition **/
  1575.     /*************************************************/
  1576.     
  1577.     switch( tempPatHeader.compMode)
  1578.     {
  1579.         case 'MAD1':
  1580.             inOutCount = sizeof( PatHeader) + tempPatHeader.patBytes;
  1581.         break;
  1582.         
  1583.         case 'NONE':
  1584.             inOutCount = sizeof( PatHeader) + MDriver->header->numChn * tempPatHeader.size * sizeof( Cmd);
  1585.         break;
  1586.         
  1587.         default:
  1588.             return MADIncompatibleFile;
  1589.         break;
  1590.     }
  1591.     
  1592.     MDriver->partition[ i] = ( PatData*) NewPtr( inOutCount);
  1593.     if( MDriver->partition[ i] == 0L)
  1594.     {
  1595.         for( x = 0; x < i; x++)
  1596.         {
  1597.             if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1598.         }
  1599.         DisposePtr( (Ptr) MDriver->header);
  1600.         DisposePtr( (Ptr) MDriver);
  1601.         
  1602.         return MADNeedMemory;
  1603.     }
  1604.     switch( InPutType)
  1605.     {
  1606.         #ifdef _MAC_H
  1607.         case MADRsrcType:
  1608.             ReadPartialResource( MADRsrc, OffSetToSample, MDriver->partition[ i], inOutCount);
  1609.             OffSetToSample += inOutCount;
  1610.         break;
  1611.         #endif
  1612.         
  1613.         case MADFileType:
  1614.             if( iRead( inOutCount, (Ptr) MDriver->partition[ i], srcFile)) theErr = MADIncompatibleFile;
  1615.         break;
  1616.         
  1617.         case MADPtrType:
  1618.             BlockMoveData( MADPtr + OffSetToSample, MDriver->partition[ i], inOutCount);
  1619.             OffSetToSample += inOutCount;
  1620.         break;
  1621.     }
  1622.     
  1623.     MOT32( &MDriver->partition[ i]->header.size);
  1624.     MOT32( &MDriver->partition[ i]->header.compMode);
  1625.     MOT32( &MDriver->partition[ i]->header.patBytes);
  1626.     MOT32( &MDriver->partition[ i]->header.unused2);
  1627.     
  1628.     if( MDriver->partition[ i]->header.compMode == 'MAD1')
  1629.     {
  1630.         tempPat = DecompressPartitionMAD1( MDriver, MDriver->partition[ i]);
  1631.         
  1632.         if( tempPat == 0L)
  1633.         {
  1634.             for( x = 0; x < i; x++)
  1635.             {
  1636.                 if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1637.             }
  1638.             DisposePtr( (Ptr) MDriver->header);
  1639.             DisposePtr( (Ptr) MDriver);
  1640.             
  1641.             return MADNeedMemory;
  1642.         }
  1643.         
  1644.         DisposePtr( (Ptr) MDriver->partition[ i]);
  1645.         MDriver->partition[ i] = tempPat;
  1646.     }
  1647. }
  1648.  
  1649. /**** INSTRUMENTS ****/
  1650. MDriver->fid = ( InstrData*) NewPtrClear( sizeof( InstrData) * (long) MAXINSTRU);
  1651. if( !MDriver->fid)
  1652. {
  1653.     for( x = 0; x < MDriver->header->numPat; x++)
  1654.     {
  1655.         if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1656.     }
  1657.     DisposePtr( (Ptr) MDriver->header);
  1658.     DisposePtr( (Ptr) MDriver);
  1659.     
  1660.     return MADNeedMemory;
  1661. }
  1662.  
  1663. inOutCount = sizeof( InstrData) * (long) MDriver->header->numInstru;
  1664. switch( InPutType)
  1665. {
  1666.     #ifdef _MAC_H
  1667.     case MADRsrcType:
  1668.         ReadPartialResource( MADRsrc, OffSetToSample, MDriver->fid, inOutCount);
  1669.         OffSetToSample += inOutCount;
  1670.     break;
  1671.     #endif
  1672.     
  1673.     case MADFileType:
  1674.         if( iRead( inOutCount, (Ptr) MDriver->fid, srcFile)) theErr = MADIncompatibleFile;
  1675.     break;
  1676.     
  1677.     case MADPtrType:
  1678.         BlockMoveData( MADPtr + OffSetToSample, MDriver->fid, inOutCount);
  1679.         OffSetToSample += inOutCount;
  1680.     break;
  1681. }
  1682.  
  1683. for( i = MDriver->header->numInstru-1; i >= 0 ; i--)
  1684. {
  1685.     InstrData    *curIns = &MDriver->fid[ i];
  1686.     
  1687.     MOT16( &curIns->numSamples);
  1688.     MOT16( &curIns->firstSample);
  1689.     MOT16( &curIns->volFade);
  1690.     
  1691.     for( x = 0; x < 12; x++)
  1692.     {
  1693.         MOT16( &curIns->volEnv[ x].pos);
  1694.         MOT16( &curIns->volEnv[ x].val);
  1695.     }
  1696.     
  1697.     for( x = 0; x < 12; x++)
  1698.     {
  1699.         MOT16( &curIns->pannEnv[ x].pos);
  1700.         MOT16( &curIns->pannEnv[ x].val);
  1701.     }
  1702.     
  1703.     if( i != curIns->no)
  1704.     {
  1705.         MDriver->fid[ curIns->no] = *curIns;
  1706.         MADResetInstrument( curIns);
  1707.     }
  1708. }
  1709. MDriver->header->numInstru = MAXINSTRU;
  1710.  
  1711. /**** SAMPLES ****/
  1712. MDriver->sample = ( sData**) NewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE);
  1713. if( !MDriver->sample)
  1714. {
  1715.     for( x = 0; x < MAXINSTRU ; x++) MADKillInstrument( MDriver, x);
  1716.     
  1717.     for( x = 0; x < MDriver->header->numPat; x++)
  1718.     {
  1719.         if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1720.     }
  1721.     DisposePtr( (Ptr) MDriver->header);
  1722.     DisposePtr( (Ptr) MDriver);
  1723.     
  1724.     return MADNeedMemory;
  1725. }
  1726.  
  1727. for( i = 0; i < MAXINSTRU ; i++)
  1728. {
  1729.     for( x = 0; x < MDriver->fid[ i].numSamples ; x++)
  1730.     {
  1731.         sData     *curData;
  1732.         
  1733.         // ** Read Sample header **
  1734.         
  1735.         curData = MDriver->sample[ i*MAXSAMPLE + x] = (sData*) NewPtr( sizeof( sData));
  1736.         if( curData == 0L)
  1737.         {
  1738.             for( x = 0; x < MAXINSTRU ; x++) MADKillInstrument( MDriver, x);
  1739.             
  1740.             for( x = 0; x < MDriver->header->numPat; x++)
  1741.             {
  1742.                 if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1743.             }
  1744.             DisposePtr( (Ptr) MDriver->header);
  1745.             DisposePtr( (Ptr) MDriver);
  1746.             
  1747.             return MADNeedMemory;
  1748.         }
  1749.         
  1750.         inOutCount = sizeof( sData);
  1751.         
  1752.         switch( InPutType)
  1753.         {
  1754.             #ifdef _MAC_H
  1755.             case MADRsrcType:
  1756.                 ReadPartialResource( MADRsrc, OffSetToSample, curData, inOutCount);
  1757.                 OffSetToSample += inOutCount;
  1758.             break;
  1759.             #endif
  1760.             
  1761.             case MADFileType:
  1762.                 if( iRead( inOutCount, (Ptr) curData, srcFile)) theErr = MADIncompatibleFile;
  1763.             break;
  1764.             
  1765.             case MADPtrType:
  1766.                 BlockMoveData( MADPtr + OffSetToSample, curData, inOutCount);
  1767.                 OffSetToSample += inOutCount;
  1768.             break;
  1769.         }
  1770.         
  1771.         MOT32( &curData->size);
  1772.         MOT32( &curData->loopBeg);
  1773.         MOT32( &curData->loopSize);
  1774.         MOT16( &curData->c2spd);
  1775.         
  1776.         // ** Read Sample DATA
  1777.         
  1778.         curData->data = NewPtr( curData->size);
  1779.         if( curData->data == 0L)
  1780.         {
  1781.             for( x = 0; x < MAXINSTRU ; x++) MADKillInstrument( MDriver, x);
  1782.             
  1783.             for( x = 0; x < MDriver->header->numPat; x++)
  1784.             {
  1785.                 if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1786.             }
  1787.             DisposePtr( (Ptr) MDriver->header);
  1788.             DisposePtr( (Ptr) MDriver);
  1789.             
  1790.             return MADNeedMemory;
  1791.         }
  1792.         
  1793.         inOutCount = curData->size;
  1794.         switch( InPutType)
  1795.         {
  1796.             #ifdef _MAC_H
  1797.             case MADRsrcType:
  1798.                 ReadPartialResource( MADRsrc, OffSetToSample, curData->data, inOutCount);
  1799.                 OffSetToSample += inOutCount;
  1800.             break;
  1801.             #endif
  1802.             
  1803.             case MADFileType:
  1804.                 if( iRead( inOutCount, (Ptr) curData->data, srcFile)) theErr = MADIncompatibleFile;
  1805.             break;
  1806.             
  1807.             case MADPtrType:
  1808.                 BlockMoveData( MADPtr + OffSetToSample, curData->data, inOutCount);
  1809.                 OffSetToSample += inOutCount;
  1810.             break;
  1811.         }
  1812.         
  1813.         if( curData->amp == 16)
  1814.         {
  1815.             long     ll;
  1816.             short    *shortPtr = (short*) curData->data;
  1817.             
  1818.             for( ll = 0; ll < curData->size/2; ll++) MOT16( &shortPtr[ ll]);
  1819.         }
  1820.     }
  1821. }
  1822.  
  1823. for( i = 0; i < MAXINSTRU; i++) MDriver->fid[ i].firstSample = i * MAXSAMPLE;
  1824. /*********************/
  1825.  
  1826. MDriver->header->MAD = 'MADI';
  1827.  
  1828. //MADCleanDriver( MDriver);
  1829.  
  1830. *music = MDriver;
  1831.  
  1832. return( noErr);
  1833. }
  1834.  
  1835. OSErr MADKillSample( MADMusic *MDriver, short ins, short sample)
  1836. {
  1837.     short    i;
  1838.     Boolean    IsReading;
  1839.     
  1840.     if( MDriver->header == 0L) return -1;
  1841.     
  1842.         IsReading = MDriver->musicUnderModification;
  1843.         MDriver->musicUnderModification = true;
  1844.     
  1845.     if( MDriver->sample[ ins * MAXSAMPLE + sample] == 0L) return -1;
  1846.     if( MDriver->fid[ ins].numSamples <= sample) return -1;
  1847.     
  1848.     if( MDriver->sample[ ins * MAXSAMPLE + sample]->data) DisposePtr( MDriver->sample[ ins * MAXSAMPLE + sample]->data);
  1849.     DisposePtr( (Ptr) MDriver->sample[ ins * MAXSAMPLE + sample]);
  1850.     MDriver->sample[ ins * MAXSAMPLE + sample] = 0L;
  1851.     
  1852.     MDriver->fid[ ins].numSamples--;
  1853.     
  1854.     for( i = sample ; i < MDriver->fid[ ins].numSamples; i++)
  1855.     {
  1856.         MDriver->sample[ ins * MAXSAMPLE + i] = MDriver->sample[ ins * MAXSAMPLE + i + 1];
  1857.     }
  1858.     
  1859.     for( i = 0; i < NUMBER_NOTES; i++)
  1860.     {
  1861.         if( MDriver->fid[ ins].what[ i] >= sample)
  1862.         {
  1863.             if( MDriver->fid[ ins].what[ i] > 0)
  1864.                 MDriver->fid[ ins].what[ i]--;
  1865.         }
  1866.     }
  1867.     
  1868.         MDriver->musicUnderModification = IsReading;
  1869.     
  1870.     return noErr;
  1871. }
  1872.  
  1873. sData    * MADCreateSample( MADMusic *MDriver, short ins, short sample)
  1874. {
  1875.     sData    *curData;
  1876.  
  1877.     curData = (sData*) NewPtrClear( sizeof( sData));
  1878.     if( curData)
  1879.     {
  1880.         curData->size        = 0L;
  1881.         curData->loopBeg    = 0L;
  1882.         curData->loopSize    = 0L;
  1883.         curData->vol        = MAX_VOLUME;
  1884.         curData->c2spd        = NOFINETUNE;
  1885.         curData->loopType    = eClassicLoop;
  1886.         curData->amp        = 8;
  1887.         curData->relNote    = 0;
  1888.         // curData->name
  1889.         curData->data        = 0L;
  1890.         
  1891.         // Install it
  1892.         
  1893.         if( sample < MDriver->fid[ ins].numSamples) MyDebugStr( __LINE__, __FILE__, "");
  1894.         
  1895.         MDriver->sample[ ins * MAXSAMPLE + sample] = curData;
  1896.         MDriver->fid[ ins].numSamples++;
  1897.     }
  1898.     
  1899.     return curData;        
  1900. }
  1901.  
  1902. void MADPurgeTrack( MADDriverRec *intDriver)
  1903. {
  1904.     short    i;
  1905.     
  1906.     for( i=0; i<intDriver->DriverSettings.numChn; i++)
  1907.     {
  1908.     //    intDriver->chan[i].lastWord        = 0;
  1909.     //    intDriver->chan[i].curLevel        = 0;
  1910.         intDriver->chan[i].prevPtr        = 0;
  1911.         
  1912.         intDriver->chan[i].curPtr = intDriver->chan[i].maxPtr;
  1913.         intDriver->chan[i].samplePtr = 0L;
  1914.         intDriver->chan[i].lAC        = 0;
  1915.         intDriver->chan[i].loopBeg = 0;
  1916.         intDriver->chan[i].loopSize = 0;
  1917.         intDriver->chan[i].RemoverWorking = false;
  1918.         intDriver->chan[i].TICKREMOVESIZE = 1;
  1919.     }
  1920. }
  1921.  
  1922. OSErr MADCleanCurrentMusic( MADMusic *MDriver, MADDriverRec *intDriver)
  1923. {
  1924.     if( MDriver->header != 0L)
  1925.     {
  1926.         short i, x;
  1927.         
  1928.         if( intDriver)
  1929.         {
  1930.             if( intDriver->Pat > MDriver->header->numPat) MADReset( intDriver);
  1931.             if( intDriver->PartitionReader > MDriver->partition[ intDriver->Pat]->header.size) MADReset( intDriver);
  1932.         }
  1933.         
  1934.         for( i = 0; i < MAXINSTRU; i++)
  1935.         {
  1936.             for( x = 0; x < 96; x++)
  1937.             {
  1938.                 if( MDriver->fid[ i].what[ x])
  1939.                 {
  1940.                     if( MDriver->fid[ i].what[ x] >= MDriver->fid[ i].numSamples)
  1941.                     {
  1942.                         MDriver->fid[ i].what[ x] = 0;
  1943.                     }
  1944.                 }
  1945.             }
  1946.             
  1947.             for( x = 0; x < MDriver->fid[ i].numSamples; x++)
  1948.             {
  1949.                 sData    *curData = MDriver->sample[ i * MAXSAMPLE + x];
  1950.                 
  1951.                 if( curData == 0L)
  1952.                 {
  1953.                     curData = MDriver->sample[ i * MAXSAMPLE + x] = MADCreateSample( MDriver, i, x);
  1954.                 }
  1955.                 
  1956.                 if( curData->data == 0L)
  1957.                 {
  1958.                     curData->data = NewPtr( 0);
  1959.                     curData->size = 0;
  1960.                 }
  1961.                 
  1962.                 if( curData->size <= 0)
  1963.                 {
  1964.                     curData->loopBeg = 0;
  1965.                     curData->loopSize = 0;
  1966.                 }
  1967.                 
  1968.                 if( curData->loopSize < 0) curData->loopSize = 0;
  1969.                 
  1970.                 if( curData->loopBeg > curData->size)
  1971.                 {
  1972.                     curData->loopBeg = 0;
  1973.                     curData->loopSize = 0;
  1974.                 }
  1975.                 
  1976.                 if( curData->loopBeg + curData->loopSize > curData->size)
  1977.                 {
  1978.                     curData->loopBeg = 0;
  1979.                     curData->loopSize = 0;
  1980.                 }
  1981.             }
  1982.         }
  1983.     }
  1984.     
  1985.     return noErr;
  1986. }
  1987.  
  1988. OSErr MADResetInstrument( InstrData        *curIns)
  1989. {
  1990.     short i;
  1991.  
  1992.     for( i = 0; i < 32; i++) curIns->name[ i]    = 0;
  1993.     curIns->type        = 0;
  1994.     curIns->numSamples    = 0;
  1995.     
  1996.     /**/
  1997.     
  1998.     for( i = 0; i < 96; i++) curIns->what[ i]        = 0;
  1999.     for( i = 0; i < 12; i++)
  2000.     {
  2001.         curIns->volEnv[ i].pos        = 0;
  2002.         curIns->volEnv[ i].val        = 0;
  2003.     }
  2004.     for( i = 0; i < 12; i++)
  2005.     {
  2006.         curIns->pannEnv[ i].pos    = 0;
  2007.         curIns->pannEnv[ i].val    = 0;
  2008.     }
  2009.     curIns->volSize        = 0;
  2010.     curIns->pannSize    = 0;
  2011.     
  2012.     curIns->volSus        = 0;
  2013.     curIns->volBeg        = 0;
  2014.     curIns->volEnd        = 0;
  2015.     
  2016.     curIns->pannSus        = 0;
  2017.     curIns->pannBeg        = 0;
  2018.     curIns->pannEnd        = 0;
  2019.     
  2020.     curIns->volType        = 0;
  2021.     curIns->pannType    = 0;
  2022.     
  2023.     curIns->volFade        = DEFAULT_VOLFADE;
  2024.     curIns->vibDepth    = 0;
  2025.     curIns->vibRate        = 0;
  2026.  
  2027.     return noErr;
  2028. }
  2029.  
  2030. OSErr MADKillInstrument( MADMusic *music, short ins)
  2031. {
  2032. short                i;
  2033. InstrData        *curIns;
  2034. Boolean            IsReading;
  2035.  
  2036.     if( music == 0L) return -1;
  2037.     
  2038.     curIns = &music->fid[ ins];
  2039.     
  2040.     IsReading = music->musicUnderModification;
  2041.     music->musicUnderModification = true;
  2042.     
  2043.     for( i = 0; i < curIns->numSamples; i++)
  2044.     {
  2045.         if( music->sample[ ins * MAXSAMPLE + i] != 0L)
  2046.         {
  2047.             if( music->sample[ ins * MAXSAMPLE + i]->data != 0L)
  2048.             {
  2049.                 DisposePtr( (Ptr) music->sample[ ins * MAXSAMPLE + i]->data);
  2050.                 music->sample[ ins * MAXSAMPLE + i]->data = 0L;
  2051.             }
  2052.             DisposePtr( (Ptr) music->sample[ ins * MAXSAMPLE + i]);
  2053.             music->sample[ ins * MAXSAMPLE + i] = 0L;
  2054.         }
  2055.     }
  2056.     
  2057.     MADResetInstrument( curIns);
  2058.     
  2059.     music->musicUnderModification = IsReading;
  2060.     
  2061.     return noErr;
  2062. }
  2063.  
  2064. OSErr MADKillCmd( Cmd    *aCmd)
  2065. {
  2066.     aCmd->cmd     = 0;
  2067.     aCmd->note     = 0xFF;
  2068.     aCmd->arg     = 0;
  2069.     aCmd->ins     = 0;
  2070.     aCmd->vol     = 0xFF;
  2071.  
  2072.     return noErr;
  2073. }
  2074.  
  2075. void MADCheckSpeed( MADMusic *MDriver, MADDriverRec *intDriver)
  2076. {
  2077.     short            i, x, y;
  2078.     short            curPartitionReader;
  2079.     Cmd                *aCmd;
  2080.     Boolean            CmdSpeed = false, FineFound = false;
  2081.  
  2082.     if( !MDriver) return;
  2083.     if( MDriver->header == 0L) return;
  2084.     if( !intDriver) return;
  2085.     
  2086.     for( i = intDriver->PL; i >= 0; i--)
  2087.     {
  2088.         if( i == intDriver->PL)
  2089.         {
  2090.             curPartitionReader = intDriver->PartitionReader;
  2091.             if( curPartitionReader >= MDriver->partition[ MDriver->header->oPointers[ i]]->header.size) curPartitionReader--;
  2092.         }
  2093.         else
  2094.         {
  2095.             curPartitionReader = MDriver->partition[ MDriver->header->oPointers[ i]]->header.size-1;
  2096.         }
  2097.  
  2098.         for( x = curPartitionReader; x >= 0; x--)
  2099.         {
  2100.             for( y = MDriver->header->numChn-1; y >= 0 ; y--)
  2101.             {
  2102.                 aCmd = GetMADCommand( x, y, MDriver->partition[ MDriver->header->oPointers[ i]]);
  2103.                 
  2104.                 if( aCmd->cmd == speedE)
  2105.                 {                    
  2106.                     if( aCmd->arg < 32)
  2107.                     {
  2108.                         if( aCmd->arg != 0)
  2109.                         {
  2110.                             if( CmdSpeed == false) intDriver->speed = aCmd->arg;
  2111.                             CmdSpeed = true;
  2112.                         }
  2113.                     }
  2114.                     else
  2115.                     {
  2116.                         if( aCmd->arg != 0)
  2117.                         {
  2118.                             if( FineFound == false) intDriver->finespeed = aCmd->arg;
  2119.                             FineFound = true;
  2120.                         }
  2121.                     }
  2122.                 }
  2123.             }
  2124.             if( CmdSpeed == true && FineFound == true) return;
  2125.         }
  2126.     }
  2127.         
  2128.  
  2129.     if( !CmdSpeed)     intDriver->speed         = MDriver->header->speed;
  2130.     if( !FineFound) intDriver->finespeed     = MDriver->header->tempo;
  2131. }
  2132.  
  2133. OSErr MADPlayMusic( MADDriverRec *MDriver)
  2134. {
  2135.     if( !MDriver) return MADParametersErr;
  2136.     if( !MDriver->curMusic) return MADDriverHasNoMusic;
  2137.     
  2138.     MDriver->Reading = true;
  2139.     
  2140.     return noErr;
  2141. }
  2142.  
  2143. OSErr MADStopMusic( MADDriverRec *MDriver)
  2144. {
  2145.     if( !MDriver) return MADParametersErr;
  2146.     if( !MDriver->curMusic) return MADDriverHasNoMusic;
  2147.     
  2148.     MDriver->Reading = false;
  2149.     
  2150.     return noErr;
  2151. }
  2152.  
  2153. OSErr MADStartDriver( MADDriverRec *MDriver)
  2154. {
  2155.     MDriver->MADPlay = true;
  2156.     MDriver->Reading = false;
  2157.     MDriver->musicEnd = false;
  2158.     
  2159.     MADCleanDriver( MDriver);
  2160.     
  2161.     MADCheckSpeed( MDriver->curMusic, MDriver);
  2162.     
  2163.     switch( MDriver->DriverSettings.driverMode)
  2164.     {
  2165.         #ifdef _MAC_H
  2166. /*    case AWACSoundDriver:
  2167.         if( (*(unsigned char *)AMICCodecStatus2 & kHeadphonesInserted))
  2168.         {
  2169.             curR1 = 0;
  2170.             curR1 |= kSpeakerMuteBit;
  2171.             WRITE_AWACS_REG_1;
  2172.         }
  2173.  
  2174.         
  2175.         CLEAR_SND_OUT_BUF_INTS;
  2176.         CLEAR_UNDERRUN_INT;
  2177.         ENABLE_BUF_AND_UNDERRUN_INTS;
  2178.         SND_OUT_DMA_ENABLE;
  2179.     break;
  2180.     
  2181.     case ASCSoundDriver:*/
  2182.     case MIDISoundDriver:
  2183.         PlayChannel( MDriver);
  2184.     break;
  2185.     
  2186.     case SoundManagerDriver:
  2187.         PlayChannel( MDriver);
  2188.     break;
  2189.     #endif
  2190.     }
  2191.     
  2192.     return( noErr);
  2193. }
  2194.  
  2195. OSErr MADStopDriver( MADDriverRec *MDriver)
  2196. {
  2197.     MDriver->MADPlay = false;
  2198.  
  2199.     switch( MDriver->DriverSettings.driverMode)
  2200.     {
  2201.     #ifdef _MAC_H
  2202. /*    case AWACSoundDriver:
  2203.         DISABLE_BUF_AND_UNDERRUN_INTS;
  2204.         SND_OUT_DMA_DISABLE;
  2205.         CLEAR_UNDERRUN_INT;
  2206.     break;
  2207.     
  2208.     case ASCSoundDriver:*/
  2209.     case MIDISoundDriver:
  2210.         AllNoteOff( MDriver);
  2211.         StopChannel( MDriver);
  2212.     break;
  2213.     
  2214.     case SoundManagerDriver:
  2215.         StopChannel( MDriver);
  2216.     break;
  2217.     #endif
  2218.     }
  2219.     
  2220.     MADCleanDriver( MDriver);
  2221.     
  2222.     return( noErr);
  2223. }
  2224.  
  2225. OSErr MADReset( MADDriverRec *MDriver)
  2226. {
  2227.     MADCleanDriver( MDriver);
  2228.     
  2229.     MDriver->BufCounter = 0;
  2230.     MDriver->BytesToGenerate = 0;
  2231.     MDriver->smallcounter = 128;
  2232.     
  2233.     MDriver->PL = 0;
  2234.     MDriver->PartitionReader = 0;
  2235.     
  2236.     if( MDriver->curMusic != 0L)
  2237.     {
  2238.         MDriver->Pat = MDriver->curMusic->header->oPointers[ MDriver->PL];
  2239.         MDriver->speed = MDriver->curMusic->header->speed;
  2240.         MDriver->finespeed = MDriver->curMusic->header->tempo;
  2241.     }
  2242.     
  2243.     return( noErr);
  2244. }
  2245.  
  2246. short MADGetNextReader( MADMusic *music, MADDriverRec *intDriver, short cur, short *pat)
  2247. {
  2248.     if( music == 0L) return 0;
  2249.     if( intDriver == 0L) return 0;
  2250.     if( music->header == 0L) return 0;
  2251.     
  2252.     cur++;
  2253.     if( cur >= music->partition[ intDriver->Pat]->header.size)
  2254.     {
  2255.         cur = 0;
  2256.         
  2257.         if( intDriver->JumpToNextPattern)
  2258.         {
  2259.             *pat = music->header->oPointers[ intDriver->PL + 1];
  2260.             
  2261.             if( intDriver->speed == 1 && intDriver->PL + 1 >= music->header->numPointers)
  2262.             {
  2263.                 *pat = music->header->oPointers[ 0];
  2264.             }
  2265.         }
  2266.     }
  2267.     
  2268.     return cur;
  2269. }
  2270.  
  2271. OSErr MADDisposeMusic( MADMusic **music)
  2272. {
  2273.     short        x;
  2274.     
  2275.     if( !music) return noErr;
  2276.     if( !*music) return noErr;
  2277.     
  2278. //    if( (*music)->currentDriver != 0L)
  2279.     {
  2280.         (*music)->musicUnderModification = true;
  2281.     //    MADPurgeTrack(  (*music)->currentDriver);
  2282.     //    MADCleanDriver(  (*music)->currentDriver);
  2283.         
  2284.     //    MADDetachMusicFromDriver( *music);
  2285.     }
  2286.     
  2287.     if(  (*music)->header != 0L)
  2288.     {
  2289.         for( x = 0; x < (*music)->header->numPat ; x++)
  2290.         {
  2291.             if(  (*music)->partition[ x] != 0L) DisposePtr( (Ptr)  (*music)->partition[ x]);
  2292.              (*music)->partition[ x] = 0L;
  2293.         }
  2294.         
  2295.         for( x = (*music)->header->numPat; x < MAXPATTERN ; x++)
  2296.         {
  2297.             if(  (*music)->partition[ x] != 0L) DebugStr("\pMADDispose (*music) !");
  2298.         }
  2299.         
  2300.         for( x = 0; x < MAXINSTRU ; x++)
  2301.         {
  2302.             MADKillInstrument( *music, x);
  2303.         }
  2304.         
  2305.         DisposePtr( (Ptr) (*music)->header);
  2306.         
  2307.         if(  (*music)->fid == 0L) DebugStr("\pMADDispose (*music) !");
  2308.         DisposePtr( (Ptr) (*music)->fid);
  2309.         
  2310.         if(  (*music)->sample == 0L) DebugStr("\pMADDispose (*music) !");
  2311.         DisposePtr( (Ptr) (*music)->sample);
  2312.     }
  2313.     
  2314.     DisposePtr( (Ptr) *music);
  2315.      *music = 0L;
  2316.     
  2317.     return( noErr);
  2318. }
  2319.  
  2320. #pragma options align=mac68k
  2321.  
  2322. Boolean    MADIsPressed( unsigned char* km2, unsigned short k)
  2323. {
  2324.     return( (Boolean) ((km2[k>>3] >> (k & 7)) & 1) );
  2325. }
  2326.  
  2327. #ifdef _MAC_H
  2328. OSErr MADPlaySoundDataSYNC( MADDriverRec *MDriver, Ptr soundPtr, long size, long channel, long note, long amplitude, long loopBeg, long loopSize, unsigned long rate, Boolean stereo)
  2329. {
  2330.     OSErr        iErr;
  2331.     Boolean        continueLoop;
  2332.     KeyMap        km;
  2333.     
  2334.     iErr = MADPlaySoundData( MDriver, soundPtr, size, channel, note, amplitude, 0, 0, rate, stereo);
  2335.     if( iErr == noErr)
  2336.     {
  2337.         continueLoop = true;
  2338.         while( continueLoop)
  2339.         {
  2340.             GetKeys( km);
  2341.             
  2342.             if( MDriver->chan[ channel].samplePtr == 0L) continueLoop = false;
  2343.             if( MADIsPressed( (unsigned char*) km, 0x37) && MADIsPressed( (unsigned char*) km, 0x2F)) continueLoop = false;
  2344.             if( Button()) continueLoop = false;
  2345.         }
  2346.         
  2347.         if( MDriver->chan[ channel].samplePtr != 0L)
  2348.         {
  2349.             MDriver->chan[ channel].curPtr         = MDriver->chan[ channel].maxPtr;
  2350.             MDriver->chan[ channel].samplePtr    = 0L;
  2351.             MDriver->chan[ channel].lAC            = 0;
  2352.             MDriver->chan[ channel].loopBeg     = 0;
  2353.             MDriver->chan[ channel].loopSize     = 0;
  2354.         }
  2355.     }
  2356.     
  2357.     return iErr;
  2358. }
  2359. #endif
  2360.  
  2361. OSErr MADPlaySoundData( MADDriverRec *MDriver, Ptr soundPtr, long size, long channel, long note, long amplitude, long loopBeg, long loopSize, unsigned long rate, Boolean stereo)
  2362. {
  2363.     Channel *curVoice;
  2364.     
  2365.     if( channel < 0)                                    return MADParametersErr;
  2366.     if( channel >= MDriver->DriverSettings.numChn)    return MADParametersErr;
  2367.     
  2368.     if( MDriver->curMusic != 0L)
  2369.     {
  2370.         MDriver->curMusic->musicUnderModification = true;
  2371.     }
  2372.     
  2373.     curVoice = &MDriver->chan[ channel];
  2374.     
  2375.     curVoice->prevPtr        = 0L;
  2376.     curVoice->samplePtr        = soundPtr;
  2377.     curVoice->stereo            = stereo;
  2378.     
  2379.     curVoice->maxPtr     = curVoice->curPtr = curVoice->begPtr = soundPtr;
  2380.     curVoice->maxPtr     += size;
  2381.     curVoice->sizePtr    = size;
  2382.     curVoice->lAC        = 0;
  2383.     curVoice->pingpong    = false;
  2384.     curVoice->preOff        = 0xFFFFFFFF;
  2385.     curVoice->preVal        = 0;
  2386.     curVoice->spreVal        = 0;
  2387.     curVoice->preVal2        = *curVoice->curPtr;
  2388.     if( curVoice->amp == 8) curVoice->preVal2R    = *(curVoice->curPtr+1);
  2389.     else curVoice->preVal2R    = *(curVoice->curPtr+2);
  2390.     curVoice->spreVal2    = *(short*) curVoice->curPtr;
  2391.     curVoice->spreVal2R    = *(short*) (curVoice->curPtr+2);
  2392.     
  2393.     if( note == 0xFF) note = 48;
  2394.     curVoice->note        = note;
  2395.     curVoice->ins        = 0;
  2396.     curVoice->viboffset    = 0;
  2397.     curVoice->amp        = amplitude;
  2398.     curVoice->fineTune    = rate >> 16L;
  2399.     
  2400.     curVoice->period    = GetOldPeriod( curVoice->note, curVoice->fineTune, MDriver);
  2401.     
  2402.     if( loopBeg > size)                     { loopBeg = 0; loopSize = 0;}
  2403.     if( loopBeg + loopSize > size)             { loopBeg = 0; loopSize = 0;}
  2404.     
  2405.     curVoice->loopBeg         = loopBeg;
  2406.     curVoice->loopSize         = loopSize;
  2407.     
  2408.     if( loopSize > 0)
  2409.     {
  2410.         curVoice->maxPtr     = (Ptr) ((long) curVoice->begPtr + loopBeg + loopSize);
  2411.     }
  2412.     
  2413.     curVoice->pann        = 32;
  2414.     
  2415.     curVoice->vol             = 64;
  2416.     curVoice->volFade        = 32767;
  2417.     curVoice->volEnv        = 64;
  2418.     curVoice->KeyOn            = true;
  2419.     
  2420.     if( MDriver->curMusic != 0L)
  2421.     {
  2422.         MDriver->curMusic->musicUnderModification = false;
  2423.     }
  2424.     
  2425.     return noErr;
  2426. }
  2427.  
  2428. void MADKeyOFF( MADDriverRec *MDriver, short track)
  2429. {
  2430.     if( track == -1)
  2431.     {
  2432.         short i;
  2433.         
  2434.         for( i = 0; i < MAXTRACK; i++) MDriver->chan[ i].KeyOn = false;
  2435.     }
  2436.     else MDriver->chan[ track].KeyOn = false;
  2437. }
  2438.  
  2439. #ifdef _MAC_H
  2440. OSErr MADPlaySndHandle( MADDriverRec *MDriver, Handle sound, long channel, long note)
  2441. {
  2442.     Ptr             soundPtr;
  2443.     short             soundFormat;
  2444.     short             numSynths, numCmds;
  2445.     long             offset;
  2446.     SoundHeaderPtr     header;
  2447.     CmpSoundHeader    *CmpHeader;
  2448.     ExtSoundHeader    *ExtHeader;
  2449.     long            i;
  2450.     Boolean            stereo = false;
  2451.     Channel            *curVoice;
  2452.     OSType            Scomp;
  2453.     long            Ssize, Samp, SbaseFreq, Sc2spd, Sstart, Send;
  2454.     Ptr                rPtr;
  2455.     
  2456.     /* make the sound safe to use at interrupt time. */
  2457.     HLock( sound);
  2458.     
  2459.     soundPtr = *sound;
  2460.     
  2461.     /* determine what format sound we have. */
  2462.     soundFormat = *(short*)soundPtr;
  2463.     
  2464.     switch(soundFormat)
  2465.     {
  2466.         case 1:                        /* format 1 sound. */
  2467.             /* look inside the format 1 resource and deduce offsets. */
  2468.             numSynths = ((short*)soundPtr)[1];                    /* get # synths. */
  2469.             numCmds = *(short*)(soundPtr+4+numSynths*6);        /* get # commands. */
  2470.         break;
  2471.         
  2472.         case 2:                        /* format 2 sound. */
  2473.             numSynths = 0;            /* format 2 sounds have no synth's. */
  2474.             numCmds = ((short*)soundPtr)[2];
  2475.         break;
  2476.         
  2477.         default:                    /* jack says, what about 12? or 6? */
  2478.             HUnlock( sound);
  2479.             return MADUnknowErr;
  2480.         break;
  2481.     } 
  2482.  
  2483.     /* compute address of sound header. */
  2484.     offset = 6 + 6*numSynths + 8*numCmds;
  2485.     header = (SoundHeaderPtr) (StripAddress(*sound) + offset);
  2486.     
  2487.     switch( header->encode)
  2488.     {
  2489.         case cmpSH:
  2490.             CmpHeader = (CmpSoundHeader*) header;
  2491.             if( CmpHeader->numChannels > 2)
  2492.             {
  2493.                 HUnlock( sound);
  2494.                 return MADUnknowErr;
  2495.             }
  2496.             
  2497.             if( CmpHeader->numChannels == 2) stereo = true;
  2498.             else stereo = false;
  2499.             
  2500.             Sstart        = CmpHeader->loopStart;
  2501.             Send        = CmpHeader->loopEnd;
  2502.             Samp        = CmpHeader->sampleSize;
  2503.             Sc2spd        = CmpHeader->sampleRate;
  2504.             SbaseFreq     = CmpHeader->baseFrequency;
  2505.             Ssize         = CmpHeader->numFrames;
  2506.             rPtr         = (Ptr) CmpHeader->sampleArea;
  2507.             Scomp        = CmpHeader->compressionID;
  2508.         break;
  2509.  
  2510.         case extSH:
  2511.             ExtHeader    = (ExtSoundHeader*) header;
  2512.             if( ExtHeader->numChannels > 2)
  2513.             {
  2514.                 HUnlock( sound);
  2515.                 return MADUnknowErr;
  2516.             }
  2517.             
  2518.             if( ExtHeader->numChannels == 2) stereo = true;
  2519.             else stereo = false;
  2520.             
  2521.             Sstart        = ExtHeader->loopStart;
  2522.             Send        = ExtHeader->loopEnd;
  2523.             Samp        = ExtHeader->sampleSize;
  2524.             Sc2spd        = ExtHeader->sampleRate;
  2525.             SbaseFreq     = ExtHeader->baseFrequency;
  2526.             Ssize         = ExtHeader->numFrames;
  2527.             rPtr         = (Ptr) ExtHeader->sampleArea;
  2528.             Scomp        = 'NONE';
  2529.         break;
  2530.         
  2531.         default:
  2532.         case stdSH:
  2533.             Sstart        = header->loopStart;
  2534.             Send        = header->loopEnd;
  2535.             if( header->encode == 0x40) Samp        = 0;
  2536.             else Samp        = 8;
  2537.             Sc2spd        = header->sampleRate;
  2538.             SbaseFreq     = header->baseFrequency;
  2539.             Ssize         = header->length;
  2540.             rPtr         = (Ptr) header->sampleArea;
  2541.             Scomp        = 'NONE';
  2542.         break;
  2543.     }
  2544.     
  2545.     if( Samp == 16)
  2546.     {
  2547.         Ssize     *= 2L;
  2548.         Sstart     *= 2L;
  2549.         Send     *= 2L;
  2550.         
  2551.         if( stereo)
  2552.         {
  2553.             Ssize     *= 2L;
  2554.             Sstart     *= 2L;
  2555.             Send     *= 2L;
  2556.         }
  2557.     }
  2558.     else
  2559.     {
  2560.         if( Samp == 8)
  2561.         {
  2562.             if( stereo)
  2563.             {
  2564.                 Ssize     *= 2L;
  2565.                 Sstart     *= 2L;
  2566.                 Send     *= 2L;
  2567.             }
  2568.             
  2569.             for( i = 0; i < Ssize; i++) rPtr[ i] = rPtr[ i] - 0x80;
  2570.             
  2571.             switch( header->encode)
  2572.             {
  2573.                 case extSH:
  2574.                     ExtHeader->sampleSize = 0;
  2575.                 break;
  2576.                 
  2577.                 case cmpSH:
  2578.                     CmpHeader->sampleSize = 0;
  2579.                 break;
  2580.                 
  2581.                 default:
  2582.                 case stdSH:
  2583.                     header->encode = 0x40;
  2584.                 break;
  2585.             }
  2586.         }
  2587.         Samp = 8;
  2588.     }
  2589.     
  2590.     Sc2spd = Sc2spd >> 16L;
  2591.     
  2592.     /*********************/
  2593.     if( channel < 0)                                    return MADParametersErr;
  2594.     if( channel >= MDriver->DriverSettings.numChn)        return MADParametersErr;
  2595.     
  2596.     if( MDriver->curMusic != 0L)
  2597.     {
  2598.         MDriver->curMusic->musicUnderModification = true;
  2599.     }
  2600.     
  2601.     curVoice = &MDriver->chan[ channel];
  2602.  
  2603.     
  2604.     if( note == 0xFF) note = 48;
  2605.     
  2606.     curVoice->prevPtr        = 0L;
  2607.     curVoice->samplePtr        = rPtr;
  2608.     curVoice->stereo            = stereo;
  2609.     
  2610.     curVoice->note        = note + (60 - SbaseFreq);
  2611.     curVoice->period    = GetOldPeriod( curVoice->note, Sc2spd, MDriver);
  2612.     
  2613.     curVoice->ins        = 0;
  2614.     curVoice->viboffset    = 0;
  2615.     curVoice->amp        = Samp;
  2616.     curVoice->fineTune    = Sc2spd;
  2617.  
  2618.     curVoice->maxPtr     = curVoice->curPtr = curVoice->begPtr = rPtr;
  2619.     curVoice->maxPtr     += Ssize;
  2620.     curVoice->sizePtr    = Ssize;
  2621.     curVoice->lAC        = 0;
  2622.     curVoice->pingpong    = false;
  2623.     curVoice->preOff        = 0xFFFFFFFF;
  2624.     curVoice->preVal        = 0;
  2625.     curVoice->spreVal        = 0;
  2626.     curVoice->preVal2        = *curVoice->curPtr;
  2627.     if( curVoice->amp == 8) curVoice->preVal2R    = *(curVoice->curPtr+1);
  2628.     else curVoice->preVal2R    = *(curVoice->curPtr+2);
  2629.     curVoice->spreVal2    = *(short*) curVoice->curPtr;
  2630.     curVoice->spreVal2R    = *(short*) (curVoice->curPtr+2);
  2631.     
  2632.     if( Send == Sstart || Send - Sstart < 3)
  2633.     {
  2634.         Send = 0;
  2635.         Sstart = 0;
  2636.     }
  2637.     
  2638.     curVoice->loopBeg         = Sstart;
  2639.     curVoice->loopSize         = Send - Sstart;
  2640.     
  2641.     if( curVoice->loopSize > 0)
  2642.     {
  2643.         curVoice->maxPtr     = (Ptr) ((long) curVoice->begPtr + Send);
  2644.     }
  2645.     
  2646.     curVoice->pann        = 32;
  2647.     curVoice->vol         = 64;
  2648.     curVoice->volFade        = 32767;
  2649.     curVoice->volEnv        = 64;
  2650.     curVoice->KeyOn        = true;
  2651.     
  2652.     if( MDriver->curMusic != 0L)
  2653.     {
  2654.         MDriver->curMusic->musicUnderModification = false;
  2655.     }
  2656.     
  2657.     return noErr;
  2658. }
  2659.  
  2660. #endif
  2661.  
  2662. #pragma options align=reset
  2663.  
  2664. Cmd* GetMADCommand( short PosX, short    TrackIdX, PatData*    tempMusicPat)
  2665. {
  2666.     if( PosX < 0) PosX = 0;
  2667.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  2668.     
  2669.     return( &(tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  2670. }
  2671.  
  2672. void MADDisposeVolumeTable( MADDriverRec *intDriver)
  2673. {
  2674.     if( intDriver->DriverSettings.outPutMode == DeluxeStereoOutPut) MADKillOverShoot( intDriver);
  2675. }
  2676.  
  2677. OSErr MADCreateVolumeTable( MADDriverRec *intDriver)
  2678. {
  2679. long            Tracks = intDriver->DriverSettings.numChn;
  2680. OSErr            theErr;
  2681.  
  2682.     theErr = MADCreateMicroDelay( intDriver);            if( theErr != noErr) return theErr;
  2683.  
  2684.     switch( intDriver->DriverSettings.outPutMode)
  2685.     {
  2686.         case DeluxeStereoOutPut:            Tracks    = 1;        MADCreateOverShoot( intDriver);    break;
  2687.         case PolyPhonic:                    Tracks     = 1;        break;
  2688.     }
  2689.     
  2690.     return( noErr);
  2691. }
  2692.  
  2693. void MADChangeTracks( MADDriverRec *MDriver, short newVal)
  2694. {
  2695.     Boolean    play = MDriver->MADPlay, reading = MDriver->Reading;
  2696.     
  2697.     MADStopDriver( MDriver);
  2698.     
  2699.     MDriver->DriverSettings.numChn = newVal;
  2700.     
  2701.     MADDisposeVolumeTable( MDriver);
  2702.     MADCreateVolumeTable( MDriver);
  2703.     
  2704.     MDriver->trackDiv = MDriver->DriverSettings.numChn;
  2705.     
  2706.     if( play) MADStartDriver( MDriver);
  2707.     if( reading) MADPlayMusic( MDriver);
  2708. }
  2709.  
  2710. void UpdateTracksNumber( MADDriverRec *MDriver)
  2711. {
  2712.     if( MDriver->curMusic != 0L)
  2713.     {
  2714.         if( MDriver->curMusic->header != 0L)
  2715.         {
  2716.             if( MDriver->curMusic->header->numChn != MDriver->DriverSettings.numChn)
  2717.             {
  2718.                 MADChangeTracks( MDriver, MDriver->curMusic->header->numChn);
  2719.             }
  2720.         }
  2721.     }
  2722. }
  2723.  
  2724. OSErr MADCreateVibrato( MADDriverRec *MDriver)
  2725. {
  2726. short            i, vibrato_table[ 64] = 
  2727.  
  2728. {
  2729.     0,24,49,74,97,120,141,161,
  2730.     180,197,212,224,235,244,250,253,
  2731.     255,253,250,244,235,224,212,197,
  2732.     180,161,141,120,97,74,49,24
  2733. };
  2734.  
  2735. /*    {
  2736.         0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  2737.         510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  2738.         -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  2739.         -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  2740.         -195,-148,-99,-49
  2741.     };*/
  2742.     
  2743.     
  2744.  
  2745.     for( i = 0; i < 64; i++) MDriver->vibrato_table[ i] = vibrato_table[ i];
  2746.  
  2747.     return noErr;
  2748.  
  2749. /*
  2750. double            base, pitch;
  2751. short            i, j, k;
  2752.  
  2753.     for (j = -8; j < 8; j++)
  2754.     {
  2755.         k = j < 0 ? j + 16 : j;
  2756.         base = AMIGA_CLOCKFREQ2 / 440.0 / 4.0 / pow(2.0, j/96.0);
  2757.     
  2758.         for (i = 0; i < NUMBER_NOTES + 4; i++)
  2759.         {
  2760.                 pitch = base / pow(2.0, (i+1) /12.0);
  2761.                 pitchTable[ i][ k] = floor(pitch + 0.5);
  2762.         }
  2763.     }
  2764. */
  2765. }
  2766. /*
  2767. #ifdef _MAC_H
  2768.  
  2769. #define VIA 0x1D4
  2770.  
  2771. #if defined(powerc) || defined (__powerc) || defined( NO_ASM)
  2772. void VIAOn2()
  2773. {
  2774.     register    Ptr        VIAPtr = *(Ptr*) VIA, ASCBasePtr = *(Ptr*) ASCBase;
  2775.  
  2776.     BitSet( (Ptr) VIAPtr, 0);
  2777.  
  2778.     if( ASCBasePtr[ 0x801] == 0)
  2779.     {
  2780.         ASCBasePtr[ 0x801] = 0x01;
  2781.         ASCBasePtr[ 0x806] = 0xFF;
  2782.     }
  2783.     
  2784.     BitSet( &ASCBasePtr[ 0x802], 6);
  2785. }
  2786.  
  2787. void SndOff()
  2788. {
  2789.     Ptr        VIAPtr = *(Ptr*) VIA;
  2790.  
  2791.     BitSet( (Ptr) VIAPtr, 0);
  2792.     *((Byte*) SoundActive) = 0x05;
  2793. }
  2794.  
  2795. void SndOn()
  2796. {
  2797.     Ptr        VIAPtr = *(Ptr*) VIA;
  2798.  
  2799.     if( BitTst( (Ptr) VIAPtr, 0))
  2800.     {
  2801.         BitClr( (Ptr) VIAPtr, 0);
  2802.          *((Byte*) SoundActive) = 0x05;
  2803.     }
  2804. }
  2805. #else
  2806.  
  2807. void VIAOn2()
  2808. {
  2809. asm
  2810. {
  2811.         MOVEA.L VIA,A0
  2812.         BSET    #7,(A0)
  2813.         
  2814.         MOVE.L    ASCBase, A0
  2815.         TST.B    0x801(A0)
  2816.         BNE.S   @lcf_2
  2817.         MOVE.B    #01,     0x801(A0)
  2818.         MOVE.B    #0xFF,     0x806(A0)
  2819. @lcf_2    BSET    #1,        0x802(A0)
  2820. }
  2821. }
  2822.  
  2823. void SndOff()
  2824. {
  2825. asm
  2826. {
  2827.     MOVEA.L    VIA,A0
  2828.     BSET    #7,(A0)
  2829.     MOVE.B    #0x05, SoundActive
  2830. }
  2831. }
  2832.  
  2833. void SndOn()
  2834. {
  2835. asm
  2836. {        MOVEA.L VIA,A0
  2837.          BTST    #7,(A0)
  2838.          BEQ.S   @lcf_1
  2839.          BCLR    #7,(A0)
  2840.          MOVE.B    #0x05, SoundActive
  2841. @lcf_1    
  2842. }
  2843. }
  2844. #endif
  2845. #endif*/
  2846.  
  2847. enum
  2848. {
  2849.     ins     = 1,
  2850.     note    = 2,
  2851.     cmd        = 4,
  2852.     argu    = 8,
  2853.     vol        = 16
  2854. };
  2855.  
  2856. PatData* DecompressPartitionMAD1( MADMusic *MDriver, PatData* myPat)
  2857. {
  2858.     PatData*                finalPtr;
  2859.     Byte                     *srcPtr;
  2860.     Cmd                        *myCmd;
  2861.     short                    maxCmd;
  2862.     Byte                    set;
  2863.     
  2864.     finalPtr = ( PatData*) NewPtr( sizeof( PatHeader) + myPat->header.size * MDriver->header->numChn * sizeof( Cmd));
  2865.     if( finalPtr == 0L) return 0L;
  2866.     
  2867.     BlockMoveData( myPat, finalPtr, sizeof( PatHeader));
  2868.  
  2869.     srcPtr = (Byte*) myPat->Cmds;
  2870.     myCmd = (Cmd*) finalPtr->Cmds;
  2871.     maxCmd = finalPtr->header.size * MDriver->header->numChn;
  2872.     
  2873.     /*** Decompression Routine ***/
  2874.     
  2875.     while( maxCmd != 0)
  2876.     {
  2877.         maxCmd--;
  2878.         
  2879.         MADKillCmd( myCmd);
  2880.         
  2881.         set = *srcPtr++;
  2882.         
  2883.         if( set & ins)    myCmd->ins = *srcPtr++;
  2884.         if( set & note)    myCmd->note = *srcPtr++;
  2885.         if( set & cmd)    myCmd->cmd = *srcPtr++;
  2886.         if( set & argu)    myCmd->arg = *srcPtr++;
  2887.         if( set & vol)    myCmd->vol = *srcPtr++;
  2888.         
  2889.         myCmd++;
  2890.     }
  2891.     
  2892.     return finalPtr;
  2893. }
  2894.  
  2895. PatData* CompressPartitionMAD1( MADMusic *MDriver, PatData* myPat)
  2896. {
  2897.     PatData*                finalPtr;
  2898.     Byte                     *dstPtr, *setByte;
  2899.     Cmd                        *myCmd;
  2900.     long                    maxCmd;
  2901.     long                    NewPtrSize;
  2902.     Byte                    set;
  2903.     
  2904.     finalPtr = ( PatData*) NewPtr( sizeof( PatHeader) + myPat->header.size * MDriver->header->numChn * 6L);
  2905.     if( finalPtr == 0L) DebugStr("\pCompressPartitionMAD1");
  2906.     
  2907.     BlockMoveData( myPat, finalPtr, sizeof( PatHeader));
  2908.     
  2909.     dstPtr = (Byte*) finalPtr->Cmds;
  2910.     myCmd = (Cmd*) myPat->Cmds;
  2911.     maxCmd = myPat->header.size * MDriver->header->numChn;
  2912.     
  2913.     /*** Compression Routine ***/
  2914.     
  2915.     set         = 0;
  2916.     NewPtrSize     = 0;
  2917.     
  2918.     while( maxCmd != 0)
  2919.     {
  2920.         maxCmd--;
  2921.         
  2922.         // Set byte
  2923.         set            = 0;
  2924.         setByte     = dstPtr;
  2925.         *dstPtr++     = 0x00;
  2926.         NewPtrSize++;
  2927.         ////////
  2928.         
  2929.         if( myCmd->ins > 0)
  2930.         {
  2931.             set += ins;
  2932.             *dstPtr++ = myCmd->ins;
  2933.             NewPtrSize++;
  2934.         }
  2935.         
  2936.         if( myCmd->note != 0xFF)
  2937.         {
  2938.             set += note;
  2939.             *dstPtr++ = myCmd->note;
  2940.             NewPtrSize++;
  2941.         }
  2942.  
  2943.         if( myCmd->cmd > 0)
  2944.         {
  2945.             set += cmd;
  2946.             *dstPtr++ = myCmd->cmd;
  2947.             NewPtrSize++;
  2948.         }
  2949.  
  2950.         if( myCmd->arg > 0)
  2951.         {
  2952.             set += argu;
  2953.             *dstPtr++ = myCmd->arg;
  2954.             NewPtrSize++;
  2955.         }
  2956.  
  2957.         if( myCmd->vol != 0xFF)
  2958.         {
  2959.             set += vol;
  2960.             *dstPtr++ = myCmd->vol;
  2961.             NewPtrSize++;
  2962.         }
  2963.         // Set byte
  2964.         *setByte = set;
  2965.         ////////
  2966.         
  2967.         myCmd++;
  2968.     }
  2969.     
  2970.     finalPtr->header.patBytes = NewPtrSize;
  2971.     
  2972. //    SetPtrSize( (Ptr) finalPtr, (long)(NewPtrSize + sizeof( PatHeader)));
  2973.     
  2974.     return finalPtr;
  2975. }
  2976.  
  2977. /*
  2978. #ifdef _MAC_H
  2979. void DMAIntInstall( unsigned long refCon, Ptr handlerAddr, unsigned long vectorBit)
  2980. {
  2981.     Ptr        ExpandPtr = (Ptr) 0x02B6L;
  2982.     Ptr        dstPtr;
  2983.     
  2984.     dstPtr = *((Ptr*) ExpandPtr);
  2985.     dstPtr += 0x0210;
  2986.     dstPtr = *((Ptr*) dstPtr);
  2987.     
  2988.     *((long*)( 0x04L + (long) dstPtr + vectorBit*8L)) = (long) handlerAddr;
  2989.     *((long*)( 0x08L + (long) dstPtr + vectorBit*8L)) = refCon;
  2990. }
  2991.  
  2992. void DMAIntRemove( unsigned long vectorBit)
  2993. {
  2994.     Ptr        ExpandPtr = (Ptr) 0x02B6L;
  2995.     Ptr        dstPtr;
  2996.     
  2997.     dstPtr = *((Ptr*)ExpandPtr);
  2998.     dstPtr += 0x0210;
  2999.     dstPtr = *((Ptr*) dstPtr);
  3000.     
  3001.     *((long*)( 0x04L + (long) dstPtr + vectorBit*8L)) = (long) 0x0060;
  3002.     *((long*)( 0x08L + (long) dstPtr + vectorBit*8L)) = 0L;
  3003. }
  3004.  
  3005.  
  3006. extern MADDriverRec *MODPlayMADDriver;
  3007.  
  3008. void    InstallMODVBL( MADDriverRec *MDriver)
  3009. {
  3010. //    THz             savedZone;
  3011.  
  3012.     MDriver->VBL.VBL.qLink        = 0L;        // VLBQueue
  3013.     MDriver->VBL.VBL.qType        = vType;
  3014.         
  3015. //    savedZone = GetZone();
  3016. //    SetZone( SystemZone());
  3017.     MDriver->VBL.VBL.vblAddr        = NewVBLProc( MODPlay);
  3018. //    SetZone( savedZone);
  3019.     
  3020.     MDriver->VBL.VBL.vblCount        = 0;
  3021.     MDriver->VBL.VBL.vblPhase        = 0;
  3022.     MDriver->VBL.VBLA5                    = (long) MDriver;
  3023.     
  3024.     MODPlayMADDriver = MDriver;
  3025.     
  3026.     InstallPersistentVBL( MDriver, &MDriver->VBL.VBL);
  3027. }
  3028.  
  3029. void    RemoveMODVBL( MADDriverRec *MDriver)
  3030. {
  3031.     OSErr    myErr;
  3032.     
  3033.     myErr = VRemove( (QElemPtr) &MDriver->VBL);
  3034.     
  3035.     if( MDriver->SysHeapPtr != 0L)
  3036.     {
  3037.         DisposeRoutineDescriptor( MDriver->VBL.VBL.vblAddr);
  3038.         
  3039.         DisposePtr( MDriver->SysHeapPtr);
  3040.         MDriver->SysHeapPtr = 0L;
  3041.     }
  3042. }
  3043.  
  3044. void InstallPersistentVBL( MADDriverRec *MDriver, VBLTask *theVBLRec)
  3045. {
  3046.     #define kJMPInstr     0x4EF9
  3047.     #define kJMPSize         6
  3048.     
  3049.     OSErr    myErr;
  3050.     Ptr        tempPtr;
  3051.  
  3052.     MDriver->SysHeapPtr = NewPtrSys( kJMPSize);
  3053.     if( MDriver->SysHeapPtr == 0L) DebugStr("\pNewPtrSys Error");
  3054.  
  3055.     *(short*) MDriver->SysHeapPtr = kJMPInstr;
  3056.     tempPtr = (Ptr) MDriver->SysHeapPtr + sizeof( short);
  3057.     
  3058.     *(ProcPtr*)tempPtr = (ProcPtr) theVBLRec->vblAddr;
  3059.     
  3060.     #if defined(powerc) || defined (__powerc)
  3061.     #else
  3062.     theVBLRec->vblAddr = (VBLUPP) ( MDriver->SysHeapPtr);
  3063.     #endif
  3064.     
  3065.     myErr = VInstall( (QElemPtr) theVBLRec);
  3066.     if( myErr) DebugStr("\pError in VInstall");
  3067. }
  3068. #endif*/