Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_start_stop.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_start_stop.c
2 @brief Play a sine wave for several seconds
3 - start and stop the stream multiple times.
4
5 @author Ross Bencina <[email protected]>
6 @author Phil Burk <[email protected]>
7 */
8 /*
9 * $Id: patest_start_stop.c,v 1.1 2006/06/10 21:30:57 dmazzoni 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 * Any person wishing to distribute modifications to the Software is
27 * requested to send the modifications to the original developer so that
28 * they can be incorporated into the canonical version.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
34 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 *
38 */
39 #include <stdio.h>
40 #include <math.h>
41 #include "portaudio.h"
42
43 #define OUTPUT_DEVICE Pa_GetDefaultOutputDevice() /* default output device */
44
45 #define NUM_SECONDS (3)
46 #define NUM_LOOPS (4)
47 #define SAMPLE_RATE (44100)
48 #define FRAMES_PER_BUFFER (400)
49
50 #ifndef M_PI
51 #define M_PI (3.14159265)
52 #endif
53
54 #define TABLE_SIZE (200)
55 typedef struct
56 {
57 float sine[TABLE_SIZE];
58 int left_phase;
59 int right_phase;
60 }
61 paTestData;
62
63 /* This routine will be called by the PortAudio engine when audio is needed.
64 ** It may called at interrupt level on some machines so don't do anything
65 ** that could mess up the system like calling malloc() or free().
66 */
67 static int patestCallback( const void *inputBuffer, void *outputBuffer,
68 unsigned long framesPerBuffer,
69 const PaStreamCallbackTimeInfo* timeInfo,
70 PaStreamCallbackFlags statusFlags,
71 void *userData )
72 {
73 paTestData *data = (paTestData*)userData;
74 float *out = (float*)outputBuffer;
75 unsigned long i;
76
77 (void) timeInfo; /* Prevent unused variable warnings. */
78 (void) statusFlags;
79 (void) inputBuffer;
80
81 for( i=0; i<framesPerBuffer; i++ )
82 {
83 *out++ = data->sine[data->left_phase]; /* left */
84 *out++ = data->sine[data->right_phase]; /* right */
85 data->left_phase += 1;
86 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
87 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
88 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
89 }
90
91 return paContinue;
92 }
93
94 /*******************************************************************/
95 int main(void);
96 int main(void)
97 {
98 PaStreamParameters outputParameters;
99 PaStream *stream;
100 PaError err;
101 paTestData data;
102 int i;
103
104
105 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
106
107 /* initialise sinusoidal wavetable */
108 for( i=0; i<TABLE_SIZE; i++ )
109 {
110 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
111 }
112 data.left_phase = data.right_phase = 0;
113
114 err = Pa_Initialize();
115 if( err != paNoError ) goto error;
116
117 outputParameters.device = OUTPUT_DEVICE;
118 outputParameters.channelCount = 2; /* stereo output */
119 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
120 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
121 outputParameters.hostApiSpecificStreamInfo = NULL;
122
123 err = Pa_OpenStream(
124 &stream,
125 NULL, /* no input */
126 &outputParameters,
127 SAMPLE_RATE,
128 FRAMES_PER_BUFFER,
129 paClipOff, /* we won't output out of range samples so don't bother clipping them */
130 patestCallback,
131 &data );
132 if( err != paNoError ) goto error;
133
134 for( i=0; i<NUM_LOOPS; i++ )
135 {
136 err = Pa_StartStream( stream );
137 if( err != paNoError ) goto error;
138
139 printf("Play for %d seconds.\n", NUM_SECONDS );
140 Pa_Sleep( NUM_SECONDS * 1000 );
141
142 err = Pa_StopStream( stream );
143 if( err != paNoError ) goto error;
144
145 printf("Stopped.\n" );
146 Pa_Sleep( 1000 );
147 }
148
149 err = Pa_CloseStream( stream );
150 if( err != paNoError ) goto error;
151
152 Pa_Terminate();
153 printf("Test finished.\n");
154
155 return err;
156 error:
157 Pa_Terminate();
158 fprintf( stderr, "An error occured while using the portaudio stream\n" );
159 fprintf( stderr, "Error number: %d\n", err );
160 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
161 return err;
162 }