comparison src/portaudio_20140130/examples/paex_sine.c @ 124:e3d5853d5918

Current stable PortAudio source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:11:05 +0100
parents
children
comparison
equal deleted inserted replaced
123:0cef3a1bd1ae 124:e3d5853d5918
1 /** @file paex_sine.c
2 @ingroup examples_src
3 @brief Play a sine wave for several seconds.
4 @author Ross Bencina <rossb@audiomulch.com>
5 @author Phil Burk <philburk@softsynth.com>
6 */
7 /*
8 * $Id: paex_sine.c 1752 2011-09-08 03:21:55Z philburk $
9 *
10 * This program uses the PortAudio Portable Audio Library.
11 * For more information see: http://www.portaudio.com/
12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining
15 * a copy of this software and associated documentation files
16 * (the "Software"), to deal in the Software without restriction,
17 * including without limitation the rights to use, copy, modify, merge,
18 * publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so,
20 * subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be
23 * included in all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 */
33
34 /*
35 * The text above constitutes the entire PortAudio license; however,
36 * the PortAudio community also makes the following non-binding requests:
37 *
38 * Any person wishing to distribute modifications to the Software is
39 * requested to send the modifications to the original developer so that
40 * they can be incorporated into the canonical version. It is also
41 * requested that these non-binding requests be included along with the
42 * license above.
43 */
44 #include <stdio.h>
45 #include <math.h>
46 #include "portaudio.h"
47
48 #define NUM_SECONDS (5)
49 #define SAMPLE_RATE (44100)
50 #define FRAMES_PER_BUFFER (64)
51
52 #ifndef M_PI
53 #define M_PI (3.14159265)
54 #endif
55
56 #define TABLE_SIZE (200)
57 typedef struct
58 {
59 float sine[TABLE_SIZE];
60 int left_phase;
61 int right_phase;
62 char message[20];
63 }
64 paTestData;
65
66 /* This routine will be called by the PortAudio engine when audio is needed.
67 ** It may called at interrupt level on some machines so don't do anything
68 ** that could mess up the system like calling malloc() or free().
69 */
70 static int patestCallback( const void *inputBuffer, void *outputBuffer,
71 unsigned long framesPerBuffer,
72 const PaStreamCallbackTimeInfo* timeInfo,
73 PaStreamCallbackFlags statusFlags,
74 void *userData )
75 {
76 paTestData *data = (paTestData*)userData;
77 float *out = (float*)outputBuffer;
78 unsigned long i;
79
80 (void) timeInfo; /* Prevent unused variable warnings. */
81 (void) statusFlags;
82 (void) inputBuffer;
83
84 for( i=0; i<framesPerBuffer; i++ )
85 {
86 *out++ = data->sine[data->left_phase]; /* left */
87 *out++ = data->sine[data->right_phase]; /* right */
88 data->left_phase += 1;
89 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
90 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
91 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
92 }
93
94 return paContinue;
95 }
96
97 /*
98 * This routine is called by portaudio when playback is done.
99 */
100 static void StreamFinished( void* userData )
101 {
102 paTestData *data = (paTestData *) userData;
103 printf( "Stream Completed: %s\n", data->message );
104 }
105
106 /*******************************************************************/
107 int main(void);
108 int main(void)
109 {
110 PaStreamParameters outputParameters;
111 PaStream *stream;
112 PaError err;
113 paTestData data;
114 int i;
115
116
117 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
118
119 /* initialise sinusoidal wavetable */
120 for( i=0; i<TABLE_SIZE; i++ )
121 {
122 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
123 }
124 data.left_phase = data.right_phase = 0;
125
126 err = Pa_Initialize();
127 if( err != paNoError ) goto error;
128
129 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
130 if (outputParameters.device == paNoDevice) {
131 fprintf(stderr,"Error: No default output device.\n");
132 goto error;
133 }
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(
140 &stream,
141 NULL, /* no input */
142 &outputParameters,
143 SAMPLE_RATE,
144 FRAMES_PER_BUFFER,
145 paClipOff, /* we won't output out of range samples so don't bother clipping them */
146 patestCallback,
147 &data );
148 if( err != paNoError ) goto error;
149
150 sprintf( data.message, "No Message" );
151 err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
152 if( err != paNoError ) goto error;
153
154 err = Pa_StartStream( stream );
155 if( err != paNoError ) goto error;
156
157 printf("Play for %d seconds.\n", NUM_SECONDS );
158 Pa_Sleep( NUM_SECONDS * 1000 );
159
160 err = Pa_StopStream( stream );
161 if( err != paNoError ) goto error;
162
163 err = Pa_CloseStream( stream );
164 if( err != paNoError ) goto error;
165
166 Pa_Terminate();
167 printf("Test finished.\n");
168
169 return err;
170 error:
171 Pa_Terminate();
172 fprintf( stderr, "An error occured while using the portaudio stream\n" );
173 fprintf( stderr, "Error number: %d\n", err );
174 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
175 return err;
176 }