3
@brief Ring modulate the audio input with a sine wave for 20 seconds.
4
@author Ross Bencina <rossb@audiomulch.com>
7
* $Id: patest1.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
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
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:
21
* The above copyright notice and this permission notice shall be
22
* included in all copies or substantial portions of the Software.
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.
34
* The text above constitutes the entire PortAudio license; however,
35
* the PortAudio community also makes the following non-binding requests:
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
46
#include "portaudio.h"
49
#define M_PI (3.14159265)
52
#define SAMPLE_RATE (44100)
62
static int patest1Callback( const void *inputBuffer, void *outputBuffer,
63
unsigned long framesPerBuffer,
64
const PaStreamCallbackTimeInfo* timeInfo,
65
PaStreamCallbackFlags statusFlags,
68
patest1data *data = (patest1data*)userData;
69
float *in = (float*)inputBuffer;
70
float *out = (float*)outputBuffer;
71
int framesToCalc = framesPerBuffer;
75
if( data->sampsToGo < framesPerBuffer )
77
framesToCalc = data->sampsToGo;
78
finished = paComplete;
82
finished = paContinue;
85
for( ; i<framesToCalc; i++ )
87
*out++ = *in++ * data->sine[data->phase]; /* left */
88
*out++ = *in++ * data->sine[data->phase++]; /* right */
89
if( data->phase >= 100 )
93
data->sampsToGo -= framesToCalc;
95
/* zero remainder of final buffer if not already done */
96
for( ; i<framesPerBuffer; i++ )
98
*out++ = 0; /* left */
99
*out++ = 0; /* right */
105
int main(int argc, char* argv[]);
106
int main(int argc, char* argv[])
112
PaStreamParameters inputParameters, outputParameters;
113
const PaHostErrorInfo* herr;
115
printf("patest1.c\n"); fflush(stdout);
116
printf("Ring modulate input for 20 seconds.\n"); fflush(stdout);
118
/* initialise sinusoidal wavetable */
119
for( i=0; i<100; i++ )
120
data.sine[i] = sin( ((double)i/100.) * M_PI * 2. );
122
data.sampsToGo = SAMPLE_RATE * 20; /* 20 seconds. */
124
/* initialise portaudio subsytem */
125
err = Pa_Initialize();
127
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
128
inputParameters.channelCount = 2; /* stereo input */
129
inputParameters.sampleFormat = paFloat32; /* 32 bit floating point input */
130
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
131
inputParameters.hostApiSpecificStreamInfo = NULL;
133
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
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;
143
(double)SAMPLE_RATE, /* Samplerate in Hertz. */
144
512, /* Small buffers */
145
paClipOff, /* We won't output out of range samples so don't bother clipping them. */
148
if( err != paNoError ) goto done;
150
err = Pa_StartStream( stream );
151
if( err != paNoError ) goto done;
153
printf( "Press any key to end.\n" ); fflush(stdout);
155
getc( stdin ); /* wait for input before exiting */
157
err = Pa_AbortStream( stream );
158
if( err != paNoError ) goto done;
160
printf( "Waiting for stream to complete...\n" );
162
/* sleep until playback has finished */
163
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(1000);
164
if( err < 0 ) goto done;
166
err = Pa_CloseStream( stream );
167
if( err != paNoError ) goto done;
172
if( err != paNoError )
174
fprintf( stderr, "An error occured while using portaudio\n" );
175
if( err == paUnanticipatedHostError )
177
fprintf( stderr, " unanticipated host error.\n");
178
herr = Pa_GetLastHostErrorInfo();
181
fprintf( stderr, " Error number: %ld\n", herr->errorCode );
183
fprintf( stderr, " Error text: %s\n", herr->errorText );
186
fprintf( stderr, " Pa_GetLastHostErrorInfo() failed!\n" );
190
fprintf( stderr, " Error number: %d\n", err );
191
fprintf( stderr, " Error text: %s\n", Pa_GetErrorText( err ) );
194
err = 1; /* Always return 0 or 1, but no other return codes. */