home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 43 / Mac Magazin and MacEasy Magazine CD - Issue 43.iso / Software / Multimedia / Sound / PlayerPRO 4.6 Dev.Kit / MADH Library 4.6 / MADLibrary Source / MainDriver.c < prev    next >
Encoding:
Text File  |  1998-02-12  |  68.0 KB  |  3,147 lines  |  [TEXT/CWIE]

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