1
/** @file patest_read_write_wire.c
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
9
* $Id: patest_read_write_wire.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
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
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:
23
* The above copyright notice and this permission notice shall be
24
* included in all copies or substantial portions of the Software.
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.
36
* The text above constitutes the entire PortAudio license; however,
37
* the PortAudio community also makes the following non-binding requests:
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
48
#include "portaudio.h"
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) /**/
57
/* Select sample format. */
59
#define PA_SAMPLE_TYPE paFloat32
61
#define SAMPLE_SILENCE (0.0f)
62
#define PRINTF_S_FORMAT "%.8f"
64
#define PA_SAMPLE_TYPE paInt16
66
#define SAMPLE_SILENCE (0)
67
#define PRINTF_S_FORMAT "%d"
69
#define PA_SAMPLE_TYPE paInt8
71
#define SAMPLE_SILENCE (0)
72
#define PRINTF_S_FORMAT "%d"
74
#define PA_SAMPLE_TYPE paUInt8
75
typedef unsigned char SAMPLE;
76
#define SAMPLE_SILENCE (128)
77
#define PRINTF_S_FORMAT "%d"
81
/*******************************************************************/
85
PaStreamParameters inputParameters, outputParameters;
86
PaStream *stream = NULL;
93
printf("patest_read_write_wire.c\n"); fflush(stdout);
95
numBytes = FRAMES_PER_BUFFER * NUM_CHANNELS * sizeof(SAMPLE);
96
sampleBlock = (SAMPLE *) malloc( numBytes );
97
if( sampleBlock == NULL )
99
printf("Could not allocate record array.\n");
102
for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ )
103
sampleBlock[i] = (SAMPLE_SILENCE);
105
err = Pa_Initialize();
106
if( err != paNoError ) goto error;
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;
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;
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;
139
err = Pa_StartStream( stream );
140
if( err != paNoError ) goto error;
141
printf("Wire on. Will run one minute.\n"); fflush(stdout);
143
for( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
145
err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
147
err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
150
err = Pa_StopStream( stream );
151
if( err != paNoError ) goto error;
153
for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ )
154
sampleBlock[i] = (SAMPLE_SILENCE);
156
err = Pa_StartStream( stream );
157
if( err != paNoError ) goto error;
158
printf("Wire on. Interrupt to stop.\n"); fflush(stdout);
162
err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
164
err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
167
err = Pa_StopStream( stream );
168
if( err != paNoError ) goto error;
170
Pa_CloseStream( stream );
179
Pa_AbortStream( stream );
180
Pa_CloseStream( stream );
184
if( err & paInputOverflow )
185
fprintf( stderr, "Input Overflow.\n" );
186
if( err & paOutputUnderflow )
187
fprintf( stderr, "Output Underflow.\n" );
192
Pa_AbortStream( stream );
193
Pa_CloseStream( stream );
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 ) );