~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/pa_fuzz.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** @file pa_fuzz.c
2
 
        @ingroup test_src
3
 
    @brief Distort input like a fuzz box.
4
 
        @author Phil Burk  http://www.softsynth.com
5
 
*/
6
 
/*
7
 
 * $Id: pa_fuzz.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
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
 
 
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;
140
 
 
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;
146
 
 
147
 
    err = Pa_OpenStream(
148
 
              &stream,
149
 
              &inputParameters,
150
 
              &outputParameters,
151
 
              SAMPLE_RATE,
152
 
              FRAMES_PER_BUFFER,
153
 
              0, /* paClipOff, */  /* we won't output out of range samples so don't bother clipping them */
154
 
              fuzzCallback,
155
 
              NULL );
156
 
    if( err != paNoError ) goto error;
157
 
 
158
 
    err = Pa_StartStream( stream );
159
 
    if( err != paNoError ) goto error;
160
 
 
161
 
    printf("Hit ENTER to stop program.\n");
162
 
    getchar();
163
 
    err = Pa_CloseStream( stream );
164
 
    if( err != paNoError ) goto error;
165
 
 
166
 
    printf("Finished. gNumNoInputs = %d\n", gNumNoInputs );
167
 
    Pa_Terminate();
168
 
    return 0;
169
 
 
170
 
error:
171
 
    Pa_Terminate();
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 ) );
175
 
    return -1;
176
 
}