source: portaudio/branches/V18.1/pa_tests/paqa_errs.c @ 136

Revision 136, 13.7 KB checked in by philburk, 8 years ago (diff)

Minor tweaks while debugging pa_mac_core.c

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * $Id$
3 * paqa_devs.c
4 * Self Testing Quality Assurance app for PortAudio
5 * Do lots of bad things to test error reporting.
6 *
7 * Author: Phil Burk  http://www.softsynth.com
8 *
9 * This program uses the PortAudio Portable Audio Library.
10 * For more information see: http://www.portaudio.com
11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * Any person wishing to distribute modifications to the Software is
25 * requested to send the modifications to the original developer so that
26 * they can be incorporated into the canonical version.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37#include <stdio.h>
38#include <math.h>
39#include "portaudio.h"
40/****************************************** Definitions ***********/
41#define MODE_INPUT      (0)
42#define MODE_OUTPUT     (1)
43#define FRAMES_PER_BUFFER       (64)
44#define SAMPLE_RATE        (44100.0)
45#define NUM_BUFFERS              (0)
46typedef struct PaQaData
47{
48    unsigned long  framesLeft;
49    int            numChannels;
50    int            bytesPerSample;
51    int            mode;
52}
53PaQaData;
54/****************************************** Prototypes ***********/
55static void TestDevices( int mode );
56static void TestFormats( int mode, PaDeviceID deviceID, double sampleRate,
57                         int numChannels );
58static int TestBadOpens( void );
59static int TestBadActions( void );
60static int QaCallback( void *inputBuffer, void *outputBuffer,
61                       unsigned long framesPerBuffer,
62                       PaTimestamp outTime, void *userData );
63/****************************************** Globals ***********/
64static int gNumPassed = 0;
65static int gNumFailed = 0;
66/****************************************** Macros ***********/
67/* Print ERROR if it fails. Tally success or failure. */
68/* Odd do-while wrapper seems to be needed for some compilers. */
69#define EXPECT(_exp) \
70    do \
71    { \
72        if ((_exp)) {\
73            gNumPassed++; \
74        } \
75        else { \
76            printf("\nERROR - 0x%x - %s for %s\n", result, Pa_GetErrorText(result), #_exp ); \
77            gNumFailed++; \
78            goto error; \
79        } \
80    } while(0)
81#define HOPEFOR(_exp) \
82    do \
83    { \
84        if ((_exp)) {\
85            gNumPassed++; \
86        } \
87        else { \
88            printf("\nERROR - 0x%x - %s for %s\n", result, Pa_GetErrorText(result), #_exp ); \
89            gNumFailed++; \
90        } \
91    } while(0)
92/*******************************************************************/
93/* This routine will be called by the PortAudio engine when audio is needed.
94** It may be called at interrupt level on some machines so don't do anything
95** that could mess up the system like calling malloc() or free().
96*/
97static int QaCallback( void *inputBuffer, void *outputBuffer,
98                       unsigned long framesPerBuffer,
99                       PaTimestamp outTime, void *userData )
100{
101    unsigned long i;
102    unsigned char *out = (unsigned char *) outputBuffer;
103    PaQaData *data = (PaQaData *) userData;
104    (void) inputBuffer; /* Prevent "unused variable" warnings. */
105    (void) outTime;
106
107    /* Zero out buffer so we don't hear terrible noise. */
108    if( data->mode == MODE_OUTPUT )
109    {
110        unsigned long numBytes = framesPerBuffer * data->numChannels * data->bytesPerSample;
111        for( i=0; i<numBytes; i++ )
112        {
113            *out++ = 0;
114        }
115    }
116    /* Are we through yet? */
117    if( data->framesLeft > framesPerBuffer )
118    {
119        data->framesLeft -= framesPerBuffer;
120        return 0;
121    }
122    else
123    {
124        data->framesLeft = 0;
125        return 1;
126    }
127}
128/*******************************************************************/
129int main(void);
130int main(void)
131{
132    PaError result;
133    EXPECT( ((result=Pa_Initialize()) == 0) );
134    TestBadOpens();
135    TestBadActions();
136error:
137    Pa_Terminate();
138    printf("QA Report: %d passed, %d failed.\n", gNumPassed, gNumFailed );
139    return 0;
140}
141/*******************************************************************/
142static int TestBadOpens( void )
143{
144    PortAudioStream *stream = NULL;
145    PaError result;
146    PaQaData myData;
147    /* Setup data for synthesis thread. */
148    myData.framesLeft = (unsigned long) (SAMPLE_RATE * 100); /* 100 seconds */
149    myData.numChannels = 1;
150    myData.mode = MODE_OUTPUT;
151    HOPEFOR( (/* No devices specified. */
152                 (result = Pa_OpenStream(
153                               &stream,
154                               paNoDevice, 0, paFloat32, NULL,
155                               paNoDevice, 0, paFloat32, NULL,
156                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
157                               paClipOff,
158                               QaCallback,
159                               &myData )
160                 ) == paInvalidDeviceId) );
161    HOPEFOR( ( /* Out of range input device specified. */
162                 (result = Pa_OpenStream(
163                               &stream,
164                               Pa_CountDevices(), 0, paFloat32, NULL,
165                               paNoDevice, 0, paFloat32, NULL,
166                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
167                               paClipOff,
168                               QaCallback,
169                               &myData )
170                 ) == paInvalidDeviceId) );
171
172    HOPEFOR( ( /* Out of range output device specified. */
173                 (result = Pa_OpenStream(
174                               &stream,
175                               paNoDevice, 0, paFloat32, NULL,
176                               Pa_CountDevices(), 0, paFloat32, NULL,
177                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
178                               paClipOff,
179                               QaCallback,
180                               &myData )
181                 ) == paInvalidDeviceId) );
182    HOPEFOR( ( /* Zero input channels. */
183                 (result = Pa_OpenStream(
184                               &stream,
185                               Pa_GetDefaultInputDeviceID(), 0, paFloat32, NULL,
186                               paNoDevice, 0, paFloat32, NULL,
187                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
188                               paClipOff,
189                               QaCallback,
190                               &myData )
191                 ) == paInvalidChannelCount) );
192    HOPEFOR( ( /* Zero output channels. */
193                 (result = Pa_OpenStream(
194                               &stream,
195                               paNoDevice, 0, paFloat32, NULL,
196                               Pa_GetDefaultOutputDeviceID(), 0, paFloat32, NULL,
197                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
198                               paClipOff,
199                               QaCallback,
200                               &myData )
201                 ) == paInvalidChannelCount) );
202    HOPEFOR( ( /* Nonzero input channels but no device. */
203                 (result = Pa_OpenStream(
204                               &stream,
205                               Pa_GetDefaultInputDeviceID(), 2, paFloat32, NULL,
206                               paNoDevice, 2, paFloat32, NULL,
207                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
208                               paClipOff,
209                               QaCallback,
210                               &myData )
211                 ) == paInvalidChannelCount) );
212
213    HOPEFOR( ( /* Nonzero output channels but no device. */
214                 (result = Pa_OpenStream(
215                               &stream,
216                               paNoDevice, 2, paFloat32, NULL,
217                               Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
218                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
219                               paClipOff,
220                               QaCallback,
221                               &myData )
222                 ) == paInvalidChannelCount) );
223    HOPEFOR( ( /* NULL stream pointer. */
224                 (result = Pa_OpenStream(
225                               NULL,
226                               paNoDevice, 0, paFloat32, NULL,
227                               Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
228                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
229                               paClipOff,
230                               QaCallback,
231                               &myData )
232                 ) == paBadStreamPtr) );
233    HOPEFOR( ( /* Low sample rate. */
234                 (result = Pa_OpenStream(
235                               &stream,
236                               paNoDevice, 0, paFloat32, NULL,
237                               Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
238                               1.0, FRAMES_PER_BUFFER, NUM_BUFFERS,
239                               paClipOff,
240                               QaCallback,
241                               &myData )
242                 ) == paInvalidSampleRate) );
243    HOPEFOR( ( /* High sample rate. */
244                 (result = Pa_OpenStream(
245                               &stream,
246                               paNoDevice, 0, paFloat32, NULL,
247                               Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
248                               10000000.0, FRAMES_PER_BUFFER, NUM_BUFFERS,
249                               paClipOff,
250                               QaCallback,
251                               &myData )
252                 ) == paInvalidSampleRate) );
253    HOPEFOR( ( /* NULL callback. */
254                 (result = Pa_OpenStream(
255                               &stream,
256                               paNoDevice, 0, paFloat32, NULL,
257                               Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
258                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
259                               paClipOff,
260                               NULL,
261                               &myData )
262                 ) == paNullCallback) );
263    HOPEFOR( ( /* Bad flag. */
264                 (result = Pa_OpenStream(
265                               &stream,
266                               paNoDevice, 0, paFloat32, NULL,
267                               Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
268                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
269                               (1<<3),
270                               QaCallback,
271                               &myData )
272                 ) == paInvalidFlag) );
273
274#if 0 /* FIXME - this is legal for some implementations. */
275    HOPEFOR( ( /* Use input device as output device. */
276                 (result = Pa_OpenStream(
277                               &stream,
278                               paNoDevice, 0, paFloat32, NULL,
279                               Pa_GetDefaultInputDeviceID(), 2, paFloat32, NULL,
280                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
281                               paClipOff,
282                               QaCallback,
283                               &myData )
284                 ) == paInvalidDeviceId) );
285
286    HOPEFOR( ( /* Use output device as input device. */
287                 (result = Pa_OpenStream(
288                               &stream,
289                               Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
290                               paNoDevice, 0, paFloat32, NULL,
291                               SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
292                               paClipOff,
293                               QaCallback,
294                               &myData )
295                 ) == paInvalidDeviceId) );
296#endif
297
298    if( stream != NULL ) Pa_CloseStream( stream );
299    return result;
300}
301/*******************************************************************/
302static int TestBadActions( void )
303{
304    PortAudioStream *stream = NULL;
305    PaError result;
306    PaQaData myData;
307    /* Setup data for synthesis thread. */
308    myData.framesLeft = (unsigned long) (SAMPLE_RATE * 100); /* 100 seconds */
309    myData.numChannels = 1;
310    myData.mode = MODE_OUTPUT;
311    /* Default output. */
312    EXPECT( ((result = Pa_OpenStream(
313                           &stream,
314                           paNoDevice, 0, paFloat32, NULL,
315                           Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL,
316                           SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS,
317                           paClipOff,
318                           QaCallback,
319                           &myData )
320             ) == 0) );
321    HOPEFOR( ((result = Pa_StartStream( NULL )) == paBadStreamPtr) );
322    HOPEFOR( ((result = Pa_StopStream( NULL )) == paBadStreamPtr) );
323    HOPEFOR( ((result = Pa_StreamActive( NULL )) == paBadStreamPtr) );
324    HOPEFOR( ((result = Pa_CloseStream( NULL )) == paBadStreamPtr) );
325    HOPEFOR( ((result = (PaError)Pa_StreamTime( NULL )) != 0) );
326    HOPEFOR( ((result = (PaError)Pa_GetCPULoad( NULL )) != 0) );
327error:
328    if( stream != NULL ) Pa_CloseStream( stream );
329    return result;
330}
Note: See TracBrowser for help on using the repository browser.