PortAudio  2.0
paex_read_write_wire.c
Go to the documentation of this file.
1 
8 /*
9  * $Id: patest_read_record.c 757 2004-02-13 07:48:10Z rossbencina $
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 <string.h>
49 #include "portaudio.h"
50 
51 /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
52 #define SAMPLE_RATE (44100)
53 #define FRAMES_PER_BUFFER (1024)
54 #define NUM_CHANNELS (2)
55 #define NUM_SECONDS (15)
56 /* #define DITHER_FLAG (paDitherOff) */
57 #define DITHER_FLAG (0)
58 
59 /* @todo Underflow and overflow is disabled until we fix priming of blocking write. */
60 #define CHECK_OVERFLOW (0)
61 #define CHECK_UNDERFLOW (0)
62 
63 
64 /* Select sample format. */
65 #if 0
66 #define PA_SAMPLE_TYPE paFloat32
67 #define SAMPLE_SIZE (4)
68 #define SAMPLE_SILENCE (0.0f)
69 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
70 #define PRINTF_S_FORMAT "%.8f"
71 #elif 0
72 #define PA_SAMPLE_TYPE paInt16
73 #define SAMPLE_SIZE (2)
74 #define SAMPLE_SILENCE (0)
75 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
76 #define PRINTF_S_FORMAT "%d"
77 #elif 1
78 #define PA_SAMPLE_TYPE paInt24
79 #define SAMPLE_SIZE (3)
80 #define SAMPLE_SILENCE (0)
81 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
82 #define PRINTF_S_FORMAT "%d"
83 #elif 0
84 #define PA_SAMPLE_TYPE paInt8
85 #define SAMPLE_SIZE (1)
86 #define SAMPLE_SILENCE (0)
87 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
88 #define PRINTF_S_FORMAT "%d"
89 #else
90 #define PA_SAMPLE_TYPE paUInt8
91 #define SAMPLE_SIZE (1)
92 #define SAMPLE_SILENCE (128)
93 #define CLEAR( a ) { \
94  int i; \
95  for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ ) \
96  ((unsigned char *)a)[i] = (SAMPLE_SILENCE); \
97 }
98 #define PRINTF_S_FORMAT "%d"
99 #endif
100 
101 
102 /*******************************************************************/
103 int main(void);
104 int main(void)
105 {
106  PaStreamParameters inputParameters, outputParameters;
107  PaStream *stream = NULL;
108  PaError err;
109  char *sampleBlock;
110  int i;
111  int numBytes;
112 
113 
114  printf("patest_read_write_wire.c\n"); fflush(stdout);
115 
116  numBytes = FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE ;
117  sampleBlock = (char *) malloc( numBytes );
118  if( sampleBlock == NULL )
119  {
120  printf("Could not allocate record array.\n");
121  exit(1);
122  }
123  CLEAR( sampleBlock );
124 
125  err = Pa_Initialize();
126  if( err != paNoError ) goto error;
127 
128  inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
129  printf( "Input device # %d.\n", inputParameters.device );
130  printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency );
131  printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency );
132  inputParameters.channelCount = NUM_CHANNELS;
133  inputParameters.sampleFormat = PA_SAMPLE_TYPE;
134  inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ;
135  inputParameters.hostApiSpecificStreamInfo = NULL;
136 
137  outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
138  printf( "Output device # %d.\n", outputParameters.device );
139  printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency );
140  printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency );
141  outputParameters.channelCount = NUM_CHANNELS;
142  outputParameters.sampleFormat = PA_SAMPLE_TYPE;
143  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
144  outputParameters.hostApiSpecificStreamInfo = NULL;
145 
146  /* -- setup -- */
147 
148  err = Pa_OpenStream(
149  &stream,
150  &inputParameters,
151  &outputParameters,
152  SAMPLE_RATE,
153  FRAMES_PER_BUFFER,
154  paClipOff, /* we won't output out of range samples so don't bother clipping them */
155  NULL, /* no callback, use blocking API */
156  NULL ); /* no callback, so no callback userData */
157  if( err != paNoError ) goto error;
158 
159  err = Pa_StartStream( stream );
160  if( err != paNoError ) goto error;
161  printf("Wire on. Will run %d seconds.\n", NUM_SECONDS); fflush(stdout);
162 
163  for( i=0; i<(NUM_SECONDS*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
164  {
165  err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
166  if( err && CHECK_UNDERFLOW ) goto xrun;
167  err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
168  if( err && CHECK_OVERFLOW ) goto xrun;
169  }
170  err = Pa_StopStream( stream );
171  if( err != paNoError ) goto error;
172 
173  CLEAR( sampleBlock );
174 /*
175  err = Pa_StartStream( stream );
176  if( err != paNoError ) goto error;
177  printf("Wire on. Interrupt to stop.\n"); fflush(stdout);
178 
179  while( 1 )
180  {
181  err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
182  if( err ) goto xrun;
183  err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
184  if( err ) goto xrun;
185  }
186  err = Pa_StopStream( stream );
187  if( err != paNoError ) goto error;
188 
189  Pa_CloseStream( stream );
190 */
191  free( sampleBlock );
192 
193  Pa_Terminate();
194  return 0;
195 
196 xrun:
197  if( stream ) {
198  Pa_AbortStream( stream );
199  Pa_CloseStream( stream );
200  }
201  free( sampleBlock );
202  Pa_Terminate();
203  if( err & paInputOverflow )
204  fprintf( stderr, "Input Overflow.\n" );
205  if( err & paOutputUnderflow )
206  fprintf( stderr, "Output Underflow.\n" );
207  return -2;
208 
209 error:
210  if( stream ) {
211  Pa_AbortStream( stream );
212  Pa_CloseStream( stream );
213  }
214  free( sampleBlock );
215  Pa_Terminate();
216  fprintf( stderr, "An error occured while using the portaudio stream\n" );
217  fprintf( stderr, "Error number: %d\n", err );
218  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
219  return -1;
220 }
221 
PaError Pa_Initialize(void)
PaTime defaultHighInputLatency
Definition: portaudio.h:462
PaDeviceIndex Pa_GetDefaultInputDevice(void)
PaError Pa_StopStream(PaStream *stream)
void PaStream
Definition: portaudio.h:584
#define paOutputUnderflow
Definition: portaudio.h:685
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaTime defaultLowInputLatency
Definition: portaudio.h:459
#define paInputOverflow
Definition: portaudio.h:679
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
Definition: portaudio.h:528
The portable PortAudio API.
PaSampleFormat sampleFormat
Definition: portaudio.h:508
#define paClipOff
Definition: portaudio.h:610
int PaError
Definition: portaudio.h:70
PaTime suggestedLatency
Definition: portaudio.h:521
PaError Pa_AbortStream(PaStream *stream)
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
PaDeviceIndex device
Definition: portaudio.h:495
PaError Pa_ReadStream(PaStream *stream, void *buffer, unsigned long frames)
PaError Pa_WriteStream(PaStream *stream, const void *buffer, unsigned long frames)
const char * Pa_GetErrorText(PaError errorCode)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)

Generated for PortAudio by  doxygen1.8.7