changeset 4300:060c9865ea17

forgotten files
author Eugene Zagidullin <e.asphyx@gmail.com>
date Sat, 23 Feb 2008 16:37:02 +0300
parents a16edefb8836
children 3f5f638c055b
files src/audacious/af_compat.h src/audacious/af_equalizer.c src/audacious/equalizer_flow.c src/audacious/equalizer_flow.h
diffstat 4 files changed, 497 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/audacious/af_compat.h	Sat Feb 23 16:37:02 2008 +0300
@@ -0,0 +1,113 @@
+/*
+ * MPlayer libaf compatibility stuff
+ */
+
+#ifndef AF_COMPAT_H
+#define AF_COMPAT_H
+
+#include <glib.h>
+#include "main.h"
+
+/* Number of channels */
+#ifndef AF_NCH
+#define AF_NCH 6
+#endif
+
+/* Format */
+#define AF_FORMAT_BE		(0<<0) // Big Endian
+#define AF_FORMAT_LE		(1<<0) // Little Endian
+#define AF_FORMAT_F		(1<<2) // Foating point
+#define AF_FORMAT_32BIT		(3<<3)
+#define AF_FORMAT_FLOAT_LE	(AF_FORMAT_F|AF_FORMAT_32BIT|AF_FORMAT_LE)
+#define AF_FORMAT_FLOAT_BE	(AF_FORMAT_F|AF_FORMAT_32BIT|AF_FORMAT_BE)
+
+#if G_BYTE_ORDER == G_BIG_ENDIAN // Native endian of cpu
+#define AF_FORMAT_FLOAT_NE AF_FORMAT_FLOAT_BE
+#else
+#define AF_FORMAT_FLOAT_NE AF_FORMAT_FLOAT_LE
+#endif
+
+#define AF_MSG_INFO	 0 ///< Important information
+
+#define af_msg(a,...) AUDDBG(__VA_ARGS__);
+
+/* Control */
+#define AF_CONTROL_SET			0x00000000
+#define AF_CONTROL_GET			0x00000001
+
+#define AF_CONTROL_MANDATORY		0x10000000
+#define AF_CONTROL_OPTIONAL		0x20000000
+#define AF_CONTROL_FILTER_SPECIFIC	0x40000000
+
+#define AF_CONTROL_REINIT  		0x00000100 | AF_CONTROL_MANDATORY
+#define AF_CONTROL_COMMAND_LINE		0x00000300 | AF_CONTROL_OPTIONAL
+#define AF_CONTROL_EQUALIZER_GAIN 	0x00001C00 | AF_CONTROL_FILTER_SPECIFIC
+
+/* Return values */
+#define AF_DETACH   2
+#define AF_OK       1
+#define AF_TRUE     1
+#define AF_FALSE    0
+#define AF_UNKNOWN -1
+#define AF_ERROR   -2
+#define AF_FATAL   -3
+
+/* Flags used for defining the behavior of an audio filter */
+#define AF_FLAGS_REENTRANT 	0x00000000
+#define AF_FLAGS_NOT_REENTRANT 	0x00000001
+
+/* Audio data chunk */
+typedef struct af_data_s
+{
+  void* audio;  /* data buffer */
+  int len;      /* buffer length */
+  int rate;	/* sample rate */
+  int nch;	/* number of channels */
+  int format;	/* format */
+  int bps; 	/* bytes per sample */
+} af_data_t;
+
+struct af_instance_s;
+/* Audio filter information not specific for current instance, but for
+   a specific filter */ 
+typedef struct af_info_s 
+{
+  const char *info;
+  const char *name;
+  const char *author;
+  const char *comment;
+  const int flags;
+  int (*open)(struct af_instance_s* vf);
+} af_info_t;
+
+/* Linked list of audio filters */
+typedef struct af_instance_s
+{
+  af_info_t* info;
+  int (*control)(struct af_instance_s* af, int cmd, void* arg);
+  void (*uninit)(struct af_instance_s* af);
+  af_data_t* (*play)(struct af_instance_s* af, af_data_t* data);
+  void* setup;	  // setup data for this specific instance and filter
+  af_data_t* data; // configuration for outgoing data stream
+  struct af_instance_s* next;
+  struct af_instance_s* prev;  
+  double delay; /* Delay caused by the filter, in units of bytes read without
+		 * corresponding output */
+  double mul; /* length multiplier: how much does this instance change
+		 the length of the buffer. */
+}af_instance_t;
+
+/*********************************************
+  Extended control used with arguments that operates on only one
+  channel at the time
+*/
+typedef struct af_control_ext_s{
+  void* arg;	// Argument
+  int	ch;	// Chanel number
+}af_control_ext_t;
+
+#ifndef clamp
+#define clamp(a,min,max) (((a)>(max))?(max):(((a)<(min))?(min):(a)))
+#endif
+
+#endif /* AF_COMPAT_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/audacious/af_equalizer.c	Sat Feb 23 16:37:02 2008 +0300
@@ -0,0 +1,262 @@
+/*=============================================================================
+//	
+//  This software has been released under the terms of the GNU General Public
+//  license. See http://www.gnu.org/copyleft/gpl.html for details.
+//
+//  Copyright 2001 Anders Johansson ajh@atri.curtin.edu.au
+//
+//=============================================================================
+*/
+
+/* Equalizer filter, implementation of a 10 band time domain graphic
+   equalizer using IIR filters. The IIR filters are implemented using a
+   Direct Form II approach, but has been modified (b1 == 0 always) to
+   save computation.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <inttypes.h>
+#include <math.h>
+
+#include "af_compat.h"
+
+#define L   	2      // Storage for filter taps
+#define KM  	10     // Max number of bands 
+
+#define Q   1.2247449
+/* Q value for band-pass filters 1.2247=(3/2)^(1/2)
+   gives 4dB suppression @ Fc*2 and Fc/2 */
+
+/* Center frequencies for band-pass filters
+   The different frequency bands are:	
+   nr.    	center frequency
+   0  	31.25 Hz
+   1 	62.50 Hz
+   2	125.0 Hz
+   3	250.0 Hz
+   4	500.0 Hz
+   5	1.000 kHz
+   6	2.000 kHz
+   7	4.000 kHz
+   8	8.000 kHz
+   9 	16.00 kHz
+*/
+#define CF  	{31.25,62.5,125,250,500,1000,2000,4000,8000,16000}
+
+// Maximum and minimum gain for the bands
+#define G_MAX	+12.0
+#define G_MIN	-12.0	
+
+// Data for specific instances of this filter
+typedef struct af_equalizer_s
+{
+  float   a[KM][L];        	// A weights
+  float   b[KM][L];	     	// B weights
+  float   wq[AF_NCH][KM][L];  	// Circular buffer for W data
+  float   g[AF_NCH][KM];      	// Gain factor for each channel and band
+  int     K; 		   	// Number of used eq bands
+  int     channels;        	// Number of channels
+  float   gain_factor;     // applied at output to avoid clipping
+} af_equalizer_t;
+
+static int af_test_output(struct af_instance_s* af, af_data_t* out)
+{
+  if((af->data->format != out->format) || 
+     (af->data->bps    != out->bps)    ||
+     (af->data->rate   != out->rate)   ||
+     (af->data->nch    != out->nch)){
+    memcpy(out,af->data,sizeof(af_data_t));
+    return AF_FALSE;
+  }
+  return AF_OK;
+}
+
+// 2nd order Band-pass Filter design
+static void bp2(float* a, float* b, float fc, float q){
+  double th= 2.0 * M_PI * fc;
+  double C = (1.0 - tan(th*q/2.0))/(1.0 + tan(th*q/2.0));
+
+  a[0] = (1.0 + C) * cos(th);
+  a[1] = -1 * C;
+  
+  b[0] = (1.0 - C)/2.0;
+  b[1] = -1.0050;
+}
+
+// Initialization and runtime control
+static int control(struct af_instance_s* af, int cmd, void* arg)
+{
+  af_equalizer_t* s   = (af_equalizer_t*)af->setup; 
+
+  switch(cmd){
+  case AF_CONTROL_REINIT:{
+    int k =0, i =0;
+    float F[KM] = CF;
+    
+    s->gain_factor=0.0;
+
+    // Sanity check
+    if(!arg) return AF_ERROR;
+    
+    af->data->rate   = ((af_data_t*)arg)->rate;
+    af->data->nch    = ((af_data_t*)arg)->nch;
+    af->data->format = AF_FORMAT_FLOAT_NE;
+    af->data->bps    = 4;
+    
+    // Calculate number of active filters
+    s->K=KM;
+    while(F[s->K-1] > (float)af->data->rate/2.2)
+      s->K--;
+    
+    if(s->K != KM)
+      af_msg(AF_MSG_INFO,"[equalizer] Limiting the number of filters to" 
+	     " %i due to low sample rate.\n",s->K);
+
+    // Generate filter taps
+    for(k=0;k<s->K;k++)
+      bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q);
+
+    // Calculate how much this plugin adds to the overall time delay
+    af->delay = 2 * af->data->nch * af->data->bps;
+    
+    // Calculate gain factor to prevent clipping at output
+    for(k=0;k<AF_NCH;k++)
+    {
+        for(i=0;i<KM;i++)
+        {
+            if(s->gain_factor < s->g[k][i]) s->gain_factor=s->g[k][i];
+        }
+    }
+
+    s->gain_factor=log10(s->gain_factor + 1.0) * 20.0;
+	 
+    if(s->gain_factor > 0.0)
+    {
+        s->gain_factor=0.1+(s->gain_factor/12.0);
+    }else{
+        s->gain_factor=1;
+    }
+	
+    return af_test_output(af,arg);
+  }
+  case AF_CONTROL_COMMAND_LINE:{
+    float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
+    int i,j;
+    sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1], 
+	   &g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]);
+    for(i=0;i<AF_NCH;i++){
+      for(j=0;j<KM;j++){
+	((af_equalizer_t*)af->setup)->g[i][j] = 
+	  pow(10.0,clamp(g[j],G_MIN,G_MAX)/20.0)-1.0;
+      }
+    }
+    return AF_OK;
+  }
+  case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_SET:{
+    float* gain = ((af_control_ext_t*)arg)->arg;
+    int    ch   = ((af_control_ext_t*)arg)->ch;
+    int    k;
+    if(ch >= AF_NCH || ch < 0)
+      return AF_ERROR;
+
+    for(k = 0 ; k<KM ; k++)
+      s->g[ch][k] = pow(10.0,clamp(gain[k],G_MIN,G_MAX)/20.0)-1.0;
+
+    return AF_OK;
+  }
+  case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_GET:{
+    float* gain = ((af_control_ext_t*)arg)->arg;
+    int    ch   = ((af_control_ext_t*)arg)->ch;
+    int    k;
+    if(ch >= AF_NCH || ch < 0)
+      return AF_ERROR;
+
+    for(k = 0 ; k<KM ; k++)
+      gain[k] = log10(s->g[ch][k]+1.0) * 20.0;
+
+    return AF_OK;
+  }
+  }
+  return AF_UNKNOWN;
+}
+
+// Deallocate memory 
+static void uninit(struct af_instance_s* af)
+{
+  if(af->data)
+    free(af->data);
+  if(af->setup)
+    free(af->setup);
+}
+
+// Filter data through filter
+static af_data_t* play(struct af_instance_s* af, af_data_t* data)
+{
+  af_data_t*       c 	= data;			    	// Current working data
+  af_equalizer_t*  s 	= (af_equalizer_t*)af->setup; 	// Setup 
+  uint32_t  	   ci  	= af->data->nch; 	    	// Index for channels
+  uint32_t	   nch 	= af->data->nch;   	    	// Number of channels
+
+  while(ci--){
+    float*	g   = s->g[ci];      // Gain factor 
+    float*	in  = ((float*)c->audio)+ci;
+    float*	out = ((float*)c->audio)+ci;
+    float* 	end = in + c->len/4; // Block loop end
+
+    while(in < end){
+      register int	k  = 0;		// Frequency band index
+      register float 	yt = *in; 	// Current input sample
+      in+=nch;
+      
+      // Run the filters
+      for(;k<s->K;k++){
+ 	// Pointer to circular buffer wq
+ 	register float* wq = s->wq[ci][k];
+ 	// Calculate output from AR part of current filter
+ 	register float w=yt*s->b[k][0] + wq[0]*s->a[k][0] + wq[1]*s->a[k][1];
+ 	// Calculate output form MA part of current filter
+ 	yt+=(w + wq[1]*s->b[k][1])*g[k];
+ 	// Update circular buffer
+ 	wq[1] = wq[0];
+	wq[0] = w;
+      }
+      // Calculate output 
+      *out=yt*s->gain_factor;
+      out+=nch;
+    }
+  }
+  return c;
+}
+
+// Allocate memory and set function pointers
+int equalizer_open(af_instance_t* af){
+  af->control=control;
+  af->uninit=uninit;
+  af->play=play;
+  af->mul=1;
+  af->data=calloc(1,sizeof(af_data_t));
+  af->setup=calloc(1,sizeof(af_equalizer_t));
+  if(af->data == NULL || af->setup == NULL)
+    return AF_ERROR;
+  return AF_OK;
+}
+
+// Description of this filter
+/*af_info_t af_info_equalizer = {
+  "Equalizer audio filter",
+  "equalizer",
+  "Anders",
+  "",
+  AF_FLAGS_NOT_REENTRANT,
+  af_open
+};*/
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/audacious/equalizer_flow.c	Sat Feb 23 16:37:02 2008 +0300
@@ -0,0 +1,112 @@
+/*  Audacious - Cross-platform multimedia player
+ *  Copyright (C) 2005-2008  Audacious team
+ *
+ *  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; under version 3 of the License.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses>.
+ *
+ *  The Audacious team does not consider modular code linking to
+ *  Audacious or using our public API to be a derived work.
+ */
+
+#define AUD_DEBUG
+
+#include <glib.h>
+#include "main.h"
+#include "plugin.h"
+#include "flow.h"
+
+#include "af_compat.h"
+#include "equalizer_flow.h"
+
+int equalizer_open(af_instance_t* af); /* af_equalizer.c */
+
+static af_instance_t *eq = NULL;
+static gint eq_nch = 0;
+static gint eq_rate = 0;
+static gboolean bands_changed = FALSE;
+
+static void
+equalizer_flow_reinit(gint rate, gint nch)
+{
+    af_data_t data;
+
+    AUDDBG("\n");
+    if(eq == NULL) return;
+
+    data.rate = rate;
+    data.nch = nch;
+    data.bps = 4;
+    data.format = AF_FORMAT_FLOAT_NE;
+    eq->control(eq, AF_CONTROL_REINIT, &data);
+}
+
+void
+equalizer_flow(FlowContext *context)
+{
+    af_data_t data;
+
+    if(!cfg.equalizer_active || eq == NULL) return;
+
+    if(context->fmt != FMT_FLOAT) {
+        context->error = TRUE;
+        return;
+    }
+
+    if(eq_nch != context->channels ||
+       eq_rate != context->srate ||
+       bands_changed) {
+        equalizer_flow_reinit(context->srate, context->channels);
+        eq_nch = context->channels;
+        eq_rate = context->srate;
+        bands_changed = FALSE;
+    }
+    
+    data.nch = context->channels;
+    data.audio = context->data;
+    data.len = context->len;
+    eq->play(eq, &data);
+}
+
+void
+equalizer_flow_set_bands(gfloat pre, gfloat *bands)
+{
+    int i;
+    af_control_ext_t ctl;
+    AUDDBG("\n");
+    
+    if(eq == NULL) {
+        eq = g_malloc(sizeof(af_instance_t));
+        equalizer_open(eq);
+    }
+    
+    ctl.arg = bands;
+    for(i = 0; i < AF_NCH; i++) {
+        ctl.ch = i;
+        eq->control(eq, AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_SET, &ctl);
+    }
+
+    bands_changed = TRUE;
+}
+
+void
+equalizer_flow_free()
+{
+    AUDDBG("\n");
+    if(eq != NULL) {
+        eq->uninit(eq);
+        g_free(eq);
+        eq = NULL;
+        eq_nch = 0;
+        eq_rate = 0;
+        bands_changed = FALSE;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/audacious/equalizer_flow.h	Sat Feb 23 16:37:02 2008 +0300
@@ -0,0 +1,10 @@
+#ifndef EQUALIZER_FLOW_H
+#define EQUALIZER_FLOW_H
+
+#include "flow.h"
+
+void equalizer_flow(FlowContext *context);
+void equalizer_flow_set_bands(gfloat pre, gfloat *bands);
+void equalizer_flow_free();
+
+#endif