
/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: pcm/include/ALSAConverter.hh
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */

/* Short Description:
 *   ALSA IOStream data converter templates.
 */

#ifndef LOWLEVEL__ALSACONVERTER_HH__
#define LOWLEVEL__ALSACONVERTER_HH__

#include <string>
#include <iostream>

#include "ALSAHelper.hh"    // our ALSA C++ bindings
#include "BasicIOStream.hh"

namespace GSMP {
  
  class BasicALSAConverter {
  public:
    BasicALSAConverter (channel_index i_channels)
      : channels (i_channels), allocated_samples (0), samples (0) {};
    virtual ~BasicALSAConverter () {};
    
    virtual void MixWrite (BasicStream* src, local_index start_index) = 0;
    virtual snd_pcm_sframes_t Write (ALSAPCM* pcm_card) = 0;
    
    virtual snd_pcm_sframes_t Read (ALSAPCM* pcm_card,
				    local_index read_size) = 0;
    virtual void MixRead (BasicStream* dst, local_index read_size) = 0;
    
  protected:
    channel_index channels;
    local_index allocated_samples, samples;
  };
  
  template <typename T, typename I>
  class ALSAConverter : public BasicALSAConverter
  {
  public:
    ALSAConverter (channel_index i_channels)
      : BasicALSAConverter (i_channels),
	buffer (0) {
	if (!I::IsInterleaved) {
	  buffers = new T* [i_channels];
	  for (channel_index ch = 0; ch < i_channels; ++ch)
	    buffers[ch] = 0;
	}
      };
    ~ALSAConverter () {
      if (I::IsInterleaved) {
	if (buffer) {
	  delete [] buffer;
	  buffer = 0;
	}
      }
      else {
	for (channel_index ch = 0; ch < channels; ++ch) {
	  delete [] buffers[ch];
	  buffers[ch] = 0;
	}
	delete buffers;
      }
    };
    
    virtual void MixWrite (BasicStream* src, local_index start_index);
    virtual snd_pcm_sframes_t Write (ALSAPCM* pcm_card);
    
    virtual snd_pcm_sframes_t Read (ALSAPCM* pcm_card, local_index read_index);
    virtual void MixRead (BasicStream* dst, local_index read_index);
    
  protected:
    
    T* buffer;
    T** buffers;
  };

} // end namespace GSMP

  /* include template implementation */
#define LOWLEVEL__ALSACONVERTER_TMPL__
#include "template/ALSAConverter.tcc"
#undef LOWLEVEL__ALSACONVERTER_TMPL__

#endif // LOWLEVEL__ALSACONVERTER_HH__
