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 / LineRecord.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-03  |  7.5 KB  |  230 lines  |  [TEXT/MPS ]

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