2 * This audio filter delays the output signal for the different
3 * channels and can be used for simple position panning.
4 * An extension for this filter would be a reverb.
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.
32 #define UPDATEQI(qi) qi=(qi+1)&(L-1)
34 // Data for specific instances of this filter
35 typedef struct af_delay_s
37 void* q
[AF_NCH
]; // Circular queues used for delaying audio signal
38 int wi
[AF_NCH
]; // Write index
40 float d
[AF_NCH
]; // Delay [ms]
43 // Initialization and runtime control
44 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
46 af_delay_t
* s
= af
->setup
;
48 case AF_CONTROL_REINIT
:{
51 // Free prevous delay queues
52 for(i
=0;i
<af
->data
->nch
;i
++){
57 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
58 af
->data
->nch
= ((af_data_t
*)arg
)->nch
;
59 af
->data
->format
= ((af_data_t
*)arg
)->format
;
60 af
->data
->bps
= ((af_data_t
*)arg
)->bps
;
62 // Allocate new delay queues
63 for(i
=0;i
<af
->data
->nch
;i
++){
64 s
->q
[i
] = calloc(L
,af
->data
->bps
);
66 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[delay] Out of memory\n");
69 return control(af
,AF_CONTROL_DELAY_LEN
| AF_CONTROL_SET
,s
->d
);
71 case AF_CONTROL_COMMAND_LINE
:{
75 while(n
&& i
< AF_NCH
){
76 sscanf(cl
,"%f:%n",&s
->d
[i
],&n
);
77 if(n
==0 || cl
[n
-1] == '\0')
84 case AF_CONTROL_DELAY_LEN
| AF_CONTROL_SET
:{
86 if(AF_OK
!= af_from_ms(AF_NCH
, arg
, s
->wi
, af
->data
->rate
, 0.0, 1000.0))
89 for(i
=0;i
<AF_NCH
;i
++){
90 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "[delay] Channel %i delayed by %0.3fms\n",
91 i
,clamp(s
->d
[i
],0.0,1000.0));
92 mp_msg(MSGT_AFILTER
, MSGL_DBG3
, "[delay] Channel %i delayed by %i samples\n",
97 case AF_CONTROL_DELAY_LEN
| AF_CONTROL_GET
:{
99 for(i
=0;i
<AF_NCH
;i
++){
101 s
->wi
[i
] = L
- (s
->ri
- s
->wi
[i
]);
103 s
->wi
[i
] = s
->wi
[i
] - s
->ri
;
105 return af_to_ms(AF_NCH
, s
->wi
, arg
, af
->data
->rate
);
112 static void uninit(struct af_instance_s
* af
)
117 for(i
=0;i
<AF_NCH
;i
++)
118 if(((af_delay_t
*)(af
->setup
))->q
[i
])
119 free(((af_delay_t
*)(af
->setup
))->q
[i
]);
124 // Filter data through filter
125 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
127 af_data_t
* c
= data
; // Current working data
128 af_delay_t
* s
= af
->setup
; // Setup for this instance
129 int nch
= c
->nch
; // Number of channels
130 int len
= c
->len
/c
->bps
; // Number of sample in data chunk
133 for(ch
=0;ch
<nch
;ch
++){
136 int8_t* a
= c
->audio
;
137 int8_t* q
= s
->q
[ch
];
140 for(i
=ch
;i
<len
;i
+=nch
){
150 int16_t* a
= c
->audio
;
151 int16_t* q
= s
->q
[ch
];
154 for(i
=ch
;i
<len
;i
+=nch
){
164 int32_t* a
= c
->audio
;
165 int32_t* q
= s
->q
[ch
];
168 for(i
=ch
;i
<len
;i
+=nch
){
183 // Allocate memory and set function pointers
184 static int af_open(af_instance_t
* af
){
189 af
->data
=calloc(1,sizeof(af_data_t
));
190 af
->setup
=calloc(1,sizeof(af_delay_t
));
191 if(af
->data
== NULL
|| af
->setup
== NULL
)
196 // Description of this filter
197 af_info_t af_info_delay
= {
198 "Delay audio filter",