ASX Version4.20.14

record/main.c

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

/* $Header: /home/eliot/asi/repo/cvsrepo/Repository/apps/asx/examples/record/main.c,v 1.9 2011/05/04 19:13:02 as-dxb Exp $ */
#include "stdio.h"
#include "stdlib.h"
#include "asx.h"

ASX_HANDLE hSystem=0;

int CheckError(ASX_HANDLE hObj, int nLine);

int main(int argc, char* argv[])
{
    ASX_HANDLE hAdapter;
    ASX_HANDLE hMixer;
    ASX_HANDLE hRecorder;
    ASX_ERROR asxError;
    enum asxFILE_MODE eFileMode = asxFILE_MODE_CREATE;
    int nAdapterToUse=0;
    int c;
    int nPaused = 0;
    int nKeepGoing = 1;
    
    if(argc<2)
    {
        printf("Filename to record is a required parameter.\n"); 
        return -1;
    }
    if( (argv[1][0] == '-' || argv[1][0] == '/') &&
        (argv[1][1] == 'a' || argv[1][1] == 'A') )
    {
        eFileMode = asxFILE_MODE_APPEND;
        if(argc<3)
        {
            printf("Filename to record is a required parameter.\n"); 
            return -1;
        }
    }

    // create the system
    ASX_System_Create(ASX_SYSTEM_TYPE_HPI,&hSystem);
    CheckError(hSystem, __LINE__);

    // get the adapter
    ASX_System_GetAdapter(hSystem,nAdapterToUse,&hAdapter);
    CheckError(hAdapter, __LINE__);

    // get the mixer handle
    ASX_Adapter_GetMixer( hAdapter, &hMixer );
    CheckError(hAdapter, __LINE__);

    // get a recorder object
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
        hMixer,
        asxNODE_NONE,0,
        asxNODE_RECORDER,0,
        asxCONTROL_RECORDER,
        &hRecorder);
    CheckError(hMixer, __LINE__);

    // open the player and pass in the file to be played
    ASX_Recorder_Open(
        hRecorder, argv[argc-1],
        asxFILE_FORMAT_WAV,     // file format = asxFILE_FORMAT_WAV, _MP3, _RAW
        eFileMode,              // file mode = asxFILE_MODE_CREATE or _APPEND
        2,                      // channels = 1 or 2 at present
        asxAUDIO_FORMAT_PCM16,  // audio format = asxAUDIO_FORMAT_PCM8, _PCM16, _PCM24, _PCM32, _PCM32_FLOAT, _MPEG_L2, _MPEG_L3, _DOLBY_AC2, _MPEG_AACPLUS
        44100,                  // sample rate = 8 to 192000 Hz
        0,                      // bitrate = 8000 to 384000 bps (MPEG only
        asxRECORD_MODE_STEREO   // asxRECORD_MODE_STEREO, _JOINT_STEREO, _DUAL_MONO
    );
    CheckError(hRecorder, __LINE__);

    // start playing the file at offset 0 seconds.
    ASX_Recorder_Start( hRecorder );
    CheckError(hRecorder, __LINE__);

    // wait for record completion
    while(nKeepGoing){
        if(nPaused)
            printf("Press \'r\' to resume or \'x\' to end recording.\nCommand: ");
        else
            printf("Press \'p\' to pause or \'x\' to end recording.\nCommand: ");

        c = getchar();
        // get the rest of the line
        while(getchar()!='\n');
        switch(c){
        case 'p':
            if(nPaused)
            {
                printf("Invalid command.\n");
            }
            else
            {
                ASX_Recorder_Pause( hRecorder );
                nPaused = 1;
            }
            break;
        case 'r':
            if(!nPaused)
            {
                printf("Invalid command.\n");
            }
            else
            {
                ASX_Recorder_Start( hRecorder );
                nPaused = 0;
            }
            break;
        case 'x':
            nKeepGoing = 0;
            break;
        default:
            printf("Invalid command.\n");
            break;
        }
    }

    ASX_Recorder_Stop( hRecorder );
    CheckError(hRecorder, __LINE__);

    printf("Recording complete.\n");
    
    // close the file being played
    ASX_Recorder_Close(hRecorder);
    CheckError(hRecorder, __LINE__);

    printf("Press ENTER to exit\n");
    getchar();
    ASX_System_Delete(hSystem);
    return 0;
}

int CheckError(ASX_HANDLE hObj, int nLine)
{
    int nError;
    int asxSubSystemErrorCode=0;
    char *pszAsxErrorString;
    char *pszAsxSubSystemErrorString;
    int nLen1,nLen2;

    ASX_Error_GetLast( hObj, (ASX_ERROR*)&nError, &asxSubSystemErrorCode);
    if(!nError)
        return 0;
    ASX_Error_GetLastString( hObj, 0,0,&nLen1,0,0,&nLen2);
    pszAsxErrorString = (char *)malloc(nLen1);
    pszAsxSubSystemErrorString = (char *)malloc(nLen2);
    ASX_Error_GetLastString( hObj, pszAsxErrorString,nLen1,&nLen1,pszAsxSubSystemErrorString,nLen2,&nLen2);
    printf("Error: #%d, %s - Subsystem Error: #%d, %s \n", 
        nError,  
        pszAsxErrorString, 
        asxSubSystemErrorCode, 
        pszAsxSubSystemErrorString );
    printf("When called from source %s line %d\n",__FILE__,nLine);

    printf("Press ENTER to exit\n");
    getchar();
    free(pszAsxErrorString);
    free(pszAsxSubSystemErrorString);
    ASX_System_Delete(hSystem);
    exit(1);
    return 1;
}