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 2004 Alex Beregszaszi & Pierre Lombard
8 //=============================================================================
21 // Data for specific instances of this filter
22 typedef struct af_extrastereo_s
27 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
);
28 static af_data_t
* play_float(struct af_instance_s
* af
, af_data_t
* data
);
30 // Initialization and runtime control
31 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
33 af_extrastereo_t
* s
= (af_extrastereo_t
*)af
->setup
;
36 case AF_CONTROL_REINIT
:{
38 if(!arg
) return AF_ERROR
;
40 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
42 if (((af_data_t
*)arg
)->format
== AF_FORMAT_FLOAT_NE
)
44 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
46 af
->play
= play_float
;
49 af
->data
->format
= AF_FORMAT_S16_NE
;
54 return af_test_output(af
,(af_data_t
*)arg
);
56 case AF_CONTROL_COMMAND_LINE
:{
58 sscanf((char*)arg
,"%f", &f
);
62 case AF_CONTROL_ES_MUL
| AF_CONTROL_SET
:
63 s
->mul
= *(float*)arg
;
65 case AF_CONTROL_ES_MUL
| AF_CONTROL_GET
:
66 *(float*)arg
= s
->mul
;
73 static void uninit(struct af_instance_s
* af
)
81 // Filter data through filter
82 static af_data_t
* play_s16(struct af_instance_s
* af
, af_data_t
* data
)
84 af_extrastereo_t
*s
= af
->setup
;
86 int16_t *a
= (int16_t*)data
->audio
; // Audio data
87 int len
= data
->len
/2; // Number of samples
90 for (i
= 0; i
< len
; i
+=2)
92 avg
= (a
[i
] + a
[i
+ 1]) / 2;
94 l
= avg
+ (int)(s
->mul
* (a
[i
] - avg
));
95 r
= avg
+ (int)(s
->mul
* (a
[i
+ 1] - avg
));
97 a
[i
] = clamp(l
, SHRT_MIN
, SHRT_MAX
);
98 a
[i
+ 1] = clamp(r
, SHRT_MIN
, SHRT_MAX
);
104 static af_data_t
* play_float(struct af_instance_s
* af
, af_data_t
* data
)
106 af_extrastereo_t
*s
= af
->setup
;
108 float *a
= (float*)data
->audio
; // Audio data
109 int len
= data
->len
/4; // Number of samples
112 for (i
= 0; i
< len
; i
+=2)
114 avg
= (a
[i
] + a
[i
+ 1]) / 2;
116 l
= avg
+ (s
->mul
* (a
[i
] - avg
));
117 r
= avg
+ (s
->mul
* (a
[i
+ 1] - avg
));
119 a
[i
] = af_softclip(l
);
120 a
[i
+ 1] = af_softclip(r
);
126 // Allocate memory and set function pointers
127 static int af_open(af_instance_t
* af
){
132 af
->data
=calloc(1,sizeof(af_data_t
));
133 af
->setup
=calloc(1,sizeof(af_extrastereo_t
));
134 if(af
->data
== NULL
|| af
->setup
== NULL
)
137 ((af_extrastereo_t
*)af
->setup
)->mul
= 2.5;
141 // Description of this filter
142 af_info_t af_info_extrastereo
= {
145 "Alex Beregszaszi & Pierre Lombard",
147 AF_FLAGS_NOT_REENTRANT
,