| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <math.h> |
| 38 |
#include "portaudio.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#define SAMPLE_RATE (44100) |
| 45 |
#define PA_SAMPLE_TYPE paFloat32 |
| 46 |
#define FRAMES_PER_BUFFER (64) |
| 47 |
|
| 48 |
typedef float SAMPLE; |
| 49 |
|
| 50 |
float CubicAmplifier( float input ); |
| 51 |
static int fuzzCallback( const void *inputBuffer, void *outputBuffer, |
| 52 |
unsigned long framesPerBuffer, |
| 53 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 54 |
PaStreamCallbackFlags statusFlags, |
| 55 |
void *userData ); |
| 56 |
|
| 57 |
|
| 58 |
float CubicAmplifier( float input ) |
| 59 |
{ |
| 60 |
float output, temp; |
| 61 |
if( input < 0.0 ) |
| 62 |
{ |
| 63 |
temp = input + 1.0f; |
| 64 |
output = (temp * temp * temp) - 1.0f; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
temp = input - 1.0f; |
| 69 |
output = (temp * temp * temp) + 1.0f; |
| 70 |
} |
| 71 |
|
| 72 |
return output; |
| 73 |
} |
| 74 |
#define FUZZ(x) CubicAmplifier(CubicAmplifier(CubicAmplifier(CubicAmplifier(x)))) |
| 75 |
|
| 76 |
static int gNumNoInputs = 0; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static int fuzzCallback( const void *inputBuffer, void *outputBuffer, |
| 82 |
unsigned long framesPerBuffer, |
| 83 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 84 |
PaStreamCallbackFlags statusFlags, |
| 85 |
void *userData ) |
| 86 |
{ |
| 87 |
SAMPLE *out = (SAMPLE*)outputBuffer; |
| 88 |
const SAMPLE *in = (const SAMPLE*)inputBuffer; |
| 89 |
unsigned int i; |
| 90 |
(void) timeInfo; |
| 91 |
(void) statusFlags; |
| 92 |
(void) userData; |
| 93 |
|
| 94 |
if( inputBuffer == NULL ) |
| 95 |
{ |
| 96 |
for( i=0; i<framesPerBuffer; i++ ) |
| 97 |
{ |
| 98 |
*out++ = 0; |
| 99 |
*out++ = 0; |
| 100 |
} |
| 101 |
gNumNoInputs += 1; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
for( i=0; i<framesPerBuffer; i++ ) |
| 106 |
{ |
| 107 |
*out++ = FUZZ(*in++); |
| 108 |
*out++ = *in++; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return paContinue; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
int main(void); |
| 117 |
int main(void) |
| 118 |
{ |
| 119 |
PaStreamParameters inputParameters, outputParameters; |
| 120 |
PaStream *stream; |
| 121 |
PaError err; |
| 122 |
|
| 123 |
err = Pa_Initialize(); |
| 124 |
if( err != paNoError ) goto error; |
| 125 |
|
| 126 |
|
| 127 |
inputParameters.device = Pa_GetDefaultInputDevice(); |
| 128 |
inputParameters.channelCount = 2; |
| 129 |
inputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 130 |
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; |
| 131 |
inputParameters.hostApiSpecificStreamInfo = NULL; |
| 132 |
|
| 133 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 134 |
outputParameters.channelCount = 2; |
| 135 |
outputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 136 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 137 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 138 |
|
| 139 |
err = Pa_OpenStream( |
| 140 |
&stream, |
| 141 |
&inputParameters, |
| 142 |
&outputParameters, |
| 143 |
SAMPLE_RATE, |
| 144 |
FRAMES_PER_BUFFER, |
| 145 |
0, |
| 146 |
fuzzCallback, |
| 147 |
NULL ); |
| 148 |
if( err != paNoError ) goto error; |
| 149 |
|
| 150 |
err = Pa_StartStream( stream ); |
| 151 |
if( err != paNoError ) goto error; |
| 152 |
|
| 153 |
printf("Hit ENTER to stop program.\n"); |
| 154 |
getchar(); |
| 155 |
err = Pa_CloseStream( stream ); |
| 156 |
if( err != paNoError ) goto error; |
| 157 |
|
| 158 |
printf("Finished. gNumNoInputs = %d\n", gNumNoInputs ); |
| 159 |
Pa_Terminate(); |
| 160 |
return 0; |
| 161 |
|
| 162 |
error: |
| 163 |
Pa_Terminate(); |
| •164 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 165 |
fprintf( stderr, "Error number: %d\n", err ); |
| 166 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 167 |
return -1; |
| 168 |
} |