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 //=============================================================================
23 // Data for specific instances of this filter
24 typedef struct af_sinesuppress_s
34 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
);
35 static af_data_t
* play_float(struct af_instance_s
* af
, af_data_t
* data
);
37 // Initialization and runtime control
38 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
40 af_sinesuppress_t
* s
= (af_sinesuppress_t
*)af
->setup
;
43 case AF_CONTROL_REINIT
:{
45 if(!arg
) return AF_ERROR
;
47 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
50 if (((af_data_t
*)arg
)->format
== AF_FORMAT_FLOAT_NE
)
52 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
54 af
->play
= play_float
;
58 af
->data
->format
= AF_FORMAT_S16_NE
;
63 return af_test_output(af
,(af_data_t
*)arg
);
65 case AF_CONTROL_COMMAND_LINE
:{
67 sscanf((char*)arg
,"%f:%f", &f1
,&f2
);
72 case AF_CONTROL_SS_FREQ
| AF_CONTROL_SET
:
73 s
->freq
= *(float*)arg
;
75 case AF_CONTROL_SS_FREQ
| AF_CONTROL_GET
:
76 *(float*)arg
= s
->freq
;
78 case AF_CONTROL_SS_DECAY
| AF_CONTROL_SET
:
79 s
->decay
= *(float*)arg
;
81 case AF_CONTROL_SS_DECAY
| AF_CONTROL_GET
:
82 *(float*)arg
= s
->decay
;
89 static void uninit(struct af_instance_s
* af
)
97 // Filter data through filter
98 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
)
100 af_sinesuppress_t
*s
= af
->setup
;
102 int16_t *a
= (int16_t*)data
->audio
; // Audio data
103 int len
= data
->len
/2; // Number of samples
105 for (i
= 0; i
< len
; i
++)
107 double co
= cos(s
->pos
);
108 double si
= sin(s
->pos
);
110 s
->real
+= co
* a
[i
];
111 s
->imag
+= si
* a
[i
];
114 a
[i
] -= (s
->real
* co
+ s
->imag
* si
) / s
->ref
;
116 s
->real
-= s
->real
* s
->decay
;
117 s
->imag
-= s
->imag
* s
->decay
;
118 s
->ref
-= s
->ref
* s
->decay
;
120 s
->pos
+= 2 * M_PI
* s
->freq
/ data
->rate
;
123 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
);
129 static af_data_t
* play_float(struct af_instance_s
* af
, af_data_t
* data
)
131 af_sinesuppress_t
*s
= af
->setup
;
133 float *a
= (float*)data
->audio
; // Audio data
134 int len
= data
->len
/4; // Number of samples
137 for (i
= 0; i
< len
; i
+=2)
139 avg
= (a
[i
] + a
[i
+ 1]) / 2;
141 /* l = avg + (s->mul * (a[i] - avg));
142 r = avg + (s->mul * (a[i + 1] - avg));*/
144 a
[i
] = af_softclip(l
);
145 a
[i
+ 1] = af_softclip(r
);
152 // Allocate memory and set function pointers
153 static int open(af_instance_t
* af
){
159 af
->data
=calloc(1,sizeof(af_data_t
));
160 af
->setup
=calloc(1,sizeof(af_sinesuppress_t
));
161 if(af
->data
== NULL
|| af
->setup
== NULL
)
164 ((af_sinesuppress_t
*)af
->setup
)->freq
= 50.0;
165 ((af_sinesuppress_t
*)af
->setup
)->decay
= 0.0001;
169 // Description of this filter
170 af_info_t af_info_sinesuppress
= {
173 "Michael Niedermayer",