The following is some example HPI code that can be compiled under Windows or Linux.

Windows: In your project/makefile, you will need to define HPI_OS_WIN32_USER and HPIDLL_IMPORTS and link with ASIHPI32.LIB that comes in the Windows HPI SDK.

Linux: In your project/makefile, you will need to define HPI_OS_LINUX.

// $Header: /Repository/apps/thpi/thpif.c,v 1.8 2008/05/16 13:30:46 as-age Exp $
// THPIF.C
// Test Hardware Programming Interface (HPI)  using HPI functions
//
//

//#include <stdafx.h>   // for Microsoft apps only

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "hpi.h"

// local protos
void THPI_Message( HPI_MESSAGE *phm, HPI_RESPONSE *phr);
void THPI_PrintMsg( HPI_MESSAGE *phm );
void THPI_PrintResult( HPI_RESPONSE *phr );
void HandleError( HW16 wHE );

// file read/write
void THPI_WavFileOpen( short nIndex, char *pszFile);
short THPI_WavFileRead( short nIndex, unsigned char *pbData, long lLength);
void THPI_WavFileClose( short nIndex );


void main()
{

        HW16                    wHE=0;  // HPI error
        HW32                    dwVersion=0;
        HPI_HSUBSYS             *phSubSys;      // handle to audio subsystem
        HPI_RESOURCE    Resource;
        HPI_FORMAT              Format;
        HW16                    wAdapterIndex=0;
        HW16                    wNumAdapters=0;
        HW16                    awAdapterList[20];
        HW16                    wListLength=20;
        HPI_HOSTREAM            hOutStream0=0;
        HPI_HOSTREAM            hOutStream1=0;
        HW32                    dwStrBufferSize=0;
        HW32                    dwDataToPlay=0;
        HW16                    wState=0;
        HPI_HMIXER              hMixer=0;
        HPI_HCONTROL            hControl=0;
        short                   anGain0_01dB[HPI_MAX_CHANNELS];

        #define BLOCK_SIZE 8192
        unsigned char   abBuffer[BLOCK_SIZE];
        HW32    i=0;
        short   nPeakLeft=0, nPeakRight=0;

        printf("\n** Test HPI using Functions **\n");

        // open subsystem and find adapters
        phSubSys = HPI_SubSysCreate();
        HandleError( wHE );
        printf("HPI_SubSys_Create\n");

        wHE = HPI_SubSysGetVersion( phSubSys, &dwVersion );
        HandleError( wHE );
        printf("HPI_SubSys_Version=%lx\n", dwVersion );

        wHE = HPI_SubSysFindAdapters(
                phSubSys,
                &wNumAdapters,
                awAdapterList,
                wListLength
                );
        HandleError( wHE );
        printf("HPI_SubSysFindAdapters NumberAdapters=%d ", wNumAdapters);
        for(i=0; i<4; i++)
                printf("%ld=%X ", i, awAdapterList[i] );
        printf("\n\n");

        // open adapter
        {
        HW16    wNumOutStreams;
        HW16    wNumInStreams;
        HW32    dwSerialNumber;
        HW16    wAdapterType;
        HW16    wVersion;

        wHE = HPI_AdapterOpen(
                phSubSys,
                wAdapterIndex
                );
        HandleError( wHE );
        printf("HPI Open_Adapter\n");

        wHE = HPI_AdapterGetInfo(
                phSubSys,
                wAdapterIndex,
                &wNumOutStreams,
                &wNumInStreams,
                &wVersion,
                &dwSerialNumber,
                &wAdapterType
                );
        HandleError( wHE );
        printf("HPI Get_Adapter_Info\n");
        printf("Adapter Index=%d NumOutStreams=%d NumInStreams=%d Version=%d S/N=%ld Adapter=ASI%X\n",
                wAdapterIndex, wNumOutStreams, wNumInStreams, wVersion, dwSerialNumber, wAdapterType );
        }

        // open the out_streams 0 and 1

        wHE = HPI_OutStreamOpen(
                phSubSys,
                wAdapterIndex,
                0,
                &hOutStream0
                );
        printf("HPI_OutStreamOpen 0: handle=%lX\n", hOutStream0);
        HandleError( wHE );

        wHE = HPI_OutStreamOpen(
                phSubSys,
                wAdapterIndex,
                1,
                &hOutStream
                );
        printf("HPI_OutStreamOpen 1: handle=%lX\n", hOutStream1);
        HandleError( wHE );

        // find out some info about the device
        wHE = HPI_OutStreamGetInfo(
                phSubSys,
                hOutStream0,
                NULL,
                &dwStrBufferSize,
                NULL
                );
        printf("HPI_OutStreamGetInfo 0: BufferSize=%ld\n", dwStrBufferSize);
        HandleError( wHE );

        // open the mixer of this adapter
        wHE = HPI_MixerOpen(
                phSubSys,
                wAdapterIndex,
                &hMixer
                );
        printf("HPI_MixerOpen: handle=%lX\n", hMixer);
        HandleError( wHE );

        // get mixer volume control on the connection
        // between out_stream 0 and lineOut 0
        wHE = HPI_MixerGetControl(
                        phSubSys,
                        hMixer,
                        HPI_SOURCENODE_OSTREAM,
                        0,
                        HPI_DESTNODE_LINEOUT,
                        0,
                        HPI_CONTROL_VOLUME,
                        &hControl
                        );
        HandleError( wHE );

        // set the level on LineOut 1 to +19 dBu
        anGain0_01dB[0] = 19 * HPI_UNITS_PER_dB;
        anGain0_01dB[1] = 19 * HPI_UNITS_PER_dB;
        wHE = HPI_MixerGetControl( phSubSys, hMixer,
                        0, 0,                           // source "none", index "none"
                        HPI_DESTNODE_LINEOUT, 0,        // destination line out, index 0
                        HPI_CONTROL_LEVEL, &hControl
                        );
        HandleError( wHE );
        wHE = HPI_LevelSetGain(phSubSys,hControl,anGain0_01dB);
        printf("Set line out 1 output level to +19 dBu\n");
        HandleError( wHE );
        // set the level on LineIn 1 to +19 dBu
        wHE = HPI_MixerGetControl(phSubSys,hMixer,
                        HPI_SOURCENODE_LINEIN,  0,      // source line in, index 0
                        0, 0,                           // destination "none", index "none"
                        HPI_CONTROL_LEVEL, &hControl
                        );
        HandleError( wHE );
        wHE = HPI_LevelSetGain(phSubSys,hControl,anGain0_01dB);
        printf("Set line in 1 input level to +19 dBu\n");
        HandleError( wHE );

        // count the number of line outs
        wHE = 0;
        i=0;
        while(!wHE)
        {
                wHE = HPI_MixerGetControl(
                                phSubSys,
                                hMixer,
                                0,
                                0,
                                HPI_DESTNODE_LINEOUT,
                                i,
                                HPI_CONTROL_METER,
                                &hControl
                                );
                if(!wHE)
                        i++;
        }
        printf("Number of line outs found - %d\n",i);


        // open some audio files to play
        THPI_WavFileOpen( 0, "c:\\asi\\audio\\mp2\\BETTY.WAV" );
        THPI_WavFileOpen( 1, "c:\\asi\\audio\\mp2\\LONELY.WAV" );

        // setup format and size of data block
        // we will use to send audio data to out_stream
        // we would normally get the format directly from the file
        // or audio data
        wHE = HPI_FormatCreate(
                &Format,
                1,          // mono channel
                HPI_FORMAT_MPEG_L2,
                44100,          //sample rate
                128000,     //128k bits/sec
                0                       // no attributes
                );
        HandleError( wHE );

        // preload buffer of device
        printf("Preload ");
        for(i=0; i<(dwStrBufferSize/BLOCK_SIZE); i++)
        {
                // out_stream #1
                THPI_WavFileRead( 0, abBuffer, BLOCK_SIZE );

                wHE = HPI_OutStreamWriteBuf(
                        phSubSys,
                        hOutStream0,
                        abBuffer,
                        BLOCK_SIZE,
                        &Format
                        );

                printf(".");

        }
        printf("\n");


        // start play back
        wHE = HPI_OutStreamStart(
                                phSubSys,
                                hOutStream0
                                );
        HandleError( wHE );
        printf("OutStream - start\n");

        // keep buffers full of data
        // while playing
        while(1)
        {
                char szPeakL[40];
                char szPeakR[40];
                short anPeakLog[2] = {0,0};
                short j=0;

                // get the meter level value
                wHE = HPI_MeterGetPeak(
                        phSubSys,
                        hControl,
                        anPeakLog
                );

                // create level bar
                #define BARLEN 10
                nPeakLeft = anPeakLog[0]/3;
                if(nPeakLeft < -BARLEN) nPeakLeft = -BARLEN;
                if(nPeakLeft > 0 ) nPeakLeft = 0;
                nPeakLeft = BARLEN+nPeakLeft;
                strcpy(szPeakL,"");
                for(j=0; j<nPeakLeft; j++)
                        strcat(szPeakL, "*");
                nPeakRight = anPeakLog[1]/3;
                if(nPeakRight < -BARLEN) nPeakRight = -BARLEN;
                if(nPeakRight > 0 ) nPeakRight = 0;
                nPeakRight = BARLEN+nPeakRight;
                strcpy(szPeakR,"");
                for(j=0; j<nPeakRight; j++)
                        strcat(szPeakR, "*");

                //get state of device
                wHE = HPI_OutStreamGetInfo(
                        phSubSys,
                        hOutStream0,
                        &wState,
                        NULL,
                        &dwDataToPlay
                );

                printf("DataToPlay=%06ld State=%d PeakMeter:L=%11s R=%11s\r", dwDataToPlay, wState, szPeakL, szPeakR );

                if( wState != HPI_STATE_PLAYING )
                        break;

                if( dwDataToPlay < dwStrBufferSize/2)
                {
                        // read a block of audio data from the file
                        if( ! THPI_WavFileRead( 0, abBuffer, BLOCK_SIZE ))
                        {
                                // write it to the device
                                wHE = HPI_OutStreamWriteBuf(
                                        phSubSys,
                                        hOutStream0,
                                        abBuffer,
                                        BLOCK_SIZE,
                                        &Format
                                        );
                }
                }

                // stop if the user presses a key
                if(kbhit())
                        break;
        }
        printf("\n");

        wHE = HPI_OutStreamStop(
                        phSubSys,
                        hOutStream0 );
        printf("HPI OStream_Stop\n" );
        HandleError( wHE );

        wHE = HPI_OutStreamClose(
                        phSubSys,
                        hOutStream0 );
        printf("HPI OStream_Close_0\n" );
        HandleError( wHE );

        wHE = HPI_OutStreamClose(
                        phSubSys,
                        hOutStream1 );
        printf("HPI OStream_Close_1 \n" );
        HandleError( wHE );

        wHE = HPI_MixerClose(
                        phSubSys,
                        hMixer );
        printf("HPI Mixer_Close\n" );
        HandleError( wHE );

        wHE = HPI_AdapterClose(
                        phSubSys,
                        wAdapterIndex );
        printf("HPI Adapter_Close\n" );
        HandleError( wHE );

        THPI_WavFileClose(0);

        getch();
}

FILE *gpFile[4];

void THPI_WavFileOpen( short nIndex, char *pszFile)
{
        gpFile[nIndex] = fopen(pszFile,"rb");
        if(!gpFile[nIndex])
        {
                printf("****ERROR**** - can't open file\n");
                getch();
                exit(0);
        }
        fseek(gpFile[nIndex], 0x50, SEEK_SET);

}

short THPI_WavFileRead( short nIndex, unsigned char *pbData, long lLength)
{
        long lNumRead;
        lNumRead = fread( pbData, 1, lLength, gpFile[nIndex] );                 //read WAV file
        if( lNumRead != lLength)
                return(1);
        else
                return(0);
}

void THPI_WavFileClose(short nIndex )
{
        fclose(gpFile[nIndex]);
}


void    HandleError( HW16 wHE )
{
        char szError[256];

        if(wHE)
        {
                HPI_GetErrorText( wHE, szError );
                printf("ERROR %d %s\n", wHE, szError);
                printf("press a key...\n");
                getch();
        }
}



Generated on Fri Aug 1 13:33:17 2008 for AudioScience HPI by  doxygen 1.4.6-NO