source: portaudio/branches/V18.1/pa_tests/patest_clip.c @ 90

Revision 90, 6.1 KB checked in by phil, 9 years ago (diff)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * $Id$
3 * patest_clip.c
4 * Play a sine wave using the Portable Audio api for several seconds
5 * at an amplitude that would require clipping.
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, unsigned long flags, float amplitude );
55/* This routine will be called by the PortAudio engine when audio is needed.
56** It may called at interrupt level on some machines so don't do anything
57** that could mess up the system like calling malloc() or free().
58*/
59static int sineCallback( void *inputBuffer, void *outputBuffer,
60                         unsigned long framesPerBuffer,
61                         PaTimestamp outTime, void *userData )
62{
63    paTestData *data = (paTestData*)userData;
64    float *out = (float*)outputBuffer;
65    float amplitude = data->amplitude;
66    unsigned int i;
67    (void) inputBuffer; /* Prevent "unused variable" warnings. */
68    (void) outTime;
69
70    for( i=0; i<framesPerBuffer; i++ )
71    {
72        *out++ = amplitude * data->sine[data->left_phase];  /* left */
73        *out++ = amplitude * data->sine[data->right_phase];  /* right */
74        data->left_phase += 1;
75        if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
76        data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
77        if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
78    }
79    return 0;
80}
81/*******************************************************************/
82int main(void);
83int main(void)
84{
85    PaError err;
86    paTestData DATA;
87    int i;
88    printf("PortAudio Test: output sine wave with and without clipping.\n");
89    /* initialise sinusoidal wavetable */
90    for( i=0; i<TABLE_SIZE; i++ )
91    {
92        DATA.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
93    }
94    printf("\nHalf amplitude. Should sound like sine wave.\n"); fflush(stdout);
95    err = PlaySine( &DATA, paClipOff | paDitherOff, 0.5f );
96    if( err < 0 ) goto error;
97    printf("\nFull amplitude. Should sound like sine wave.\n"); fflush(stdout);
98    err = PlaySine( &DATA, paClipOff | paDitherOff, 0.999f );
99    if( err < 0 ) goto error;
100    printf("\nOver range with clipping and dithering turned OFF. Should sound very nasty.\n");
101    fflush(stdout);
102    err = PlaySine( &DATA, paClipOff | paDitherOff, 1.1f );
103    if( err < 0 ) goto error;
104    printf("\nOver range with clipping and dithering turned ON.  Should sound smoother than previous.\n");
105    fflush(stdout);
106    err = PlaySine( &DATA, paNoFlag, 1.1f );
107    if( err < 0 ) goto error;
108    printf("\nOver range with paClipOff but dithering ON.\n"
109           "That forces clipping ON so it should sound the same as previous.\n");
110    fflush(stdout);
111    err = PlaySine( &DATA, paClipOff, 1.1f );
112    if( err < 0 ) goto error;
113    return 0;
114error:
115    fprintf( stderr, "An error occured while using the portaudio stream\n" );
116    fprintf( stderr, "Error number: %d\n", err );
117    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
118    return 1;
119}
120/*****************************************************************************/
121PaError PlaySine( paTestData *data, unsigned long flags, float amplitude )
122{
123    PortAudioStream *stream;
124    PaError err;
125    data->left_phase = data->right_phase = 0;
126    data->amplitude = amplitude;
127    err = Pa_Initialize();
128    if( err != paNoError ) goto error;
129    err = Pa_OpenStream(
130              &stream,
131              paNoDevice,/* default input device */
132              0,              /* no input */
133              paFloat32,  /* 32 bit floating point input */
134              NULL,
135              Pa_GetDefaultOutputDeviceID(), /* default output device */
136              2,              /* stereo output */
137              paFloat32,      /* 32 bit floating point output */
138              NULL,
139              SAMPLE_RATE,
140              1024,
141              0,              /* number of buffers, if zero then use default minimum */
142              flags,      /* we won't output out of range samples so don't bother clipping them */
143              sineCallback,
144              data );
145    if( err != paNoError ) goto error;
146    err = Pa_StartStream( stream );
147    if( err != paNoError ) goto error;
148    Pa_Sleep( NUM_SECONDS * 1000 );
149    printf("CPULoad = %8.6f\n", Pa_GetCPULoad( stream ) );
150    err = Pa_CloseStream( stream );
151    if( err != paNoError ) goto error;
152    Pa_Terminate();
153    return paNoError;
154error:
155    return err;
156}
Note: See TracBrowser for help on using the repository browser.