/* Simple sample rate increase using linear interpolation */

#ifndef UPSAMPLER_H
#define UPSAMPLER_H

#include <assert.h>

#ifdef __cplusplus
	extern "C" {
#endif

/* All input and output is in stereo. Individual samples are counted, not pairs,
so all counts should be a multiple of 2. */

/* Changes input/output ratio. Does *not* clear buffer, allowing changes to ratio
without disturbing sound. */
void upsampler_set_rate( int in_rate, int out_rate );

/* Clears input buffer */
void upsampler_clear( void );

/* Number of samples that can be written to buffer */
int upsampler_max_write( void );

/* Pointer to where new input samples should be written */
short* upsampler_buffer( void );

/* Tells resampler that 'count' samples have been written to input buffer.
'count' should be a multiple of 2. */
void upsampler_write( int count );

/* Resamples and reads at most 'count' samples and returns number of samples
actually written to '*out'. 'count' must be a multiple of 2. */
int upsampler_read( short* out, int count );

/* There is currently no function to find out how many output samples can be
read, since it is not at all simple to calculate. */


#ifndef RESAMPLER_BUF_SIZE
	#define RESAMPLER_BUF_SIZE (4096 + 8)
#endif

/* Private */
extern short upsampler_buf [RESAMPLER_BUF_SIZE];
extern short* upsampler_write_pos;

inline int    upsampler_max_write( void ) { return upsampler_buf + RESAMPLER_BUF_SIZE - upsampler_write_pos; }

inline short* upsampler_buffer( void ) { return upsampler_write_pos; }

inline void   upsampler_write( int count )
{
	upsampler_write_pos += count;
	
	/* fails if you overfill buffer */
	assert( upsampler_write_pos <= upsampler_buf + RESAMPLER_BUF_SIZE );
}

#ifdef __cplusplus
	}
#endif

#endif
