patest1: use mono input (#706)

Avoid error when requesting stereo input.

Also removed confusing stop after 20 seconds
that conflicted with stopping by key press.

For #595
This commit is contained in:
Phil Burk 2022-05-02 19:27:59 -06:00 committed by GitHub
parent 4134a2c0a7
commit 80ef9ac9c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 37 deletions

View File

@ -49,13 +49,15 @@
#define M_PI (3.14159265)
#endif
#define SAMPLE_RATE (44100)
#define SAMPLE_RATE (44100)
#define NUM_INPUT_CHANNELS (1)
#define NUM_OUTPUT_CHANNELS (2)
#define SINE_TABLE_SIZE (100)
typedef struct
{
float sine[100];
float sine[SINE_TABLE_SIZE];
int phase;
int sampsToGo;
}
patest1data;
@ -68,38 +70,19 @@ static int patest1Callback( const void *inputBuffer, void *outputBuffer,
patest1data *data = (patest1data*)userData;
float *in = (float*)inputBuffer;
float *out = (float*)outputBuffer;
int framesToCalc = framesPerBuffer;
unsigned long i = 0;
int finished;
if( data->sampsToGo < framesPerBuffer )
{
framesToCalc = data->sampsToGo;
finished = paComplete;
}
else
{
finished = paContinue;
}
for( ; i<framesToCalc; i++ )
{
*out++ = *in++ * data->sine[data->phase]; /* left */
*out++ = *in++ * data->sine[data->phase++]; /* right */
if( data->phase >= 100 )
data->phase = 0;
}
data->sampsToGo -= framesToCalc;
/* zero remainder of final buffer if not already done */
for( ; i<framesPerBuffer; i++ )
{
*out++ = 0; /* left */
*out++ = 0; /* right */
/* Mono input, stereo output. */
float output = *in++ * data->sine[data->phase++];
if( data->phase >= SINE_TABLE_SIZE )
data->phase = 0;
*out++ = output; /* left */
*out++ = output; /* right */
}
return finished;
return paContinue;
}
int main(int argc, char* argv[]);
@ -113,13 +96,12 @@ int main(int argc, char* argv[])
const PaHostErrorInfo* herr;
printf("patest1.c\n"); fflush(stdout);
printf("Ring modulate input for 20 seconds.\n"); fflush(stdout);
printf("Ring modulate input until ENTER key pressed.\n"); fflush(stdout);
/* initialise sinusoidal wavetable */
for( i=0; i<100; i++ )
data.sine[i] = sin( ((double)i/100.) * M_PI * 2. );
for( i=0; i<SINE_TABLE_SIZE; i++ )
data.sine[i] = sin( ((double)i/SINE_TABLE_SIZE) * M_PI * 2. );
data.phase = 0;
data.sampsToGo = SAMPLE_RATE * 20; /* 20 seconds. */
/* initialise portaudio subsystem */
err = Pa_Initialize();
@ -129,7 +111,7 @@ int main(int argc, char* argv[])
fprintf(stderr, "Error: No input default device.\n");
goto done;
}
inputParameters.channelCount = 2; /* stereo input */
inputParameters.channelCount = NUM_INPUT_CHANNELS; /* mono input */
inputParameters.sampleFormat = paFloat32; /* 32 bit floating point input */
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
@ -139,7 +121,7 @@ int main(int argc, char* argv[])
fprintf(stderr,"Error: No default output device.\n");
goto done;
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.channelCount = NUM_OUTPUT_CHANNELS; /* stereo output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
@ -149,7 +131,7 @@ int main(int argc, char* argv[])
&inputParameters,
&outputParameters,
(double)SAMPLE_RATE, /* Samplerate in Hertz. */
512, /* Small buffers */
256, /* Small buffers */
paClipOff, /* We won't output out of range samples so don't bother clipping them. */
patest1Callback,
&data );
@ -168,7 +150,7 @@ int main(int argc, char* argv[])
printf( "Waiting for stream to complete...\n" );
/* sleep until playback has finished */
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(1000);
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
if( err < 0 ) goto done;
err = Pa_CloseStream( stream );