home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Sound / SetSoundInput / SetSoundInput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-28  |  6.2 KB  |  194 lines  |  [TEXT/MPS ]

  1. #include    <Quickdraw.h>
  2. #include    <Windows.h>
  3. #include    <Dialogs.h>
  4. #include    <Files.h>
  5. #include    <StandardFile.h>
  6. #include    <Packages.h>
  7. #include    <OSEvents.h>
  8. #include    <Memory.h>
  9. #include    <Sound.h>
  10. #include    <SoundInput.h>
  11. #include    <OSUtils.h>
  12. #include     <ToolUtils.h>
  13. #include     <Types.h>
  14. #include    <TextUtils.h>
  15.  
  16. struct soundRateData{
  17.     short numberOfRatesSupported;
  18.     Handle ratesSupported;
  19. } soundRateInfo;
  20.  
  21. void main()
  22. {
  23.     SndChannelPtr            mySoundChannel;
  24.     StandardFileReply        fileReply;
  25.     short                    myFRefNum;
  26.     short                    numChannels = 1;
  27.     short                    sampleSize = 0;
  28.     short                    twosComplementSetting = 1;
  29.     long                    soundDuration = 20000;
  30.     OSErr                    err = 0;
  31.     long                    mySIRefNum;
  32.     Fixed                    previousSampleRate = 0;
  33.     Fixed                    mySISampleRate = 0x00000000;
  34.     SPBPtr                    mySIParamBlockPtr;
  35.     ParmBlkPtr                myIOParamBlockPtr;
  36.     Fixed                     *myFixedPointer;
  37.     Fixed                     myFixedVariable = 0;
  38.     Boolean                    allDone = false;
  39.     
  40.     InitGraf(&qd.thePort);
  41.     FlushEvents(everyEvent, 0);
  42.     InitWindows();
  43.     InitDialogs(nil);
  44.     InitCursor();
  45.     
  46.     soundRateInfo.numberOfRatesSupported = 0;
  47.     soundRateInfo.ratesSupported = nil;
  48.     
  49.     /*Allocate our own sound channel--we don't have to for this but why not!*/
  50.     mySoundChannel = (SndChannelPtr) NewPtr(sizeof (SndChannel));
  51.     err = SndNewChannel(&mySoundChannel, sampledSynth, 0, nil);
  52.     if (err != noErr)
  53.         DebugStr("\p Failure at SndNewChannel");
  54.     
  55.     /*Select a file to record to*/
  56.     StandardGetFile(nil, -1, (SFTypeList) nil, &fileReply);
  57.     err = FSpOpenDF (&fileReply.sfFile, fsRdWrPerm, &myFRefNum);
  58.     if (err != noErr)
  59.         DebugStr("\p Failure at call to FSpOpenDF");
  60.                 
  61.     /*Open the sound input device*/
  62.     err = SPBOpenDevice("\p",siWritePermission,&mySIRefNum);
  63.     if (err != noErr)
  64.         DebugStr("\p Failure at call to SPBOpenDevice");
  65.         
  66.     /*Get the current sound input rate to be restored when we're done.
  67.     We're not going to do anything with this now, it is here as an example.
  68.     You would want to get information for all settings you are going to change below
  69.     in order to restore them later*/
  70.     
  71.     err = SPBGetDeviceInfo(mySIRefNum,siSampleRate, (Ptr) &previousSampleRate);
  72.     if (err != noErr)
  73.         DebugStr("\p Failure at call to SPBGetDeviceInfo");
  74.         
  75.     
  76.     /*Set the number of channels we will be using*/
  77.     err = SPBSetDeviceInfo(mySIRefNum,siNumberChannels, (Ptr) &numChannels);
  78.     if (err != noErr)
  79.         DebugStr("\p Failure at call to SPBSetDeviceInfo numChannels");
  80.     
  81.         
  82.     /*Get the  number of sample rates supported*/
  83.     err = SPBGetDeviceInfo(mySIRefNum,siSampleRateAvailable,(Ptr) &soundRateInfo);
  84.     if (err != noErr)
  85.         DebugStr("\p Failure at call to SPBSetDeviceInfo set to 0");
  86.     
  87.     /*Now we move to the end of our list of sample rates to get the best one--this is a little flaky
  88.     since I am using the fact that the highest rate is currently the last rate in the 
  89.     buffer. . .if this were to change I'd be hosed 8-)*/
  90.     
  91.     myFixedPointer = (Fixed *)(*(soundRateInfo.ratesSupported));
  92.     
  93.     
  94.     myFixedPointer = myFixedPointer + ((soundRateInfo.numberOfRatesSupported) -1);
  95.     myFixedVariable = *myFixedPointer;
  96.     switch (myFixedVariable)
  97.     {
  98.         case 0xBB800000:
  99.             mySISampleRate = 0xBB800000;
  100.             sampleSize = 16;
  101.             break;
  102.         case 0xAC440000:
  103.             mySISampleRate = 0xAC440000;
  104.             sampleSize = 16;
  105.             break;
  106.         case 0x56EE8BA3:
  107.             mySISampleRate = 0x56EE8BA3;
  108.             sampleSize = 8;
  109.             break;
  110.         case 0x56220000:
  111.             mySISampleRate = 0x56220000;
  112.             sampleSize = 8;
  113.             break;
  114.         default:
  115.             mySISampleRate = 0x56EE8BA3;
  116.             sampleSize = 8;
  117.             break;
  118.     }
  119.             
  120.     /*Set the sound input rate to the rate that we want*/
  121.     err = SPBSetDeviceInfo(mySIRefNum,siSampleRate, (Ptr) &mySISampleRate);
  122.     if (err != noErr)
  123.         DebugStr("\p Failure at call to SPBSetDeviceInfo sampleRate");
  124.         
  125.     /*Set the sample size to the size that we want*/
  126.     err = SPBSetDeviceInfo(mySIRefNum,siSampleSize, (Ptr) &sampleSize);
  127.     if (err != noErr)
  128.         DebugStr("\p Failure at call to SPBSetDeviceInfo sampleSize");
  129.         
  130.     /*If we're using 8-bit data we need to set the state of the two's complement feature*/
  131.     err = SPBSetDeviceInfo(mySIRefNum,siTwosComplementOnOff, (Ptr) &twosComplementSetting);
  132.     if (err != noErr)
  133.         DebugStr("\p Failure at call to SPBSetDeviceInfo twosComplement");
  134.     
  135.     /*Set up our AIFF file header*/
  136.     err = SetupAIFFHeader(myFRefNum,1,mySISampleRate, sampleSize, 'NONE', 0, 0);
  137.     if (err != noErr)
  138.         DebugStr("\p Failure at call to SetupAIFFHeader");
  139.         
  140.     /*Set up our param block and record our sound*/
  141.     mySIParamBlockPtr = (SPBPtr) NewPtr(sizeof (SPB));
  142.     if (MemError() != noErr || mySIParamBlockPtr == nil)
  143.         DebugStr("\p Failure at memory allocation for Sound Param Block");
  144.     
  145.     mySIParamBlockPtr->inRefNum = mySIRefNum;
  146.     mySIParamBlockPtr->count = 0;
  147.     mySIParamBlockPtr->milliseconds = soundDuration;
  148.     mySIParamBlockPtr->completionRoutine = nil;
  149.     mySIParamBlockPtr->interruptRoutine = nil;
  150.     mySIParamBlockPtr->userLong = 0;
  151.     mySIParamBlockPtr->unused1 = 0;
  152.     
  153.     err = SPBRecordToFile (myFRefNum, mySIParamBlockPtr, false);
  154.     if (err != noErr)
  155.         DebugStr("\p Failure at call to SPBRecordToFile");
  156.     
  157.     /*We're done recording so close the device*/
  158.     err = SPBCloseDevice(mySIRefNum);
  159.     if (err != noErr)
  160.         DebugStr("\p Failure at call to SPBCloseDevice");
  161.  
  162.     /*Now that we've recorded to our file, let's setup for playing the sound data back*/
  163.     err = SPBMillisecondsToBytes(mySIRefNum, &soundDuration); 
  164.     myIOParamBlockPtr = (ParmBlkPtr) NewPtr(sizeof (IOParam));
  165.     if (MemError() != noErr || myIOParamBlockPtr == nil)
  166.         DebugStr("\p Failure at memory allocation for ioParamBlock");
  167.     
  168.     myIOParamBlockPtr->ioParam.ioCompletion = nil; //Setup our ioParam Block
  169.     myIOParamBlockPtr->ioParam.ioRefNum = myFRefNum;
  170.     myIOParamBlockPtr->ioParam.ioPosMode = fsFromStart;
  171.     myIOParamBlockPtr->ioParam.ioPosOffset = 0;
  172.     
  173.     err = PBSetFPos(myIOParamBlockPtr, false); //Set our sound file position to the beginning 
  174.     if (err != noErr)
  175.         DebugStr("\p Failure at call to PBSetFPos");
  176.         
  177.     err = SetupAIFFHeader(myFRefNum,1,mySISampleRate, sampleSize, 'NONE', soundDuration, 0);
  178.     if (err != noErr)
  179.         DebugStr("\p Failure at call to SetupAIFFHeader");
  180.         
  181.     err = SndStartFilePlay(mySoundChannel, myFRefNum, 0, 30000, nil, nil, nil, false);
  182.     if (err != noErr)
  183.         DebugStr("\p Failure at call to SndStartFilePlay");
  184.         
  185.     /*Dispose of Channel*/
  186.     SndDisposeChannel(mySoundChannel, true);
  187.     
  188.     /*Dispose of param block pointers*/
  189.     DisposePtr ((Ptr) myIOParamBlockPtr);
  190.     DisposePtr ((Ptr) mySIParamBlockPtr);
  191.     
  192.     /*Close the file. . .goodnight!*/
  193.     FSClose(myFRefNum);
  194. }