
#ifndef __TK_PASTREAM_H__
#define __TK_PASTREAM_H__

YG("portaudio")

#include <pthread.h>

#include <alsa/asoundlib.h>



/* @class PaStream,Object
 */
YC class _PaStream : public YAC_Object {
  public:
   void *stream_cbk; // Function object/handle
   void *stream_cbk_context; // YAC script context handle

   YAC_FloatArray *input_buf;
   YAC_FloatArray *output_buf;

   snd_pcm_t *handle;
   sBool     b_hw;
   sBool     b_running;
   sBool     b_active;
   sBool     b_processing_cbk;
   pthread_t pthread_id;

   unsigned char out_buf[TKPORTAUDIO_MAX_BUFSIZE];
   sUI period_size; // actual alsa period size, ideally = PAR_LATENCY
   sUI num_periods;
   sF32 dbg_avg_avail;

   sUI sample_rate;
   sSI sample_format; // paInt8, paInt16, paInt24, paFloat32

   sUI num_left; // number of fragments in out_buf that can be written to device

  public:
   void waitProcessCbk(void);
   sSI pcmAvailUpdateRecover(void);

   sBool hwParamsSet(snd_pcm_hw_params_t *hwparams, int _line);
   sBool hwParamsGet(snd_pcm_hw_params_t *hwparams, int _line);

  public:
   _PaStream(void);
   ~_PaStream();

   YAC(_PaStream);

   // for internal use only
   sBool initStream(void *_cbk,
                    _PaStreamParameters *_inParams,
                    _PaStreamParameters *_outParams,
                    sUI _sampleRate
                    );

   sBool initStream2(void *_cbk,
                    _PaStreamParameters *_inParams,
                    _PaStreamParameters *_outParams,
                     sUI _sampleRate,
                     sUI _periodSize,
                     sUI _bufferSize
                    );


   /* @method start
      Start audio I/O
    */
   YM void _start(void);


   /* @method stop
      Stop audio I/O
    */
   YM void _stop(void);

   
   /* @method abort
    */
   YM void _abort(void);


   /* @method close
      Close stream and free resources.
    */
   YM void _close(void);


   /* @method isStopped:boolean
    */
   YM sSI _isStopped(void);


   /* @method isActive:boolean
    */
   YM sSI _isActive(void);


   /* @method getCpuLoad:float
    */
   YM sF32 _getCpuLoad(void);


   /* @method getNumInputChannels:int
    */
   YM sSI _getNumInputChannels(void);


   /* @method getNumOutputChannels:int
    */
   YM sSI _getNumOutputChannels(void);


   /* @method getInputLatency:float
    */
   YM sF32 _getInputLatency(void);


   /* @method getOutputLatency:float
    */
   YM sF32 _getOutputLatency(void);


   /* @method getSampleRate:float
    */
   YM sF32 _getSampleRate(void);
};


#endif // __TK_PASTREAM_H__
