5 This filter adds a center channel to the audio stream by
6 averaging the left and right channel.
7 There are two runtime controls one for setting which channel to
8 insert the center-audio into called AF_CONTROL_SUB_CH.
10 FIXME: implement a high-pass filter for better results.
19 // Data for specific instances of this filter
20 typedef struct af_center_s
22 int ch
; // Channel number which to insert the filtered data
25 // Initialization and runtime control
26 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
28 af_center_t
* s
= af
->setup
;
31 case AF_CONTROL_REINIT
:{
33 if(!arg
) return AF_ERROR
;
35 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
36 af
->data
->nch
= max(s
->ch
+1,((af_data_t
*)arg
)->nch
);
37 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
40 return af_test_output(af
,(af_data_t
*)arg
);
42 case AF_CONTROL_COMMAND_LINE
:{
44 sscanf(arg
,"%i", &ch
);
45 return control(af
,AF_CONTROL_CENTER_CH
| AF_CONTROL_SET
, &ch
);
47 case AF_CONTROL_CENTER_CH
| AF_CONTROL_SET
: // Requires reinit
49 if((*(int*)arg
>= AF_NCH
) || (*(int*)arg
< 0)){
50 af_msg(AF_MSG_ERROR
,"[sub] Center channel number must be between "
51 " 0 and %i current value is %i\n", AF_NCH
-1, *(int*)arg
);
56 case AF_CONTROL_CENTER_CH
| AF_CONTROL_GET
:
64 static void uninit(struct af_instance_s
* af
)
72 // Filter data through filter
73 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
75 af_data_t
* c
= data
; // Current working data
76 af_center_t
* s
= af
->setup
; // Setup for this instance
77 float* a
= c
->audio
; // Audio data
78 int len
= c
->len
/4; // Number of samples in current audio block
79 int nch
= c
->nch
; // Number of channels
80 int ch
= s
->ch
; // Channel in which to insert the center audio
84 for(i
=0;i
<len
;i
+=nch
){
85 // Average left and right
86 a
[i
+ch
] = (a
[i
]/2) + (a
[i
+1]/2);
92 // Allocate memory and set function pointers
93 static int af_open(af_instance_t
* af
){
99 af
->data
=calloc(1,sizeof(af_data_t
));
100 af
->setup
=s
=calloc(1,sizeof(af_center_t
));
101 if(af
->data
== NULL
|| af
->setup
== NULL
)
103 // Set default values
104 s
->ch
= 1; // Channel nr 2
108 // Description of this filter
109 af_info_t af_info_center
= {
110 "Audio filter for adding a center channel",
114 AF_FLAGS_NOT_REENTRANT
,