2
* $Id: debug_dual.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
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.
8
* Author: Phil Burk <philburk@softsynth.com>
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
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:
22
* The above copyright notice and this permission notice shall be
23
* included in all copies or substantial portions of the Software.
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.
35
* The text above constitutes the entire PortAudio license; however,
36
* the PortAudio community also makes the following non-binding requests:
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
47
#include "portaudio.h"
50
#define NUM_SECONDS (8)
51
#define SLEEP_DUR (800)
52
#define SAMPLE_RATE (44100)
53
#define FRAMES_PER_BUFFER (256)
55
#define MIN_LATENCY_MSEC (200)
56
#define NUM_BUFFERS ((MIN_LATENCY_MSEC * SAMPLE_RATE) / (FRAMES_PER_BUFFER * 1000))
58
#define NUM_BUFFERS (0)
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)
65
#define M_PI (3.14159265)
67
#define TABLE_SIZE (400)
70
float sine[TABLE_SIZE + 1]; // add one for guard point for interpolation
71
float phase_increment;
76
/* Convert phase between and 1.0 to sine value
77
* using linear interpolation.
79
float LookupSine( paTestData *data, float phase );
80
float LookupSine( paTestData *data, float phase )
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);
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().
94
static int patestCallback( void *inputBuffer, void *outputBuffer,
95
unsigned long framesPerBuffer,
96
PaTimestamp outTime, void *userData )
98
paTestData *data = (paTestData*)userData;
99
float *out = (float*)outputBuffer;
102
(void) outTime; /* Prevent unused variable warnings. */
106
for( i=0; i<framesPerBuffer; i++ )
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);
121
PaError TestStart( PortAudioStream **streamPtr, PaDeviceID devID,
123
/*******************************************************************/
127
PortAudioStream *stream1, *stream2;
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");
139
err = Pa_StopStream( stream1 );
140
if( err != paNoError ) goto error;
141
err = Pa_StopStream( stream2 );
142
if( err != paNoError ) goto error;
144
printf("Test finished.\n");
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 ) );
153
PaError TestStart( PortAudioStream **streamPtr, PaDeviceID devID, paTestData *data )
155
PortAudioStream *stream;
158
/* initialise sinusoidal wavetable */
159
for( i=0; i<TABLE_SIZE; i++ )
161
data->sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
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 );
171
paFloat32, /* 32 bit floating point input */
174
2, /* stereo output */
175
paFloat32, /* 32 bit floating point output */
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 */
183
if( err != paNoError ) goto error;
184
err = Pa_StartStream( stream );
185
if( err != paNoError ) goto error;