natsuki.mplayerhq.hu -> rsync.mplayerhq.hu
[mplayer/glamo.git] / libaf / af_gate.c
blobf0e7a3df9ff1eb50e7ee13152e57cc885df603c2
1 /*=============================================================================
2 //
3 // This software has been released under the terms of the GNU General Public
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
5 //
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au
7 //
8 //=============================================================================
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #include <unistd.h>
16 #include <inttypes.h>
17 #include <math.h>
18 #include <limits.h>
20 #include "af.h"
22 // Data for specific instances of this filter
23 typedef struct af_gate_s
25 int enable[AF_NCH]; // Enable/disable / channel
26 float time[AF_NCH]; // Forgetting factor for power estimate
27 float pow[AF_NCH]; // Estimated power level [dB]
28 float tresh[AF_NCH]; // Threshold [dB]
29 int attack[AF_NCH]; // Attack time [ms]
30 int release[AF_NCH]; // Release time [ms]
31 float range[AF_NCH]; // Range level [dB]
32 }af_gate_t;
34 // Initialization and runtime control
35 static int control(struct af_instance_s* af, int cmd, void* arg)
37 af_gate_t* s = (af_gate_t*)af->setup;
38 switch(cmd){
39 case AF_CONTROL_REINIT:
40 // Sanity check
41 if(!arg) return AF_ERROR;
43 af->data->rate = ((af_data_t*)arg)->rate;
44 af->data->nch = ((af_data_t*)arg)->nch;
45 af->data->format = AF_FORMAT_FLOAT_NE;
46 af->data->bps = 4;
48 // Time constant set to 0.1s
49 // s->alpha = (1.0/0.2)/(2.0*M_PI*(float)((af_data_t*)arg)->rate);
50 return af_test_output(af,(af_data_t*)arg);
51 case AF_CONTROL_COMMAND_LINE:{
52 /* float v=-10.0; */
53 /* float vol[AF_NCH]; */
54 /* float s=0.0; */
55 /* float clipp[AF_NCH]; */
56 /* int i; */
57 /* sscanf((char*)arg,"%f:%f", &v, &s); */
58 /* for(i=0;i<AF_NCH;i++){ */
59 /* vol[i]=v; */
60 /* clipp[i]=s; */
61 /* } */
62 /* if(AF_OK != control(af,AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_SET, clipp)) */
63 /* return AF_ERROR; */
64 /* return control(af,AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, vol); */
67 case AF_CONTROL_GATE_ON_OFF | AF_CONTROL_SET:
68 memcpy(s->enable,(int*)arg,AF_NCH*sizeof(int));
69 return AF_OK;
70 case AF_CONTROL_GATE_ON_OFF | AF_CONTROL_GET:
71 memcpy((int*)arg,s->enable,AF_NCH*sizeof(int));
72 return AF_OK;
73 case AF_CONTROL_GATE_THRESH | AF_CONTROL_SET:
74 return af_from_dB(AF_NCH,(float*)arg,s->tresh,20.0,-60.0,-1.0);
75 case AF_CONTROL_GATE_THRESH | AF_CONTROL_GET:
76 return af_to_dB(AF_NCH,s->tresh,(float*)arg,10.0);
77 case AF_CONTROL_GATE_ATTACK | AF_CONTROL_SET:
78 return af_from_ms(AF_NCH,(float*)arg,s->attack,af->data->rate,500.0,0.1);
79 case AF_CONTROL_GATE_ATTACK | AF_CONTROL_GET:
80 return af_to_ms(AF_NCH,s->attack,(float*)arg,af->data->rate);
81 case AF_CONTROL_GATE_RELEASE | AF_CONTROL_SET:
82 return af_from_ms(AF_NCH,(float*)arg,s->release,af->data->rate,3000.0,10.0);
83 case AF_CONTROL_GATE_RELEASE | AF_CONTROL_GET:
84 return af_to_ms(AF_NCH,s->release,(float*)arg,af->data->rate);
85 case AF_CONTROL_GATE_RANGE | AF_CONTROL_SET:
86 return af_from_dB(AF_NCH,(float*)arg,s->range,20.0,100.0,0.0);
87 case AF_CONTROL_GATE_RANGE | AF_CONTROL_GET:
88 return af_to_dB(AF_NCH,s->range,(float*)arg,10.0);
90 return AF_UNKNOWN;
93 // Deallocate memory
94 static void uninit(struct af_instance_s* af)
96 if(af->data)
97 free(af->data);
98 if(af->setup)
99 free(af->setup);
102 // Filter data through filter
103 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
105 af_data_t* c = data; // Current working data
106 af_gate_t* s = (af_gate_t*)af->setup; // Setup for this instance
107 float* a = (float*)c->audio; // Audio data
108 int len = c->len/4; // Number of samples
109 int ch = 0; // Channel counter
110 register int nch = c->nch; // Number of channels
111 register int i = 0;
114 // Noise gate
115 for(ch = 0; ch < nch ; ch++){
116 if(s->enable[ch]){
117 float t = 1.0 - s->time[ch];
118 for(i=ch;i<len;i+=nch){
119 register float x = a[i];
120 register float pow = x*x;
121 s->pow[ch] = t*s->pow[ch] +
122 pow*s->time[ch]; // LP filter
123 if(pow < s->pow[ch]){
126 else{
129 a[i] = x;
133 return c;
136 // Allocate memory and set function pointers
137 static int open(af_instance_t* af){
138 af->control=control;
139 af->uninit=uninit;
140 af->play=play;
141 af->mul.n=1;
142 af->mul.d=1;
143 af->data=calloc(1,sizeof(af_data_t));
144 af->setup=calloc(1,sizeof(af_gate_t));
145 if(af->data == NULL || af->setup == NULL)
146 return AF_ERROR;
147 return AF_OK;
150 // Description of this filter
151 af_info_t af_info_gate = {
152 "Noise gate audio filter",
153 "gate",
154 "Anders",
156 AF_FLAGS_NOT_REENTRANT,
157 open