| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <math.h> |
| 40 |
#include <string.h> |
| 41 |
#include "portaudio.h" |
| 42 |
|
| 43 |
#define OUTPUT_DEVICE (Pa_GetDefaultOutputDeviceID()) |
| 44 |
|
| 45 |
#define SAMPLE_RATE (22050) |
| 46 |
#define FRAMES_PER_BUFFER (256) |
| 47 |
#define MIN_LATENCY_MSEC (400) |
| 48 |
#define NUM_BUFFERS ((MIN_LATENCY_MSEC * SAMPLE_RATE) / (FRAMES_PER_BUFFER * 1000)) |
| 49 |
#ifndef M_PI |
| 50 |
#define M_PI (3.14159265) |
| 51 |
#endif |
| 52 |
typedef struct |
| 53 |
{ |
| 54 |
int liveChannel; |
| 55 |
int numChannels; |
| 56 |
} |
| 57 |
paTestData; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static int patestCallback( void *inputBuffer, void *outputBuffer, |
| 63 |
unsigned long framesPerBuffer, |
| 64 |
PaTimestamp outTime, void *userData ) |
| 65 |
{ |
| 66 |
paTestData *data = (paTestData*)userData; |
| 67 |
float *out = (float*)outputBuffer; |
| 68 |
float *in = (float*)inputBuffer; |
| 69 |
int i; |
| 70 |
int finished = 0; |
| 71 |
(void) outTime; |
| 72 |
(void) inputBuffer; |
| 73 |
|
| 74 |
if( in == NULL ) return 0; |
| 75 |
for( i=0; i<(int)framesPerBuffer; i++ ) |
| 76 |
{ |
| 77 |
|
| 78 |
*out++ = in[data->liveChannel]; |
| 79 |
*out++ = in[data->liveChannel]; |
| 80 |
in += data->numChannels; |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
int PaFindDeviceByName( const char *name ) |
| 86 |
{ |
| 87 |
int i; |
| 88 |
int numDevices; |
| 89 |
const PaDeviceInfo *pdi; |
| 90 |
int len = strlen( name ); |
| 91 |
PaDeviceID result = paNoDevice; |
| 92 |
numDevices = Pa_CountDevices(); |
| 93 |
for( i=0; i<numDevices; i++ ) |
| 94 |
{ |
| 95 |
pdi = Pa_GetDeviceInfo( i ); |
| 96 |
if( strncmp( name, pdi->name, len ) == 0 ) |
| 97 |
{ |
| 98 |
result = i; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
return result; |
| 103 |
} |
| 104 |
|
| 105 |
int main(void); |
| 106 |
int main(void) |
| 107 |
{ |
| 108 |
PortAudioStream *stream; |
| 109 |
PaError err; |
| 110 |
paTestData data; |
| 111 |
int i; |
| 112 |
PaDeviceID inputDevice; |
| 113 |
const PaDeviceInfo *pdi; |
| 114 |
printf("PortAudio Test: input signal from each channel. %d buffers\n", NUM_BUFFERS ); |
| 115 |
data.liveChannel = 0; |
| 116 |
err = Pa_Initialize(); |
| 117 |
if( err != paNoError ) goto error; |
| 118 |
#ifdef INPUT_DEVICE_NAME |
| 119 |
printf("Try to use device: %s\n", INPUT_DEVICE_NAME ); |
| 120 |
inputDevice = PaFindDeviceByName(INPUT_DEVICE_NAME); |
| 121 |
if( inputDevice == paNoDevice ) |
| 122 |
{ |
| 123 |
printf("Could not find %s. Using default instead.\n", INPUT_DEVICE_NAME ); |
| 124 |
inputDevice = Pa_GetDefaultInputDeviceID(); |
| 125 |
} |
| 126 |
#else |
| 127 |
printf("Using default input device.\n"); |
| 128 |
inputDevice = Pa_GetDefaultInputDeviceID(); |
| 129 |
#endif |
| 130 |
pdi = Pa_GetDeviceInfo( inputDevice ); |
| 131 |
if( pdi == NULL ) |
| 132 |
{ |
| 133 |
printf("Could not get device info!\n"); |
| 134 |
goto error; |
| 135 |
} |
| 136 |
data.numChannels = pdi->maxInputChannels; |
| 137 |
printf("Input Device name is %s\n", pdi->name ); |
| 138 |
printf("Input Device has %d channels.\n", pdi->maxInputChannels); |
| 139 |
err = Pa_OpenStream( |
| 140 |
&stream, |
| 141 |
inputDevice, |
| 142 |
pdi->maxInputChannels, |
| 143 |
paFloat32, |
| 144 |
NULL, |
| 145 |
OUTPUT_DEVICE, |
| 146 |
2, |
| 147 |
paFloat32, |
| 148 |
NULL, |
| 149 |
SAMPLE_RATE, |
| 150 |
FRAMES_PER_BUFFER, |
| 151 |
NUM_BUFFERS, |
| 152 |
paClipOff, |
| 153 |
patestCallback, |
| 154 |
&data ); |
| 155 |
if( err != paNoError ) goto error; |
| 156 |
data.liveChannel = 0; |
| 157 |
err = Pa_StartStream( stream ); |
| 158 |
if( err != paNoError ) goto error; |
| 159 |
for( i=0; i<data.numChannels; i++ ) |
| 160 |
{ |
| 161 |
data.liveChannel = i; |
| 162 |
printf("Channel %d being sent to output. Hit ENTER for next channel.", i ); |
| 163 |
fflush(stdout); |
| 164 |
getchar(); |
| 165 |
} |
| 166 |
err = Pa_StopStream( stream ); |
| 167 |
if( err != paNoError ) goto error; |
| 168 |
|
| 169 |
err = Pa_CloseStream( stream ); |
| 170 |
Pa_Terminate(); |
| 171 |
printf("Test finished.\n"); |
| 172 |
return err; |
| 173 |
error: |
| 174 |
Pa_Terminate(); |
| •175 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 176 |
fprintf( stderr, "Error number: %d\n", err ); |
| 177 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 178 |
return err; |
| 179 |
} |