Mercurial > hg > sv-dependency-builds
comparison src/portaudio_20161030/test/patest_sine8.c @ 140:59a8758c56b1
Add source for PortAudio stable v190600_20161030
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Tue, 03 Jan 2017 13:44:07 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
139:413e081fcc6f | 140:59a8758c56b1 |
---|---|
1 /** @file patest_sine8.c | |
2 @ingroup test_src | |
3 @brief Test 8 bit data: play a sine wave for several seconds. | |
4 @author Ross Bencina <rossb@audiomulch.com> | |
5 */ | |
6 /* | |
7 * $Id$ | |
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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR | |
28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
31 */ | |
32 | |
33 /* | |
34 * The text above constitutes the entire PortAudio license; however, | |
35 * the PortAudio community also makes the following non-binding requests: | |
36 * | |
37 * Any person wishing to distribute modifications to the Software is | |
38 * requested to send the modifications to the original developer so that | |
39 * they can be incorporated into the canonical version. It is also | |
40 * requested that these non-binding requests be included along with the | |
41 * license above. | |
42 */ | |
43 | |
44 #include <stdio.h> | |
45 #include <math.h> | |
46 #include "portaudio.h" | |
47 | |
48 #define NUM_SECONDS (8) | |
49 #define SAMPLE_RATE (44100) | |
50 #define TABLE_SIZE (200) | |
51 #define TEST_UNSIGNED (0) | |
52 | |
53 #if TEST_UNSIGNED | |
54 #define TEST_FORMAT paUInt8 | |
55 typedef unsigned char sample_t; | |
56 #define SILENCE ((sample_t)0x80) | |
57 #else | |
58 #define TEST_FORMAT paInt8 | |
59 typedef char sample_t; | |
60 #define SILENCE ((sample_t)0x00) | |
61 #endif | |
62 | |
63 #ifndef M_PI | |
64 #define M_PI (3.14159265) | |
65 #endif | |
66 | |
67 typedef struct | |
68 { | |
69 sample_t sine[TABLE_SIZE]; | |
70 int left_phase; | |
71 int right_phase; | |
72 unsigned int framesToGo; | |
73 } | |
74 paTestData; | |
75 | |
76 /* This routine will be called by the PortAudio engine when audio is needed. | |
77 ** It may called at interrupt level on some machines so don't do anything | |
78 ** that could mess up the system like calling malloc() or free(). | |
79 */ | |
80 static int patestCallback( const void *inputBuffer, void *outputBuffer, | |
81 unsigned long framesPerBuffer, | |
82 const PaStreamCallbackTimeInfo* timeInfo, | |
83 PaStreamCallbackFlags statusFlags, | |
84 void *userData ) | |
85 { | |
86 paTestData *data = (paTestData*)userData; | |
87 sample_t *out = (sample_t*)outputBuffer; | |
88 int i; | |
89 int framesToCalc; | |
90 int finished = 0; | |
91 (void) inputBuffer; /* Prevent unused variable warnings. */ | |
92 | |
93 if( data->framesToGo < framesPerBuffer ) | |
94 { | |
95 framesToCalc = data->framesToGo; | |
96 data->framesToGo = 0; | |
97 finished = 1; | |
98 } | |
99 else | |
100 { | |
101 framesToCalc = framesPerBuffer; | |
102 data->framesToGo -= framesPerBuffer; | |
103 } | |
104 | |
105 for( i=0; i<framesToCalc; i++ ) | |
106 { | |
107 *out++ = data->sine[data->left_phase]; /* left */ | |
108 *out++ = data->sine[data->right_phase]; /* right */ | |
109 data->left_phase += 1; | |
110 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; | |
111 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ | |
112 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; | |
113 } | |
114 /* zero remainder of final buffer */ | |
115 for( ; i<(int)framesPerBuffer; i++ ) | |
116 { | |
117 *out++ = SILENCE; /* left */ | |
118 *out++ = SILENCE; /* right */ | |
119 } | |
120 return finished; | |
121 } | |
122 | |
123 /*******************************************************************/ | |
124 int main(void); | |
125 int main(void) | |
126 { | |
127 PaStreamParameters outputParameters; | |
128 PaStream* stream; | |
129 PaError err; | |
130 paTestData data; | |
131 PaTime streamOpened; | |
132 int i, totalSamps; | |
133 | |
134 #if TEST_UNSIGNED | |
135 printf("PortAudio Test: output UNsigned 8 bit sine wave.\n"); | |
136 #else | |
137 printf("PortAudio Test: output signed 8 bit sine wave.\n"); | |
138 #endif | |
139 /* initialise sinusoidal wavetable */ | |
140 for( i=0; i<TABLE_SIZE; i++ ) | |
141 { | |
142 data.sine[i] = SILENCE + (char) (127.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. )); | |
143 } | |
144 data.left_phase = data.right_phase = 0; | |
145 data.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */ | |
146 | |
147 err = Pa_Initialize(); | |
148 if( err != paNoError ) | |
149 goto error; | |
150 | |
151 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */ | |
152 if (outputParameters.device == paNoDevice) { | |
153 fprintf(stderr,"Error: No default output device.\n"); | |
154 goto error; | |
155 } | |
156 outputParameters.channelCount = 2; /* Stereo output. */ | |
157 outputParameters.sampleFormat = TEST_FORMAT; | |
158 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; | |
159 outputParameters.hostApiSpecificStreamInfo = NULL; | |
160 err = Pa_OpenStream( &stream, | |
161 NULL, /* No input. */ | |
162 &outputParameters, | |
163 SAMPLE_RATE, | |
164 256, /* Frames per buffer. */ | |
165 paClipOff, /* We won't output out of range samples so don't bother clipping them. */ | |
166 patestCallback, | |
167 &data ); | |
168 if( err != paNoError ) | |
169 goto error; | |
170 | |
171 streamOpened = Pa_GetStreamTime( stream ); /* Time in seconds when stream was opened (approx). */ | |
172 | |
173 err = Pa_StartStream( stream ); | |
174 if( err != paNoError ) | |
175 goto error; | |
176 | |
177 /* Watch until sound is halfway finished. */ | |
178 /* (Was ( Pa_StreamTime( stream ) < (totalSamps/2) ) in V18. */ | |
179 while( (Pa_GetStreamTime( stream ) - streamOpened) < (PaTime)NUM_SECONDS / 2.0 ) | |
180 Pa_Sleep(10); | |
181 | |
182 /* Stop sound. */ | |
183 printf("Stopping Stream.\n"); | |
184 err = Pa_StopStream( stream ); | |
185 if( err != paNoError ) | |
186 goto error; | |
187 | |
188 printf("Pause for 2 seconds.\n"); | |
189 Pa_Sleep( 2000 ); | |
190 | |
191 printf("Starting again.\n"); | |
192 err = Pa_StartStream( stream ); | |
193 if( err != paNoError ) | |
194 goto error; | |
195 | |
196 printf("Waiting for sound to finish.\n"); | |
197 | |
198 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) | |
199 Pa_Sleep(100); | |
200 if( err < 0 ) | |
201 goto error; | |
202 | |
203 err = Pa_CloseStream( stream ); | |
204 if( err != paNoError ) | |
205 goto error; | |
206 | |
207 Pa_Terminate(); | |
208 printf("Test finished.\n"); | |
209 return err; | |
210 error: | |
211 Pa_Terminate(); | |
212 fprintf( stderr, "An error occured while using the portaudio stream\n" ); | |
213 fprintf( stderr, "Error number: %d\n", err ); | |
214 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); | |
215 return err; | |
216 } |