| 1 |
/** @file patest_dither.c |
| 2 |
@brief Attempt to hear difference between dithered and non-dithered signal. |
| 3 |
|
| 4 |
This only has an effect if the native format is 16 bit. |
| 5 |
|
| 6 |
@author Phil Burk http://www.softsynth.com |
| 7 |
*/ |
| 8 |
/* |
| 9 |
* $Id: patest_dither.c,v 1.1 2006/06/10 21:30:56 dmazzoni 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 |
* Any person wishing to distribute modifications to the Software is |
| 27 |
* requested to send the modifications to the original developer so that |
| 28 |
* they can be incorporated into the canonical version. |
| 29 |
* |
| 30 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 31 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 32 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 33 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 34 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 36 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 |
* |
| 38 |
*/ |
| 39 |
#include <stdio.h> |
| 40 |
#include <math.h> |
| 41 |
|
| 42 |
#include "portaudio.h" |
| 43 |
|
| 44 |
#define NUM_SECONDS (5) |
| 45 |
#define SAMPLE_RATE (44100) |
| 46 |
#ifndef M_PI |
| 47 |
#define M_PI (3.14159265) |
| 48 |
#endif |
| 49 |
#define TABLE_SIZE (200) |
| 50 |
|
| 51 |
typedef struct paTestData |
| 52 |
{ |
| 53 |
float sine[TABLE_SIZE]; |
| 54 |
float amplitude; |
| 55 |
int left_phase; |
| 56 |
int right_phase; |
| 57 |
} |
| 58 |
paTestData; |
| 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, void *userData ) |
| 68 |
{ |
| 69 |
paTestData *data = (paTestData*)userData; |
| 70 |
float *out = (float*)outputBuffer; |
| 71 |
float amplitude = data->amplitude; |
| 72 |
unsigned int i; |
| 73 |
(void) inputBuffer; |
| 74 |
|
| 75 |
for( i=0; i<framesPerBuffer; i++ ) |
| 76 |
{ |
| 77 |
*out++ = amplitude * data->sine[data->left_phase]; /* left */ |
| 78 |
*out++ = amplitude * data->sine[data->right_phase]; /* right */ |
| 79 |
data->left_phase += 1; |
| 80 |
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
| 81 |
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ |
| 82 |
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
| 83 |
} |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
/*****************************************************************************/ |
| 88 |
/* |
| 89 |
V18 version did not call Pa_Terminate() if Pa_Initialize() failed. |
| 90 |
This V19 version ALWAYS calls Pa_Terminate(). PS. |
| 91 |
*/ |
| 92 |
PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude ); |
| 93 |
PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude ) |
| 94 |
{ |
| 95 |
PaStream* stream; |
| 96 |
PaStreamParameters outputParameters; |
| 97 |
PaError err; |
| 98 |
|
| 99 |
data->left_phase = data->right_phase = 0; |
| 100 |
data->amplitude = amplitude; |
| 101 |
|
| 102 |
err = Pa_Initialize(); |
| 103 |
if (err != paNoError) |
| 104 |
goto done; |
| 105 |
|
| 106 |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ |
| 107 |
outputParameters.channelCount = 2; /* stereo output */ |
| 108 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 109 |
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */ |
| 110 |
/* When you change this, also */ |
| 111 |
/* adapt the callback routine! */ |
| 112 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device ) |
| 113 |
->defaultLowOutputLatency; /* Low latency. */ |
| 114 |
err = Pa_OpenStream( &stream, |
| 115 |
NULL, /* No input. */ |
| 116 |
&outputParameters, |
| 117 |
SAMPLE_RATE, |
| 118 |
1024, /* frames per buffer */ |
| 119 |
flags, |
| 120 |
sineCallback, |
| 121 |
(void*)data ); |
| 122 |
if (err != paNoError) |
| 123 |
goto done; |
| 124 |
|
| 125 |
err = Pa_StartStream( stream ); |
| 126 |
if (err != paNoError) |
| 127 |
goto done; |
| 128 |
|
| 129 |
Pa_Sleep( NUM_SECONDS * 1000 ); |
| 130 |
printf("CPULoad = %8.6f\n", Pa_GetStreamCpuLoad(stream)); |
| 131 |
|
| 132 |
err = Pa_CloseStream( stream ); |
| 133 |
done: |
| 134 |
Pa_Sleep( 250 ); /* Just a small silence. */ |
| 135 |
Pa_Terminate(); |
| 136 |
return err; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/*******************************************************************/ |
| 141 |
int main(void); |
| 142 |
int main(void) |
| 143 |
{ |
| 144 |
PaError err; |
| 145 |
paTestData DATA; |
| 146 |
int i; |
| 147 |
float amplitude = 4.0 / (1<<15); |
| 148 |
|
| 149 |
printf("PortAudio Test: output EXTREMELY QUIET sine wave with and without dithering.\n"); |
| 150 |
/* initialise sinusoidal wavetable */ |
| 151 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 152 |
{ |
| 153 |
DATA.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); |
| 154 |
} |
| 155 |
printf("\nNo treatment..\n"); fflush(stdout); |
| 156 |
err = PlaySine( &DATA, paClipOff | paDitherOff, amplitude ); |
| 157 |
if( err < 0 ) goto done; |
| 158 |
|
| 159 |
printf("\nClip..\n"); |
| 160 |
fflush(stdout); |
| 161 |
err = PlaySine( &DATA, paDitherOff, amplitude ); |
| 162 |
if( err < 0 ) goto done; |
| 163 |
|
| 164 |
printf("\nClip and Dither..\n"); |
| 165 |
fflush(stdout); |
| 166 |
err = PlaySine( &DATA, paNoFlag, amplitude ); |
| 167 |
done: |
| 168 |
if (err) |
| 169 |
{ |
| 170 |
fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
| 171 |
fprintf( stderr, "Error number: %d\n", err ); |
| 172 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 173 |
err = 1; /* Though PlaySine() already called Pa_Terminate(), */ |
| 174 |
} /* we may still call Pa_GetErrorText(). */ |
| 175 |
else |
| 176 |
printf("\n(Don't forget to turn the VOLUME DOWN after listening so carefully.)\n"); |
| 177 |
return err; /* 0 or 1. */ |
| 178 |
} |