Convert another forgotten ADD_ALL_EXESUFS call.
[mplayer/glamo.git] / libaf / af_gate.c
blob26c7e8ad03a532fbfd98ecfe89f8d9f7215cf8e6
1 /*
2 * Copyright (C) 2002 Anders Johansson ajh@atri.curtin.edu.au
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <inttypes.h>
26 #include <math.h>
27 #include <limits.h>
29 #include "af.h"
31 // Data for specific instances of this filter
32 typedef struct af_gate_s
34 int enable[AF_NCH]; // Enable/disable / channel
35 float time[AF_NCH]; // Forgetting factor for power estimate
36 float pow[AF_NCH]; // Estimated power level [dB]
37 float tresh[AF_NCH]; // Threshold [dB]
38 int attack[AF_NCH]; // Attack time [ms]
39 int release[AF_NCH]; // Release time [ms]
40 float range[AF_NCH]; // Range level [dB]
41 }af_gate_t;
43 // Initialization and runtime control
44 static int control(struct af_instance_s* af, int cmd, void* arg)
46 af_gate_t* s = (af_gate_t*)af->setup;
47 switch(cmd){
48 case AF_CONTROL_REINIT:
49 // Sanity check
50 if(!arg) return AF_ERROR;
52 af->data->rate = ((af_data_t*)arg)->rate;
53 af->data->nch = ((af_data_t*)arg)->nch;
54 af->data->format = AF_FORMAT_FLOAT_NE;
55 af->data->bps = 4;
57 // Time constant set to 0.1s
58 // s->alpha = (1.0/0.2)/(2.0*M_PI*(float)((af_data_t*)arg)->rate);
59 return af_test_output(af,(af_data_t*)arg);
60 case AF_CONTROL_COMMAND_LINE:{
61 /* float v=-10.0; */
62 /* float vol[AF_NCH]; */
63 /* float s=0.0; */
64 /* float clipp[AF_NCH]; */
65 /* int i; */
66 /* sscanf((char*)arg,"%f:%f", &v, &s); */
67 /* for(i=0;i<AF_NCH;i++){ */
68 /* vol[i]=v; */
69 /* clipp[i]=s; */
70 /* } */
71 /* if(AF_OK != control(af,AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_SET, clipp)) */
72 /* return AF_ERROR; */
73 /* return control(af,AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, vol); */
76 case AF_CONTROL_GATE_ON_OFF | AF_CONTROL_SET:
77 memcpy(s->enable,(int*)arg,AF_NCH*sizeof(int));
78 return AF_OK;
79 case AF_CONTROL_GATE_ON_OFF | AF_CONTROL_GET:
80 memcpy((int*)arg,s->enable,AF_NCH*sizeof(int));
81 return AF_OK;
82 case AF_CONTROL_GATE_THRESH | AF_CONTROL_SET:
83 return af_from_dB(AF_NCH,(float*)arg,s->tresh,20.0,-60.0,-1.0);
84 case AF_CONTROL_GATE_THRESH | AF_CONTROL_GET:
85 return af_to_dB(AF_NCH,s->tresh,(float*)arg,10.0);
86 case AF_CONTROL_GATE_ATTACK | AF_CONTROL_SET:
87 return af_from_ms(AF_NCH,(float*)arg,s->attack,af->data->rate,500.0,0.1);
88 case AF_CONTROL_GATE_ATTACK | AF_CONTROL_GET:
89 return af_to_ms(AF_NCH,s->attack,(float*)arg,af->data->rate);
90 case AF_CONTROL_GATE_RELEASE | AF_CONTROL_SET:
91 return af_from_ms(AF_NCH,(float*)arg,s->release,af->data->rate,3000.0,10.0);
92 case AF_CONTROL_GATE_RELEASE | AF_CONTROL_GET:
93 return af_to_ms(AF_NCH,s->release,(float*)arg,af->data->rate);
94 case AF_CONTROL_GATE_RANGE | AF_CONTROL_SET:
95 return af_from_dB(AF_NCH,(float*)arg,s->range,20.0,100.0,0.0);
96 case AF_CONTROL_GATE_RANGE | AF_CONTROL_GET:
97 return af_to_dB(AF_NCH,s->range,(float*)arg,10.0);
99 return AF_UNKNOWN;
102 // Deallocate memory
103 static void uninit(struct af_instance_s* af)
105 if(af->data)
106 free(af->data);
107 if(af->setup)
108 free(af->setup);
111 // Filter data through filter
112 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
114 af_data_t* c = data; // Current working data
115 af_gate_t* s = (af_gate_t*)af->setup; // Setup for this instance
116 float* a = (float*)c->audio; // Audio data
117 int len = c->len/4; // Number of samples
118 int ch = 0; // Channel counter
119 register int nch = c->nch; // Number of channels
120 register int i = 0;
123 // Noise gate
124 for(ch = 0; ch < nch ; ch++){
125 if(s->enable[ch]){
126 float t = 1.0 - s->time[ch];
127 for(i=ch;i<len;i+=nch){
128 register float x = a[i];
129 register float pow = x*x;
130 s->pow[ch] = t*s->pow[ch] +
131 pow*s->time[ch]; // LP filter
132 if(pow < s->pow[ch]){
135 else{
138 a[i] = x;
142 return c;
145 // Allocate memory and set function pointers
146 static int af_open(af_instance_t* af){
147 af->control=control;
148 af->uninit=uninit;
149 af->play=play;
150 af->mul=1;
151 af->data=calloc(1,sizeof(af_data_t));
152 af->setup=calloc(1,sizeof(af_gate_t));
153 if(af->data == NULL || af->setup == NULL)
154 return AF_ERROR;
155 return AF_OK;
158 // Description of this filter
159 af_info_t af_info_gate = {
160 "Noise gate audio filter",
161 "gate",
162 "Anders",
164 AF_FLAGS_NOT_REENTRANT,
165 af_open