Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_buffer.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /** @file patest_buffer.c
2 @brief Test opening streams with different buffer sizes.
3 @author Phil Burk http://www.softsynth.com
4 */
5 /*
6 * $Id: patest_buffer.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
7 *
8 * This program uses the PortAudio Portable Audio Library.
9 * For more information see: http://www.portaudio.com
10 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files
14 * (the "Software"), to deal in the Software without restriction,
15 * including without limitation the rights to use, copy, modify, merge,
16 * publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so,
18 * subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * Any person wishing to distribute modifications to the Software is
24 * requested to send the modifications to the original developer so that
25 * they can be incorporated into the canonical version.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
31 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 *
35 */
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <math.h>
39 #include "portaudio.h"
40 #define NUM_SECONDS (3)
41 #define SAMPLE_RATE (44100)
42 #ifndef M_PI
43 #define M_PI (3.14159265)
44 #endif
45 #define TABLE_SIZE (200)
46
47 #define BUFFER_TABLE 14
48 long buffer_table[] = {paFramesPerBufferUnspecified,16,32,64,128,200,256,500,512,600,723,1000,1024,2345};
49
50 typedef struct
51 {
52 short sine[TABLE_SIZE];
53 int left_phase;
54 int right_phase;
55 unsigned int sampsToGo;
56 }
57 paTestData;
58 PaError TestOnce( int buffersize, PaDeviceIndex );
59
60 /* This routine will be called by the PortAudio engine when audio is needed.
61 ** It may called at interrupt level on some machines so don't do anything
62 ** that could mess up the system like calling malloc() or free().
63 */
64 static int patest1Callback( const void *inputBuffer, void *outputBuffer,
65 unsigned long framesPerBuffer,
66 const PaStreamCallbackTimeInfo* timeInfo,
67 PaStreamCallbackFlags statusFlags,
68 void *userData )
69 {
70 paTestData *data = (paTestData*)userData;
71 short *out = (short*)outputBuffer;
72 unsigned int i;
73 int finished = 0;
74 (void) inputBuffer; /* Prevent "unused variable" warnings. */
75
76 if( data->sampsToGo < framesPerBuffer )
77 {
78 /* final buffer... */
79
80 for( i=0; i<data->sampsToGo; i++ )
81 {
82 *out++ = data->sine[data->left_phase]; /* left */
83 *out++ = data->sine[data->right_phase]; /* right */
84 data->left_phase += 1;
85 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
86 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
87 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
88 }
89 /* zero remainder of final buffer */
90 for( ; i<framesPerBuffer; i++ )
91 {
92 *out++ = 0; /* left */
93 *out++ = 0; /* right */
94 }
95
96 finished = 1;
97 }
98 else
99 {
100 for( i=0; i<framesPerBuffer; i++ )
101 {
102 *out++ = data->sine[data->left_phase]; /* left */
103 *out++ = data->sine[data->right_phase]; /* right */
104 data->left_phase += 1;
105 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
106 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
107 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
108 }
109 data->sampsToGo -= framesPerBuffer;
110 }
111 return finished;
112 }
113
114 /*******************************************************************/
115 int main(int argc, char **args);
116 int main(int argc, char **args)
117 {
118 int i;
119 int device = -1;
120 PaError err;
121 printf("Test opening streams with different buffer sizes\n");
122 if( argc > 1 ) {
123 device=atoi( args[1] );
124 printf("Using device number %d.\n\n", device );
125 } else {
126 printf("Using default device.\n\n" );
127 }
128
129 for (i = 0 ; i < BUFFER_TABLE; i++)
130 {
131 printf("Buffer size %ld\n", buffer_table[i]);
132 err = TestOnce(buffer_table[i], device);
133 if( err < 0 ) return 0;
134
135 }
136 return 0;
137 }
138
139
140 PaError TestOnce( int buffersize, PaDeviceIndex device )
141 {
142 PaStreamParameters outputParameters;
143 PaStream *stream;
144 PaError err;
145 paTestData data;
146 int i;
147 int totalSamps;
148 /* initialise sinusoidal wavetable */
149 for( i=0; i<TABLE_SIZE; i++ )
150 {
151 data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
152 }
153 data.left_phase = data.right_phase = 0;
154 data.sampsToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
155 err = Pa_Initialize();
156 if( err != paNoError ) goto error;
157
158 if( device == -1 )
159 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
160 else
161 outputParameters.device = device ;
162 outputParameters.channelCount = 2; /* stereo output */
163 outputParameters.sampleFormat = paInt16; /* 32 bit floating point output */
164 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
165 outputParameters.hostApiSpecificStreamInfo = NULL;
166 err = Pa_OpenStream(
167 &stream,
168 NULL, /* no input */
169 &outputParameters,
170 SAMPLE_RATE,
171 buffersize, /* frames per buffer */
172 (paClipOff | paDitherOff),
173 patest1Callback,
174 &data );
175 if( err != paNoError ) goto error;
176
177 err = Pa_StartStream( stream );
178 if( err != paNoError ) goto error;
179 printf("Waiting for sound to finish.\n");
180 Pa_Sleep(1000*NUM_SECONDS);
181 err = Pa_CloseStream( stream );
182 if( err != paNoError ) goto error;
183 Pa_Terminate();
184 return paNoError;
185 error:
186 Pa_Terminate();
187 fprintf( stderr, "An error occured while using the portaudio stream\n" );
188 fprintf( stderr, "Error number: %d\n", err );
189 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
190 fprintf( stderr, "Host Error message: %s\n", Pa_GetLastHostErrorInfo()->errorText );
191 return err;
192 }