1
/** @file patest_clip.c
3
@brief Play a sine wave for several seconds at an amplitude
4
that would require clipping.
6
@author Phil Burk http://www.softsynth.com
9
* $Id: patest_clip.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
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
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:
23
* The above copyright notice and this permission notice shall be
24
* included in all copies or substantial portions of the Software.
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.
36
* The text above constitutes the entire PortAudio license; however,
37
* the PortAudio community also makes the following non-binding requests:
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
48
#include "portaudio.h"
50
#define NUM_SECONDS (4)
51
#define SAMPLE_RATE (44100)
53
#define M_PI (3.14159265)
55
#define TABLE_SIZE (200)
57
typedef struct paTestData
59
float sine[TABLE_SIZE];
66
PaError PlaySine( paTestData *data, unsigned long flags, float amplitude );
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().
72
static int sineCallback( const void *inputBuffer, void *outputBuffer,
73
unsigned long framesPerBuffer,
74
const PaStreamCallbackTimeInfo* timeInfo,
75
PaStreamCallbackFlags statusFlags,
78
paTestData *data = (paTestData*)userData;
79
float *out = (float*)outputBuffer;
80
float amplitude = data->amplitude;
82
(void) inputBuffer; /* Prevent "unused variable" warnings. */
86
for( i=0; i<framesPerBuffer; i++ )
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;
97
/*******************************************************************/
105
printf("PortAudio Test: output sine wave with and without clipping.\n");
106
/* initialise sinusoidal wavetable */
107
for( i=0; i<TABLE_SIZE; i++ )
109
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
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;
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;
120
printf("\nOver range with clipping and dithering turned OFF. Should sound very nasty.\n");
122
err = PlaySine( &data, paClipOff | paDitherOff, 1.1f );
123
if( err < 0 ) goto error;
125
printf("\nOver range with clipping and dithering turned ON. Should sound smoother than previous.\n");
127
err = PlaySine( &data, paNoFlag, 1.1f );
128
if( err < 0 ) goto error;
130
printf("\nOver range with paClipOff but dithering ON.\n"
131
"That forces clipping ON so it should sound the same as previous.\n");
133
err = PlaySine( &data, paClipOff, 1.1f );
134
if( err < 0 ) goto 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 ) );
143
/*****************************************************************************/
144
PaError PlaySine( paTestData *data, unsigned long flags, float amplitude )
146
PaStreamParameters outputParameters;
150
data->left_phase = data->right_phase = 0;
151
data->amplitude = amplitude;
153
err = Pa_Initialize();
154
if( err != paNoError ) goto error;
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;
171
if( err != paNoError ) goto error;
173
err = Pa_StartStream( stream );
174
if( err != paNoError ) goto error;
176
Pa_Sleep( NUM_SECONDS * 1000 );
177
printf("CPULoad = %8.6f\n", Pa_GetStreamCpuLoad( stream ) );
179
err = Pa_CloseStream( stream );
180
if( err != paNoError ) goto error;