Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasapi: Fixed Pa_ReadStream()/Pa_WriteStream() for 1-channel (Mono) I/O #948

Merged
39 changes: 33 additions & 6 deletions examples/paex_read_write_wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
#include <string.h>
#include "portaudio.h"

/* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (512)
#define NUM_SECONDS (10)
/* #define DITHER_FLAG (paDitherOff) */
#define DITHER_FLAG (0)
/*#define SAMPLE_RATE (17932) // Test failure to open with this value. */
#define SAMPLE_RATE (44100)
#define CHANNELS (2) /* set to 0 to use all device channels */
#define FRAMES_PER_BUFFER (512)
#define NUM_SECONDS (10)
/*#define DITHER_FLAG (paDitherOff) */
#define DITHER_FLAG (0)
#define USE_LOOPBACK_INPUT (0)

/* Select sample format. */
#if 1
Expand Down Expand Up @@ -105,6 +107,30 @@ int main(void)
if( err != paNoError ) goto error2;

inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */

#if USE_LOOPBACK_INPUT
dmitrykos marked this conversation as resolved.
Show resolved Hide resolved
for (i = Pa_GetDeviceCount() - 1; i >= 0; i--)
{
const PaDeviceInfo* device;
if ((device = Pa_GetDeviceInfo(i)) != NULL)
{
// Note: [Loopback] name postfix is provided by the WASAPI device only
if (strstr(device->name, "[Loopback]") != NULL)
dmitrykos marked this conversation as resolved.
Show resolved Hide resolved
{
inputParameters.device = i;
inputInfo = device;
break;
}
}
}

if (inputInfo == NULL)
{
fprintf(stderr, "Error: Loopback device not found.\n");
return -1;
}
#endif

printf( "Input device # %d.\n", inputParameters.device );
inputInfo = Pa_GetDeviceInfo( inputParameters.device );
printf( " Name: %s\n", inputInfo->name );
Expand All @@ -120,6 +146,7 @@ int main(void)

numChannels = inputInfo->maxInputChannels < outputInfo->maxOutputChannels
? inputInfo->maxInputChannels : outputInfo->maxOutputChannels;
numChannels = (CHANNELS ? CHANNELS : numChannels);
dmitrykos marked this conversation as resolved.
Show resolved Hide resolved
printf( "Num channels = %d.\n", numChannels );

inputParameters.channelCount = numChannels;
Expand Down
18 changes: 12 additions & 6 deletions examples/paex_write_sine.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#define NUM_SECONDS (5)
#define SAMPLE_RATE (44100)
#define CHANNELS (2)
dmitrykos marked this conversation as resolved.
Show resolved Hide resolved
#define FRAMES_PER_BUFFER (1024)

#ifndef M_PI
Expand All @@ -63,7 +64,7 @@ int main(void)
PaStreamParameters outputParameters;
PaStream *stream;
PaError err;
float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
float buffer[FRAMES_PER_BUFFER][CHANNELS]; /* stereo output buffer */
float sine[TABLE_SIZE]; /* sine wavetable */
int left_phase = 0;
int right_phase = 0;
Expand All @@ -89,7 +90,7 @@ int main(void)
fprintf(stderr,"Error: No default output device.\n");
goto error;
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.channelCount = CHANNELS;
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = 0.050; // Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
Expand Down Expand Up @@ -122,11 +123,15 @@ int main(void)
for( j=0; j < FRAMES_PER_BUFFER; j++ )
{
buffer[j][0] = sine[left_phase]; /* left */
buffer[j][1] = sine[right_phase]; /* right */
left_phase += left_inc;
if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
right_phase += right_inc;
if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;

if (CHANNELS == 2)
{
buffer[j][1] = sine[right_phase]; /* right */
right_phase += right_inc;
if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
}
}

err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
Expand All @@ -137,7 +142,8 @@ int main(void)
if( err != paNoError ) goto error;

++left_inc;
++right_inc;
if (CHANNELS == 2)
++right_inc;

Pa_Sleep( 1000 );
}
Expand Down
Loading
Loading