comparison libaf/af_comp.c @ 8607:d6f40a06867b

Changes includes: - Improved runtime control system - 3 New filter panning, compressor/limiter and a noise gate - The compressor/limiter and the noise gate are not yet finished - The panning filter does combined mixing and channel routing and can be used to down-mix from stereo to mono (for example) - Improvements to volume and channel - volume now has a very good soft clipping using sin() - channel can handle generic routing of audio data - Conversion of all filters to handle floating point data - Cleanup of message printing - Fix for the sig 11 bug reported by Denes
author anders
date Sat, 28 Dec 2002 13:59:53 +0000
parents
children 440301fef3fe
comparison
equal deleted inserted replaced
8606:d80edba39db9 8607:d6f40a06867b
1 /*=============================================================================
2 //
3 // This software has been released under the terms of the GNU 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 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include <unistd.h>
15 #include <inttypes.h>
16 #include <math.h>
17 #include <limits.h>
18
19 #include "af.h"
20
21 // Data for specific instances of this filter
22 typedef struct af_comp_s
23 {
24 int enable[AF_NCH]; // Enable/disable / channel
25 float time[AF_NCH]; // Forgetting factor for power estimate
26 float pow[AF_NCH]; // Estimated power level [dB]
27 float tresh[AF_NCH]; // Threshold [dB]
28 float attack[AF_NCH]; // Attack time [ms]
29 float release[AF_NCH]; // Release time [ms]
30 float ratio[AF_NCH]; // Compression ratio
31 }af_comp_t;
32
33 // Initialization and runtime control
34 static int control(struct af_instance_s* af, int cmd, void* arg)
35 {
36 af_comp_t* s = (af_comp_t*)af->setup;
37 int i;
38
39 switch(cmd){
40 case AF_CONTROL_REINIT:
41 // Sanity check
42 if(!arg) return AF_ERROR;
43
44 af->data->rate = ((af_data_t*)arg)->rate;
45 af->data->nch = ((af_data_t*)arg)->nch;
46 af->data->format = AF_FORMAT_F | AF_FORMAT_NE;
47 af->data->bps = 4;
48
49 // Time constant set to 0.1s
50 // s->alpha = (1.0/0.2)/(2.0*M_PI*(float)((af_data_t*)arg)->rate);
51 return af_test_output(af,(af_data_t*)arg);
52 case AF_CONTROL_COMMAND_LINE:{
53 /* float v=-10.0; */
54 /* float vol[AF_NCH]; */
55 /* float s=0.0; */
56 /* float clipp[AF_NCH]; */
57 /* int i; */
58 /* sscanf((char*)arg,"%f:%f", &v, &s); */
59 /* for(i=0;i<AF_NCH;i++){ */
60 /* vol[i]=v; */
61 /* clipp[i]=s; */
62 /* } */
63 /* if(AF_OK != control(af,AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_SET, clipp)) */
64 /* return AF_ERROR; */
65 /* return control(af,AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, vol); */
66 }
67 case AF_CONTROL_COMP_ON_OFF | AF_CONTROL_SET:
68 memcpy(s->enable,(int*)arg,AF_NCH*sizeof(int));
69 return AF_OK;
70 case AF_CONTROL_COMP_ON_OFF | AF_CONTROL_GET:
71 memcpy((int*)arg,s->enable,AF_NCH*sizeof(int));
72 return AF_OK;
73 case AF_CONTROL_COMP_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_COMP_THRESH | AF_CONTROL_GET:
76 return af_to_dB(AF_NCH,s->tresh,(float*)arg,10.0);
77 case AF_CONTROL_COMP_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_COMP_ATTACK | AF_CONTROL_GET:
80 return af_to_ms(AF_NCH,s->attack,(float*)arg,af->data->rate);
81 case AF_CONTROL_COMP_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_COMP_RELEASE | AF_CONTROL_GET:
84 return af_to_ms(AF_NCH,s->release,(float*)arg,af->data->rate);
85 case AF_CONTROL_COMP_RATIO | AF_CONTROL_SET:
86 for(i=0;i<AF_NCH;i++)
87 s->ratio[i] = clamp(((float*)arg)[i],1.0,10.0);
88 return AF_OK;
89 case AF_CONTROL_COMP_RATIO | AF_CONTROL_GET:
90 for(i=0;i<AF_NCH;i++)
91 ((float*)arg)[i] = s->ratio[i];
92 return AF_OK;
93 }
94 return AF_UNKNOWN;
95 }
96
97 // Deallocate memory
98 static void uninit(struct af_instance_s* af)
99 {
100 if(af->data)
101 free(af->data);
102 if(af->setup)
103 free(af->setup);
104 }
105
106 // Filter data through filter
107 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
108 {
109 af_data_t* c = data; // Current working data
110 af_comp_t* s = (af_comp_t*)af->setup; // Setup for this instance
111 float* a = (float*)c->audio; // Audio data
112 int len = c->len/4; // Number of samples
113 int ch = 0; // Channel counter
114 register int nch = c->nch; // Number of channels
115 register int i = 0;
116
117 // Compress/expand
118 for(ch = 0; ch < nch ; ch++){
119 if(s->enable[ch]){
120 float t = 1.0 - s->time[ch];
121 for(i=ch;i<len;i+=nch){
122 register float x = a[i];
123 register float pow = x*x;
124 s->pow[ch] = t*s->pow[ch] +
125 pow*s->time[ch]; // LP filter
126 if(pow < s->pow[ch]){
127 ;
128 }
129 else{
130 ;
131 }
132 a[i] = x;
133 }
134 }
135 }
136 return c;
137 }
138
139 // Allocate memory and set function pointers
140 static int open(af_instance_t* af){
141 af->control=control;
142 af->uninit=uninit;
143 af->play=play;
144 af->mul.n=1;
145 af->mul.d=1;
146 af->data=calloc(1,sizeof(af_data_t));
147 af->setup=calloc(1,sizeof(af_comp_t));
148 if(af->data == NULL || af->setup == NULL)
149 return AF_ERROR;
150 return AF_OK;
151 }
152
153 // Description of this filter
154 af_info_t af_info_comp = {
155 "Compressor/expander audio filter",
156 "comp",
157 "Anders",
158 "",
159 AF_FLAGS_NOT_REENTRANT,
160 open
161 };