annotate libaf/af_gate.c @ 13381:8db5683d2b3c

fix playback of files with displaysize not set (aspect was set to NaN for these)
author reimar
date Sat, 18 Sep 2004 14:35:19 +0000
parents 93212da0032e
children 14090f7300a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
1 /*=============================================================================
d6f40a06867b Changes includes:
anders
parents:
diff changeset
2 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
3 // This software has been released under the terms of the GNU Public
d6f40a06867b Changes includes:
anders
parents:
diff changeset
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
d6f40a06867b Changes includes:
anders
parents:
diff changeset
5 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au
d6f40a06867b Changes includes:
anders
parents:
diff changeset
7 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
8 //=============================================================================
d6f40a06867b Changes includes:
anders
parents:
diff changeset
9 */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
10
d6f40a06867b Changes includes:
anders
parents:
diff changeset
11 #include <stdio.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
12 #include <stdlib.h>
8623
440301fef3fe Added/reordered #includes to silence warnings about "implicit declaration".
rathann
parents: 8607
diff changeset
13 #include <string.h>
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
14
d6f40a06867b Changes includes:
anders
parents:
diff changeset
15 #include <unistd.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
16 #include <inttypes.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
17 #include <math.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
18 #include <limits.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
19
d6f40a06867b Changes includes:
anders
parents:
diff changeset
20 #include "af.h"
d6f40a06867b Changes includes:
anders
parents:
diff changeset
21
d6f40a06867b Changes includes:
anders
parents:
diff changeset
22 // Data for specific instances of this filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
23 typedef struct af_gate_s
d6f40a06867b Changes includes:
anders
parents:
diff changeset
24 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
25 int enable[AF_NCH]; // Enable/disable / channel
d6f40a06867b Changes includes:
anders
parents:
diff changeset
26 float time[AF_NCH]; // Forgetting factor for power estimate
d6f40a06867b Changes includes:
anders
parents:
diff changeset
27 float pow[AF_NCH]; // Estimated power level [dB]
d6f40a06867b Changes includes:
anders
parents:
diff changeset
28 float tresh[AF_NCH]; // Threshold [dB]
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
29 int attack[AF_NCH]; // Attack time [ms]
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
30 int release[AF_NCH]; // Release time [ms]
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
31 float range[AF_NCH]; // Range level [dB]
d6f40a06867b Changes includes:
anders
parents:
diff changeset
32 }af_gate_t;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
33
d6f40a06867b Changes includes:
anders
parents:
diff changeset
34 // Initialization and runtime control
d6f40a06867b Changes includes:
anders
parents:
diff changeset
35 static int control(struct af_instance_s* af, int cmd, void* arg)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
36 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
37 af_gate_t* s = (af_gate_t*)af->setup;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
38 switch(cmd){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
39 case AF_CONTROL_REINIT:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
40 // Sanity check
d6f40a06867b Changes includes:
anders
parents:
diff changeset
41 if(!arg) return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
42
d6f40a06867b Changes includes:
anders
parents:
diff changeset
43 af->data->rate = ((af_data_t*)arg)->rate;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
44 af->data->nch = ((af_data_t*)arg)->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
45 af->data->format = AF_FORMAT_F | AF_FORMAT_NE;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
46 af->data->bps = 4;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
47
d6f40a06867b Changes includes:
anders
parents:
diff changeset
48 // Time constant set to 0.1s
d6f40a06867b Changes includes:
anders
parents:
diff changeset
49 // s->alpha = (1.0/0.2)/(2.0*M_PI*(float)((af_data_t*)arg)->rate);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
50 return af_test_output(af,(af_data_t*)arg);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
51 case AF_CONTROL_COMMAND_LINE:{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
52 /* float v=-10.0; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
53 /* float vol[AF_NCH]; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
54 /* float s=0.0; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
55 /* float clipp[AF_NCH]; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
56 /* int i; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
57 /* sscanf((char*)arg,"%f:%f", &v, &s); */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
58 /* for(i=0;i<AF_NCH;i++){ */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
59 /* vol[i]=v; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
60 /* clipp[i]=s; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
61 /* } */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
62 /* if(AF_OK != control(af,AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_SET, clipp)) */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
63 /* return AF_ERROR; */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
64 /* return control(af,AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, vol); */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
65 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
66
d6f40a06867b Changes includes:
anders
parents:
diff changeset
67 case AF_CONTROL_GATE_ON_OFF | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
68 memcpy(s->enable,(int*)arg,AF_NCH*sizeof(int));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
69 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
70 case AF_CONTROL_GATE_ON_OFF | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
71 memcpy((int*)arg,s->enable,AF_NCH*sizeof(int));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
72 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
73 case AF_CONTROL_GATE_THRESH | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
74 return af_from_dB(AF_NCH,(float*)arg,s->tresh,20.0,-60.0,-1.0);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
75 case AF_CONTROL_GATE_THRESH | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
76 return af_to_dB(AF_NCH,s->tresh,(float*)arg,10.0);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
77 case AF_CONTROL_GATE_ATTACK | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
78 return af_from_ms(AF_NCH,(float*)arg,s->attack,af->data->rate,500.0,0.1);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
79 case AF_CONTROL_GATE_ATTACK | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
80 return af_to_ms(AF_NCH,s->attack,(float*)arg,af->data->rate);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
81 case AF_CONTROL_GATE_RELEASE | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
82 return af_from_ms(AF_NCH,(float*)arg,s->release,af->data->rate,3000.0,10.0);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
83 case AF_CONTROL_GATE_RELEASE | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
84 return af_to_ms(AF_NCH,s->release,(float*)arg,af->data->rate);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
85 case AF_CONTROL_GATE_RANGE | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
86 return af_from_dB(AF_NCH,(float*)arg,s->range,20.0,100.0,0.0);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
87 case AF_CONTROL_GATE_RANGE | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
88 return af_to_dB(AF_NCH,s->range,(float*)arg,10.0);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
89 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
90 return AF_UNKNOWN;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
91 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
92
d6f40a06867b Changes includes:
anders
parents:
diff changeset
93 // Deallocate memory
d6f40a06867b Changes includes:
anders
parents:
diff changeset
94 static void uninit(struct af_instance_s* af)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
95 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
96 if(af->data)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
97 free(af->data);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
98 if(af->setup)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
99 free(af->setup);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
100 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
101
d6f40a06867b Changes includes:
anders
parents:
diff changeset
102 // Filter data through filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
103 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
104 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
105 af_data_t* c = data; // Current working data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
106 af_gate_t* s = (af_gate_t*)af->setup; // Setup for this instance
d6f40a06867b Changes includes:
anders
parents:
diff changeset
107 float* a = (float*)c->audio; // Audio data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
108 int len = c->len/4; // Number of samples
d6f40a06867b Changes includes:
anders
parents:
diff changeset
109 int ch = 0; // Channel counter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
110 register int nch = c->nch; // Number of channels
d6f40a06867b Changes includes:
anders
parents:
diff changeset
111 register int i = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
112
d6f40a06867b Changes includes:
anders
parents:
diff changeset
113
d6f40a06867b Changes includes:
anders
parents:
diff changeset
114 // Noise gate
d6f40a06867b Changes includes:
anders
parents:
diff changeset
115 for(ch = 0; ch < nch ; ch++){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
116 if(s->enable[ch]){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
117 float t = 1.0 - s->time[ch];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
118 for(i=ch;i<len;i+=nch){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
119 register float x = a[i];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
120 register float pow = x*x;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
121 s->pow[ch] = t*s->pow[ch] +
d6f40a06867b Changes includes:
anders
parents:
diff changeset
122 pow*s->time[ch]; // LP filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
123 if(pow < s->pow[ch]){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
124 ;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
125 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
126 else{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
127 ;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
128 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
129 a[i] = x;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
130 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
131 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
132 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
133 return c;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
134 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
135
d6f40a06867b Changes includes:
anders
parents:
diff changeset
136 // Allocate memory and set function pointers
d6f40a06867b Changes includes:
anders
parents:
diff changeset
137 static int open(af_instance_t* af){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
138 af->control=control;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
139 af->uninit=uninit;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
140 af->play=play;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
141 af->mul.n=1;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
142 af->mul.d=1;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
143 af->data=calloc(1,sizeof(af_data_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
144 af->setup=calloc(1,sizeof(af_gate_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
145 if(af->data == NULL || af->setup == NULL)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
146 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
147 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
148 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
149
d6f40a06867b Changes includes:
anders
parents:
diff changeset
150 // Description of this filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
151 af_info_t af_info_gate = {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
152 "Noise gate audio filter",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
153 "gate",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
154 "Anders",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
155 "",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
156 AF_FLAGS_NOT_REENTRANT,
d6f40a06867b Changes includes:
anders
parents:
diff changeset
157 open
d6f40a06867b Changes includes:
anders
parents:
diff changeset
158 };