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 ** 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.
51 */
52 #define SAMPLE_RATE (44100)
53 #define PA_SAMPLE_TYPE paFloat32
54 #define FRAMES_PER_BUFFER (64)
55 
56 typedef float SAMPLE;
57 
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,
63  void *userData );
64 
65 /* Non-linear amplifier with soft distortion curve. */
66 float CubicAmplifier( float input )
67 {
68  float output, temp;
69  if( input < 0.0 )
70  {
71  temp = input + 1.0f;
72  output = (temp * temp * temp) - 1.0f;
73  }
74  else
75  {
76  temp = input - 1.0f;
77  output = (temp * temp * temp) + 1.0f;
78  }
79 
80  return output;
81 }
82 #define FUZZ(x) CubicAmplifier(CubicAmplifier(CubicAmplifier(CubicAmplifier(x))))
83 
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().
88 */
89 static int fuzzCallback( const void *inputBuffer, void *outputBuffer,
90  unsigned long framesPerBuffer,
91  const PaStreamCallbackTimeInfo* timeInfo,
92  PaStreamCallbackFlags statusFlags,
93  void *userData )
94 {
95  SAMPLE *out = (SAMPLE*)outputBuffer;
96  const SAMPLE *in = (const SAMPLE*)inputBuffer;
97  unsigned int i;
98  (void) timeInfo; /* Prevent unused variable warnings. */
99  (void) statusFlags;
100  (void) userData;
101 
102  if( inputBuffer == NULL )
103  {
104  for( i=0; i<framesPerBuffer; i++ )
105  {
106  *out++ = 0; /* left - silent */
107  *out++ = 0; /* right - silent */
108  }
109  gNumNoInputs += 1;
110  }
111  else
112  {
113  for( i=0; i<framesPerBuffer; i++ )
114  {
115  *out++ = FUZZ(*in++); /* left - distorted */
116  *out++ = *in++; /* right - clean */
117  }
118  }
119 
120  return paContinue;
121 }
122 
123 /*******************************************************************/
124 int main(void);
125 int main(void)
126 {
127  PaStreamParameters inputParameters, outputParameters;
128  PaStream *stream;
129  PaError err;
130 
131  err = Pa_Initialize();
132  if( err != paNoError ) goto error;
133 
134  inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
135  if (inputParameters.device == paNoDevice) {
136  fprintf(stderr,"Error: No default input device.\n");
137  goto error;
138  }
139  inputParameters.channelCount = 2; /* stereo input */
140  inputParameters.sampleFormat = PA_SAMPLE_TYPE;
141  inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
142  inputParameters.hostApiSpecificStreamInfo = NULL;
143 
144  outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
145  if (outputParameters.device == paNoDevice) {
146  fprintf(stderr,"Error: No default output device.\n");
147  goto error;
148  }
149  outputParameters.channelCount = 2; /* stereo output */
150  outputParameters.sampleFormat = PA_SAMPLE_TYPE;
151  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
152  outputParameters.hostApiSpecificStreamInfo = NULL;
153 
154  err = Pa_OpenStream(
155  &stream,
156  &inputParameters,
157  &outputParameters,
158  SAMPLE_RATE,
159  FRAMES_PER_BUFFER,
160  0, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
161  fuzzCallback,
162  NULL );
163  if( err != paNoError ) goto error;
164 
165  err = Pa_StartStream( stream );
166  if( err != paNoError ) goto error;
167 
168  printf("Hit ENTER to stop program.\n");
169  getchar();
170  err = Pa_CloseStream( stream );
171  if( err != paNoError ) goto error;
172 
173  printf("Finished. gNumNoInputs = %d\n", gNumNoInputs );
174  Pa_Terminate();
175  return 0;
176 
177 error:
178  Pa_Terminate();
179  fprintf( stderr, "An error occured while using the portaudio stream\n" );
180  fprintf( stderr, "Error number: %d\n", err );
181  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
182  return -1;
183 }
PaError Pa_Initialize(void)
PaDeviceIndex Pa_GetDefaultInputDevice(void)
void PaStream
Definition: portaudio.h:635
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:510
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
Definition: portaudio.h:579
The portable PortAudio API.
PaSampleFormat sampleFormat
Definition: portaudio.h:559
int PaError
Definition: portaudio.h:121
PaTime suggestedLatency
Definition: portaudio.h:572
unsigned long PaStreamCallbackFlags
Definition: portaudio.h:712
#define paNoDevice
Definition: portaudio.h:220
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
PaDeviceIndex device
Definition: portaudio.h:546
const char * Pa_GetErrorText(PaError errorCode)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)