_source/SimpleApp_Sound.c

/*
    File:       SimpleApp_Sound.c
 
    Contains:   Routines demonstrating how to play a sound with many of the same features as
                QuickTime.
 
    Written by: Mark Cookson    
 
    Copyright:  Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/31/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include "SimpleApp_Sound.h"
 
/*-----------------------------------------------------------------------*/
pascal  void    MyScrollAction (ControlHandle control, short part)
/*-----------------------------------------------------------------------*/
{
    long    position = GetControlValue (control);
 
    if (part) {
        switch (part) {
            case kControlUpButtonPart:
                position -= oneBuffer;
                if (CDStyle == true) {
                    if (gPlayBackwards == true) {
                        ASoundPlayBackwards (mySoundInfo, true);
                    }
                }
                break;
            case kControlDownButtonPart:
                position += oneBuffer;
                break;
            case kControlPageUpPart:
                position -= tenBuffers;
                if (CDStyle == true) {
                    if (gPlayBackwards == true) {
                        ASoundPlayBackwards (mySoundInfo, true);
                    }
                }
                break;
            case kControlPageDownPart:
                position += tenBuffers;
                break;
            default:
                DebugStr ("\pWhere did you click?");
        }
        SetControlValue (control, position);
        ASoundSetCurBuffer (mySoundInfo, position);
 
        if (CDStyle == true) {
            ASoundSetCurBuffer (mySoundInfo, position);
        }
    }
}
 
/*-----------------------------------------------------------------------*/
pascal  void    DoScroll (const ControlHandle control,const short pPart)
/*-----------------------------------------------------------------------*/
{
    long    position    = nil;
    Point   pt          = {nil, nil};
    short   part        = pPart;
    OSErr   theError    = noErr;
    
    if (part != nil) {
        switch (part) {
            case kControlIndicatorPart:
                if (CDStyle == false) {
                    theError = ASoundPauseForAdjust (mySoundInfo);
                }
                GetMouse(&pt);
                part = TrackControl (control, pt, nil);
                position = GetControlValue (control);
                ASoundSetCurBuffer (mySoundInfo, position);
                break;
            case kControlUpButtonPart:
            case kControlDownButtonPart:
            case kControlPageUpPart:
            case kControlPageDownPart: {
                ControlActionUPP actionProc = NewControlActionProc (MyScrollAction);
                if (CDStyle == false) {
                    theError = ASoundPauseForAdjust (mySoundInfo);
                }
                part = TrackControl (control, pt, actionProc);
                DisposeRoutineDescriptor (actionProc);
                break;
            }
        }
    
        if (CDStyle == false) {
            position = GetControlValue (control);
            ASoundSetCurBuffer (mySoundInfo, position);
            theError = ASoundPauseForAdjust (mySoundInfo);
        }
    }
}
 
/*-----------------------------------------------------------------------*/
        void    MyIdleProc (EventRecord *evt)
/*-----------------------------------------------------------------------*/
{
#ifndef __SC__
#pragma unused (evt)
#endif
    long        curBuf      = nil;
 
    if (soundPlaying == true) {
        curBuf = ASoundGetCurBuffer (mySoundInfo);
        if (curBuf != nil) {
            SetControlValue (scrollBar, curBuf);
        }
    }
 
    if (ASoundIsDone (mySoundInfo) == true) {
        EnableControl (startButton);
        DisableControl (stopButton);
        DisableControl (resumeButton);
        DisableControl (scrollBar);
        SetControlValue (scrollBar, kScrollMinValue);
        ASoundDonePlaying (mySoundInfo, kCloseFile + kFreeMem);
        SAL_InstallIdleProc((SalEvent_ProcPtr)nil);
    }
}
 
/*-----------------------------------------------------------------------*/
pascal OSErr    DoPlay (const ButtonItemRef pButtonItemRef,const long modifiers)
/*-----------------------------------------------------------------------*/
{
    OSErr   theErr;
 
#pragma unused (pButtonItemRef,modifiers)
 
    theErr = ASoundGetFileToPlay (mySoundInfo);
    if (theErr == noErr) {
        theErr = ASoundStartPlaying (mySoundInfo, nil);
        if (theErr == noErr) {
            SAL_DisableMe();
            EnableControl(resumeButton);
            EnableControl(stopButton);
            paused = false;
            SAL_InstallIdleProc ((SalEvent_ProcPtr)MyIdleProc);
            EnableControl (scrollBar);
            SetControlMaximum (scrollBar, ASoundGetNumBuffers (mySoundInfo) - 1);
        }
    }
 
    return theErr;
}
 
/*-----------------------------------------------------------------------*/
pascal OSErr DoPause(const ButtonItemRef pButtonItemRef,const long modifiers)
/*-----------------------------------------------------------------------*/
{
    Boolean         theError;
 
#pragma unused (pButtonItemRef,modifiers)
 
    theError = ASoundPause (mySoundInfo);
 
    if (paused == false) {
        paused = true;
        SAL_SetTitle ("\pResume Sound");
    }
    else {
        paused = false;
        SAL_SetTitle ("\pPause Sound");
    }
 
    return theError;
}
 
/*-----------------------------------------------------------------------*/
pascal OSErr    DoStop(const ButtonItemRef pButtonItemRef,const long modifiers)
/*-----------------------------------------------------------------------*/
{
    OSErr theError;
 
#pragma unused (pButtonItemRef,modifiers)
 
    theError = ASoundStop (mySoundInfo);
 
    SAL_DisableMe();
    EnableControl(startButton);
    DisableControl(resumeButton); 
 
    if (paused == true) {
        SetControlTitle(resumeButton,"\pPause Sound");
    }
 
    return theError;
}
 
/*-----------------------------------------------------------------------*/
pascal  short   DoPostGroupHit (const long  modifiers,
                                const ControlRef theControl,
                                const ButtonItemRef brh,
                                const short item)
/*-----------------------------------------------------------------------*/
{
#pragma unused (modifiers)
#pragma unused (theControl)
#pragma unused (brh)
 
    switch (item) {
        case qtStyle:
            DisableControl (checkBox);
            SetControlValue (checkBox, false);
            CDStyle = false;
            break;
        case cdStyle:
            EnableControl (checkBox);
            SetControlValue (checkBox, gPlayBackwards);
            CDStyle = true;
            break;
        default:
            DebugStr ("\pDoPostGroupHit got an invalid control passed to it!");
    }
 
    return nil;
}
 
/*-----------------------------------------------------------------------*/
pascal short DoPlayBackwards(const ButtonItemRef pButtonItemRef,const long modifiers)
/*-----------------------------------------------------------------------*/
{
#pragma unused (pButtonItemRef,modifiers)
 
    gPlayBackwards = !gPlayBackwards;
    SetControlValue (checkBox, gPlayBackwards);
 
    return nil;
}
 
/*-----------------------------------------------------------------------*/
        void    main (void)
/*-----------------------------------------------------------------------*/
{
    Rect    r;
    short   err;
    Str255 names[] = {"\pQuickTime style",
                      "\pCD style"};
 
    SAL_InitSimpleApp (2, kSAL_UseStandardMenu);        /* Simple App Sets up the Tool Box For us */
    SAL_GetDocumentWindow(128,nil);                     /* Get our stored window */
 
    SAL_SetRectLocation (&r, 10, 2);                /* This Sets a rects anchor point */
    SAL_SetRectDimensions (&r, 90, 20);             /* This Sets its size without changing it position */
    err = SAL_InstallPushButton (1, gSAL_CurrentWindow, "\pPlay Sound", &r, (char)0,  DoPlay, nil);
    startButton = gSAL_CurrentControl;
 
    SAL_SetRectLocation (&r, 110, 2);               /* This Sets a rects anchor point */
    SAL_SetRectDimensions (&r, 100, 20);            /* This Sets its size without changing it position */
    err = SAL_InstallPushButton (2, gSAL_CurrentWindow, "\pPause Sound", &r, (char)0,  DoPause, nil);
    resumeButton  = gSAL_CurrentControl;
    DisableControl (resumeButton);
 
    SAL_SetRectLocation (&r, 220, 2);               /* This Sets a rects anchor point */
    SAL_SetRectDimensions (&r, 90, 20);             /* This Sets its size without changing it position */
    err = SAL_InstallPushButton (3, gSAL_CurrentWindow, "\pStop Sound", &r, (char)0,  DoStop, nil);
    stopButton = gSAL_CurrentControl;               /* lets remember the control handle */
    DisableControl (stopButton);                    /* disable the control */
 
    SAL_SetRectDimensions (&r, 300, 16);
    SAL_SetRectLocation (&r, 10, 30);
    err = SAL_InstallScrollBar (4, gSAL_CurrentWindow, "\p", &r, (char)0, DoScroll, nil,nil);
    scrollBar = gSAL_CurrentControl;        /* get the object control handle */
    DisableControl (scrollBar);
    SetControlMinimum (scrollBar, kScrollMinValue);
    SetControlMaximum (scrollBar, kScrollMinValue);
    SetControlValue (scrollBar, kScrollMinValue);
 
    SAL_SetRectLocation (&r, 20, 100);
    err = SAL_InstallCheckBox(5, gSAL_CurrentWindow, "\pBackwards playing", &r, nil, false, DoPlayBackwards, nil);
    checkBox = gSAL_CurrentControl;
    DisableControl (checkBox);
 
    SAL_SetRectLocation (&r, 10, 60);
    SAL_SetRectDimensions (&r, 130, 40);
    err = SAL_InstallRadioGroup (6,             /* Group ID */
                            gSAL_CurrentWindow, /* owner window */
                            2,                  /* Number of items */
                            "\pRadio Group",    /* Group Name */
                            &names,             /* Array of button Names */
                            &r,                 /* Group bounds */
                            nil,                /* default item (zero based) */
                            2,                  /* Horz spacing */
                            2,                  /* vert spacing */
                            16,                 /* Button Height */
                            130,                /* Button widths */
                            nil,                /* Pre Hit Proc */
                            DoPostGroupHit,     /* Post Hit Proc */
                            nil                 /* Group Update Proc */
                            );
 
    SAL_GetPrintArea (gSAL_CurrentWindow,&r);           /* Set the print area top coordinate */
    r.top = 22;                                 /* so that you don't scroll the button out of view */
    SAL_SetPrintArea (gSAL_CurrentWindow,&r);
    mySoundInfo = ASoundNew (&err);
 
    if (err == noErr)
        SAL_Run ();                                 /* Let SimpleApp handle the rest */
}
 
// utility functions since I already keep the control handle around
/*-----------------------------------------------------------------------*/
    void EnableControl (ControlRef cr)
/*-----------------------------------------------------------------------*/
{
    if (cr != nil) {
        HiliteControl(cr, nil);
    }
}
 
/*-----------------------------------------------------------------------*/
    void DisableControl (ControlRef cr)
/*-----------------------------------------------------------------------*/
{
    if (cr != nil) {
        HiliteControl(cr, 255);
    }
}