Debian Codegraph @

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

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /** @file patest_write_sine.c
2 @brief Play a sine wave for several seconds using the blocking API (Pa_WriteStream())
3 @author Ross Bencina <[email protected]>
4 @author Phil Burk <[email protected]>
5 */
6 /*
7 * $Id: patest_write_sine.c,v 1.1 2006/06/10 21:30:57 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 (1024)
44
45 #ifndef M_PI
46 #define M_PI (3.14159265)
47 #endif
48
49 #define TABLE_SIZE (200)
50
51
52 int main(void);
53 int main(void)
54 {
55 PaStreamParameters outputParameters;
56 PaStream *stream;
57 PaError err;
58 float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
59 float sine[TABLE_SIZE]; /* sine wavetable */
60 int left_phase = 0;
61 int right_phase = 0;
62 int left_inc = 1;
63 int right_inc = 3; /* higher pitch so we can distinguish left and right. */
64 int i, j, k;
65 int bufferCount;
66
67
68 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
69
70 /* initialise sinusoidal wavetable */
71 for( i=0; i<TABLE_SIZE; i++ )
72 {
73 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
74 }
75
76
77 err = Pa_Initialize();
78 if( err != paNoError ) goto error;
79
80 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
81 outputParameters.channelCount = 2; /* stereo output */
82 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
83 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
84 outputParameters.hostApiSpecificStreamInfo = NULL;
85
86 err = Pa_OpenStream(
87 &stream,
88 NULL, /* no input */
89 &outputParameters,
90 SAMPLE_RATE,
91 FRAMES_PER_BUFFER,
92 paClipOff, /* we won't output out of range samples so don't bother clipping them */
93 NULL, /* no callback, use blocking API */
94 NULL ); /* no callback, so no callback userData */
95 if( err != paNoError ) goto error;
96
97
98 printf( "Play 3 times, higher each time.\n" );
99
100 for( k=0; k < 3; ++k )
101 {
102 err = Pa_StartStream( stream );
103 if( err != paNoError ) goto error;
104
105 printf("Play for %d seconds.\n", NUM_SECONDS );
106
107 bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
108
109 for( i=0; i < bufferCount; i++ )
110 {
111 for( j=0; j < FRAMES_PER_BUFFER; j++ )
112 {
113 buffer[j][0] = sine[left_phase]; /* left */
114 buffer[j][1] = sine[right_phase]; /* right */
115 left_phase += left_inc;
116 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
117 right_phase += right_inc;
118 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
119 }
120
121 err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
122 if( err != paNoError ) goto error;
123 }
124
125 err = Pa_StopStream( stream );
126 if( err != paNoError ) goto error;
127
128 ++left_inc;
129 ++right_inc;
130
131 Pa_Sleep( 1000 );
132 }
133
134 err = Pa_CloseStream( stream );
135 if( err != paNoError ) goto error;
136
137 Pa_Terminate();
138 printf("Test finished.\n");
139
140 return err;
141 error:
142 Pa_Terminate();
143 fprintf( stderr, "An error occured while using the portaudio stream\n" );
144 fprintf( stderr, "Error number: %d\n", err );
145 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
146 return err;
147 }