3
@brief Distort input like a fuzz box.
4
@author Phil Burk http://www.softsynth.com
7
* $Id: pa_fuzz.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"
48
** Note that many of the older ISA sound cards on PCs do NOT support
49
** full duplex audio (simultaneous record and playback).
50
** And some only support full duplex at lower sample rates.
52
#define SAMPLE_RATE (44100)
53
#define PA_SAMPLE_TYPE paFloat32
54
#define FRAMES_PER_BUFFER (64)
58
float CubicAmplifier( float input );
59
static int fuzzCallback( const void *inputBuffer, void *outputBuffer,
60
unsigned long framesPerBuffer,
61
const PaStreamCallbackTimeInfo* timeInfo,
62
PaStreamCallbackFlags statusFlags,
65
/* Non-linear amplifier with soft distortion curve. */
66
float CubicAmplifier( float input )
72
output = (temp * temp * temp) - 1.0f;
77
output = (temp * temp * temp) + 1.0f;
82
#define FUZZ(x) CubicAmplifier(CubicAmplifier(CubicAmplifier(CubicAmplifier(x))))
84
static int gNumNoInputs = 0;
85
/* This routine will be called by the PortAudio engine when audio is needed.
86
** It may be called at interrupt level on some machines so don't do anything
87
** that could mess up the system like calling malloc() or free().
89
static int fuzzCallback( const void *inputBuffer, void *outputBuffer,
90
unsigned long framesPerBuffer,
91
const PaStreamCallbackTimeInfo* timeInfo,
92
PaStreamCallbackFlags statusFlags,
95
SAMPLE *out = (SAMPLE*)outputBuffer;
96
const SAMPLE *in = (const SAMPLE*)inputBuffer;
98
(void) timeInfo; /* Prevent unused variable warnings. */
102
if( inputBuffer == NULL )
104
for( i=0; i<framesPerBuffer; i++ )
106
*out++ = 0; /* left - silent */
107
*out++ = 0; /* right - silent */
113
for( i=0; i<framesPerBuffer; i++ )
115
*out++ = FUZZ(*in++); /* left - distorted */
116
*out++ = *in++; /* right - clean */
123
/*******************************************************************/
127
PaStreamParameters inputParameters, outputParameters;
131
err = Pa_Initialize();
132
if( err != paNoError ) goto error;
135
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
136
inputParameters.channelCount = 2; /* stereo input */
137
inputParameters.sampleFormat = PA_SAMPLE_TYPE;
138
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
139
inputParameters.hostApiSpecificStreamInfo = NULL;
141
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
142
outputParameters.channelCount = 2; /* stereo output */
143
outputParameters.sampleFormat = PA_SAMPLE_TYPE;
144
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
145
outputParameters.hostApiSpecificStreamInfo = NULL;
153
0, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
156
if( err != paNoError ) goto error;
158
err = Pa_StartStream( stream );
159
if( err != paNoError ) goto error;
161
printf("Hit ENTER to stop program.\n");
163
err = Pa_CloseStream( stream );
164
if( err != paNoError ) goto error;
166
printf("Finished. gNumNoInputs = %d\n", gNumNoInputs );
172
fprintf( stderr, "An error occured while using the portaudio stream\n" );
173
fprintf( stderr, "Error number: %d\n", err );
174
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );