Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_leftright.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_leftright.c
2 @brief Play different tone sine waves that
3 alternate between left and right channel.
4
5 The low tone should be on the left channel.
6
7 @author Ross Bencina <[email protected]>
8 @author Phil Burk <[email protected]>
9 */
10 /*
11 * $Id: patest_leftright.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
12 *
13 * This program uses the PortAudio Portable Audio Library.
14 * For more information see: http://www.audiomulch.com/portaudio/
15 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining
18 * a copy of this software and associated documentation files
19 * (the "Software"), to deal in the Software without restriction,
20 * including without limitation the rights to use, copy, modify, merge,
21 * publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so,
23 * subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be
26 * included in all copies or substantial portions of the Software.
27 *
28 * Any person wishing to distribute modifications to the Software is
29 * requested to send the modifications to the original developer so that
30 * they can be incorporated into the canonical version.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
35 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
36 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
37 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 *
40 */
41 #include <stdio.h>
42 #include <math.h>
43 #include "portaudio.h"
44
45 #define NUM_SECONDS (8)
46 #define SAMPLE_RATE (44100)
47 #define FRAMES_PER_BUFFER (512)
48 #ifndef M_PI
49 #define M_PI (3.14159265)
50 #endif
51 #define TABLE_SIZE (200)
52 typedef struct
53 {
54 float sine[TABLE_SIZE];
55 int left_phase;
56 int right_phase;
57 int toggle;
58 int countDown;
59 }
60 paTestData;
61 /* This routine will be called by the PortAudio engine when audio is needed.
62 ** It may called at interrupt level on some machines so don't do anything
63 ** that could mess up the system like calling malloc() or free().
64 */
65 static int patestCallback( const void *inputBuffer,
66 void *outputBuffer,
67 unsigned long framesPerBuffer,
68 const PaStreamCallbackTimeInfo* timeInfo,
69 PaStreamCallbackFlags statusFlags,
70 void *userData )
71 {
72 paTestData *data = (paTestData*)userData;
73 float *out = (float*)outputBuffer;
74 unsigned long i;
75 int finished = 0;
76 /* Prevent unused variable warnings. */
77 (void) inputBuffer;
78
79 for( i=0; i<framesPerBuffer; i++ )
80 {
81 if( data->toggle )
82 {
83 *out++ = data->sine[data->left_phase]; /* left */
84 *out++ = 0; /* right */
85 }
86 else
87 {
88 *out++ = 0; /* left */
89 *out++ = data->sine[data->right_phase]; /* right */
90 }
91
92 data->left_phase += 1;
93 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
94 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
95 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
96 }
97
98 if( data->countDown < 0 )
99 {
100 data->countDown = SAMPLE_RATE;
101 data->toggle = !data->toggle;
102 }
103 data->countDown -= framesPerBuffer;
104
105 return finished;
106 }
107
108 /*******************************************************************/
109 int main(void);
110 int main(void)
111 {
112 PaStream *stream;
113 PaStreamParameters outputParameters;
114 PaError err;
115 paTestData data;
116 int i;
117 int timeout;
118
119 printf("Play different tone sine waves that alternate between left and right channel.\n");
120 printf("The low tone should be on the left channel.\n");
121
122 /* initialise sinusoidal wavetable */
123 for( i=0; i<TABLE_SIZE; i++ )
124 {
125 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
126 }
127 data.left_phase = data.right_phase = data.toggle = 0;
128 data.countDown = SAMPLE_RATE;
129
130 err = Pa_Initialize();
131 if( err != paNoError ) goto error;
132
133 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
134 outputParameters.channelCount = 2; /* stereo output */
135 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
136 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
137 outputParameters.hostApiSpecificStreamInfo = NULL;
138
139 err = Pa_OpenStream( &stream,
140 NULL, /* No input. */
141 &outputParameters, /* As above. */
142 SAMPLE_RATE,
143 FRAMES_PER_BUFFER,
144 paClipOff, /* we won't output out of range samples so don't bother clipping them */
145 patestCallback,
146 &data );
147 if( err != paNoError ) goto error;
148
149 err = Pa_StartStream( stream );
150 if( err != paNoError ) goto error;
151
152 printf("Play for several seconds.\n");
153 timeout = NUM_SECONDS * 4;
154 while( timeout > 0 )
155 {
156 Pa_Sleep( 300 ); /*(Irix very much likes sleeps <= 1000 ms.)*/
157 timeout -= 1;
158 }
159
160 err = Pa_StopStream( stream );
161 if( err != paNoError ) goto error;
162 err = Pa_CloseStream( stream );
163 if( err != paNoError ) goto error;
164 Pa_Terminate();
165 printf("Test finished.\n");
166 return err;
167 error:
168 Pa_Terminate();
169 fprintf( stderr, "An error occured while using the portaudio stream\n" );
170 fprintf( stderr, "Error number: %d\n", err );
171 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
172 return err;
173 }