source: portaudio/branches/V18.1/pa_tests/patest_dither.c @ 136

Revision 136, 5.7 KB checked in by philburk, 8 years ago (diff)

Minor tweaks while debugging pa_mac_core.c

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * $Id$
3 * patest_dither.c
4 * Attempt to hear difference between dithered and non-dithered signal.
5 * This only has an effect if the native format is 16 bit.
6 *
7 * Author: Phil Burk  http://www.softsynth.com
8 *
9 * This program uses the PortAudio Portable Audio Library.
10 * For more information see: http://www.portaudio.com
11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * Any person wishing to distribute modifications to the Software is
25 * requested to send the modifications to the original developer so that
26 * they can be incorporated into the canonical version.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37#include <stdio.h>
38#include <math.h>
39#include "portaudio.h"
40#define NUM_SECONDS   (4)
41#define SAMPLE_RATE   (44100)
42#ifndef M_PI
43#define M_PI  (3.14159265)
44#endif
45#define TABLE_SIZE   (200)
46typedef struct paTestData
47{
48    float sine[TABLE_SIZE];
49    float amplitude;
50    int left_phase;
51    int right_phase;
52}
53paTestData;
54PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude );
55static int sineCallback( void *inputBuffer, void *outputBuffer,
56                         unsigned long framesPerBuffer,
57                         PaTimestamp outTime, void *userData );
58/* This routine will be called by the PortAudio engine when audio is needed.
59** It may called at interrupt level on some machines so don't do anything
60** that could mess up the system like calling malloc() or free().
61*/
62static int sineCallback( void *inputBuffer, void *outputBuffer,
63                         unsigned long framesPerBuffer,
64                         PaTimestamp outTime, void *userData )
65{
66    paTestData *data = (paTestData*)userData;
67    float *out = (float*)outputBuffer;
68    float amplitude = data->amplitude;
69    unsigned int i;
70    (void) outTime;
71    (void) inputBuffer;
72    for( i=0; i<framesPerBuffer; i++ )
73    {
74        *out++ = amplitude * data->sine[data->left_phase];  /* left */
75        *out++ = amplitude * data->sine[data->right_phase];  /* right */
76        data->left_phase += 1;
77        if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
78        data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
79        if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
80    }
81    return 0;
82}
83/*******************************************************************/
84int main(void);
85int main(void)
86{
87    PaError err;
88    paTestData DATA;
89    int i;
90    float amplitude = 32.0 / (1<<15);
91    printf("PortAudio Test: output EXTREMELY QUIET sine wave with and without dithering.\n");
92    /* initialise sinusoidal wavetable */
93    for( i=0; i<TABLE_SIZE; i++ )
94    {
95        DATA.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
96    }
97    printf("\nNo treatment..\n"); fflush(stdout);
98    err = PlaySine( &DATA, paClipOff | paDitherOff, amplitude );
99    if( err < 0 ) goto error;
100    printf("\nClip.\n");
101    fflush(stdout);
102    err = PlaySine( &DATA, paDitherOff, amplitude );
103    if( err < 0 ) goto error;
104    printf("\nClip and Dither.\n");
105    fflush(stdout);
106    err = PlaySine( &DATA, paNoFlag, amplitude );
107    if( err < 0 ) goto error;
108    return 0;
109error:
110    fprintf( stderr, "An error occured while using the portaudio stream\n" );
111    fprintf( stderr, "Error number: %d\n", err );
112    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
113    return -1;
114}
115/*****************************************************************************/
116PaError PlaySine( paTestData *data, PaStreamFlags flags, float amplitude )
117{
118    PortAudioStream *stream;
119    PaError err;
120    data->left_phase = data->right_phase = 0;
121    data->amplitude = amplitude;
122    err = Pa_Initialize();
123    if( err != paNoError ) goto error;
124    err = Pa_OpenStream(
125              &stream,
126              paNoDevice,/* default input device */
127              0,              /* no input */
128              paFloat32,  /* 32 bit floating point input */
129              NULL,
130              Pa_GetDefaultOutputDeviceID(), /* default output device */
131              2,              /* stereo output */
132              paFloat32,      /* 32 bit floating point output */
133              NULL,
134              SAMPLE_RATE,
135              1024,
136              0,              /* number of buffers, if zero then use default minimum */
137              flags,      /* we won't output out of range samples so don't bother clipping them */
138              sineCallback,
139              (void *)data );
140    if( err != paNoError ) goto error;
141
142    err = Pa_StartStream( stream );
143    if( err != paNoError ) goto error;
144    Pa_Sleep( NUM_SECONDS * 1000 );
145    printf("CPULoad = %8.6f\n", Pa_GetCPULoad( stream ) );
146    err = Pa_CloseStream( stream );
147    if( err != paNoError ) goto error;
148    Pa_Terminate();
149    return paNoError;
150error:
151    return err;
152}
Note: See TracBrowser for help on using the repository browser.