1 /*
2  * $Id: debug_dual.c,v 1.1 2006/06/10 21:30:56 dmazzoni 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  * Any person wishing to distribute modifications to the Software is
26  * requested to send the modifications to the original developer so that
27  * they can be incorporated into the canonical version.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
33  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  *
37  */
38 #include <stdio.h>
39 #include <math.h>
40 #include "portaudio.h"
41 #define DEV_ID_1            (13)
42 #define DEV_ID_2            (15)
43 #define NUM_SECONDS         (8)
44 #define SLEEP_DUR           (800)
45 #define SAMPLE_RATE         (44100)
46 #define FRAMES_PER_BUFFER   (256)
47 #if 0
48 #define MIN_LATENCY_MSEC    (200)
49 #define NUM_BUFFERS         ((MIN_LATENCY_MSEC * SAMPLE_RATE) / (FRAMES_PER_BUFFER * 1000))
50 #else
51 #define NUM_BUFFERS         (0)
52 #endif
53 #define MIN_FREQ            (100.0f)
54 #define MAX_FREQ            (4000.0f)
55 #define FREQ_SCALAR         (1.00002f)
56 #define CalcPhaseIncrement(freq)  (freq/SAMPLE_RATE)
57 #ifndef M_PI
58 #define M_PI  (3.14159265)
59 #endif
60 #define TABLE_SIZE   (400)
61 typedef struct
62 {
63     float sine[TABLE_SIZE + 1]; // add one for guard point for interpolation
64     float phase_increment;
65     float left_phase;
66     float right_phase;
67 }
68 paTestData;
69 /* Convert phase between and 1.0 to sine value
70  * using linear interpolation.
71  */
72 float LookupSine( paTestData *data, float phase );
LookupSine(paTestData * data,float phase)73 float LookupSine( paTestData *data, float phase )
74 {
75     float fIndex = phase*TABLE_SIZE;
76     int   index = (int) fIndex;
77     float fract = fIndex - index;
78     float lo = data->sine[index];
79     float hi = data->sine[index+1];
80     float val = lo + fract*(hi-lo);
81     return val;
82 }
83 /* This routine will be called by the PortAudio engine when audio is needed.
84 ** It may called at interrupt level on some machines so don't do anything
85 ** that could mess up the system like calling malloc() or free().
86 */
patestCallback(void * inputBuffer,void * outputBuffer,unsigned long framesPerBuffer,PaTimestamp outTime,void * userData)87 static int patestCallback( void *inputBuffer, void *outputBuffer,
88                            unsigned long framesPerBuffer,
89                            PaTimestamp outTime, void *userData )
90 {
91     paTestData *data = (paTestData*)userData;
92     float *out = (float*)outputBuffer;
93     unsigned long i;
94     int finished = 0;
95     (void) outTime; /* Prevent unused variable warnings. */
96     (void) inputBuffer;
97 
98 
99     for( i=0; i<framesPerBuffer; i++ )
100     {
101         *out++ = LookupSine(data, data->left_phase);  /* left */
102         *out++ = LookupSine(data, data->right_phase);  /* right */
103         data->left_phase += data->phase_increment;
104         if( data->left_phase >= 1.0f ) data->left_phase -= 1.0f;
105         data->right_phase += (data->phase_increment * 1.5f); /* fifth above */
106         if( data->right_phase >= 1.0f ) data->right_phase -= 1.0f;
107         /* sweep frequency then start over. */
108         data->phase_increment *= FREQ_SCALAR;
109         if( data->phase_increment > CalcPhaseIncrement(MAX_FREQ) ) data->phase_increment = CalcPhaseIncrement(MIN_FREQ);
110     }
111     return 0;
112 }
113 
114 PaError TestStart( PortAudioStream **streamPtr, PaDeviceID devID,
115                    paTestData *data );
116 /*******************************************************************/
117 int main(void);
main(void)118 int main(void)
119 {
120     PortAudioStream *stream1, *stream2;
121     PaError err;
122     paTestData DATA1, DATA2;
123     printf("PortAudio Test: DUAL sine sweep. ask for %d buffers\n", NUM_BUFFERS );
124     err = Pa_Initialize();
125     if( err != paNoError ) goto error;
126     err = TestStart( &stream1, DEV_ID_1, &DATA1 );
127     if( err != paNoError ) goto error;
128     err = TestStart( &stream2, DEV_ID_2, &DATA2 );
129     if( err != paNoError ) goto error;
130     printf("Hit ENTER\n");
131     getchar();
132     err = Pa_StopStream( stream1 );
133     if( err != paNoError ) goto error;
134     err = Pa_StopStream( stream2 );
135     if( err != paNoError ) goto error;
136     Pa_Terminate();
137     printf("Test finished.\n");
138     return err;
139 error:
140     Pa_Terminate();
141     fprintf( stderr, "An error occured while using the portaudio stream\n" );
142     fprintf( stderr, "Error number: %d\n", err );
143     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
144     return err;
145 }
TestStart(PortAudioStream ** streamPtr,PaDeviceID devID,paTestData * data)146 PaError TestStart( PortAudioStream **streamPtr, PaDeviceID devID, paTestData *data )
147 {
148     PortAudioStream *stream;
149     PaError err;
150     int i;
151     /* initialise sinusoidal wavetable */
152     for( i=0; i<TABLE_SIZE; i++ )
153     {
154         data->sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
155     }
156     data->sine[TABLE_SIZE] = data->sine[0]; // set guard point
157     data->left_phase = data->right_phase = 0.0;
158     data->phase_increment = CalcPhaseIncrement(MIN_FREQ);
159     printf("PortAudio Test: output device = %d\n", devID );
160     err = Pa_OpenStream(
161               &stream,
162               paNoDevice,
163               0,              /* no input */
164               paFloat32,  /* 32 bit floating point input */
165               NULL,
166               devID,
167               2,              /* stereo output */
168               paFloat32,      /* 32 bit floating point output */
169               NULL,
170               SAMPLE_RATE,
171               FRAMES_PER_BUFFER,
172               NUM_BUFFERS,    /* number of buffers, if zero then use default minimum */
173               paClipOff|paDitherOff, /* we won't output out of range samples so don't bother clipping them */
174               patestCallback,
175               data );
176     if( err != paNoError ) goto error;
177     err = Pa_StartStream( stream );
178     if( err != paNoError ) goto error;
179     *streamPtr = stream;
180     return 0;
181 error:
182     return err;
183 }
184