Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /** @file patest_longsine.c
2 @brief Play a sine wave until ENTER hit.
3 @author Phil Burk http://www.softsynth.com
4 */
5 /*
6 * $Id: patest_longsine.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
37 #include <stdio.h>
38 #include <math.h>
39
40 #include "portaudio.h"
41
42 #define SAMPLE_RATE (44100)
43
44 #ifndef M_PI
45 #define M_PI (3.14159265)
46 #endif
47
48 #define TABLE_SIZE (200)
49 typedef struct
50 {
51 float sine[TABLE_SIZE];
52 int left_phase;
53 int right_phase;
54 }
55 paTestData;
56
57 /* This routine will be called by the PortAudio engine when audio is needed.
58 ** It may called at interrupt level on some machines so don't do anything
59 ** that could mess up the system like calling malloc() or free().
60 */
61 static int patestCallback(const void* inputBuffer,
62 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 int i;
71 (void) inputBuffer; /* Prevent unused argument warning. */
72 for( i=0; i<framesPerBuffer; i++ )
73 {
74 *out++ = data->sine[data->left_phase]; /* left */
75 *out++ = data->sine[data->right_phase]; /* right */
76 data->left_phase += 1;
77 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
78 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
79 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
80 }
81 return 0;
82 }
83
84 /*******************************************************************/
85 int main(void);
86 int main(void)
87 {
88 PaStreamParameters outputParameters;
89 PaStream *stream;
90 PaError err;
91 paTestData data;
92 int i;
93 printf("PortAudio Test: output sine wave.\n");
94
95 /* initialise sinusoidal wavetable */
96 for( i=0; i<TABLE_SIZE; i++ )
97 {
98 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
99 }
100 data.left_phase = data.right_phase = 0;
101
102 err = Pa_Initialize();
103 if( err != paNoError ) goto error;
104
105 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
106 outputParameters.channelCount = 2; /* stereo output */
107 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
108 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
109 outputParameters.hostApiSpecificStreamInfo = NULL;
110
111 err = Pa_OpenStream( &stream,
112 NULL, /* No input. */
113 &outputParameters, /* As above. */
114 SAMPLE_RATE,
115 256, /* Frames per buffer. */
116 paClipOff, /* No out of range samples expected. */
117 patestCallback,
118 &data );
119 if( err != paNoError ) goto error;
120
121 err = Pa_StartStream( stream );
122 if( err != paNoError ) goto error;
123
124 printf("Hit ENTER to stop program.\n");
125 getchar();
126
127 err = Pa_CloseStream( stream );
128 if( err != paNoError ) goto error;
129 Pa_Terminate();
130
131 printf("Test finished.\n");
132 return err;
133
134 error:
135 Pa_Terminate();
•136 fprintf( stderr, "An error occurred while using the portaudio stream\n" );
137 fprintf( stderr, "Error number: %d\n", err );
138 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
139 return err;
140 }