record/main.c

This is an example of how to use the ASX Recorder control.

00001 /* $Header: /Repository/apps/asx/examples/record/main.c,v 1.8 2008/02/12 15:28:29 as-age Exp $ */
00002 #include "stdio.h"
00003 #include "stdlib.h"
00004 #include "asx.h"
00005 
00006 ASX_HANDLE hSystem=0;
00007 
00008 int CheckError(ASX_HANDLE hObj, int nLine);
00009 
00010 int main(int argc, char* argv[])
00011 {
00012     ASX_HANDLE hAdapter;
00013     ASX_HANDLE hMixer;
00014     ASX_HANDLE hRecorder;
00015     ASX_ERROR asxError;
00016     enum asxFILE_MODE eFileMode = asxFILE_MODE_CREATE;
00017     int nAdapterToUse=0;
00018     int c;
00019     int nPaused = 0;
00020     int nKeepGoing = 1;
00021     
00022     if(argc<2)
00023     {
00024         printf("Filename to record is a required parameter.\n"); 
00025         return -1;
00026     }
00027     if( (argv[1][0] == '-' || argv[1][0] == '/') &&
00028         (argv[1][1] == 'a' || argv[1][1] == 'A') )
00029     {
00030         eFileMode = asxFILE_MODE_APPEND;
00031         if(argc<3)
00032         {
00033             printf("Filename to record is a required parameter.\n"); 
00034             return -1;
00035         }
00036     }
00037 
00038     // create the system
00039     ASX_System_Create(ASX_SYSTEM_TYPE_HPI,&hSystem);
00040     CheckError(hSystem, __LINE__);
00041 
00042     // get the adapter
00043     ASX_System_GetAdapter(hSystem,nAdapterToUse,&hAdapter);
00044     CheckError(hAdapter, __LINE__);
00045 
00046     // get the mixer handle
00047     ASX_Adapter_GetMixer( hAdapter, &hMixer );
00048     CheckError(hAdapter, __LINE__);
00049 
00050     // get a recorder object
00051     asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
00052         hMixer,
00053         asxNODE_NONE,0,
00054         asxNODE_RECORDER,0,
00055         asxCONTROL_RECORDER,
00056         &hRecorder);
00057     CheckError(hMixer, __LINE__);
00058 
00059     // open the player and pass in the file to be played
00060     ASX_Recorder_Open(
00061         hRecorder, argv[argc-1],
00062         asxFILE_FORMAT_WAV,     // file format = asxFILE_FORMAT_WAV, _MP3, _RAW
00063         eFileMode,              // file mode = asxFILE_MODE_CREATE or _APPEND
00064         2,                      // channels = 1 or 2 at present
00065         asxAUDIO_FORMAT_PCM16,  // audio format = asxAUDIO_FORMAT_PCM8, _PCM16, _PCM24, _PCM32, _PCM32_FLOAT, _MPEG_L2, _MPEG_L3, _DOLBY_AC2, _MPEG_AACPLUS
00066         44100,                  // sample rate = 8 to 192000 Hz
00067         0,                      // bitrate = 8000 to 384000 bps (MPEG only
00068         asxRECORD_MODE_STEREO   // asxRECORD_MODE_STEREO, _JOINT_STEREO, _DUAL_MONO
00069     );
00070     CheckError(hRecorder, __LINE__);
00071 
00072     // start playing the file at offset 0 seconds.
00073     ASX_Recorder_Start( hRecorder );
00074     CheckError(hRecorder, __LINE__);
00075 
00076     // wait for record completion
00077     while(nKeepGoing){
00078         if(nPaused)
00079             printf("Press \'r\' to resume or \'x\' to end recording.\nCommand: ");
00080         else
00081             printf("Press \'p\' to pause or \'x\' to end recording.\nCommand: ");
00082 
00083         c = getchar();
00084         // get the rest of the line
00085         while(getchar()!='\n');
00086         switch(c){
00087         case 'p':
00088             if(nPaused)
00089             {
00090                 printf("Invalid command.\n");
00091             }
00092             else
00093             {
00094                 ASX_Recorder_Pause( hRecorder );
00095                 nPaused = 1;
00096             }
00097             break;
00098         case 'r':
00099             if(!nPaused)
00100             {
00101                 printf("Invalid command.\n");
00102             }
00103             else
00104             {
00105                 ASX_Recorder_Start( hRecorder );
00106                 nPaused = 0;
00107             }
00108             break;
00109         case 'x':
00110             nKeepGoing = 0;
00111             break;
00112         default:
00113             printf("Invalid command.\n");
00114             break;
00115         }
00116     }
00117 
00118     ASX_Recorder_Stop( hRecorder );
00119     CheckError(hRecorder, __LINE__);
00120 
00121     printf("Recording complete.\n");
00122     
00123     // close the file being played
00124     ASX_Recorder_Close(hRecorder);
00125     CheckError(hRecorder, __LINE__);
00126 
00127     printf("Press ENTER to exit\n");
00128     getchar();
00129     ASX_System_Delete(hSystem);
00130     return 0;
00131 }
00132 
00133 int CheckError(ASX_HANDLE hObj, int nLine)
00134 {
00135     int nError;
00136     int asxSubSystemErrorCode=0;
00137     char *pszAsxErrorString;
00138     char *pszAsxSubSystemErrorString;
00139     int nLen1,nLen2;
00140 
00141     ASX_Error_GetLast( hObj, &nError, &asxSubSystemErrorCode);
00142     if(!nError)
00143         return 0;
00144     ASX_Error_GetLastString( hObj, 0,0,&nLen1,0,0,&nLen2);
00145     pszAsxErrorString = (char *)malloc(nLen1);
00146     pszAsxSubSystemErrorString = (char *)malloc(nLen2);
00147     ASX_Error_GetLastString( hObj, pszAsxErrorString,nLen1,&nLen1,pszAsxSubSystemErrorString,nLen2,&nLen2);
00148     printf("Error: #%d, %s - Subsystem Error: #%ld, %s \n", 
00149         nError,  
00150         pszAsxErrorString, 
00151         asxSubSystemErrorCode, 
00152         pszAsxSubSystemErrorString );
00153     printf("When called from source %s line %d\n",__FILE__,nLine);
00154 
00155     printf("Press ENTER to exit\n");
00156     getchar();
00157     free(pszAsxErrorString);
00158     free(pszAsxSubSystemErrorString);
00159     ASX_System_Delete(hSystem);
00160     exit(1);
00161     return 1;
00162 }
00163 

Generated on Tue Nov 18 13:03:40 2008 for ASX by  doxygen 1.4.6-NO