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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_clip.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 patest_clip.c
2
 
        @ingroup test_src
3
 
        @brief Play a sine wave for several seconds at an amplitude 
4
 
        that would require clipping.
5
 
 
6
 
        @author Phil Burk  http://www.softsynth.com
7
 
*/
8
 
/*
9
 
 * $Id: patest_clip.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
10
 
 *
11
 
 * This program uses the PortAudio Portable Audio Library.
12
 
 * For more information see: http://www.portaudio.com
13
 
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
14
 
 *
15
 
 * Permission is hereby granted, free of charge, to any person obtaining
16
 
 * a copy of this software and associated documentation files
17
 
 * (the "Software"), to deal in the Software without restriction,
18
 
 * including without limitation the rights to use, copy, modify, merge,
19
 
 * publish, distribute, sublicense, and/or sell copies of the Software,
20
 
 * and to permit persons to whom the Software is furnished to do so,
21
 
 * subject to the following conditions:
22
 
 *
23
 
 * The above copyright notice and this permission notice shall be
24
 
 * included in all copies or substantial portions of the Software.
25
 
 *
26
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29
 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
30
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
31
 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32
 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
 
 */
34
 
 
35
 
/*
36
 
 * The text above constitutes the entire PortAudio license; however, 
37
 
 * the PortAudio community also makes the following non-binding requests:
38
 
 *
39
 
 * Any person wishing to distribute modifications to the Software is
40
 
 * requested to send the modifications to the original developer so that
41
 
 * they can be incorporated into the canonical version. It is also 
42
 
 * requested that these non-binding requests be included along with the 
43
 
 * license above.
44
 
 */
45
 
 
46
 
#include <stdio.h>
47
 
#include <math.h>
48
 
#include "portaudio.h"
49
 
 
50
 
#define NUM_SECONDS   (4)
51
 
#define SAMPLE_RATE   (44100)
52
 
#ifndef M_PI
53
 
#define M_PI  (3.14159265)
54
 
#endif
55
 
#define TABLE_SIZE   (200)
56
 
 
57
 
typedef struct paTestData
58
 
{
59
 
    float sine[TABLE_SIZE];
60
 
    float amplitude;
61
 
    int left_phase;
62
 
    int right_phase;
63
 
}
64
 
paTestData;
65
 
 
66
 
PaError PlaySine( paTestData *data, unsigned long flags, float amplitude );
67
 
 
68
 
/* This routine will be called by the PortAudio engine when audio is needed.
69
 
** It may called at interrupt level on some machines so don't do anything
70
 
** that could mess up the system like calling malloc() or free().
71
 
*/
72
 
static int sineCallback( const void *inputBuffer, void *outputBuffer,
73
 
                            unsigned long framesPerBuffer,
74
 
                            const PaStreamCallbackTimeInfo* timeInfo,
75
 
                            PaStreamCallbackFlags statusFlags,
76
 
                            void *userData )
77
 
{
78
 
    paTestData *data = (paTestData*)userData;
79
 
    float *out = (float*)outputBuffer;
80
 
    float amplitude = data->amplitude;
81
 
    unsigned int i;
82
 
    (void) inputBuffer; /* Prevent "unused variable" warnings. */
83
 
    (void) timeInfo;
84
 
    (void) statusFlags;
85
 
 
86
 
    for( i=0; i<framesPerBuffer; i++ )
87
 
    {
88
 
        *out++ = amplitude * data->sine[data->left_phase];  /* left */
89
 
        *out++ = amplitude * data->sine[data->right_phase];  /* right */
90
 
        data->left_phase += 1;
91
 
        if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
92
 
        data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
93
 
        if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
94
 
    }
95
 
    return 0;
96
 
}
97
 
/*******************************************************************/
98
 
int main(void);
99
 
int main(void)
100
 
{
101
 
    PaError err;
102
 
    paTestData data;
103
 
    int i;
104
 
 
105
 
    printf("PortAudio Test: output sine wave with and without clipping.\n");
106
 
    /* initialise sinusoidal wavetable */
107
 
    for( i=0; i<TABLE_SIZE; i++ )
108
 
    {
109
 
        data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
110
 
    }
111
 
 
112
 
    printf("\nHalf amplitude. Should sound like sine wave.\n"); fflush(stdout);
113
 
    err = PlaySine( &data, paClipOff | paDitherOff, 0.5f );
114
 
    if( err < 0 ) goto error;
115
 
 
116
 
    printf("\nFull amplitude. Should sound like sine wave.\n"); fflush(stdout);
117
 
    err = PlaySine( &data, paClipOff | paDitherOff, 0.999f );
118
 
    if( err < 0 ) goto error;
119
 
 
120
 
    printf("\nOver range with clipping and dithering turned OFF. Should sound very nasty.\n");
121
 
    fflush(stdout);
122
 
    err = PlaySine( &data, paClipOff | paDitherOff, 1.1f );
123
 
    if( err < 0 ) goto error;
124
 
 
125
 
    printf("\nOver range with clipping and dithering turned ON.  Should sound smoother than previous.\n");
126
 
    fflush(stdout);
127
 
    err = PlaySine( &data, paNoFlag, 1.1f );
128
 
    if( err < 0 ) goto error;
129
 
 
130
 
    printf("\nOver range with paClipOff but dithering ON.\n"
131
 
           "That forces clipping ON so it should sound the same as previous.\n");
132
 
    fflush(stdout);
133
 
    err = PlaySine( &data, paClipOff, 1.1f );
134
 
    if( err < 0 ) goto error;
135
 
    
136
 
    return 0;
137
 
error:
138
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
139
 
    fprintf( stderr, "Error number: %d\n", err );
140
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
141
 
    return 1;
142
 
}
143
 
/*****************************************************************************/
144
 
PaError PlaySine( paTestData *data, unsigned long flags, float amplitude )
145
 
{
146
 
    PaStreamParameters outputParameters;
147
 
    PaStream *stream;
148
 
    PaError err;
149
 
 
150
 
    data->left_phase = data->right_phase = 0;
151
 
    data->amplitude = amplitude;
152
 
    
153
 
    err = Pa_Initialize();
154
 
    if( err != paNoError ) goto error;
155
 
 
156
 
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
157
 
    outputParameters.channelCount = 2;       /* stereo output */
158
 
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
159
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
160
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
161
 
    
162
 
    err = Pa_OpenStream(
163
 
              &stream,
164
 
              NULL, /* no input */
165
 
              &outputParameters,
166
 
              SAMPLE_RATE,
167
 
              1024,
168
 
              flags,
169
 
              sineCallback,
170
 
              data );
171
 
    if( err != paNoError ) goto error;
172
 
 
173
 
    err = Pa_StartStream( stream );
174
 
    if( err != paNoError ) goto error;
175
 
 
176
 
    Pa_Sleep( NUM_SECONDS * 1000 );
177
 
    printf("CPULoad = %8.6f\n", Pa_GetStreamCpuLoad( stream ) );
178
 
 
179
 
    err = Pa_CloseStream( stream );
180
 
    if( err != paNoError ) goto error;
181
 
    
182
 
    Pa_Terminate();
183
 
    return paNoError;
184
 
error:
185
 
    return err;
186
 
}