2 * Copyright (C) 2006 Michael Niedermayer
3 * Copyright (C) 2004 Alex Beregszaszi
4 * based upon af_extrastereo.c by Pierre Lombard
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 // Data for specific instances of this filter
34 typedef struct af_sinesuppress_s
44 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
);
45 //static af_data_t* play_float(struct af_instance_s* af, af_data_t* data);
47 // Initialization and runtime control
48 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
50 af_sinesuppress_t
* s
= (af_sinesuppress_t
*)af
->setup
;
53 case AF_CONTROL_REINIT
:{
55 if(!arg
) return AF_ERROR
;
57 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
60 if (((af_data_t
*)arg
)->format
== AF_FORMAT_FLOAT_NE
)
62 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
64 af
->play
= play_float
;
68 af
->data
->format
= AF_FORMAT_S16_NE
;
73 return af_test_output(af
,(af_data_t
*)arg
);
75 case AF_CONTROL_COMMAND_LINE
:{
77 sscanf((char*)arg
,"%f:%f", &f1
,&f2
);
82 case AF_CONTROL_SS_FREQ
| AF_CONTROL_SET
:
83 s
->freq
= *(float*)arg
;
85 case AF_CONTROL_SS_FREQ
| AF_CONTROL_GET
:
86 *(float*)arg
= s
->freq
;
88 case AF_CONTROL_SS_DECAY
| AF_CONTROL_SET
:
89 s
->decay
= *(float*)arg
;
91 case AF_CONTROL_SS_DECAY
| AF_CONTROL_GET
:
92 *(float*)arg
= s
->decay
;
99 static void uninit(struct af_instance_s
* af
)
107 // Filter data through filter
108 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
)
110 af_sinesuppress_t
*s
= af
->setup
;
112 int16_t *a
= (int16_t*)data
->audio
; // Audio data
113 int len
= data
->len
/2; // Number of samples
115 for (i
= 0; i
< len
; i
++)
117 double co
= cos(s
->pos
);
118 double si
= sin(s
->pos
);
120 s
->real
+= co
* a
[i
];
121 s
->imag
+= si
* a
[i
];
124 a
[i
] -= (s
->real
* co
+ s
->imag
* si
) / s
->ref
;
126 s
->real
-= s
->real
* s
->decay
;
127 s
->imag
-= s
->imag
* s
->decay
;
128 s
->ref
-= s
->ref
* s
->decay
;
130 s
->pos
+= 2 * M_PI
* s
->freq
/ data
->rate
;
133 mp_msg(MSGT_AFILTER
, MSGL_V
, "[sinesuppress] f:%8.2f: amp:%8.2f\n", s
->freq
, sqrt(s
->real
*s
->real
+ s
->imag
*s
->imag
) / s
->ref
);
139 static af_data_t
* play_float(struct af_instance_s
* af
, af_data_t
* data
)
141 af_sinesuppress_t
*s
= af
->setup
;
143 float *a
= (float*)data
->audio
; // Audio data
144 int len
= data
->len
/4; // Number of samples
147 for (i
= 0; i
< len
; i
+=2)
149 avg
= (a
[i
] + a
[i
+ 1]) / 2;
151 /* l = avg + (s->mul * (a[i] - avg));
152 r = avg + (s->mul * (a[i + 1] - avg));*/
154 a
[i
] = af_softclip(l
);
155 a
[i
+ 1] = af_softclip(r
);
162 // Allocate memory and set function pointers
163 static int af_open(af_instance_t
* af
){
168 af
->data
=calloc(1,sizeof(af_data_t
));
169 af
->setup
=calloc(1,sizeof(af_sinesuppress_t
));
170 if(af
->data
== NULL
|| af
->setup
== NULL
)
173 ((af_sinesuppress_t
*)af
->setup
)->freq
= 50.0;
174 ((af_sinesuppress_t
*)af
->setup
)->decay
= 0.0001;
178 // Description of this filter
179 af_info_t af_info_sinesuppress
= {
182 "Michael Niedermayer",