Skip to content

Commit

Permalink
wasapi: Fixed Pa_ReadStream()/Pa_WriteStream() for 1-channel (Mono) s…
Browse files Browse the repository at this point in the history
…tream, made some examples configurable for 1-channel operation for easier testing of Mono I/O
  • Loading branch information
dmitrykos committed Jul 26, 2024
1 parent 09b7731 commit 6248ea0
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 138 deletions.
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)
#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
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)
{
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);
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)
#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

0 comments on commit 6248ea0

Please sign in to comment.