#ifndef _AUDIOOUT_H
#define _AUDIOOUT_H

/**
 * This is a special midiplayer_t wrapper for sample-accurate wave-output production.
 * 
 * Based on the specifyed samplerate it provides
 *   ao_execute_midi(...,num_samples_to_execute_midi_for)
 * encapsulating
 *   midiplayer_runto(...,abs_tickpos,...)
 * 
 * The midi events will be delivered to (*outfn)().
 *
 * NOTE: midiplayer_* can still be called directly.
 *   Seeking, for example, will probably not need sample-accurate 
 *   precision. Then to resync audio_out_t  call ao_seek().
 *
 */

#include "midiplayer.h"

typedef struct {
  MIDIPLAYER_OUTFN outfn;
  void *outdata;
  midiplayer_t *player;  // "public"

  int samplerate,ppq;
  int samples_per_quarter;
  // our abstickpos is calculated from these:
  int quarter_pos,q_sample_pos; // always in terms of latest tempo
  float temposcale;
} audio_out_t;

// allows player==NULL   as pre-initialization
void ao_setup(audio_out_t *ao,midiplayer_t *player,int samplerate,MIDIPLAYER_OUTFN outfn,void *outdata);

// will not call midiplayer:
void ao_rewind(audio_out_t *ao); // also required after reassignment of .player! (which has to be in rewinded state)

// will call midiplayer:    -- abstime<0 will reinit midi and call ao_rewind
void ao_simple_seek(audio_out_t *ao,float abstime);

// will not call midiplayer:
void ao_seek(audio_out_t *ao,int abstickpos,int usec_per_quarter); // ...current tempo

void ao_execute_midi(audio_out_t *ao,int samples);

void ao_set_tempo_scaling(audio_out_t *ao,float factor); // 1.0 is normal speed

#endif
