Fixes
[mplayer/glamo.git] / libaf / af_delay.c
bloba362086cb8cd96ec6464eff7deb3230ac419a500
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.
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <inttypes.h>
10 #include "af.h"
12 #define L 65536
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
21 int ri; // Read index
22 float d[AF_NCH]; // Delay [ms]
23 }af_delay_t;
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;
29 switch(cmd){
30 case AF_CONTROL_REINIT:{
31 int i;
33 // Free prevous delay queues
34 for(i=0;i<af->data->nch;i++){
35 if(s->q[i])
36 free(s->q[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);
47 if(NULL == s->q[i])
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:{
54 int n = 1;
55 int i = 0;
56 char* cl = arg;
57 while(n && i < AF_NCH ){
58 sscanf(cl,"%f:%n",&s->d[i],&n);
59 if(n==0 || cl[n-1] == '\0')
60 break;
61 cl=&cl[n];
62 i++;
64 return AF_OK;
66 case AF_CONTROL_DELAY_LEN | AF_CONTROL_SET:{
67 int i;
68 if(AF_OK != af_from_ms(AF_NCH, arg, s->wi, af->data->rate, 0.0, 1000.0))
69 return AF_ERROR;
70 s->ri = 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",
75 i,s->wi[i]);
77 return AF_OK;
79 case AF_CONTROL_DELAY_LEN | AF_CONTROL_GET:{
80 int i;
81 for(i=0;i<AF_NCH;i++){
82 if(s->ri > s->wi[i])
83 s->wi[i] = L - (s->ri - s->wi[i]);
84 else
85 s->wi[i] = s->wi[i] - s->ri;
87 return af_to_ms(AF_NCH, s->wi, arg, af->data->rate);
90 return AF_UNKNOWN;
93 // Deallocate memory
94 static void uninit(struct af_instance_s* af)
96 int i;
97 if(af->data)
98 free(af->data);
99 for(i=0;i<AF_NCH;i++)
100 if(((af_delay_t*)(af->setup))->q[i])
101 free(((af_delay_t*)(af->setup))->q[i]);
102 if(af->setup)
103 free(af->setup);
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
113 int ri = 0;
114 int ch,i;
115 for(ch=0;ch<nch;ch++){
116 switch(c->bps){
117 case 1:{
118 int8_t* a = c->audio;
119 int8_t* q = s->q[ch];
120 int wi = s->wi[ch];
121 ri = s->ri;
122 for(i=ch;i<len;i+=nch){
123 q[wi] = a[i];
124 a[i] = q[ri];
125 UPDATEQI(wi);
126 UPDATEQI(ri);
128 s->wi[ch] = wi;
129 break;
131 case 2:{
132 int16_t* a = c->audio;
133 int16_t* q = s->q[ch];
134 int wi = s->wi[ch];
135 ri = s->ri;
136 for(i=ch;i<len;i+=nch){
137 q[wi] = a[i];
138 a[i] = q[ri];
139 UPDATEQI(wi);
140 UPDATEQI(ri);
142 s->wi[ch] = wi;
143 break;
145 case 4:{
146 int32_t* a = c->audio;
147 int32_t* q = s->q[ch];
148 int wi = s->wi[ch];
149 ri = s->ri;
150 for(i=ch;i<len;i+=nch){
151 q[wi] = a[i];
152 a[i] = q[ri];
153 UPDATEQI(wi);
154 UPDATEQI(ri);
156 s->wi[ch] = wi;
157 break;
161 s->ri = ri;
162 return c;
165 // Allocate memory and set function pointers
166 static int open(af_instance_t* af){
167 af->control=control;
168 af->uninit=uninit;
169 af->play=play;
170 af->mul.n=1;
171 af->mul.d=1;
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)
175 return AF_ERROR;
176 return AF_OK;
179 // Description of this filter
180 af_info_t af_info_delay = {
181 "Delay audio filter",
182 "delay",
183 "Anders",
185 AF_FLAGS_REENTRANT,
186 open