PortAudio  2.0
pa_fuzz.c
Go to the documentation of this file.
1 
6 /*
7  * $Id$
8  *
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
12  *
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:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
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.
31  */
32 
33 /*
34  * The text above constitutes the entire PortAudio license; however,
35  * the PortAudio community also makes the following non-binding requests:
36  *
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
41  * license above.
42  */
43 
44 #include <stdio.h>
45 #include <math.h>
46 #include "portaudio.h"
47 /*
48  * Simulate a guitar distortion pedal.
49  * Record mono input and output clean and processed stereo output.
50  *
51  * Note that many of the older ISA sound cards on PCs do NOT support
52  * full duplex audio (simultaneous record and playback).
53  * And some only support full duplex at lower sample rates.
54 */
55 #define SAMPLE_RATE (44100)
56 #define PA_SAMPLE_TYPE paFloat32
57 #define FRAMES_PER_BUFFER (64)
58 
59 typedef float SAMPLE;
60 
61 float CubicAmplifier( float input );
62 static int fuzzCallback( const void *inputBuffer, void *outputBuffer,
63  unsigned long framesPerBuffer,
64  const PaStreamCallbackTimeInfo* timeInfo,
65  PaStreamCallbackFlags statusFlags,
66  void *userData );
67 
68 /* Non-linear amplifier with soft distortion curve. */
69 float CubicAmplifier( float input )
70 {
71  float output, temp;
72  if( input < 0.0 )
73  {
74  temp = input + 1.0f;
75  output = (temp * temp * temp) - 1.0f;
76  }
77  else
78  {
79  temp = input - 1.0f;
80  output = (temp * temp * temp) + 1.0f;
81  }
82 
83  return output;
84 }
85 #define FUZZ(x) CubicAmplifier(CubicAmplifier(CubicAmplifier(CubicAmplifier(x))))
86 
87 static int gNumNoInputs = 0;
88 /* This routine will be called by the PortAudio engine when audio is needed.
89 ** It may be called at interrupt level on some machines so don't do anything
90 ** that could mess up the system like calling malloc() or free().
91 */
92 static int fuzzCallback( const void *inputBuffer, void *outputBuffer,
93  unsigned long framesPerBuffer,
94  const PaStreamCallbackTimeInfo* timeInfo,
95  PaStreamCallbackFlags statusFlags,
96  void *userData )
97 {
98  SAMPLE *out = (SAMPLE*)outputBuffer;
99  const SAMPLE *in = (const SAMPLE*)inputBuffer;
100  unsigned int i;
101  (void) timeInfo; /* Prevent unused variable warnings. */
102  (void) statusFlags;
103  (void) userData;
104 
105  if( inputBuffer == NULL )
106  {
107  for( i=0; i<framesPerBuffer; i++ )
108  {
109  *out++ = 0; /* left - silent */
110  *out++ = 0; /* right - silent */
111  }
112  gNumNoInputs += 1;
113  }
114  else
115  {
116  for( i=0; i<framesPerBuffer; i++ )
117  {
118  SAMPLE sample = *in++; /* MONO input */
119  *out++ = FUZZ(sample); /* left - distorted */
120  *out++ = sample; /* right - clean */
121  }
122  }
123 
124  return paContinue;
125 }
126 
127 /*******************************************************************/
128 int main(void);
129 int main(void)
130 {
131  PaStreamParameters inputParameters, outputParameters;
132  PaStream *stream;
133  PaError err;
134 
135  err = Pa_Initialize();
136  if( err != paNoError ) goto error;
137 
138  inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
139  if (inputParameters.device == paNoDevice) {
140  fprintf(stderr,"Error: No default input device.\n");
141  goto error;
142  }
143  inputParameters.channelCount = 1; /* mono input */
144  inputParameters.sampleFormat = PA_SAMPLE_TYPE;
145  inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
146  inputParameters.hostApiSpecificStreamInfo = NULL;
147 
148  outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
149  if (outputParameters.device == paNoDevice) {
150  fprintf(stderr,"Error: No default output device.\n");
151  goto error;
152  }
153  outputParameters.channelCount = 2; /* stereo output */
154  outputParameters.sampleFormat = PA_SAMPLE_TYPE;
155  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
156  outputParameters.hostApiSpecificStreamInfo = NULL;
157 
158  err = Pa_OpenStream(
159  &stream,
160  &inputParameters,
161  &outputParameters,
162  SAMPLE_RATE,
163  FRAMES_PER_BUFFER,
164  0, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
165  fuzzCallback,
166  NULL );
167  if( err != paNoError ) goto error;
168 
169  err = Pa_StartStream( stream );
170  if( err != paNoError ) goto error;
171 
172  printf("Hit ENTER to stop program.\n");
173  getchar();
174  err = Pa_CloseStream( stream );
175  if( err != paNoError ) goto error;
176 
177  printf("Finished. gNumNoInputs = %d\n", gNumNoInputs );
178  Pa_Terminate();
179  return 0;
180 
181 error:
182  Pa_Terminate();
183  fprintf( stderr, "An error occurred while using the portaudio stream\n" );
184  fprintf( stderr, "Error number: %d\n", err );
185  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
186  return -1;
187 }
PaError Pa_Initialize(void)
PaDeviceIndex Pa_GetDefaultInputDevice(void)
void PaStream
Definition: portaudio.h:644
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaTime defaultLowInputLatency
Definition: portaudio.h:519
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
Definition: portaudio.h:588
The portable PortAudio API.
PaSampleFormat sampleFormat
Definition: portaudio.h:568
int PaError
Definition: portaudio.h:122
PaTime suggestedLatency
Definition: portaudio.h:581
unsigned long PaStreamCallbackFlags
Definition: portaudio.h:721
#define paNoDevice
Definition: portaudio.h:222
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
PaDeviceIndex device
Definition: portaudio.h:555
const char * Pa_GetErrorText(PaError errorCode)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)