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 22, 2024
1 parent 09b7731 commit f49366e
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 121 deletions.
32 changes: 29 additions & 3 deletions examples/paex_read_write_wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@

/* #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 CHANNELS (2)
#define FRAMES_PER_BUFFER (1024)
#define NUM_SECONDS (10)
/* #define DITHER_FLAG (paDitherOff) */
#define DITHER_FLAG (0)
#define DITHER_FLAG (0)
#define LISTEN_LOOPBACK (0)

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

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

#if LISTEN_LOOPBACK
for (i = Pa_GetDeviceCount() - 1; i >= 0; i--)
{
const PaDeviceInfo* device;
if ((device = Pa_GetDeviceInfo(i)) != NULL)
{
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 +145,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 f49366e

Please sign in to comment.