comparison src/portaudio/examples/paex_write_sine_nonint.c @ 4:e13257ea84a4

Add bzip2, zlib, liblo, portaudio sources
author Chris Cannam
date Wed, 20 Mar 2013 13:59:52 +0000
parents
children
comparison
equal deleted inserted replaced
3:6c505a35919a 4:e13257ea84a4
1 /** @file paex_write_sine_nonint.c
2 @ingroup examples_src
3 @brief Play a non-interleaved sine wave using the blocking API (Pa_WriteStream())
4 @author Ross Bencina <rossb@audiomulch.com>
5 @author Phil Burk <philburk@softsynth.com>
6 */
7 /*
8 * $Id: patest_write_sine.c 1368 2008-03-01 00:38:27Z rossb $
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
45 #include <stdio.h>
46 #include <math.h>
47 #include "portaudio.h"
48
49 #define NUM_SECONDS (5)
50 #define SAMPLE_RATE (44100)
51 #define FRAMES_PER_BUFFER (1024)
52
53 #ifndef M_PI
54 #define M_PI (3.14159265)
55 #endif
56
57 #define TABLE_SIZE (200)
58
59
60 int main(void);
61 int main(void)
62 {
63 PaStreamParameters outputParameters;
64 PaStream *stream;
65 PaError err;
66
67 float leftBuffer[FRAMES_PER_BUFFER];
68 float rightBuffer[FRAMES_PER_BUFFER];
69 void *buffers[2]; /* points to both non-interleaved buffers. */
70
71 float sine[TABLE_SIZE]; /* sine wavetable */
72 int left_phase = 0;
73 int right_phase = 0;
74 int left_inc = 1;
75 int right_inc = 3; /* higher pitch so we can distinguish left and right. */
76 int i, j, k;
77 int bufferCount;
78
79
80 printf("PortAudio Test: output sine wave NON-INTERLEAVED. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
81
82 /* initialise sinusoidal wavetable */
83 for( i=0; i<TABLE_SIZE; i++ )
84 {
85 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
86 }
87
88
89 err = Pa_Initialize();
90 if( err != paNoError ) goto error;
91
92 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
93 if (outputParameters.device == paNoDevice) {
94 fprintf(stderr,"Error: No default output device.\n");
95 goto error;
96 }
97 outputParameters.channelCount = 2; /* stereo output */
98 outputParameters.sampleFormat = paFloat32 | paNonInterleaved; /* 32 bit floating point output NON-INTERLEAVED */
99 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
100 outputParameters.hostApiSpecificStreamInfo = NULL;
101
102 err = Pa_OpenStream(
103 &stream,
104 NULL, /* no input */
105 &outputParameters,
106 SAMPLE_RATE,
107 FRAMES_PER_BUFFER,
108 paClipOff, /* we won't output out of range samples so don't bother clipping them */
109 NULL, /* no callback, use blocking API */
110 NULL ); /* no callback, so no callback userData */
111 if( err != paNoError ) goto error;
112
113
114 printf( "Play 3 times, higher each time.\n" );
115
116 /* Set up array of buffer pointers for Pa_WriteStream */
117 buffers[0] = leftBuffer;
118 buffers[1] = rightBuffer;
119
120 for( k=0; k < 3; ++k )
121 {
122 err = Pa_StartStream( stream );
123 if( err != paNoError ) goto error;
124
125 printf("Play for %d seconds.\n", NUM_SECONDS );
126
127 bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
128
129 for( i=0; i < bufferCount; i++ )
130 {
131 for( j=0; j < FRAMES_PER_BUFFER; j++ )
132 {
133 leftBuffer[j] = sine[left_phase]; /* left */
134 rightBuffer[j] = sine[right_phase]; /* right */
135 left_phase += left_inc;
136 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
137 right_phase += right_inc;
138 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
139 }
140
141 err = Pa_WriteStream( stream, buffers, FRAMES_PER_BUFFER );
142 if( err != paNoError ) goto error;
143 }
144
145 err = Pa_StopStream( stream );
146 if( err != paNoError ) goto error;
147
148 ++left_inc;
149 ++right_inc;
150
151 Pa_Sleep( 1000 );
152 }
153
154 err = Pa_CloseStream( stream );
155 if( err != paNoError ) goto error;
156
157 Pa_Terminate();
158 printf("Test finished.\n");
159
160 return err;
161 error:
162 Pa_Terminate();
163 fprintf( stderr, "An error occured while using the portaudio stream\n" );
164 fprintf( stderr, "Error number: %d\n", err );
165 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
166 return err;
167 }