
/*
 * --- 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/ALSAHelper.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 C++ wrapper
 */

#ifndef LOWLEVEL__ALSAHELPER_HH__
#define LOWLEVEL__ALSAHELPER_HH__

// ALSA stuff (version 1.0.0)
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#include <alsa/asoundlib.h>

#include <string>

namespace GSMP
{
  class ALSAControl
  {
  public:
    ALSAControl ();
    ~ALSAControl ();
    
    std::string GetName (int index);
    
    bool Open (const std::string& name, int mode);
    bool Close ();
    
  protected:
    /* direct ALSA states */
    snd_ctl_t* handle;
    
    /* states */
    bool opened;
    int err;
    
    /* config */
    std::string cname;
  };
  
  class ALSAPCM
  {
  public:
    
    ALSAPCM ();
    ~ALSAPCM ();
    
    bool Open (const std::string& name, snd_pcm_stream_t stream, int mode);
    bool Close ();
    
    snd_pcm_type_t Type ();
    
    /* HW Config */
    bool InstallParams ();
    
    bool SetAccess (snd_pcm_access_t access_mode);
    int GetAccess ();
    
    bool SetFormat (snd_pcm_format_t format);
    snd_pcm_format_t GetFormat ();
    
    bool SetRate (unsigned int rate);
    bool SetRateNear (unsigned int rate);
    int GetRateMin ();
    int GetRateMax ();
    int GetRate ();
    
    bool SetChannels (unsigned int channels);
    int GetChannelsMin ();
    int GetChannelsMax ();
    int GetChannels ();
    
    bool SetBufferTimeNear (unsigned int time);
    bool SetBufferSizeNear (snd_pcm_uframes_t size);
    bool SetBufferSize (snd_pcm_uframes_t size);
    int GetBufferTime ();
    snd_pcm_uframes_t GetBufferSize ();
    
    bool SetPeriodTimeNear (unsigned int time);
    bool SetPeriodSizeNear (snd_pcm_uframes_t size);
    bool SetPeriodSize (snd_pcm_uframes_t size);
    int GetPeriodTime ();
    int GetPeriodSize ();
    
    bool SetPeriods (unsigned int periods);
    bool SetPeriodsNear (unsigned int periods);
    int GetPeriodsMin ();
    int GetPeriodsMax ();
    int GetPeriods ();
    
    /* SW Config */
    bool InstallSwParams ();
    
    bool SetSwStartThreshold (snd_pcm_uframes_t count);
    snd_pcm_uframes_t GetSwStartThreshold ();
    
    bool SetSwStopThreshold (snd_pcm_uframes_t count);
    snd_pcm_uframes_t GetSwStopThreshold ();
    
    /* Control / IO */
    bool Prepare ();
    bool Drain ();
    bool Start ();
    
    // N: non_interleaved - I:interleaved
    snd_pcm_sframes_t WriteI (const void* buffer, snd_pcm_uframes_t size);
    snd_pcm_sframes_t ReadI (void* buffer, snd_pcm_uframes_t size);
    
    snd_pcm_sframes_t WriteN (void** buffers, snd_pcm_uframes_t size);
    snd_pcm_sframes_t ReadN (void** buffers, snd_pcm_uframes_t size);
    
    /* Status stuff */
    bool UpdateStatus ();
    
    snd_pcm_state_t GetStatusState ();
    void GetTriggerTStamp (snd_timestamp_t* ptr);
    void GetTStamp (snd_timestamp_t* ptr);
    snd_pcm_sframes_t GetStatusDelay ();
    snd_pcm_uframes_t GetStatusAvail ();
    snd_pcm_uframes_t GetStatusAvailMax ();
    
  protected:
    /* direct ALSA states */
    snd_ctl_t* ctl_handle;
    snd_pcm_t* handle;
    
    snd_pcm_hw_params_t* hw_params;
    snd_pcm_sw_params_t* sw_params;
    snd_pcm_status_t* status;
    
    /* states */
    bool opened;
    int err; // the ALSA return code - used in each function ...
    
    /* Config */
    std::string cname;
  };

} // namespace GSMP

#endif // LOWLEVEL__ALSAHELPER_HH__
