1
/** @file patest_dither.c
3
@brief Attempt to hear difference between dithered and non-dithered signal.
5
This only has an effect if the native format is 16 bit.
7
@author Phil Burk http://www.softsynth.com
10
* $Id: patest_dither.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
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
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:
24
* The above copyright notice and this permission notice shall be
25
* included in all copies or substantial portions of the Software.
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.
37
* The text above constitutes the entire PortAudio license; however,
38
* the PortAudio community also makes the following non-binding requests:
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
50
#include "portaudio.h"
52
#define NUM_SECONDS (5)
53
#define SAMPLE_RATE (44100)
55
#define M_PI (3.14159265)
57
#define TABLE_SIZE (200)
59
typedef struct paTestData
61
float sine[TABLE_SIZE];
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, void *userData )
77
paTestData *data = (paTestData*)userData;
78
float *out = (float*)outputBuffer;
79
float amplitude = data->amplitude;
83
for( i=0; i<framesPerBuffer; i++ )
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;
95
/*****************************************************************************/
97
V18 version did not call Pa_Terminate() if Pa_Initialize() failed.
98
This V19 version ALWAYS calls Pa_Terminate(). PS.
100
PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude );
101
PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude )
104
PaStreamParameters outputParameters;
107
data->left_phase = data->right_phase = 0;
108
data->amplitude = amplitude;
110
err = Pa_Initialize();
111
if (err != paNoError)
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. */
126
1024, /* frames per buffer */
130
if (err != paNoError)
133
err = Pa_StartStream( stream );
134
if (err != paNoError)
137
Pa_Sleep( NUM_SECONDS * 1000 );
138
printf("CPULoad = %8.6f\n", Pa_GetStreamCpuLoad(stream));
140
err = Pa_CloseStream( stream );
142
Pa_Sleep( 250 ); /* Just a small silence. */
148
/*******************************************************************/
155
float amplitude = 4.0 / (1<<15);
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++ )
161
DATA.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
163
printf("\nNo treatment..\n"); fflush(stdout);
164
err = PlaySine( &DATA, paClipOff | paDitherOff, amplitude );
165
if( err < 0 ) goto done;
167
printf("\nClip..\n");
169
err = PlaySine( &DATA, paDitherOff, amplitude );
170
if( err < 0 ) goto done;
172
printf("\nClip and Dither..\n");
174
err = PlaySine( &DATA, paNoFlag, amplitude );
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(). */
184
printf("\n(Don't forget to turn the VOLUME DOWN after listening so carefully.)\n");
185
return err; /* 0 or 1. */