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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_dither.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_dither.c
2
 
        @ingroup test_src
3
 
        @brief Attempt to hear difference between dithered and non-dithered signal.
4
 
 
5
 
        This only has an effect if the native format is 16 bit.
6
 
 
7
 
        @author Phil Burk  http://www.softsynth.com
8
 
*/
9
 
/*
10
 
 * $Id: patest_dither.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
11
 
 *
12
 
 * This program uses the PortAudio Portable Audio Library.
13
 
 * For more information see: http://www.portaudio.com
14
 
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
15
 
 *
16
 
 * Permission is hereby granted, free of charge, to any person obtaining
17
 
 * a copy of this software and associated documentation files
18
 
 * (the "Software"), to deal in the Software without restriction,
19
 
 * including without limitation the rights to use, copy, modify, merge,
20
 
 * publish, distribute, sublicense, and/or sell copies of the Software,
21
 
 * and to permit persons to whom the Software is furnished to do so,
22
 
 * subject to the following conditions:
23
 
 *
24
 
 * The above copyright notice and this permission notice shall be
25
 
 * included in all copies or substantial portions of the Software.
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
 
/*
37
 
 * The text above constitutes the entire PortAudio license; however, 
38
 
 * the PortAudio community also makes the following non-binding requests:
39
 
 *
40
 
 * Any person wishing to distribute modifications to the Software is
41
 
 * requested to send the modifications to the original developer so that
42
 
 * they can be incorporated into the canonical version. It is also 
43
 
 * requested that these non-binding requests be included along with the 
44
 
 * license above.
45
 
 */
46
 
 
47
 
#include <stdio.h>
48
 
#include <math.h>
49
 
 
50
 
#include "portaudio.h"
51
 
 
52
 
#define NUM_SECONDS   (5)
53
 
#define SAMPLE_RATE   (44100)
54
 
#ifndef M_PI
55
 
#define M_PI  (3.14159265)
56
 
#endif
57
 
#define TABLE_SIZE   (200)
58
 
 
59
 
typedef struct paTestData
60
 
{
61
 
    float sine[TABLE_SIZE];
62
 
    float amplitude;
63
 
    int   left_phase;
64
 
    int   right_phase;
65
 
}
66
 
paTestData;
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, void *userData )
76
 
{
77
 
    paTestData *data = (paTestData*)userData;
78
 
    float *out = (float*)outputBuffer;
79
 
    float amplitude = data->amplitude;
80
 
    unsigned int i;
81
 
    (void) inputBuffer;
82
 
    
83
 
    for( i=0; i<framesPerBuffer; i++ )
84
 
    {
85
 
        *out++ = amplitude * data->sine[data->left_phase];  /* left */
86
 
        *out++ = amplitude * data->sine[data->right_phase];  /* right */
87
 
        data->left_phase += 1;
88
 
        if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
89
 
        data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
90
 
        if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
91
 
    }
92
 
    return 0;
93
 
}
94
 
 
95
 
/*****************************************************************************/
96
 
/*
97
 
    V18 version did not call Pa_Terminate() if Pa_Initialize() failed.
98
 
    This V19 version ALWAYS calls Pa_Terminate(). PS.
99
 
*/
100
 
PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude );
101
 
PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude )
102
 
{
103
 
    PaStream*           stream;
104
 
    PaStreamParameters  outputParameters;
105
 
    PaError             err;
106
 
 
107
 
    data->left_phase = data->right_phase = 0;
108
 
    data->amplitude  = amplitude;
109
 
 
110
 
    err = Pa_Initialize();
111
 
    if (err != paNoError)
112
 
        goto done;
113
 
 
114
 
    outputParameters.device = Pa_GetDefaultOutputDevice();  /* default output device */
115
 
    outputParameters.channelCount = 2;                      /* stereo output */
116
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
117
 
    outputParameters.sampleFormat = paFloat32;      /* 32 bit floating point output. */
118
 
                                                    /* When you change this, also    */
119
 
                                                    /* adapt the callback routine!   */ 
120
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )
121
 
                                        ->defaultLowOutputLatency;   /* Low latency. */
122
 
    err = Pa_OpenStream( &stream,
123
 
                         NULL,                              /* No input. */
124
 
                         &outputParameters,
125
 
                         SAMPLE_RATE,
126
 
                         1024,                              /* frames per buffer */
127
 
                         flags,
128
 
                         sineCallback,
129
 
                         (void*)data );
130
 
    if (err != paNoError)
131
 
        goto done;
132
 
 
133
 
    err = Pa_StartStream( stream );
134
 
    if (err != paNoError)
135
 
        goto done;
136
 
 
137
 
    Pa_Sleep( NUM_SECONDS * 1000 );
138
 
    printf("CPULoad = %8.6f\n", Pa_GetStreamCpuLoad(stream));
139
 
    
140
 
    err = Pa_CloseStream( stream );
141
 
done:
142
 
    Pa_Sleep( 250 );  /* Just a small silence. */
143
 
    Pa_Terminate();
144
 
    return err;
145
 
}
146
 
 
147
 
 
148
 
/*******************************************************************/
149
 
int main(void);
150
 
int main(void)
151
 
{
152
 
    PaError     err;
153
 
    paTestData  DATA;
154
 
    int         i;
155
 
    float       amplitude = 4.0 / (1<<15);
156
 
    
157
 
    printf("PortAudio Test: output EXTREMELY QUIET sine wave with and without dithering.\n");
158
 
    /* initialise sinusoidal wavetable */
159
 
    for( i=0; i<TABLE_SIZE; i++ )
160
 
    {
161
 
        DATA.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
162
 
    }
163
 
    printf("\nNo treatment..\n"); fflush(stdout);
164
 
    err = PlaySine( &DATA, paClipOff | paDitherOff, amplitude );
165
 
    if( err < 0 ) goto done;
166
 
 
167
 
    printf("\nClip..\n");
168
 
    fflush(stdout);
169
 
    err = PlaySine( &DATA, paDitherOff, amplitude );
170
 
    if( err < 0 ) goto done;
171
 
 
172
 
    printf("\nClip and Dither..\n");
173
 
    fflush(stdout);
174
 
    err = PlaySine( &DATA, paNoFlag, amplitude );
175
 
done:
176
 
    if (err)
177
 
        {
178
 
        fprintf( stderr, "An error occured while using the portaudio stream\n" );
179
 
        fprintf( stderr, "Error number: %d\n", err );
180
 
        fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
181
 
        err = 1; /* Though PlaySine() already called Pa_Terminate(), */
182
 
        }        /* we may still call Pa_GetErrorText().             */
183
 
    else
184
 
        printf("\n(Don't forget to turn the VOLUME DOWN after listening so carefully.)\n");
185
 
    return err;  /* 0 or 1. */
186
 
}