Debian Codegraph @

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

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