~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_toomanysines.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** @file patest_toomanysines.c
2
 
        @ingroup test_src
3
 
        @brief Play more sine waves than we can handle in real time as a stress test.
4
 
        @author Ross Bencina <rossb@audiomulch.com>
5
 
        @author Phil Burk <philburk@softsynth.com>
6
 
*/
7
 
/*
8
 
 * $Id: patest_toomanysines.c,v 1.2 2006/09/23 18:42:52 llucius 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
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28
 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30
 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31
 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
 
 */
33
 
 
34
 
/*
35
 
 * The text above constitutes the entire PortAudio license; however, 
36
 
 * the PortAudio community also makes the following non-binding requests:
37
 
 *
38
 
 * Any person wishing to distribute modifications to the Software is
39
 
 * requested to send the modifications to the original developer so that
40
 
 * they can be incorporated into the canonical version. It is also 
41
 
 * requested that these non-binding requests be included along with the 
42
 
 * license above.
43
 
 */
44
 
 
45
 
#include <stdio.h>
46
 
#include <math.h>
47
 
#include "portaudio.h"
48
 
 
49
 
#define MAX_SINES     (500)
50
 
#define MAX_LOAD      (1.2)
51
 
#define SAMPLE_RATE   (44100)
52
 
#define FRAMES_PER_BUFFER  (512)
53
 
#ifndef M_PI
54
 
#define M_PI  (3.14159265)
55
 
#endif
56
 
#define TWOPI (M_PI * 2.0)
57
 
 
58
 
typedef struct paTestData
59
 
{
60
 
    int numSines;
61
 
    double phases[MAX_SINES];
62
 
}
63
 
paTestData;
64
 
 
65
 
/* This routine will be called by the PortAudio engine when audio is needed.
66
 
** It may called at interrupt level on some machines so don't do anything
67
 
** that could mess up the system like calling malloc() or free().
68
 
*/
69
 
static int patestCallback( const void *inputBuffer, void *outputBuffer,
70
 
                           unsigned long framesPerBuffer,
71
 
                           const PaStreamCallbackTimeInfo* timeInfo,
72
 
                           PaStreamCallbackFlags statusFlags,
73
 
                           void *userData )
74
 
{
75
 
    paTestData *data = (paTestData*)userData;
76
 
    float *out = (float*)outputBuffer;
77
 
    unsigned long i;
78
 
    int j;
79
 
    int finished = 0;
80
 
    (void) inputBuffer; /* Prevent unused variable warning. */
81
 
 
82
 
    for( i=0; i<framesPerBuffer; i++ )
83
 
    {
84
 
        float output = 0.0;
85
 
        double phaseInc = 0.02;
86
 
        double phase;
87
 
        for( j=0; j<data->numSines; j++ )
88
 
        {
89
 
            /* Advance phase of next oscillator. */
90
 
            phase = data->phases[j];
91
 
            phase += phaseInc;
92
 
            if( phase > TWOPI ) phase -= TWOPI;
93
 
 
94
 
            phaseInc *= 1.02;
95
 
            if( phaseInc > 0.5 ) phaseInc *= 0.5;
96
 
 
97
 
            /* This is not a very efficient way to calc sines. */
98
 
            output += (float) sin( phase );
99
 
            data->phases[j] = phase;
100
 
        }
101
 
        *out++ = (float) (output / data->numSines);
102
 
    }
103
 
    return finished;
104
 
}
105
 
 
106
 
/*******************************************************************/
107
 
int main(void);
108
 
int main(void)
109
 
{
110
 
    PaStreamParameters outputParameters;
111
 
    PaStream *stream;
112
 
    PaError err;
113
 
    int numStress;
114
 
    paTestData data = {0};
115
 
    double load;
116
 
 
117
 
    printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d. MAX_LOAD = %f\n",
118
 
        SAMPLE_RATE, FRAMES_PER_BUFFER, MAX_LOAD );
119
 
 
120
 
    err = Pa_Initialize();
121
 
    if( err != paNoError ) goto error;
122
 
    
123
 
    outputParameters.device = Pa_GetDefaultOutputDevice();  /* default output device */
124
 
    outputParameters.channelCount = 1;                      /* mono output */
125
 
    outputParameters.sampleFormat = paFloat32;              /* 32 bit floating point output */
126
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
127
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
128
 
 
129
 
    err = Pa_OpenStream(
130
 
              &stream,
131
 
              NULL,         /* no input */
132
 
              &outputParameters,
133
 
              SAMPLE_RATE,
134
 
              FRAMES_PER_BUFFER,
135
 
              paClipOff,    /* we won't output out of range samples so don't bother clipping them */
136
 
              patestCallback,
137
 
              &data );    
138
 
    if( err != paNoError ) goto error;
139
 
    err = Pa_StartStream( stream );
140
 
    if( err != paNoError ) goto error;
141
 
 
142
 
    /* Determine number of sines required to get to 50% */
143
 
    do
144
 
    {
145
 
        data.numSines++;
146
 
        Pa_Sleep( 100 );
147
 
 
148
 
        load = Pa_GetStreamCpuLoad( stream );
149
 
        printf("numSines = %d, CPU load = %f\n", data.numSines, load );
150
 
    }
151
 
    while( load < 0.5 );
152
 
    
153
 
    /* Calculate target stress value then ramp up to that level*/
154
 
    numStress = (int) (2.0 * data.numSines * MAX_LOAD );
155
 
    for( ; data.numSines < numStress; data.numSines++ )
156
 
    {
157
 
        Pa_Sleep( 200 );
158
 
        load = Pa_GetStreamCpuLoad( stream );
159
 
        printf("STRESSING: numSines = %d, CPU load = %f\n", data.numSines, load );
160
 
    }
161
 
    
162
 
    printf("Suffer for 5 seconds.\n");
163
 
    Pa_Sleep( 5000 );
164
 
    
165
 
    printf("Stop stream.\n");
166
 
    err = Pa_StopStream( stream );
167
 
    if( err != paNoError ) goto error;
168
 
    
169
 
    err = Pa_CloseStream( stream );
170
 
    if( err != paNoError ) goto error;
171
 
    
172
 
    Pa_Terminate();
173
 
    printf("Test finished.\n");
174
 
    return err;
175
 
error:
176
 
    Pa_Terminate();
177
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
178
 
    fprintf( stderr, "Error number: %d\n", err );
179
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
180
 
    return err;
181
 
}