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
21 changes: 15 additions & 6 deletions examples/paex_write_sine.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@

#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "portaudio.h"

#define NUM_SECONDS (5)
#define SAMPLE_RATE (44100)
#define CHANNELS (2) /* Set to 1 or 2. */
#define FRAMES_PER_BUFFER (1024)

#ifndef M_PI
Expand All @@ -63,7 +65,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 @@ -72,6 +74,8 @@ int main(void)
int i, j, k;
int bufferCount;

assert(CHANNELS >= 1 && CHANNELS <= 2);

printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);

/* initialise sinusoidal wavetable */
Expand All @@ -89,7 +93,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 +126,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 +145,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