MeterTest.c

/*
    File:       MeterTest.c
 
    Contains:   This snippet demonstrates record metering through the use of SPBGetDeviceInfo() 
                and SPBSetDeviceInfo() using the siLevelMeterOnOff selector. The code calls 
                SPBSetDeviceInfo() to initially set metering to off, then does a quick sampling of 
                sound input using SPBGetDeviceInfo(). Metering is then turned on and sampling is 
                repeated. This is an SIOW application and could be done more elegantly using a
                graphical representation for sound input levels. But you get the idea. . .      
 
    Written by: Kevin Mellander 
 
    Copyright:  Copyright © 1994-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):
                7/29/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include <Sound.h>
#include <SoundInput.h>
#include <ToolUtils.h>
#include <Types.h>
#include <stdio.h>
 
struct soundLevelData{
        short meterState;
        short meterSetting;
    } getSoundLevelInfo;
 
void main()
{
    Str255  deviceString;
    OSErr   soundErr = 0;
    long    mySIRefNum;
    short   sampletime = 0;
    short   setSoundMeterState = 1;
    
    getSoundLevelInfo.meterState = 0;
    getSoundLevelInfo.meterSetting = 0;
    
    deviceString[0] = 0;
        
    //Open the sound input device
    soundErr = SPBOpenDevice(deviceString,siWritePermission,&mySIRefNum);
    if (soundErr != noErr)
        DebugStr("\p Failure at call to SPBOpenDevice");
        
    //Set the initial state of the sound input meter to zero just to be sure we start in an "off" state--after all this *is* a test 
    soundErr = SPBSetDeviceInfo(mySIRefNum,siLevelMeterOnOff,(Ptr) &getSoundLevelInfo.meterState);
    if (soundErr != noErr)
        DebugStr("\p Failure at call to SPBSetDeviceInfo set to 0");
 
    //Monitor sound input and make sure everything is really zeroed
    for (sampletime = 0; sampletime<=100; ++sampletime)
    {
        soundErr = SPBGetDeviceInfo(mySIRefNum,siLevelMeterOnOff, (Ptr) &getSoundLevelInfo);
        if (soundErr != noErr)
            DebugStr("\p Failure at call to SPBGetDeviceInfo");
        else
            printf("The GetDeviceInfo meter state is %d and the meter setting is %d\n",getSoundLevelInfo.meterState,getSoundLevelInfo.meterSetting);
    }
    
    //Turn the sound input meter on
    soundErr = SPBSetDeviceInfo(mySIRefNum,siLevelMeterOnOff,(Ptr) &getSoundLevelInfo.meterState);
    if (soundErr != noErr)
        DebugStr("\p Failure at call to SPBSetDeviceInfo set to 1");
    else 
        printf("\nThe call to SPBGetDeviceInfo was successful;\nThe SetDeviceInfo meter state was set to %i\n\n",setSoundMeterState);
        
    
    //Monitor sound input again with meters on 
    for (sampletime = 0; sampletime<=100; ++sampletime)
    {
        soundErr = SPBGetDeviceInfo(mySIRefNum,siLevelMeterOnOff, (Ptr) &getSoundLevelInfo);
        if (soundErr != noErr)
            DebugStr("\p Failure at call to SPBGetDeviceInfo");
        else
            printf("The GetDeviceInfo meter state is %d and the meter setting is %d\n",getSoundLevelInfo.meterState,getSoundLevelInfo.meterSetting);
        
    }
 
    //We're done so close the device
    soundErr = SPBCloseDevice(mySIRefNum);
    if (soundErr != noErr)
        DebugStr("\p Failure at call to SPBCloseDevice");
}