audio: add af_lavrresample, remove old resampling filters
[mplayer2.git] / libaf / af_delay.c
blobf0a9704eaac1c298d643ecc94c7cfc6e72085190
1 /*
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.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
28 #include "af.h"
30 #define L 65536
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
39 int ri; // Read index
40 float d[AF_NCH]; // Delay [ms]
41 }af_delay_t;
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;
47 switch(cmd){
48 case AF_CONTROL_REINIT:{
49 int i;
51 // Free prevous delay queues
52 for(i=0;i<af->data->nch;i++)
53 free(s->q[i]);
55 af->data->rate = ((af_data_t*)arg)->rate;
56 af->data->nch = ((af_data_t*)arg)->nch;
57 af->data->format = ((af_data_t*)arg)->format;
58 af->data->bps = ((af_data_t*)arg)->bps;
60 // Allocate new delay queues
61 for(i=0;i<af->data->nch;i++){
62 s->q[i] = calloc(L,af->data->bps);
63 if(NULL == s->q[i])
64 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[delay] Out of memory\n");
67 return control(af,AF_CONTROL_DELAY_LEN | AF_CONTROL_SET,s->d);
69 case AF_CONTROL_COMMAND_LINE:{
70 int n = 1;
71 int i = 0;
72 char* cl = arg;
73 while(n && i < AF_NCH ){
74 sscanf(cl,"%f:%n",&s->d[i],&n);
75 if(n==0 || cl[n-1] == '\0')
76 break;
77 cl=&cl[n];
78 i++;
80 return AF_OK;
82 case AF_CONTROL_DELAY_LEN | AF_CONTROL_SET:{
83 int i;
84 if(AF_OK != af_from_ms(AF_NCH, arg, s->wi, af->data->rate, 0.0, 1000.0))
85 return AF_ERROR;
86 s->ri = 0;
87 for(i=0;i<AF_NCH;i++){
88 mp_msg(MSGT_AFILTER, MSGL_DBG2, "[delay] Channel %i delayed by %0.3fms\n",
89 i,clamp(s->d[i],0.0,1000.0));
90 mp_msg(MSGT_AFILTER, MSGL_DBG3, "[delay] Channel %i delayed by %i samples\n",
91 i,s->wi[i]);
93 return AF_OK;
95 case AF_CONTROL_DELAY_LEN | AF_CONTROL_GET:{
96 int i;
97 for(i=0;i<AF_NCH;i++){
98 if(s->ri > s->wi[i])
99 s->wi[i] = L - (s->ri - s->wi[i]);
100 else
101 s->wi[i] = s->wi[i] - s->ri;
103 return af_to_ms(AF_NCH, s->wi, arg, af->data->rate);
106 return AF_UNKNOWN;
109 // Deallocate memory
110 static void uninit(struct af_instance_s* af)
112 int i;
114 free(af->data);
115 for(i=0;i<AF_NCH;i++)
116 free(((af_delay_t*)(af->setup))->q[i]);
117 free(af->setup);
120 // Filter data through filter
121 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
123 af_data_t* c = data; // Current working data
124 af_delay_t* s = af->setup; // Setup for this instance
125 int nch = c->nch; // Number of channels
126 int len = c->len/c->bps; // Number of sample in data chunk
127 int ri = 0;
128 int ch,i;
129 for(ch=0;ch<nch;ch++){
130 switch(c->bps){
131 case 1:{
132 int8_t* a = c->audio;
133 int8_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 2:{
146 int16_t* a = c->audio;
147 int16_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;
159 case 4:{
160 int32_t* a = c->audio;
161 int32_t* q = s->q[ch];
162 int wi = s->wi[ch];
163 ri = s->ri;
164 for(i=ch;i<len;i+=nch){
165 q[wi] = a[i];
166 a[i] = q[ri];
167 UPDATEQI(wi);
168 UPDATEQI(ri);
170 s->wi[ch] = wi;
171 break;
175 s->ri = ri;
176 return c;
179 // Allocate memory and set function pointers
180 static int af_open(af_instance_t* af){
181 af->control=control;
182 af->uninit=uninit;
183 af->play=play;
184 af->mul=1;
185 af->data=calloc(1,sizeof(af_data_t));
186 af->setup=calloc(1,sizeof(af_delay_t));
187 if(af->data == NULL || af->setup == NULL)
188 return AF_ERROR;
189 return AF_OK;
192 // Description of this filter
193 af_info_t af_info_delay = {
194 "Delay audio filter",
195 "delay",
196 "Anders",
198 AF_FLAGS_REENTRANT,
199 af_open