home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / PlayRiffs 1.0.2 / PlayRiffs.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  11.1 KB  |  545 lines  |  [TEXT/CWIE]

  1. // PlayRiffs
  2. // version 1.0.2
  3. // by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951201
  5.  
  6. #include "Sound.h"
  7.  
  8. #define over qd.screenBits.bounds.right
  9. #define down qd.screenBits.bounds.bottom
  10.  
  11. PicHandle        aboutPict, pattyPict, bluePict;
  12. PixPatHandle    Miss_Patty;
  13.  
  14. short riff;
  15.  
  16. Handle            riff_1, riff_2, riff_3, riff_4, riff_5, 
  17.                 riff_6, riff_7, riff_8, riff_9;
  18.                 
  19. SndChannelPtr    chanPtr = 0L, riff_ptr = 0L, channelPtr;
  20. SndCommand        call_back, flush_out, quiet_down, buffer_it;
  21. Boolean         Jimi_Hendrix = false, hotLicks = false, 
  22.                 quiet = false, tunes = true;
  23.  
  24. char            var_1, var_2;
  25.  
  26. WindowPtr        shell_window, splash_window;
  27.  
  28. RgnHandle        mBarRgn, GrayRgn;
  29. short            *mBarHeightPtr;
  30. short            twenty_pixels;
  31. short             riffRes;
  32. Rect             splashBounds, pictRect;
  33. long             ticks;
  34.  
  35. //• Prototypes:
  36. //• Set up...
  37. void Hide_Menu_Bar (void);
  38. void Show_Menu_Bar (void);
  39. void Do_The_Splash (void);
  40. void Create_Our_Screen (void);
  41. Boolean We_Be_Hued (void);
  42. void Wake_The_Managers (void);
  43. void Bag_Some_Riffs (void);
  44. void Setup_Snd_Commands (void);
  45.  
  46. //• ...and run.
  47. void Do_The_Sound (short whichID, Boolean asynch);
  48. void Stop_The_Riffs (void);
  49. void Jam_Session (void);
  50. pascal void Auto_Redial (SndChannelPtr chan, SndCommand order);
  51. void Play_The_Riffs (void);
  52. void Main_Event_Loop (void);
  53. void Handle_Key_Hits (EventRecord *theEvent);
  54. void Put_Things_Back (void);
  55. void main (void);
  56.  
  57. //• --------------------------------------------------------------- •//
  58. //• ----------------- S E T U P - S E C T I O N ------------------- •//
  59. //• --------------------------------------------------------------- •//
  60.  
  61. void Hide_Menu_Bar (void) 
  62. {
  63.     Rect    mBarRect;
  64.  
  65.     GrayRgn = GetGrayRgn ();
  66.     mBarHeightPtr = (short *)  0x0BAA;
  67.     twenty_pixels = *mBarHeightPtr;
  68.     *mBarHeightPtr = 0;
  69.     mBarRect = qd.screenBits.bounds;
  70.     mBarRect.bottom = mBarRect.top + twenty_pixels;
  71.     mBarRgn = NewRgn ();
  72.     RectRgn (mBarRgn, &mBarRect);
  73.     UnionRgn (GrayRgn, mBarRgn, GrayRgn);
  74.     PaintOne (0L, mBarRgn);
  75. }
  76.  
  77. void Show_Menu_Bar (void) 
  78. {
  79.     *mBarHeightPtr = twenty_pixels;
  80.     DiffRgn (GrayRgn, mBarRgn, GrayRgn);
  81.     DisposeRgn (mBarRgn);
  82. }
  83.  
  84. void Do_The_Splash ()
  85. {
  86.     
  87.     //• A centered, 300w, 250h window.
  88.     SetRect (&splashBounds, over / 2 - 150, 
  89.                             down / 2 - 125, 
  90.                             over / 2 + 150,
  91.                             down / 2 + 125);
  92.  
  93.     //• "C" window so our 'ppat" will draw.
  94.     splash_window = NewCWindow (0L,             //• No storage - LIVE!
  95.                                &splashBounds,     //• As delineated above.
  96.                                "\p",             //• No place for one.
  97.                                true,             //• Yes, it's visible
  98.                                dBoxProc,         //• For dialog border.
  99.                               (WindowPtr) -1L,     //• In front.
  100.                                false,             //• No go away.
  101.                                0);                //• Reference value 0.
  102.                               
  103.     SetPort(splash_window);
  104.  
  105.     //• splash_window is our set port, so we go off local coords.
  106.     SetRect (&pictRect, 0, 0, 300, 249);
  107.     
  108.     aboutPict = GetPicture (128);
  109.     
  110.     DrawPicture (aboutPict, &pictRect);
  111.     Delay (360, &ticks);
  112.     HideWindow (splash_window);
  113. }
  114.  
  115. void Create_Our_Screen ()
  116. {
  117.     Rect windowBounds, blueRect, sisterRect;
  118.     
  119.     windowBounds = qd.screenBits.bounds;
  120.     
  121.     shell_window = NewCWindow(0L, &windowBounds, "\p", true, plainDBox, (WindowPtr) -1L, true, 0);
  122.     SetPort(shell_window);
  123.  
  124.     Miss_Patty = GetPixPat (128);
  125.  
  126.     FillCRect (&windowBounds, Miss_Patty);
  127.  
  128.     SetRect (&blueRect, 73, 217, 73 + 38, 217 + 55);
  129.     bluePict = GetPicture (130);
  130.     DrawPicture (bluePict, &blueRect);
  131. }
  132.  
  133. Boolean We_Be_Hued ()
  134. {
  135.     SysEnvRec whichMac;
  136.     SysEnvirons (2, &whichMac);
  137.     return (whichMac.hasColorQD);
  138. }
  139.  
  140. void Wake_The_Managers ()
  141. {
  142.     MaxApplZone ();
  143.     InitGraf (&qd.thePort);
  144.     InitFonts ();
  145.     FlushEvents (everyEvent, 0);
  146.     InitWindows ();
  147.     InitMenus ();
  148.     TEInit ();
  149.     InitDialogs (0L);
  150.     InitCursor ();
  151.     
  152.     MoreMasters ();
  153.     MoreMasters ();
  154.     MoreMasters ();
  155. }
  156.  
  157. void Bag_Some_Riffs ()
  158. {        
  159. //     riffRes = OpenResFile("\pRiffs");   // Tired this - stack ate heap.
  160.                                         // It needs more work.
  161. //     if (riffRes == 0L)
  162. //         ExitToShell ();
  163.     
  164.     //• We're gathering by name.
  165.     riff_1 = GetNamedResource ('snd ', "\pRiff_01");
  166.     riff_2 = GetNamedResource ('snd ', "\pRiff_02");
  167.     riff_3 = GetNamedResource ('snd ', "\pRiff_03");
  168.     riff_4 = GetNamedResource ('snd ', "\pRiff_04");
  169.     riff_5 = GetNamedResource ('snd ', "\pRiff_05");
  170.     riff_6 = GetNamedResource ('snd ', "\pRiff_06");
  171.     riff_7 = GetNamedResource ('snd ', "\pRiff_07");
  172.     riff_8 = GetNamedResource ('snd ', "\pRiff_08");
  173.     riff_9 = GetNamedResource ('snd ', "\pRiff_09");
  174. }
  175.  
  176. void Setup_Snd_Commands ()
  177. {
  178.     chanPtr = (SndChannelPtr) NewPtrClear (sizeof (SndChannel));
  179.     if (chanPtr != 0L)
  180.     {
  181.         chanPtr->qLength = stdQLength;
  182.     }
  183.     riff_ptr = 0L;
  184.     call_back.cmd = callBackCmd;
  185.     call_back.param1 = 0;
  186.     call_back.param2 = SetCurrentA5 ();
  187.     
  188.     flush_out.cmd = flushCmd;
  189.     flush_out.param1 = 0;
  190.     flush_out.param2 = 0L;
  191.     
  192.     quiet_down.cmd = quietCmd;
  193.     quiet_down.param1 = 0;
  194.     quiet_down.param2 = 0L;
  195.     
  196.     buffer_it.cmd = bufferCmd;
  197.     buffer_it.param1 = 0;
  198.     buffer_it.param2 = 0L;
  199. }
  200.  
  201. //• --------------------------------------------------------------- •//
  202. //• -------------------- R U N - S E C T I O N -------------------- •//
  203. //• --------------------------------------------------------------- •//
  204.  
  205. //• John Calhoun's sound player, from Stella Obscura (again).
  206. void Do_The_Sound (short whichID, Boolean asynch)
  207. {
  208.     Handle theSnd;
  209.     OSErr err;
  210.     Boolean soundActive;
  211.     
  212.     soundActive = true;    
  213.     
  214.     if ((soundActive))
  215.     {
  216.         theSnd = GetResource ('snd ', whichID);
  217.         
  218.         if ((theSnd != 0L) && (ResError () == noErr))
  219.         {
  220.             if ((channelPtr != 0L))
  221.             {
  222.                 err = SndDisposeChannel (channelPtr, true);
  223.                 channelPtr = 0L;
  224.             }
  225.             if ((asynch == true) && 
  226.                 (SndNewChannel 
  227.                 (&channelPtr, 0, initMono, 0L) == noErr)) 
  228.                 err = SndPlay(channelPtr, (SndListHandle)theSnd, true); 
  229.             else    
  230.                 err = SndPlay(0L, (SndListHandle)theSnd, false);
  231.         }
  232.     }
  233. }
  234.  
  235. //• This gets the ball rolling.  Auto_Redial calls Play_The_Riffs,
  236. //• as a loop (calling it back) and the ID number is switched there.
  237. void Jam_Session ()
  238. {
  239.     Boolean formost = true;
  240.     
  241.     if (! tunes)
  242.         return;
  243.         
  244.     if (Jimi_Hendrix)
  245.         return;
  246.     
  247.     //• If it's number one, it's a new sound channel.  Otherwise,
  248.     //• we just use the one we have.
  249.     if (formost)
  250.         SndNewChannel (&riff_ptr, 0, 0, 
  251.                       (SndCallBackUPP) &Auto_Redial);
  252.  
  253.     //• This assignment makes "riff" be riff_01, or ID #9001.
  254.     riff = 1;
  255.     Jimi_Hendrix = true;
  256.     formost = false;
  257.     
  258.     SndPlay (riff_ptr, (SndListHandle)riff_1, true);
  259.     SndDoCommand (riff_ptr, &call_back, true);
  260. }
  261.  
  262. //• Commonly used call back routine.
  263. pascal void Auto_Redial (SndChannelPtr chan, SndCommand order)
  264. {
  265.     long saveA5;
  266.     saveA5 = SetCurrentA5 ();
  267.     SetA5 (order.param2);
  268.     Play_The_Riffs ();
  269.     SetA5 (saveA5);
  270. }
  271.  
  272. //• This is called by Auto_Redial, which is called by SndNewChannel
  273. //• (in Jam_Session).
  274. //• It starts off playing Riff_01 (ID 9001), 
  275. void Play_The_Riffs ()
  276. {
  277.     Handle which_riff;
  278.     
  279.     //• Just a couple toggles to put in more control if needed.
  280.     if (! hotLicks || ! tunes)
  281.         return;    
  282.             
  283.     //• We are already playing riff_1 coming into this routine.
  284.     //• Format 1 snd resources, please.
  285.     switch (riff)
  286.     {
  287.         //• Plays 9002 twice, then bumps up the series value.
  288.         case 1:
  289.         case 2:
  290.             riff++;
  291.             which_riff = riff_2;
  292.         break;
  293.  
  294.         case 3:
  295.             riff++;
  296.             which_riff = riff_3;
  297.         break;
  298.  
  299.         case 4:
  300.             riff++;
  301.             which_riff = riff_4;
  302.         break;
  303.  
  304.         case 5:
  305.         case 6:
  306.             riff++;
  307.             which_riff = riff_2;
  308.         break;
  309.  
  310.         case 7:
  311.         case 8:
  312.             riff++;
  313.             which_riff = riff_5;
  314.         break;
  315.  
  316.         case 9:
  317.             riff++;
  318.             which_riff = riff_6;
  319.         break;
  320.  
  321.         case 10:
  322.             riff++;
  323.             which_riff = riff_7;
  324.         break;
  325.  
  326.         case 11:
  327.             riff++;
  328.             which_riff = riff_8;
  329.         break;
  330.  
  331.         //• History repeats itself!
  332.         case 12:
  333.         case 13:
  334.             riff++;
  335.             which_riff = riff_2;
  336.         break;
  337.  
  338.         case 14:
  339.             riff++;
  340.             which_riff = riff_5;
  341.         break;
  342.  
  343.         case 15:
  344.             riff++;
  345.             which_riff = riff_6;
  346.         break;
  347.  
  348.         case 16:
  349.             riff = 1;
  350.             which_riff = riff_9;
  351.         break;
  352.     }
  353.     
  354.      var_1 = *((Ptr) (((long) *which_riff) + 3));
  355.      var_2 = *((Ptr)  ((long) *which_riff) + 10 * var_1 + 1);
  356.      
  357.      buffer_it.param2 = ((long) *which_riff) + 10 * var_1 + 10 * var_2;
  358.  
  359.     SndDoCommand (riff_ptr, &buffer_it, true);
  360.     SndDoCommand (riff_ptr, &call_back, true);
  361. }
  362.  
  363. void Stop_The_Riffs ()
  364. {
  365.     //• Don't stop it if it's already stopped.
  366.     if (! Jimi_Hendrix)
  367.         return;
  368.         
  369.     //• Boolean meets Jimi, in a negative context.
  370.     Jimi_Hendrix = false;
  371.     
  372.     //• Plays a "sound of silence."
  373.     SndDoImmediate (riff_ptr, &quiet_down);
  374.     
  375.     //• You know what happens when you flush!
  376.     SndDoCommand (riff_ptr, &flush_out, false);
  377. }
  378.  
  379. void Main_Event_Loop ()
  380. {
  381.     short            ok;
  382.     EventRecord        theEvent;
  383.     short             key;
  384.     Rect             pattyRect;
  385.     
  386.     HiliteMenu (0);        //• What menu?
  387.     WaitNextEvent (everyEvent, &theEvent, 0L, 0L);
  388.  
  389.     //• Puts in "Patty with the blue dress, blue dress on..."
  390.     SetRect (&pattyRect, 109, 132, 109 + 97, 132 +72);
  391.     pattyPict = GetPicture (129);
  392.  
  393.     hotLicks = true;    
  394.     Jam_Session ();
  395.     
  396.     switch (theEvent.what)
  397.     {
  398.         //• We do this, instead of a mouse handling routine.
  399.         case mouseDown:
  400.             DrawPicture (pattyPict, &pattyRect);
  401.             Do_The_Sound (10111, true);
  402.         break;
  403.         
  404.         //• More bull.
  405.         case mouseUp:
  406.             Do_The_Sound (10110, true);
  407.             FillCRect (&pattyRect, Miss_Patty);
  408.         break;
  409.         
  410.         case keyDown:
  411.         case autoKey:
  412.             Handle_Key_Hits (&theEvent);
  413.         break;
  414.     
  415.         case keyUp:
  416.         break;
  417.     
  418.         //• We don't use this because we hog the whole screen.
  419.         case updateEvt:
  420.             //• Where'd this come from?
  421. //            BeginUpdate (shell_window);
  422. //            DrawBullseye (((WindowPeek) shell_window)->hilited);
  423. //            EndUpdate (shell_window);
  424.         break;
  425.     
  426.         //• Same here.
  427.         case activateEvt:
  428.         break;
  429.     }
  430. }
  431.  
  432. //• Key hits ala "CheeseToast" and 100 others.
  433. void Handle_Key_Hits (EventRecord *theEvent)
  434. {
  435.     Rect aboutBounds;
  436.     short    chCode;
  437.     long ticks;
  438.     
  439.     chCode = theEvent->message & charCodeMask;
  440.  
  441.     switch (chCode) 
  442.     {
  443.         case '1':  //• User hits the 1 key.
  444.                Do_The_Sound (10001, true);
  445.         break;
  446.  
  447.         case '2':
  448.                Do_The_Sound (10002, true);
  449.         break;
  450.  
  451.         case '3':
  452.                Do_The_Sound (10003, true);
  453.         break;
  454.  
  455.         case '4':
  456.                Do_The_Sound (10004, true);
  457.         break;
  458.  
  459.         case '5':
  460.                Do_The_Sound (10005, true);
  461.         break;
  462.  
  463.         case '6':
  464.                Do_The_Sound (10006, true);
  465.         break;
  466.  
  467.         case '7':
  468.                Do_The_Sound (10007, true);
  469.         break;
  470.  
  471.         case '8':
  472.                Do_The_Sound (10008, true);
  473.         break;
  474.  
  475.         case '9':
  476.                Do_The_Sound (10009, true);
  477.         break;
  478.  
  479.         case '0':
  480.                Do_The_Sound (10009, true);
  481.         break;
  482.  
  483.         case '*':
  484.             quiet = true;        //• Give the quiet command.
  485.             Stop_The_Riffs ();
  486.         break;
  487.  
  488.         case 'q':
  489.             Put_Things_Back ();
  490.         break;
  491.     }
  492. }
  493.  
  494. void Put_Things_Back ()
  495. {
  496.     //• Get rid of the key and mouse hit sound channel.
  497.     if (chanPtr != 0L)
  498.     {
  499.         SndDoImmediate (chanPtr, &flush_out);     //• flush.
  500.         SndDisposeChannel (chanPtr, true);
  501.     }
  502.     //• Get rid of the riff sound channel.
  503.     if (riff_ptr != 0L)
  504.     {
  505.         SndDoImmediate (riff_ptr, &flush_out);     //• flush.
  506.         SndDisposeChannel (riff_ptr, true);
  507.     }
  508.     //• Back to the bar (for drinks and socializing).
  509.     Show_Menu_Bar ();
  510.     ExitToShell ();        //• Back to Findersville.
  511. }
  512.  
  513. void main ()
  514. {
  515.     Rect r1;
  516.     short i;
  517.     SysEnvRec whichMac;
  518.     
  519.     Wake_The_Managers ();
  520.         
  521.     SetEventMask (everyEvent);
  522.  
  523.     Bag_Some_Riffs ();
  524.     Do_The_Splash ();
  525.  
  526.     //• We do this here because we still look like a desktop.
  527.     Hide_Menu_Bar ();
  528.  
  529.     SysEnvirons (2, &whichMac);
  530.     
  531.     //• We don't mess with no antiques!
  532.     if (whichMac.processor == env68000) 
  533.     {
  534.         tunes = false;
  535.         ExitToShell ();
  536.     }
  537.     Create_Our_Screen ();
  538.     Setup_Snd_Commands ();
  539.     
  540.     for (;;)
  541.         Main_Event_Loop ();
  542. //    CloseResFile(riffRes);  //• Remember?
  543. }
  544.  
  545.