| 1 |
/** @file patest1.c |
| 2 |
@brief Ring modulate the audio input with a sine wave for 20 seconds. |
| 3 |
@author Ross Bencina <[email protected]> |
| 4 |
*/ |
| 5 |
/* |
| 6 |
* $Id: patest1.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 7 |
* |
| 8 |
* This program uses the PortAudio Portable Audio Library. |
| 9 |
* For more information see: http://www.portaudio.com |
| 10 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 11 |
* |
| 12 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 13 |
* a copy of this software and associated documentation files |
| 14 |
* (the "Software"), to deal in the Software without restriction, |
| 15 |
* including without limitation the rights to use, copy, modify, merge, |
| 16 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 17 |
* and to permit persons to whom the Software is furnished to do so, |
| 18 |
* subject to the following conditions: |
| 19 |
* |
| 20 |
* The above copyright notice and this permission notice shall be |
| 21 |
* included in all copies or substantial portions of the Software. |
| 22 |
* |
| 23 |
* Any person wishing to distribute modifications to the Software is |
| 24 |
* requested to send the modifications to the original developer so that |
| 25 |
* they can be incorporated into the canonical version. |
| 26 |
* |
| 27 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 30 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 31 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 32 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 33 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 |
* |
| 35 |
*/ |
| 36 |
#include <stdio.h> |
| 37 |
#include <math.h> |
| 38 |
#include "portaudio.h" |
| 39 |
|
| 40 |
#ifndef M_PI |
| 41 |
#define M_PI (3.14159265) |
| 42 |
#endif |
| 43 |
|
| 44 |
#define SAMPLE_RATE (44100) |
| 45 |
|
| 46 |
typedef struct |
| 47 |
{ |
| 48 |
float sine[100]; |
| 49 |
int phase; |
| 50 |
int sampsToGo; |
| 51 |
} |
| 52 |
patest1data; |
| 53 |
|
| 54 |
static int patest1Callback( const void *inputBuffer, void *outputBuffer, |
| 55 |
unsigned long framesPerBuffer, |
| 56 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 57 |
PaStreamCallbackFlags statusFlags, |
| 58 |
void *userData ) |
| 59 |
{ |
| 60 |
patest1data *data = (patest1data*)userData; |
| 61 |
float *in = (float*)inputBuffer; |
| 62 |
float *out = (float*)outputBuffer; |
| 63 |
int framesToCalc = framesPerBuffer; |
| 64 |
unsigned long i = 0; |
| 65 |
int finished; |
| 66 |
|
| 67 |
if( data->sampsToGo < framesPerBuffer ) |
| 68 |
{ |
| 69 |
framesToCalc = data->sampsToGo; |
| 70 |
finished = paComplete; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
finished = paContinue; |
| 75 |
} |
| 76 |
|
| 77 |
for( ; i<framesToCalc; i++ ) |
| 78 |
{ |
| 79 |
*out++ = *in++ * data->sine[data->phase]; /* left */ |
| 80 |
*out++ = *in++ * data->sine[data->phase++]; /* right */ |
| 81 |
if( data->phase >= 100 ) |
| 82 |
data->phase = 0; |
| 83 |
} |
| 84 |
|
| 85 |
data->sampsToGo -= framesToCalc; |
| 86 |
|
| 87 |
/* zero remainder of final buffer if not already done */ |
| 88 |
for( ; i<framesPerBuffer; i++ ) |
| 89 |
{ |
| 90 |
*out++ = 0; /* left */ |
| 91 |
*out++ = 0; /* right */ |
| 92 |
} |
| 93 |
|
| 94 |
return finished; |
| 95 |
} |
| 96 |
|
| 97 |
int main(int argc, char* argv[]); |
| 98 |
int main(int argc, char* argv[]) |
| 99 |
{ |
| 100 |
PaStream *stream; |
| 101 |
PaError err; |
| 102 |
patest1data data; |
| 103 |
int i; |
| 104 |
PaStreamParameters inputParameters, outputParameters; |
| 105 |
const PaHostErrorInfo* herr; |
| 106 |
|
| 107 |
printf("patest1.c\n"); fflush(stdout); |
| 108 |
printf("Ring modulate input for 20 seconds.\n"); fflush(stdout); |
| 109 |
|
| 110 |
/* initialise sinusoidal wavetable */ |
| 111 |
for( i=0; i<100; i++ ) |
| 112 |
data.sine[i] = sin( ((double)i/100.) * M_PI * 2. ); |
| 113 |
data.phase = 0; |
| 114 |
data.sampsToGo = SAMPLE_RATE * 20; /* 20 seconds. */ |
| 115 |
|
| 116 |
/* initialise portaudio subsytem */ |
| 117 |
err = Pa_Initialize(); |
| 118 |
|
| 119 |
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */ |
| 120 |
inputParameters.channelCount = 2; /* stereo input */ |
| 121 |
inputParameters.sampleFormat = paFloat32; /* 32 bit floating point input */ |
| 122 |
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; |
| 123 |
inputParameters.hostApiSpecificStreamInfo = NULL; |
| 124 |
|
| 125 |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ |
| 126 |
outputParameters.channelCount = 2; /* stereo output */ |
| 127 |
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ |
| 128 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 129 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 130 |
|
| 131 |
err = Pa_OpenStream( |
| 132 |
&stream, |
| 133 |
&inputParameters, |
| 134 |
&outputParameters, |
| 135 |
(double)SAMPLE_RATE, /* Samplerate in Hertz. */ |
| 136 |
512, /* Small buffers */ |
| 137 |
paClipOff, /* We won't output out of range samples so don't bother clipping them. */ |
| 138 |
patest1Callback, |
| 139 |
&data ); |
| 140 |
if( err != paNoError ) goto done; |
| 141 |
|
| 142 |
err = Pa_StartStream( stream ); |
| 143 |
if( err != paNoError ) goto done; |
| 144 |
|
| 145 |
printf( "Press any key to end.\n" ); fflush(stdout); |
| 146 |
|
| 147 |
getc( stdin ); /* wait for input before exiting */ |
| 148 |
|
| 149 |
err = Pa_AbortStream( stream ); |
| 150 |
if( err != paNoError ) goto done; |
| 151 |
|
| 152 |
printf( "Waiting for stream to complete...\n" ); |
| 153 |
|
| 154 |
/* sleep until playback has finished */ |
| 155 |
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(1000); |
| 156 |
if( err < 0 ) goto done; |
| 157 |
|
| 158 |
err = Pa_CloseStream( stream ); |
| 159 |
if( err != paNoError ) goto done; |
| 160 |
|
| 161 |
done: |
| 162 |
Pa_Terminate(); |
| 163 |
|
| 164 |
if( err != paNoError ) |
| 165 |
{ |
| 166 |
fprintf( stderr, "An error occured while using portaudio\n" ); |
| 167 |
if( err == paUnanticipatedHostError ) |
| 168 |
{ |
| 169 |
fprintf( stderr, " unanticipated host error.\n"); |
| 170 |
herr = Pa_GetLastHostErrorInfo(); |
| 171 |
if (herr) |
| 172 |
{ |
| 173 |
fprintf( stderr, " Error number: %ld\n", herr->errorCode ); |
| 174 |
if (herr->errorText) |
| 175 |
fprintf( stderr, " Error text: %s\n", herr->errorText ); |
| 176 |
} |
| 177 |
else |
| 178 |
fprintf( stderr, " Pa_GetLastHostErrorInfo() failed!\n" ); |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
fprintf( stderr, " Error number: %d\n", err ); |
| 183 |
fprintf( stderr, " Error text: %s\n", Pa_GetErrorText( err ) ); |
| 184 |
} |
| 185 |
|
| 186 |
err = 1; /* Always return 0 or 1, but no other return codes. */ |
| 187 |
} |
| 188 |
|
| 189 |
printf( "bye\n" ); |
| 190 |
|
| 191 |
return err; |
| 192 |
} |