~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_read_write_wire.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** @file patest_read_write_wire.c
2
 
        @ingroup test_src
3
 
        @brief Tests full duplex blocking I/O by passing input straight to output.
4
 
        @author Bjorn Roche. XO Audio LLC for Z-Systems Engineering.
5
 
    @author based on code by: Phil Burk  http://www.softsynth.com
6
 
    @author based on code by: Ross Bencina rossb@audiomulch.com
7
 
*/
8
 
/*
9
 
 * $Id: patest_read_write_wire.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
10
 
 *
11
 
 * This program uses the PortAudio Portable Audio Library.
12
 
 * For more information see: http://www.portaudio.com
13
 
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
14
 
 *
15
 
 * Permission is hereby granted, free of charge, to any person obtaining
16
 
 * a copy of this software and associated documentation files
17
 
 * (the "Software"), to deal in the Software without restriction,
18
 
 * including without limitation the rights to use, copy, modify, merge,
19
 
 * publish, distribute, sublicense, and/or sell copies of the Software,
20
 
 * and to permit persons to whom the Software is furnished to do so,
21
 
 * subject to the following conditions:
22
 
 *
23
 
 * The above copyright notice and this permission notice shall be
24
 
 * included in all copies or substantial portions of the Software.
25
 
 *
26
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29
 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
30
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
31
 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32
 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
 
 */
34
 
 
35
 
/*
36
 
 * The text above constitutes the entire PortAudio license; however, 
37
 
 * the PortAudio community also makes the following non-binding requests:
38
 
 *
39
 
 * Any person wishing to distribute modifications to the Software is
40
 
 * requested to send the modifications to the original developer so that
41
 
 * they can be incorporated into the canonical version. It is also 
42
 
 * requested that these non-binding requests be included along with the 
43
 
 * license above.
44
 
 */
45
 
 
46
 
#include <stdio.h>
47
 
#include <stdlib.h>
48
 
#include "portaudio.h"
49
 
 
50
 
/* #define SAMPLE_RATE  (17932) // Test failure to open with this value. */
51
 
#define SAMPLE_RATE  (44100)
52
 
#define FRAMES_PER_BUFFER (1024)
53
 
#define NUM_CHANNELS    (2)
54
 
/* #define DITHER_FLAG     (paDitherOff)  */
55
 
#define DITHER_FLAG     (0) /**/
56
 
 
57
 
/* Select sample format. */
58
 
#if 1
59
 
#define PA_SAMPLE_TYPE  paFloat32
60
 
typedef float SAMPLE;
61
 
#define SAMPLE_SILENCE  (0.0f)
62
 
#define PRINTF_S_FORMAT "%.8f"
63
 
#elif 1
64
 
#define PA_SAMPLE_TYPE  paInt16
65
 
typedef short SAMPLE;
66
 
#define SAMPLE_SILENCE  (0)
67
 
#define PRINTF_S_FORMAT "%d"
68
 
#elif 0
69
 
#define PA_SAMPLE_TYPE  paInt8
70
 
typedef char SAMPLE;
71
 
#define SAMPLE_SILENCE  (0)
72
 
#define PRINTF_S_FORMAT "%d"
73
 
#else
74
 
#define PA_SAMPLE_TYPE  paUInt8
75
 
typedef unsigned char SAMPLE;
76
 
#define SAMPLE_SILENCE  (128)
77
 
#define PRINTF_S_FORMAT "%d"
78
 
#endif
79
 
 
80
 
 
81
 
/*******************************************************************/
82
 
int main(void);
83
 
int main(void)
84
 
{
85
 
    PaStreamParameters inputParameters, outputParameters;
86
 
    PaStream *stream = NULL;
87
 
    PaError err;
88
 
    SAMPLE *sampleBlock;
89
 
    int i;
90
 
    int numBytes;
91
 
    
92
 
    
93
 
    printf("patest_read_write_wire.c\n"); fflush(stdout);
94
 
 
95
 
    numBytes = FRAMES_PER_BUFFER * NUM_CHANNELS * sizeof(SAMPLE);
96
 
    sampleBlock = (SAMPLE *) malloc( numBytes );
97
 
    if( sampleBlock == NULL )
98
 
    {
99
 
        printf("Could not allocate record array.\n");
100
 
        exit(1);
101
 
    }
102
 
    for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ )
103
 
        sampleBlock[i] = (SAMPLE_SILENCE);
104
 
 
105
 
    err = Pa_Initialize();
106
 
    if( err != paNoError ) goto error;
107
 
 
108
 
    inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
109
 
    printf( "Input device # %d.\n", inputParameters.device );
110
 
    printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency );
111
 
    printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency );
112
 
    inputParameters.channelCount = NUM_CHANNELS;
113
 
    inputParameters.sampleFormat = PA_SAMPLE_TYPE;
114
 
    inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ;
115
 
    inputParameters.hostApiSpecificStreamInfo = NULL;
116
 
 
117
 
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
118
 
    printf( "Output device # %d.\n", outputParameters.device );
119
 
    printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency );
120
 
    printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency );
121
 
    outputParameters.channelCount = NUM_CHANNELS;
122
 
    outputParameters.sampleFormat = PA_SAMPLE_TYPE;
123
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
124
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
125
 
 
126
 
    /* -- setup -- */
127
 
 
128
 
   err = Pa_OpenStream(
129
 
              &stream,
130
 
              &inputParameters,
131
 
              &outputParameters,
132
 
              SAMPLE_RATE,
133
 
              FRAMES_PER_BUFFER,
134
 
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
135
 
              NULL, /* no callback, use blocking API */
136
 
              NULL ); /* no callback, so no callback userData */
137
 
    if( err != paNoError ) goto error;
138
 
 
139
 
    err = Pa_StartStream( stream );
140
 
    if( err != paNoError ) goto error;
141
 
    printf("Wire on. Will run one minute.\n"); fflush(stdout);
142
 
 
143
 
    for( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
144
 
    {
145
 
       err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
146
 
       if( err ) goto xrun;
147
 
       err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
148
 
       if( err ) goto xrun;
149
 
    }
150
 
    err = Pa_StopStream( stream );
151
 
    if( err != paNoError ) goto error;
152
 
 
153
 
    for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ )
154
 
        sampleBlock[i] = (SAMPLE_SILENCE);
155
 
 
156
 
    err = Pa_StartStream( stream );
157
 
    if( err != paNoError ) goto error;
158
 
    printf("Wire on. Interrupt to stop.\n"); fflush(stdout);
159
 
 
160
 
    while( 1 )
161
 
    {
162
 
       err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
163
 
       if( err ) goto xrun;
164
 
       err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
165
 
       if( err ) goto xrun;
166
 
    }
167
 
    err = Pa_StopStream( stream );
168
 
    if( err != paNoError ) goto error;
169
 
 
170
 
    Pa_CloseStream( stream );
171
 
 
172
 
    free( sampleBlock );
173
 
 
174
 
    Pa_Terminate();
175
 
    return 0;
176
 
 
177
 
xrun:
178
 
    if( stream ) {
179
 
       Pa_AbortStream( stream );
180
 
       Pa_CloseStream( stream );
181
 
    }
182
 
    free( sampleBlock );
183
 
    Pa_Terminate();
184
 
    if( err & paInputOverflow )
185
 
       fprintf( stderr, "Input Overflow.\n" );
186
 
    if( err & paOutputUnderflow )
187
 
       fprintf( stderr, "Output Underflow.\n" );
188
 
    return -2;
189
 
 
190
 
error:
191
 
    if( stream ) {
192
 
       Pa_AbortStream( stream );
193
 
       Pa_CloseStream( stream );
194
 
    }
195
 
    free( sampleBlock );
196
 
    Pa_Terminate();
197
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
198
 
    fprintf( stderr, "Error number: %d\n", err );
199
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
200
 
    return -1;
201
 
}
202