| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * pa_fuzz.c |
|---|
| 4 | * Distort input like a fuzz boz. |
|---|
| 5 | * |
|---|
| 6 | * Author: Phil Burk http://www.softsynth.com |
|---|
| 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( void *inputBuffer, void *outputBuffer, |
|---|
| 52 | unsigned long framesPerBuffer, |
|---|
| 53 | PaTimestamp outTime, void *userData ); |
|---|
| 54 | |
|---|
| 55 | /* Non-linear amplifier with soft distortion curve. */ |
|---|
| 56 | float CubicAmplifier( float input ) |
|---|
| 57 | { |
|---|
| 58 | float output, temp; |
|---|
| 59 | if( input < 0.0 ) |
|---|
| 60 | { |
|---|
| 61 | temp = input + 1.0f; |
|---|
| 62 | output = (temp * temp * temp) - 1.0f; |
|---|
| 63 | } |
|---|
| 64 | else |
|---|
| 65 | { |
|---|
| 66 | temp = input - 1.0f; |
|---|
| 67 | output = (temp * temp * temp) + 1.0f; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | return output; |
|---|
| 71 | } |
|---|
| 72 | #define FUZZ(x) CubicAmplifier(CubicAmplifier(CubicAmplifier(CubicAmplifier(x)))) |
|---|
| 73 | |
|---|
| 74 | static int gNumNoInputs = 0; |
|---|
| 75 | /* This routine will be called by the PortAudio engine when audio is needed. |
|---|
| 76 | ** It may be called at interrupt level on some machines so don't do anything |
|---|
| 77 | ** that could mess up the system like calling malloc() or free(). |
|---|
| 78 | */ |
|---|
| 79 | static int fuzzCallback( void *inputBuffer, void *outputBuffer, |
|---|
| 80 | unsigned long framesPerBuffer, |
|---|
| 81 | PaTimestamp outTime, void *userData ) |
|---|
| 82 | { |
|---|
| 83 | SAMPLE *out = (SAMPLE*)outputBuffer; |
|---|
| 84 | SAMPLE *in = (SAMPLE*)inputBuffer; |
|---|
| 85 | unsigned int i; |
|---|
| 86 | (void) outTime; /* Prevent unused variable warnings. */ |
|---|
| 87 | (void) userData; |
|---|
| 88 | |
|---|
| 89 | if( inputBuffer == NULL ) |
|---|
| 90 | { |
|---|
| 91 | for( i=0; i<framesPerBuffer; i++ ) |
|---|
| 92 | { |
|---|
| 93 | *out++ = 0; /* left - silent */ |
|---|
| 94 | *out++ = 0; /* right - silent */ |
|---|
| 95 | } |
|---|
| 96 | gNumNoInputs += 1; |
|---|
| 97 | } |
|---|
| 98 | else |
|---|
| 99 | { |
|---|
| 100 | for( i=0; i<framesPerBuffer; i++ ) |
|---|
| 101 | { |
|---|
| 102 | *out++ = FUZZ(*in++); /* left - distorted */ |
|---|
| 103 | *out++ = *in++; /* right - clean */ |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | return 0; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /*******************************************************************/ |
|---|
| 110 | int main(void); |
|---|
| 111 | int main(void) |
|---|
| 112 | { |
|---|
| 113 | PortAudioStream *stream; |
|---|
| 114 | PaError err; |
|---|
| 115 | |
|---|
| 116 | err = Pa_Initialize(); |
|---|
| 117 | if( err != paNoError ) goto error; |
|---|
| 118 | |
|---|
| 119 | err = Pa_OpenStream( |
|---|
| 120 | &stream, |
|---|
| 121 | Pa_GetDefaultInputDeviceID(), /* default output device */ |
|---|
| 122 | 2, /* stereo input */ |
|---|
| 123 | PA_SAMPLE_TYPE, |
|---|
| 124 | NULL, |
|---|
| 125 | Pa_GetDefaultOutputDeviceID(), /* default output device */ |
|---|
| 126 | 2, /* stereo output */ |
|---|
| 127 | PA_SAMPLE_TYPE, |
|---|
| 128 | NULL, |
|---|
| 129 | SAMPLE_RATE, |
|---|
| 130 | FRAMES_PER_BUFFER, |
|---|
| 131 | 0, /* number of buffers, if zero then use default minimum */ |
|---|
| 132 | 0, // paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
|---|
| 133 | fuzzCallback, |
|---|
| 134 | NULL ); |
|---|
| 135 | if( err != paNoError ) goto error; |
|---|
| 136 | |
|---|
| 137 | err = Pa_StartStream( stream ); |
|---|
| 138 | if( err != paNoError ) goto error; |
|---|
| 139 | |
|---|
| 140 | printf("Hit ENTER to stop program.\n"); |
|---|
| 141 | getchar(); |
|---|
| 142 | err = Pa_CloseStream( stream ); |
|---|
| 143 | if( err != paNoError ) goto error; |
|---|
| 144 | |
|---|
| 145 | printf("Finished. gNumNoInputs = %d\n", gNumNoInputs ); |
|---|
| 146 | Pa_Terminate(); |
|---|
| 147 | return 0; |
|---|
| 148 | |
|---|
| 149 | error: |
|---|
| 150 | Pa_Terminate(); |
|---|
| 151 | fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
|---|
| 152 | fprintf( stderr, "Error number: %d\n", err ); |
|---|
| 153 | fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
|---|
| 154 | return -1; |
|---|
| 155 | } |
|---|