/* BseResampler - FPU and SSE optimized FIR Resampling code
 * Copyright (C) 2006 Stefan Westerfeld
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef __BSE_RESAMPLER_HH__
#define __BSE_RESAMPLER_HH__

#include <glib.h>

G_BEGIN_DECLS

typedef void BseResampler2;

/* keep synchronized with corresponding factory enum */
typedef enum	/*< skip >*/
{
  BSE_RESAMPLER2_MODE_UPSAMPLE,
  BSE_RESAMPLER2_MODE_DOWNSAMPLE
} BseResampler2Mode;

/* keep synchronized with corresponding factory enum */
typedef enum	/*< skip >*/
{
  BSE_RESAMPLER2_PREC_48DB = 8,
  BSE_RESAMPLER2_PREC_96DB = 16
} BseResampler2Precision;

BseResampler2* bse_resampler2_create        (BseResampler2Mode      mode,
                                             BseResampler2Precision precision);
void           bse_resampler2_destroy       (BseResampler2         *resampler);
void	       bse_resampler2_process_block (BseResampler2         *resampler,
                                             const float           *input,
                                             unsigned int           n_input_samples,
					     float                 *output);
guint	       bse_resampler2_order         (BseResampler2         *resampler);

G_END_DECLS

#ifdef __cplusplus

#include <vector>

namespace Bse
{

namespace Resampler
{

/**
 * Interface for factor 2 resampling classes
 */
class Resampler2 {
public:
  /**
   * resample a data block
   */
  virtual void	  process_block (const float *input, unsigned int n_input_samples, float *output) = 0;

  /**
   * return FIR filter order
   */
  virtual guint	  order() const = 0;

  /**
   * virtual destructor for abstract class
   */
  virtual	 ~Resampler2();
};

/**
 * Factory for Resampler2 objects; will use SSE version if available
 */
class Resampler2Factory {
public:
  /**
   * FIXME: review: this could be moved out of the factory class
   */
  enum Mode {
    MODE_UPSAMPLE,
    MODE_DOWNSAMPLE
  };
  /**
   * FIXME: review: this could be moved out of the factory class
   */
  enum Precision {
    PREC_48DB = 8,
    PREC_96DB = 16
  };
  /**
   * creates a resampler
   *
   * FIXME: review: this method could also be put into Resampler2
   * (as a kind of substitute for the constructor)
   */
  static Resampler2* create (Mode mode, Precision precision);

  /**
   * default factory
   */
  static Resampler2Factory* default_factory();

  /**
   * architecture specific resampler creation function
   */
  virtual Resampler2* create_impl (Mode mode, Precision precision) = 0;

protected:
  static const double halfband_fir_96db_coeffs[32];
  static const double halfband_fir_48db_coeffs[16];

  /* Creates implementation from filter coefficients and Filter implementation class
   *
   * Since up- and downsamplers use different (scaled) coefficients, its possible
   * to specify a scaling factor. Usually 2 for upsampling and 1 for downsampling.
   */
  template<class Filter> inline Resampler2*
  create_impl_with_coeffs (const double *d,
	                   guint         order,
	                   double        scaling)
  {
    float taps[order];
    for (guint i = 0; i < order; i++)
      taps[i] = d[i] * scaling;

    Resampler2 *filter = new Filter (taps);
    g_assert (order == filter->order());
    return filter;
  }
};

} /* namespace Resampler */

} /* namespace Bse */

#endif /* __cplusplus */

#endif /* __BSE_RESAMPLER_HH__ */
