1 /* This audio filter delays the output signal for the different
2 channels and can be used for simple position panning. Extension for
3 this filter would be a reverb.
14 #define UPDATEQI(qi) qi=(qi+1)&(L-1)
16 // Data for specific instances of this filter
17 typedef struct af_delay_s
19 void* q
[AF_NCH
]; // Circular queues used for delaying audio signal
20 int wi
[AF_NCH
]; // Write index
22 float d
[AF_NCH
]; // Delay [ms]
25 // Initialization and runtime control
26 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
28 af_delay_t
* s
= af
->setup
;
30 case AF_CONTROL_REINIT
:{
33 // Free prevous delay queues
34 for(i
=0;i
<af
->data
->nch
;i
++){
39 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
40 af
->data
->nch
= ((af_data_t
*)arg
)->nch
;
41 af
->data
->format
= ((af_data_t
*)arg
)->format
;
42 af
->data
->bps
= ((af_data_t
*)arg
)->bps
;
44 // Allocate new delay queues
45 for(i
=0;i
<af
->data
->nch
;i
++){
46 s
->q
[i
] = calloc(L
,af
->data
->bps
);
48 af_msg(AF_MSG_FATAL
,"[delay] Out of memory\n");
51 return control(af
,AF_CONTROL_DELAY_LEN
| AF_CONTROL_SET
,s
->d
);
53 case AF_CONTROL_COMMAND_LINE
:{
57 while(n
&& i
< AF_NCH
){
58 sscanf(cl
,"%f:%n",&s
->d
[i
],&n
);
59 if(n
==0 || cl
[n
-1] == '\0')
66 case AF_CONTROL_DELAY_LEN
| AF_CONTROL_SET
:{
68 if(AF_OK
!= af_from_ms(AF_NCH
, arg
, s
->wi
, af
->data
->rate
, 0.0, 1000.0))
71 for(i
=0;i
<AF_NCH
;i
++){
72 af_msg(AF_MSG_DEBUG0
,"[delay] Channel %i delayed by %0.3fms\n",
73 i
,clamp(s
->d
[i
],0.0,1000.0));
74 af_msg(AF_MSG_DEBUG1
,"[delay] Channel %i delayed by %i samples\n",
79 case AF_CONTROL_DELAY_LEN
| AF_CONTROL_GET
:{
81 for(i
=0;i
<AF_NCH
;i
++){
83 s
->wi
[i
] = L
- (s
->ri
- s
->wi
[i
]);
85 s
->wi
[i
] = s
->wi
[i
] - s
->ri
;
87 return af_to_ms(AF_NCH
, s
->wi
, arg
, af
->data
->rate
);
94 static void uninit(struct af_instance_s
* af
)
100 if(((af_delay_t
*)(af
->setup
))->q
[i
])
101 free(((af_delay_t
*)(af
->setup
))->q
[i
]);
106 // Filter data through filter
107 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
109 af_data_t
* c
= data
; // Current working data
110 af_delay_t
* s
= af
->setup
; // Setup for this instance
111 int nch
= c
->nch
; // Number of channels
112 int len
= c
->len
/c
->bps
; // Number of sample in data chunk
115 for(ch
=0;ch
<nch
;ch
++){
118 int8_t* a
= c
->audio
;
119 int8_t* q
= s
->q
[ch
];
122 for(i
=ch
;i
<len
;i
+=nch
){
132 int16_t* a
= c
->audio
;
133 int16_t* q
= s
->q
[ch
];
136 for(i
=ch
;i
<len
;i
+=nch
){
146 int32_t* a
= c
->audio
;
147 int32_t* q
= s
->q
[ch
];
150 for(i
=ch
;i
<len
;i
+=nch
){
165 // Allocate memory and set function pointers
166 static int open(af_instance_t
* af
){
172 af
->data
=calloc(1,sizeof(af_data_t
));
173 af
->setup
=calloc(1,sizeof(af_delay_t
));
174 if(af
->data
== NULL
|| af
->setup
== NULL
)
179 // Description of this filter
180 af_info_t af_info_delay
= {
181 "Delay audio filter",