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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest1.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 patest1.c
2
 
        @ingroup test_src
3
 
        @brief Ring modulate the audio input with a sine wave for 20 seconds.
4
 
        @author Ross Bencina <rossb@audiomulch.com>
5
 
*/
6
 
/*
7
 
 * $Id: patest1.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
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
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29
 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
 
 */
32
 
 
33
 
/*
34
 
 * The text above constitutes the entire PortAudio license; however, 
35
 
 * the PortAudio community also makes the following non-binding requests:
36
 
 *
37
 
 * Any person wishing to distribute modifications to the Software is
38
 
 * requested to send the modifications to the original developer so that
39
 
 * they can be incorporated into the canonical version. It is also 
40
 
 * requested that these non-binding requests be included along with the 
41
 
 * license above.
42
 
 */
43
 
 
44
 
#include <stdio.h>
45
 
#include <math.h>
46
 
#include "portaudio.h"
47
 
 
48
 
#ifndef M_PI
49
 
#define M_PI (3.14159265)
50
 
#endif
51
 
 
52
 
#define SAMPLE_RATE (44100)
53
 
 
54
 
typedef struct
55
 
{
56
 
    float sine[100];
57
 
    int phase;
58
 
    int sampsToGo;
59
 
}
60
 
patest1data;
61
 
 
62
 
static int patest1Callback( const void *inputBuffer, void *outputBuffer,
63
 
                            unsigned long framesPerBuffer,
64
 
                            const PaStreamCallbackTimeInfo* timeInfo,
65
 
                            PaStreamCallbackFlags statusFlags,
66
 
                            void *userData )
67
 
{
68
 
    patest1data *data = (patest1data*)userData;
69
 
    float *in = (float*)inputBuffer;
70
 
    float *out = (float*)outputBuffer;
71
 
    int framesToCalc = framesPerBuffer;
72
 
    unsigned long i = 0;
73
 
    int finished;
74
 
 
75
 
    if( data->sampsToGo < framesPerBuffer )
76
 
    {
77
 
        framesToCalc = data->sampsToGo;
78
 
        finished = paComplete;
79
 
    }
80
 
    else
81
 
    {
82
 
        finished = paContinue;
83
 
    }
84
 
 
85
 
    for( ; i<framesToCalc; i++ )
86
 
    {
87
 
        *out++ = *in++ * data->sine[data->phase];  /* left */
88
 
        *out++ = *in++ * data->sine[data->phase++];  /* right */
89
 
        if( data->phase >= 100 )
90
 
            data->phase = 0;
91
 
    }
92
 
 
93
 
    data->sampsToGo -= framesToCalc;
94
 
 
95
 
    /* zero remainder of final buffer if not already done */
96
 
    for( ; i<framesPerBuffer; i++ )
97
 
    {
98
 
        *out++ = 0; /* left */
99
 
        *out++ = 0; /* right */
100
 
    }
101
 
    
102
 
    return finished;
103
 
}
104
 
 
105
 
int main(int argc, char* argv[]);
106
 
int main(int argc, char* argv[])
107
 
{
108
 
    PaStream                *stream;
109
 
    PaError                 err;
110
 
    patest1data             data;
111
 
    int                     i;
112
 
    PaStreamParameters      inputParameters, outputParameters;
113
 
    const PaHostErrorInfo*  herr;
114
 
 
115
 
    printf("patest1.c\n"); fflush(stdout);
116
 
    printf("Ring modulate input for 20 seconds.\n"); fflush(stdout);
117
 
    
118
 
    /* initialise sinusoidal wavetable */
119
 
    for( i=0; i<100; i++ )
120
 
        data.sine[i] = sin( ((double)i/100.) * M_PI * 2. );
121
 
    data.phase = 0;
122
 
    data.sampsToGo = SAMPLE_RATE * 20;        /* 20 seconds. */
123
 
 
124
 
    /* initialise portaudio subsytem */
125
 
    err = Pa_Initialize();
126
 
 
127
 
    inputParameters.device = Pa_GetDefaultInputDevice();    /* default input device */
128
 
    inputParameters.channelCount = 2;                       /* stereo input */
129
 
    inputParameters.sampleFormat = paFloat32;               /* 32 bit floating point input */
130
 
    inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
131
 
    inputParameters.hostApiSpecificStreamInfo = NULL;
132
 
 
133
 
    outputParameters.device = Pa_GetDefaultOutputDevice();  /* default output device */
134
 
    outputParameters.channelCount = 2;                      /* stereo output */
135
 
    outputParameters.sampleFormat = paFloat32;              /* 32 bit floating point output */
136
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
137
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
138
 
 
139
 
    err = Pa_OpenStream(
140
 
                        &stream,
141
 
                        &inputParameters,
142
 
                        &outputParameters,
143
 
                        (double)SAMPLE_RATE, /* Samplerate in Hertz. */
144
 
                        512,                 /* Small buffers */
145
 
                        paClipOff,           /* We won't output out of range samples so don't bother clipping them. */
146
 
                        patest1Callback,
147
 
                        &data );
148
 
    if( err != paNoError ) goto done;
149
 
 
150
 
    err = Pa_StartStream( stream );
151
 
    if( err != paNoError ) goto done;
152
 
    
153
 
    printf( "Press any key to end.\n" ); fflush(stdout);
154
 
         
155
 
    getc( stdin ); /* wait for input before exiting */
156
 
 
157
 
    err = Pa_AbortStream( stream );
158
 
    if( err != paNoError ) goto done;
159
 
    
160
 
    printf( "Waiting for stream to complete...\n" );
161
 
 
162
 
    /* sleep until playback has finished */
163
 
    while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(1000);
164
 
    if( err < 0 ) goto done;
165
 
 
166
 
    err = Pa_CloseStream( stream );
167
 
    if( err != paNoError ) goto done;
168
 
 
169
 
done:
170
 
    Pa_Terminate();
171
 
 
172
 
    if( err != paNoError )
173
 
    {
174
 
        fprintf( stderr, "An error occured while using portaudio\n" );
175
 
        if( err == paUnanticipatedHostError )
176
 
        {
177
 
            fprintf( stderr, " unanticipated host error.\n");
178
 
            herr = Pa_GetLastHostErrorInfo();
179
 
            if (herr)
180
 
            {
181
 
                fprintf( stderr, " Error number: %ld\n", herr->errorCode );
182
 
                if (herr->errorText)
183
 
                    fprintf( stderr, " Error text: %s\n", herr->errorText );
184
 
            }
185
 
            else
186
 
                fprintf( stderr, " Pa_GetLastHostErrorInfo() failed!\n" );
187
 
        }
188
 
        else
189
 
        {
190
 
            fprintf( stderr, " Error number: %d\n", err );
191
 
            fprintf( stderr, " Error text: %s\n", Pa_GetErrorText( err ) );
192
 
        }
193
 
 
194
 
        err = 1;          /* Always return 0 or 1, but no other return codes. */
195
 
    }
196
 
 
197
 
    printf( "bye\n" );
198
 
 
199
 
    return err;
200
 
}