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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/debug_dual.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
 
/*
2
 
 * $Id: debug_dual.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
3
 
 * debug_dual.c
4
 
 * Try to open TWO streams on separate cards.
5
 
 * Play a sine sweep using the Portable Audio api for several seconds.
6
 
 * Hacked test for debugging PA.
7
 
 *
8
 
 * Author: Phil Burk <philburk@softsynth.com>
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
 
#define DEV_ID_1            (13)
49
 
#define DEV_ID_2            (15)
50
 
#define NUM_SECONDS         (8)
51
 
#define SLEEP_DUR           (800)
52
 
#define SAMPLE_RATE         (44100)
53
 
#define FRAMES_PER_BUFFER   (256)
54
 
#if 0
55
 
#define MIN_LATENCY_MSEC    (200)
56
 
#define NUM_BUFFERS         ((MIN_LATENCY_MSEC * SAMPLE_RATE) / (FRAMES_PER_BUFFER * 1000))
57
 
#else
58
 
#define NUM_BUFFERS         (0)
59
 
#endif
60
 
#define MIN_FREQ            (100.0f)
61
 
#define MAX_FREQ            (4000.0f)
62
 
#define FREQ_SCALAR         (1.00002f)
63
 
#define CalcPhaseIncrement(freq)  (freq/SAMPLE_RATE)
64
 
#ifndef M_PI
65
 
#define M_PI  (3.14159265)
66
 
#endif
67
 
#define TABLE_SIZE   (400)
68
 
typedef struct
69
 
{
70
 
    float sine[TABLE_SIZE + 1]; // add one for guard point for interpolation
71
 
    float phase_increment;
72
 
    float left_phase;
73
 
    float right_phase;
74
 
}
75
 
paTestData;
76
 
/* Convert phase between and 1.0 to sine value
77
 
 * using linear interpolation.
78
 
 */
79
 
float LookupSine( paTestData *data, float phase );
80
 
float LookupSine( paTestData *data, float phase )
81
 
{
82
 
    float fIndex = phase*TABLE_SIZE;
83
 
    int   index = (int) fIndex;
84
 
    float fract = fIndex - index;
85
 
    float lo = data->sine[index];
86
 
    float hi = data->sine[index+1];
87
 
    float val = lo + fract*(hi-lo);
88
 
    return val;
89
 
}
90
 
/* This routine will be called by the PortAudio engine when audio is needed.
91
 
** It may called at interrupt level on some machines so don't do anything
92
 
** that could mess up the system like calling malloc() or free().
93
 
*/
94
 
static int patestCallback( void *inputBuffer, void *outputBuffer,
95
 
                           unsigned long framesPerBuffer,
96
 
                           PaTimestamp outTime, void *userData )
97
 
{
98
 
    paTestData *data = (paTestData*)userData;
99
 
    float *out = (float*)outputBuffer;
100
 
    unsigned long i;
101
 
    int finished = 0;
102
 
    (void) outTime; /* Prevent unused variable warnings. */
103
 
    (void) inputBuffer;
104
 
 
105
 
 
106
 
    for( i=0; i<framesPerBuffer; i++ )
107
 
    {
108
 
        *out++ = LookupSine(data, data->left_phase);  /* left */
109
 
        *out++ = LookupSine(data, data->right_phase);  /* right */
110
 
        data->left_phase += data->phase_increment;
111
 
        if( data->left_phase >= 1.0f ) data->left_phase -= 1.0f;
112
 
        data->right_phase += (data->phase_increment * 1.5f); /* fifth above */
113
 
        if( data->right_phase >= 1.0f ) data->right_phase -= 1.0f;
114
 
        /* sweep frequency then start over. */
115
 
        data->phase_increment *= FREQ_SCALAR;
116
 
        if( data->phase_increment > CalcPhaseIncrement(MAX_FREQ) ) data->phase_increment = CalcPhaseIncrement(MIN_FREQ);
117
 
    }
118
 
    return 0;
119
 
}
120
 
 
121
 
PaError TestStart( PortAudioStream **streamPtr, PaDeviceID devID,
122
 
                   paTestData *data );
123
 
/*******************************************************************/
124
 
int main(void);
125
 
int main(void)
126
 
{
127
 
    PortAudioStream *stream1, *stream2;
128
 
    PaError err;
129
 
    paTestData DATA1, DATA2;
130
 
    printf("PortAudio Test: DUAL sine sweep. ask for %d buffers\n", NUM_BUFFERS );
131
 
    err = Pa_Initialize();
132
 
    if( err != paNoError ) goto error;
133
 
    err = TestStart( &stream1, DEV_ID_1, &DATA1 );
134
 
    if( err != paNoError ) goto error;
135
 
    err = TestStart( &stream2, DEV_ID_2, &DATA2 );
136
 
    if( err != paNoError ) goto error;
137
 
    printf("Hit ENTER\n");
138
 
    getchar();
139
 
    err = Pa_StopStream( stream1 );
140
 
    if( err != paNoError ) goto error;
141
 
    err = Pa_StopStream( stream2 );
142
 
    if( err != paNoError ) goto error;
143
 
    Pa_Terminate();
144
 
    printf("Test finished.\n");
145
 
    return err;
146
 
error:
147
 
    Pa_Terminate();
148
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
149
 
    fprintf( stderr, "Error number: %d\n", err );
150
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
151
 
    return err;
152
 
}
153
 
PaError TestStart( PortAudioStream **streamPtr, PaDeviceID devID, paTestData *data )
154
 
{
155
 
    PortAudioStream *stream;
156
 
    PaError err;
157
 
    int i;
158
 
    /* initialise sinusoidal wavetable */
159
 
    for( i=0; i<TABLE_SIZE; i++ )
160
 
    {
161
 
        data->sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
162
 
    }
163
 
    data->sine[TABLE_SIZE] = data->sine[0]; // set guard point
164
 
    data->left_phase = data->right_phase = 0.0;
165
 
    data->phase_increment = CalcPhaseIncrement(MIN_FREQ);
166
 
    printf("PortAudio Test: output device = %d\n", devID );
167
 
    err = Pa_OpenStream(
168
 
              &stream,
169
 
              paNoDevice,
170
 
              0,              /* no input */
171
 
              paFloat32,  /* 32 bit floating point input */
172
 
              NULL,
173
 
              devID,
174
 
              2,              /* stereo output */
175
 
              paFloat32,      /* 32 bit floating point output */
176
 
              NULL,
177
 
              SAMPLE_RATE,
178
 
              FRAMES_PER_BUFFER,
179
 
              NUM_BUFFERS,    /* number of buffers, if zero then use default minimum */
180
 
              paClipOff|paDitherOff, /* we won't output out of range samples so don't bother clipping them */
181
 
              patestCallback,
182
 
              data );
183
 
    if( err != paNoError ) goto error;
184
 
    err = Pa_StartStream( stream );
185
 
    if( err != paNoError ) goto error;
186
 
    *streamPtr = stream;
187
 
    return 0;
188
 
error:
189
 
    return err;
190
 
}