To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / src / portaudio_20161030_catalina_patch / test / patest_clip.c @ 162:d43aab368df9

History | View | Annotate | Download (6.58 KB)

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$
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
    if (outputParameters.device == paNoDevice) {
158
      fprintf(stderr,"Error: No default output device.\n");
159
      goto error;
160
    }
161
    outputParameters.channelCount = 2;       /* stereo output */
162
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
163
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
164
    outputParameters.hostApiSpecificStreamInfo = NULL;
165
    
166
    err = Pa_OpenStream(
167
              &stream,
168
              NULL, /* no input */
169
              &outputParameters,
170
              SAMPLE_RATE,
171
              1024,
172
              flags,
173
              sineCallback,
174
              data );
175
    if( err != paNoError ) goto error;
176

    
177
    err = Pa_StartStream( stream );
178
    if( err != paNoError ) goto error;
179

    
180
    Pa_Sleep( NUM_SECONDS * 1000 );
181
    printf("CPULoad = %8.6f\n", Pa_GetStreamCpuLoad( stream ) );
182

    
183
    err = Pa_CloseStream( stream );
184
    if( err != paNoError ) goto error;
185
    
186
    Pa_Terminate();
187
    return paNoError;
188
error:
189
    return err;
190
}