| 1 |
/** @file pa_fuzz.c |
| 2 |
@brief Distort input like a fuzz boz. |
| 3 |
@author Phil Burk http://www.softsynth.com |
| 4 |
*/ |
| 5 |
/* |
| 6 |
* $Id: pa_fuzz.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 |
** Note that many of the older ISA sound cards on PCs do NOT support |
| 41 |
** full duplex audio (simultaneous record and playback). |
| 42 |
** And some only support full duplex at lower sample rates. |
| 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 |
/* Non-linear amplifier with soft distortion curve. */ |
| 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 |
/* This routine will be called by the PortAudio engine when audio is needed. |
| 78 |
** It may be called at interrupt level on some machines so don't do anything |
| 79 |
** that could mess up the system like calling malloc() or free(). |
| 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; /* Prevent unused variable warnings. */ |
| 91 |
(void) statusFlags; |
| 92 |
(void) userData; |
| 93 |
|
| 94 |
if( inputBuffer == NULL ) |
| 95 |
{ |
| 96 |
for( i=0; i<framesPerBuffer; i++ ) |
| 97 |
{ |
| 98 |
*out++ = 0; /* left - silent */ |
| 99 |
*out++ = 0; /* right - silent */ |
| 100 |
} |
| 101 |
gNumNoInputs += 1; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
for( i=0; i<framesPerBuffer; i++ ) |
| 106 |
{ |
| 107 |
*out++ = FUZZ(*in++); /* left - distorted */ |
| 108 |
*out++ = *in++; /* right - clean */ |
| 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(); /* default input device */ |
| 128 |
inputParameters.channelCount = 2; /* stereo input */ |
| 129 |
inputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 130 |
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; |
| 131 |
inputParameters.hostApiSpecificStreamInfo = NULL; |
| 132 |
|
| 133 |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ |
| 134 |
outputParameters.channelCount = 2; /* stereo output */ |
| 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, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */ |
| 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 occured 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 |
} |