home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / Telephone Manager / Stiletto Sources / ModuleSources / HandsetRecord.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-03  |  7.4 KB  |  226 lines  |  [TEXT/MPS ]

  1. /************************************************************************************************/
  2. /*                                                                                                */
  3. /*    Module Name:    HandsetRecord                                                                */
  4. /*                                                                                                */
  5. /*    File Name:        HandsetRecord.c                                                                */
  6. /*                                                                                                */
  7. /*    © Apple Computer, Inc. 1994-1995                                                            */
  8. /*    All Rights Reserved                                                                            */
  9. /*                                                                                                */
  10. /*    Revision History:                                                                            */
  11. /*                                                                                                */
  12. /*        Date        Who                    Modification                                            */
  13. /*                                                                                                */
  14. /*        1994-05-20    Jaakko Railo        Original version                                        */
  15. /*        1994-08-06    Gary Anwyl            Modified GetDNSndInOut to make this module                */
  16. /*        1994-02-01    Jaakko Railo        Modified LineRecord to make this module                    */
  17. /*                                                                                                */
  18. /************************************************************************************************/
  19.  
  20. /****************************************** DESCRIPTION ******************************************
  21.  
  22.     When this module is invoked it allocates a 500K buffer and records
  23.     asynchronously from the handset until the buffer is filled or
  24.     the user presses the mouse button. The module then uses the StandardFile
  25.     interface to get the name of a file in which to store the sound in the
  26.     form of a 'snd ' 128 resource. During the recording the elapsed time is
  27.     printed in the log window.
  28.  
  29. *************************************************************************************************/
  30.  
  31. /******************************************** HEADERS *******************************************/
  32.  
  33. #include "Events.h"
  34. #include "Resources.h"
  35. #include "Sound.h"
  36. #include "SoundInput.h"
  37. #include "TextUtils.h"
  38. #include "Types.h"
  39.  
  40. #ifndef __TELEPHONES__
  41. #include "Telephones.h"
  42. #endif
  43.  
  44. #include "TestModule.h"
  45.  
  46. /****************************************** DEFINITIONS *****************************************/
  47.  
  48. /****************************************** PROTOTYPES ******************************************/
  49.  
  50. OSErr    GetDeviceSettings (long myInRefNum, short *numChannels, Fixed *sampleRate, short *sampleSize, OSType *compressionType, Str255 dName);
  51. void     RecordSnd (CHRSPtr paramPtr, long myInRefNum);
  52. void     DoTest (CHRSPtr paramPtr);
  53.  
  54. /************************************************************************************************/
  55. /************************************************************************************************/
  56.  
  57.  
  58. pascal short TestModule (CHRSPtr paramPtr)
  59. {
  60.     short    returnValue = noErr;
  61.     
  62.     if (paramPtr->version <= kTestModuleVersion) {
  63.         
  64.         DoTest (paramPtr);
  65.         
  66.     }
  67.     else
  68.         returnValue = kWrongVersion;
  69.         
  70.     return (returnValue);
  71. }
  72.  
  73.  
  74. OSErr GetDeviceSettings (long myInRefNum, short *numChannels, Fixed *sampleRate, short *sampleSize, OSType *compressionType, Str255 dName)
  75. {
  76.     OSErr    myErr;
  77.     
  78.     if (((myErr = SPBGetDeviceInfo (myInRefNum, siNumberChannels, (Ptr) numChannels)) == noErr) &&
  79.         ((myErr = SPBGetDeviceInfo (myInRefNum, siSampleRate, (Ptr) sampleRate)) == noErr) &&
  80.         ((myErr = SPBGetDeviceInfo (myInRefNum, siSampleSize, (Ptr) sampleSize)) == noErr) &&
  81.         ((myErr = SPBGetDeviceInfo (myInRefNum, siCompressionType, (Ptr) compressionType)) == noErr) &&
  82.         ((myErr = SPBGetDeviceInfo (myInRefNum, siDeviceName, (Ptr) dName)) == noErr))
  83.             return (myErr);
  84.     else
  85.         return (myErr);
  86. }
  87.  
  88.  
  89. void RecordSnd (CHRSPtr paramPtr, long myInRefNum)
  90. {
  91.     Handle                 mySndH;
  92.     SPB                    mySPB;
  93.     OSErr                myErr;
  94.     long                myBuffSize;
  95.     short                myHeadrLen, myNumChans, mySampSize;
  96.     Fixed                mySampRate;
  97.     OSType                myCompType;
  98.     Str255                myDevName;
  99.     StandardFileReply    sfReply;
  100.     short                fileRefNum, i;
  101.     long                tickCount, secCount;
  102.  
  103.     for (i=1000; i!=0; i -= 250)  {
  104.         mySndH = TempNewHandle(i*1024, &myErr);
  105.         if (mySndH!=0 && myErr==noErr)
  106.             break;
  107.     }
  108.         
  109.     if (i==0) {
  110.         Print (paramPtr, "NewHandle failed");
  111.         return;
  112.     }
  113.     
  114.     myErr = GetDeviceSettings (myInRefNum, &myNumChans, &mySampRate, &mySampSize, &myCompType, myDevName);
  115. //    Print (paramPtr, "Number of channels : %d", myNumChans);
  116. //    Print (paramPtr, "Sample rate        : %lx", mySampRate);
  117. //    Print (paramPtr, "Sample size        : %d", mySampSize);
  118. //    Print (paramPtr, "Compression type   : %lx", myCompType);
  119. //    Print (paramPtr, "Device name        : %*.*s", myDevName[0], myDevName[0], &myDevName[1]);
  120.     myErr = SetupSndHeader ((SndListHandle) mySndH, myNumChans, mySampRate, mySampSize, myCompType, 60, 0, &myHeadrLen);
  121.     myBuffSize = GetHandleSize (mySndH) - myHeadrLen;
  122.     HLock (mySndH);
  123.     mySPB.inRefNum = myInRefNum;
  124.     mySPB.count = myBuffSize;
  125.     mySPB.milliseconds = 0;
  126.     mySPB.bufferLength = myBuffSize;
  127.     mySPB.bufferPtr = (Ptr)((long) *mySndH + myHeadrLen);
  128.     mySPB.completionRoutine = nil;
  129.     mySPB.interruptRoutine = nil;
  130.     mySPB.userLong = 0;
  131.     mySPB.error = noErr;
  132.     mySPB.unused1 = 0;
  133.     Print (paramPtr, "Start of recording (%ld KB)", GetHandleSize(mySndH)/1024);
  134.     Print (paramPtr, "Click mouse to quit");
  135.     myErr = SPBRecord (&mySPB, true);
  136.     if (myErr != noErr) {
  137.         Print (paramPtr, "SPBRecord failed: %d", myErr);
  138.         goto exit1;
  139.     }
  140.     tickCount = TickCount();
  141.     secCount = 0;
  142.     
  143.     while (1) {
  144.         short recordingStatus, meterLevel;
  145.         unsigned long totalSamplesToRecord, numberOfSamplesRecorded,totalMSecToRecord, numberOfMSecRecorded;
  146.         
  147.         if (Button())
  148.             break;
  149.  
  150.         myErr = SPBGetRecordingStatus(myInRefNum, &recordingStatus, &meterLevel,
  151.                     &totalSamplesToRecord, &numberOfSamplesRecorded, &totalMSecToRecord,
  152.                     &numberOfMSecRecorded);
  153.         if (myErr != noErr) {
  154.             Print (paramPtr, "SPBGetRecordingStatus failed: %d", myErr);
  155.             return;
  156.         }
  157.  
  158.         if (recordingStatus <= 0)
  159.             break;
  160.             
  161.         if ((TickCount() - tickCount) >= 60) {
  162.             tickCount = TickCount();
  163.             if ((++secCount % 5)==0)
  164.                 Print (paramPtr, "   %4ld seconds", secCount);
  165.         }
  166.     }
  167.  
  168.     SPBStopRecording(myInRefNum);
  169.     Print (paramPtr, "End of recording");
  170.     HUnlock (mySndH);
  171.     myErr = SetupSndHeader ((SndListHandle) mySndH, myNumChans, mySampRate, mySampSize, myCompType, 60, mySPB.count, &myHeadrLen);
  172.     SetHandleSize(mySndH, mySPB.count+myHeadrLen);
  173.     
  174.     StandardPutFile("\pSave SND file as:", "\pGeoPortSND", &sfReply);
  175.     if (sfReply.sfGood) {
  176.         if (sfReply.sfReplacing)
  177.             FSpDelete(&sfReply.sfFile);
  178.         FSpCreateResFile(&sfReply.sfFile, 'movr', 'sfil', -1);
  179.         myErr = ResError();
  180.         if (myErr != noErr) {
  181.             Print (paramPtr, "FSpCreateResFile failed: %d", myErr);
  182.             goto exit1;
  183.         }
  184.         fileRefNum = FSpOpenResFile(&sfReply.sfFile, fsWrPerm);
  185.         myErr = ResError();
  186.         if (myErr != noErr) {
  187.             Print (paramPtr, "FSpOpenResFile failed: %d", myErr);
  188.             goto exit1;
  189.         }
  190.         AddResource(mySndH, 'snd ', 128, sfReply.sfFile.name);
  191.         myErr = ResError();
  192.         if (myErr != noErr) {
  193.             Print (paramPtr, "AddResource failed: %d", myErr);
  194.             goto exit2;
  195.         }
  196. exit2:
  197.         CloseResFile(fileRefNum);
  198.     }
  199. exit1:    
  200.     DisposeHandle(mySndH);
  201. }
  202.  
  203.  
  204. void DoTest (CHRSPtr paramPtr)
  205. {
  206.     TELHandle    termHand = GetCurrentTELHandle (paramPtr);
  207.     OSErr        errCode;
  208.     long        inRefNum = 0;
  209.     Str255        deviceName;
  210.     
  211.     if ((errCode = TELGetHSSoundInput (termHand, deviceName)) == noErr &&
  212.         (errCode = SPBOpenDevice (deviceName, siWritePermission, &inRefNum)) == noErr) {
  213.             Print (paramPtr, "TELGetHSSoundInput --> DeviceName = %*.*s", deviceName[0], deviceName[0], &deviceName[1]);
  214.             Print (paramPtr, "SPBOpenDevice --> inRefNum = %ld", inRefNum);
  215.             RecordSnd (paramPtr, inRefNum);
  216.             
  217.             if ((errCode = SPBCloseDevice (inRefNum)) == noErr &&
  218.                 (errCode = TELDisposeHSSoundInput (termHand, deviceName)) == noErr)
  219.                     Print (paramPtr, "TELDisposeHSSoundInput --> deviceName %*.*s disposed", deviceName[0], deviceName[0], &deviceName[1]);
  220.             else
  221.                 Print (paramPtr, "### SPBCloseDevice/TELDisposeHSSoundInput failed : %d", errCode);
  222.     }
  223. }
  224.  
  225.  
  226.