Change sine generation method so that sine waves don't line up with PA buffers.
This was causing it to appear that audio was OK even when buffers were dropped..
This commit is contained in:
parent
687dfcbe4e
commit
baefbbdc4a
1 changed files with 24 additions and 16 deletions
|
|
@ -38,18 +38,21 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "portaudio.h"
|
#include "portaudio.h"
|
||||||
|
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
#define M_PI (3.14159265)
|
#define M_PI (3.14159265)
|
||||||
#endif
|
#endif
|
||||||
|
#define TWOPI (M_PI * 2.0)
|
||||||
|
|
||||||
#define DEFAULT_BUFFER_SIZE (64)
|
#define DEFAULT_BUFFER_SIZE (64)
|
||||||
#define TABLE_SIZE (200)
|
#define TABLE_SIZE (200)
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
float sine[TABLE_SIZE];
|
double left_phase;
|
||||||
int left_phase;
|
double right_phase;
|
||||||
int right_phase;
|
|
||||||
}
|
}
|
||||||
paTestData;
|
paTestData;
|
||||||
|
|
||||||
/* Very simple synthesis routine to generate two sine waves. */
|
/* Very simple synthesis routine to generate two sine waves. */
|
||||||
static int paminlatCallback( void *inputBuffer, void *outputBuffer,
|
static int paminlatCallback( void *inputBuffer, void *outputBuffer,
|
||||||
unsigned long framesPerBuffer,
|
unsigned long framesPerBuffer,
|
||||||
|
|
@ -58,15 +61,25 @@ static int paminlatCallback( void *inputBuffer, void *outputBuffer,
|
||||||
paTestData *data = (paTestData*)userData;
|
paTestData *data = (paTestData*)userData;
|
||||||
float *out = (float*)outputBuffer;
|
float *out = (float*)outputBuffer;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
double left_phaseInc = 0.02;
|
||||||
|
double right_phaseInc = 0.06;
|
||||||
|
|
||||||
|
double left_phase = data->left_phase;
|
||||||
|
double right_phase = data->right_phase;
|
||||||
|
|
||||||
for( i=0; i<framesPerBuffer; i++ )
|
for( i=0; i<framesPerBuffer; i++ )
|
||||||
{
|
{
|
||||||
*out++ = data->sine[data->left_phase]; /* left */
|
left_phase += left_phaseInc;
|
||||||
*out++ = data->sine[data->right_phase]; /* right */
|
if( left_phase > TWOPI ) left_phase -= TWOPI;
|
||||||
data->left_phase += 1;
|
*out++ = (float) sin( left_phase );
|
||||||
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
|
|
||||||
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
|
right_phase += right_phaseInc;
|
||||||
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
|
if( right_phase > TWOPI ) right_phase -= TWOPI;
|
||||||
|
*out++ = (float) sin( right_phase );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data->left_phase = left_phase;
|
||||||
|
data->right_phase = right_phase;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
void main( int argc, char **argv );
|
void main( int argc, char **argv );
|
||||||
|
|
@ -75,7 +88,6 @@ void main( int argc, char **argv )
|
||||||
PortAudioStream *stream;
|
PortAudioStream *stream;
|
||||||
PaError err;
|
PaError err;
|
||||||
paTestData data;
|
paTestData data;
|
||||||
int i;
|
|
||||||
int go;
|
int go;
|
||||||
int numBuffers = 0;
|
int numBuffers = 0;
|
||||||
int minBuffers = 0;
|
int minBuffers = 0;
|
||||||
|
|
@ -91,12 +103,8 @@ void main( int argc, char **argv )
|
||||||
/* Get bufferSize from command line. */
|
/* Get bufferSize from command line. */
|
||||||
framesPerBuffer = ( argc > 1 ) ? atol( argv[1] ) : DEFAULT_BUFFER_SIZE;
|
framesPerBuffer = ( argc > 1 ) ? atol( argv[1] ) : DEFAULT_BUFFER_SIZE;
|
||||||
printf("Frames per buffer = %d\n", framesPerBuffer );
|
printf("Frames per buffer = %d\n", framesPerBuffer );
|
||||||
/* Initialise sinusoidal wavetable. */
|
|
||||||
for( i=0; i<TABLE_SIZE; i++ )
|
data.left_phase = data.right_phase = 0.0;
|
||||||
{
|
|
||||||
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
|
|
||||||
}
|
|
||||||
data.left_phase = data.right_phase = 0;
|
|
||||||
err = Pa_Initialize();
|
err = Pa_Initialize();
|
||||||
if( err != paNoError ) goto error;
|
if( err != paNoError ) goto error;
|
||||||
/* Ask PortAudio for the recommended minimum number of buffers. */
|
/* Ask PortAudio for the recommended minimum number of buffers. */
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue