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