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