1 /*=============================================================================
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.
6 // Copyright 2006 Michael Niedermayer
7 // Copyright 2004 Alex Beregszaszi & Pierre Lombard (original af_extrastereo.c upon which this is based)
9 //=============================================================================
22 // Data for specific instances of this filter
23 typedef struct af_sinesuppress_s
33 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
);
34 //static af_data_t* play_float(struct af_instance_s* af, af_data_t* data);
36 // Initialization and runtime control
37 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
39 af_sinesuppress_t
* s
= (af_sinesuppress_t
*)af
->setup
;
42 case AF_CONTROL_REINIT
:{
44 if(!arg
) return AF_ERROR
;
46 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
49 if (((af_data_t
*)arg
)->format
== AF_FORMAT_FLOAT_NE
)
51 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
53 af
->play
= play_float
;
57 af
->data
->format
= AF_FORMAT_S16_NE
;
62 return af_test_output(af
,(af_data_t
*)arg
);
64 case AF_CONTROL_COMMAND_LINE
:{
66 sscanf((char*)arg
,"%f:%f", &f1
,&f2
);
71 case AF_CONTROL_SS_FREQ
| AF_CONTROL_SET
:
72 s
->freq
= *(float*)arg
;
74 case AF_CONTROL_SS_FREQ
| AF_CONTROL_GET
:
75 *(float*)arg
= s
->freq
;
77 case AF_CONTROL_SS_DECAY
| AF_CONTROL_SET
:
78 s
->decay
= *(float*)arg
;
80 case AF_CONTROL_SS_DECAY
| AF_CONTROL_GET
:
81 *(float*)arg
= s
->decay
;
88 static void uninit(struct af_instance_s
* af
)
96 // Filter data through filter
97 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
)
99 af_sinesuppress_t
*s
= af
->setup
;
101 int16_t *a
= (int16_t*)data
->audio
; // Audio data
102 int len
= data
->len
/2; // Number of samples
104 for (i
= 0; i
< len
; i
++)
106 double co
= cos(s
->pos
);
107 double si
= sin(s
->pos
);
109 s
->real
+= co
* a
[i
];
110 s
->imag
+= si
* a
[i
];
113 a
[i
] -= (s
->real
* co
+ s
->imag
* si
) / s
->ref
;
115 s
->real
-= s
->real
* s
->decay
;
116 s
->imag
-= s
->imag
* s
->decay
;
117 s
->ref
-= s
->ref
* s
->decay
;
119 s
->pos
+= 2 * M_PI
* s
->freq
/ data
->rate
;
122 af_msg(AF_MSG_VERBOSE
,"[sinesuppress] f:%8.2f: amp:%8.2f\n", s
->freq
, sqrt(s
->real
*s
->real
+ s
->imag
*s
->imag
) / s
->ref
);
128 static af_data_t
* play_float(struct af_instance_s
* af
, af_data_t
* data
)
130 af_sinesuppress_t
*s
= af
->setup
;
132 float *a
= (float*)data
->audio
; // Audio data
133 int len
= data
->len
/4; // Number of samples
136 for (i
= 0; i
< len
; i
+=2)
138 avg
= (a
[i
] + a
[i
+ 1]) / 2;
140 /* l = avg + (s->mul * (a[i] - avg));
141 r = avg + (s->mul * (a[i + 1] - avg));*/
143 a
[i
] = af_softclip(l
);
144 a
[i
+ 1] = af_softclip(r
);
151 // Allocate memory and set function pointers
152 static int af_open(af_instance_t
* af
){
157 af
->data
=calloc(1,sizeof(af_data_t
));
158 af
->setup
=calloc(1,sizeof(af_sinesuppress_t
));
159 if(af
->data
== NULL
|| af
->setup
== NULL
)
162 ((af_sinesuppress_t
*)af
->setup
)->freq
= 50.0;
163 ((af_sinesuppress_t
*)af
->setup
)->decay
= 0.0001;
167 // Description of this filter
168 af_info_t af_info_sinesuppress
= {
171 "Michael Niedermayer",