aboutsummaryrefslogtreecommitdiff
path: root/pd/portaudio_v18/pa_tests/paqa_devs.c
blob: 92eb6c7acc1011dbb5ada2e2d74279e6240a541f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * $Id: paqa_devs.c,v 1.1.1.1.4.1 2003/02/11 21:41:32 philburk Exp $
 * paqa_devs.c
 * Self Testing Quality Assurance app for PortAudio
 * Try to open each device and run through all the
 * possible configurations.
 *
 * Author: Phil Burk  http://www.softsynth.com
 *
 * This program uses the PortAudio Portable Audio Library.
 * For more information see: http://www.portaudio.com
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
#include <stdio.h>
#include <math.h>
#include "portaudio.h"
#include "pa_trace.h"
/****************************************** Definitions ***********/
#define MODE_INPUT      (0)
#define MODE_OUTPUT     (1)
typedef struct PaQaData
{
    unsigned long  framesLeft;
    int            numChannels;
    int            bytesPerSample;
    int            mode;
    short          sawPhase;
    PaSampleFormat format;
} PaQaData;

/****************************************** Prototypes ***********/
static void TestDevices( int mode );
static void TestFormats( int mode, PaDeviceID deviceID, double sampleRate,
                         int numChannels );
static int TestAdvance( int mode, PaDeviceID deviceID, double sampleRate,
                        int numChannels, PaSampleFormat format );
static int QaCallback( void *inputBuffer, void *outputBuffer,
                       unsigned long framesPerBuffer,
                       PaTimestamp outTime, void *userData );
                       
/****************************************** Globals ***********/
static int gNumPassed = 0;
static int gNumFailed = 0;

/****************************************** Macros ***********/
/* Print ERROR if it fails. Tally success or failure. */
/* Odd do-while wrapper seems to be needed for some compilers. */
#define EXPECT(_exp) \
    do \
    { \
        if ((_exp)) {\
            /* printf("SUCCESS for %s\n", #_exp ); */ \
            gNumPassed++; \
        } \
        else { \
            printf("ERROR - 0x%x - %s for %s\n", result, \
                   ((result == 0) ? "-" : Pa_GetErrorText(result)), \
                   #_exp ); \
            gNumFailed++; \
            goto error; \
        } \
    } while(0)
/*******************************************************************/
/* This routine will be called by the PortAudio engine when audio is needed.
** It may be called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int QaCallback( void *inputBuffer, void *outputBuffer,
                       unsigned long framesPerBuffer,
                       PaTimestamp outTime, void *userData )
{
    unsigned long  i;
    short          phase;
    PaQaData *data = (PaQaData *) userData;
    (void) inputBuffer;
    (void) outTime;

    /* Play simple sawtooth wave. */
    if( data->mode == MODE_OUTPUT )
    {
        phase = data->sawPhase;
        switch( data->format )
        {
        case paFloat32:
            {
                float *out =  (float *) outputBuffer;
                for( i=0; i<framesPerBuffer; i++ )
                {
                    phase += 0x123;
                    *out++ = (float) (phase * (1.0 / 32768.0));
                    if( data->numChannels == 2 )
                    {
                        *out++ = (float) (phase * (1.0 / 32768.0));
                    }
                }
            }
            break;

        case paInt32:
            {
                int *out =  (int *) outputBuffer;
                for( i=0; i<framesPerBuffer; i++ )
                {
                    phase += 0x123;
                    *out++ = ((int) phase ) << 16;
                    if( data->numChannels == 2 )
                    {
                        *out++ = ((int) phase ) << 16;
                    }
                }
            }
            break;
        case paInt16:
            {
                short *out =  (short *) outputBuffer;
                for( i=0; i<framesPerBuffer; i++ )
                {
                    phase += 0x123;
                    *out++ = phase;
                    if( data->numChannels == 2 )
                    {
                        *out++ = phase;
                    }
                }
            }
            break;

        default:
            {
                unsigned char *out =  (unsigned char *) outputBuffer;
                unsigned long numBytes = framesPerBuffer * data->numChannels * data->bytesPerSample;
                for( i=0; i<numBytes; i++ )
                {
                    *out++ = 0;
                }
            }
            break;
        }
        data->sawPhase = phase;
    }
    /* Are we through yet? */
    if( data->framesLeft > framesPerBuffer )
    {
        AddTraceMessage("QaCallback: running. framesLeft", data->framesLeft );
        data->framesLeft -= framesPerBuffer;
        return 0;
    }
    else
    {
        AddTraceMessage("QaCallback: DONE! framesLeft", data->framesLeft );
        data->framesLeft = 0;
        return 1;
    }
}
/*******************************************************************/
int main(void);
int main(void)
{
    PaError result;
    EXPECT( ((result=Pa_Initialize()) == 0) );
    printf("Test OUTPUT ---------------\n");
    TestDevices( MODE_OUTPUT );
    printf("Test INPUT ---------------\n");
    TestDevices( MODE_INPUT );
error:
    Pa_Terminate();
    printf("QA Report: %d passed, %d failed.\n", gNumPassed, gNumFailed );
}
/*******************************************************************
* Try each output device, through its full range of capabilities. */
static void TestDevices( int mode )
{
    int id,jc,kr;
    int maxChannels;
    const PaDeviceInfo *pdi;
    int numDevices  = Pa_CountDevices();
    /* Iterate through all devices. */
    for( id=0; id<numDevices; id++ )
    {
        pdi = Pa_GetDeviceInfo( id );
        /* Try 1 to maxChannels on each device. */
        maxChannels = ( mode == MODE_INPUT ) ? pdi->maxInputChannels : pdi->maxOutputChannels;
        for( jc=1; jc<=maxChannels; jc++ )
        {
            printf("Name         = %s\n", pdi->name );
            /* Try each legal sample rate. */
            if( pdi->numSampleRates == -1 )
            {
                double low, high;
                low = pdi->sampleRates[0];
                high = pdi->sampleRates[1];
                if( low < 8000.0 ) low = 8000.0;
                TestFormats( mode, id, low, jc );
#define TESTSR(sr) {if(((sr)>=low) && ((sr)<=high)) TestFormats( mode, id, (sr), jc ); }

                TESTSR(11025.0);
                TESTSR(22050.0);
                TESTSR(34567.0);
                TESTSR(44100.0);
                TestFormats( mode, id, high, jc );
            }
            else
            {
                for( kr=0; kr<pdi->numSampleRates; kr++ )
                {
                    TestFormats( mode, id, pdi->sampleRates[kr], jc );
                }
            }
        }
    }
}
/*******************************************************************/
static void TestFormats( int mode, PaDeviceID deviceID, double sampleRate,
                         int numChannels )
{
    TestAdvance( mode, deviceID, sampleRate, numChannels, paFloat32 ); /* */
    TestAdvance( mode, deviceID, sampleRate, numChannels, paInt16 ); /* */
    TestAdvance( mode, deviceID, sampleRate, numChannels, paInt32 ); /* */
}
/*******************************************************************/
static int TestAdvance( int mode, PaDeviceID deviceID, double sampleRate,
                        int numChannels, PaSampleFormat format )
{
    PortAudioStream *stream = NULL;
    PaError result;
    PaQaData myData;
#define FRAMES_PER_BUFFER  (64)
    printf("------ TestAdvance: %s, device = %d, rate = %g, numChannels = %d, format = %d -------\n",
           ( mode == MODE_INPUT ) ? "INPUT" : "OUTPUT",
           deviceID, sampleRate, numChannels, format);
    fflush(stdout);
    /* Setup data for synthesis thread. */
    myData.framesLeft = (unsigned long) (sampleRate * 100); /* 100 seconds */
    myData.numChannels = numChannels;
    myData.mode = mode;
    myData.format = format;
    switch( format )
    {
    case paFloat32:
    case paInt32:
    case paInt24:
        myData.bytesPerSample = 4;
        break;
    case paPackedInt24:
        myData.bytesPerSample = 3;
        break;
    default:
        myData.bytesPerSample = 2;
        break;
    }
    EXPECT( ((result = Pa_OpenStream(
                           &stream,
                           ( mode == MODE_INPUT ) ? deviceID : paNoDevice,
                           ( mode == MODE_INPUT ) ? numChannels : 0,
                           format,
                           NULL,
                           ( mode == MODE_OUTPUT ) ? deviceID : paNoDevice,
                           ( mode == MODE_OUTPUT ) ? numChannels : 0,
                           format,
                           NULL,
                           sampleRate,
                           FRAMES_PER_BUFFER,     /* frames per buffer */
                           0,             /* number of buffers, if zero then use default minimum */
                           paClipOff,     /* we won't output out of range samples so don't bother clipping them */
                           QaCallback,
                           &myData )
             ) == 0) );
    if( stream )
    {
        PaTimestamp oldStamp, newStamp;
        unsigned long oldFrames;
        int minDelay = ( mode == MODE_INPUT ) ? 1000 : 400;
        int minNumBuffers = Pa_GetMinNumBuffers( FRAMES_PER_BUFFER, sampleRate );
        int msec = (int) ((minNumBuffers * 3 * 1000.0 * FRAMES_PER_BUFFER) / sampleRate);
        if( msec < minDelay ) msec = minDelay;
        printf("msec = %d\n", msec);  /**/
        EXPECT( ((result=Pa_StartStream( stream )) == 0) );
        /* Check to make sure PortAudio is advancing timeStamp. */
        result = paNoError;
        oldStamp = Pa_StreamTime(stream);
        fflush(stdout);
        Pa_Sleep(msec);
        newStamp = Pa_StreamTime(stream);
        printf("oldStamp = %g,newStamp = %g\n", oldStamp, newStamp ); /**/
        EXPECT( (oldStamp < newStamp) );
        /* Check to make sure callback is decrementing framesLeft. */
        oldFrames = myData.framesLeft;
        Pa_Sleep(msec);
        printf("oldFrames = %d, myData.framesLeft = %d\n", oldFrames,  myData.framesLeft ); /**/
        EXPECT( (oldFrames > myData.framesLeft) );
        EXPECT( ((result=Pa_CloseStream( stream )) == 0) );
        stream = NULL;
    }
error:
    if( stream != NULL ) Pa_CloseStream( stream );
    fflush(stdout);
    return result;
}