7568
|
1 /* This audio filter doesn't really do anything useful but serves an
|
|
2 example of how audio filters work. It delays the output signal by
|
|
3 the number of seconds set by delay=n where n is the number of
|
|
4 seconds.
|
|
5 */
|
|
6 #include <stdio.h>
|
|
7 #include <stdlib.h>
|
|
8 #include <string.h>
|
|
9
|
|
10 #include "../config.h"
|
|
11 #include "../mp_msg.h"
|
|
12
|
|
13 #include "af.h"
|
|
14
|
|
15 // Data for specific instances of this filter
|
|
16 typedef struct af_delay_s
|
|
17 {
|
|
18 void* buf; // data block used for delaying audio signal
|
|
19 int len; // local buffer length
|
|
20 float tlen; // Delay in seconds
|
|
21 }af_delay_t;
|
|
22
|
|
23 // Initialization and runtime control
|
|
24 static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
25 {
|
|
26 switch(cmd){
|
|
27 case AF_CONTROL_REINIT:{
|
|
28 af->data->rate = ((af_data_t*)arg)->rate;
|
|
29 af->data->nch = ((af_data_t*)arg)->nch;
|
|
30 af->data->format = ((af_data_t*)arg)->format;
|
|
31 af->data->bps = ((af_data_t*)arg)->bps;
|
|
32
|
|
33 return af->control(af,AF_CONTROL_SET_DELAY_LEN,&((af_delay_t*)af->setup)->tlen);
|
|
34 }
|
|
35 case AF_CONTROL_SET_DELAY_LEN:{
|
|
36 af_delay_t* s = (af_delay_t*)af->setup;
|
|
37 void* bt = s->buf; // Old buffer
|
|
38 int lt = s->len; // Old len
|
|
39
|
|
40 if(*((float*)arg) > 30 || *((float*)arg) < 0){
|
|
41 mp_msg(MSGT_AFILTER,MSGL_ERR,"Error setting delay length in af_delay. Delay must be between 0s and 30s\n");
|
|
42 s->len=0;
|
|
43 s->tlen=0.0;
|
|
44 return AF_ERROR;
|
|
45 }
|
|
46
|
|
47 // Set new len and allocate new buffer
|
|
48 s->tlen = *((float*)arg);
|
|
49 s->len = af->data->rate*af->data->bps*af->data->nch*(int)s->tlen;
|
|
50 s->buf = malloc(s->len);
|
|
51 mp_msg(MSGT_AFILTER,MSGL_DBG2,"[delay] Delaying audio output by %0.2fs\n",s->tlen);
|
|
52 mp_msg(MSGT_AFILTER,MSGL_DBG3,"[delay] Delaying audio output by %i bytes\n",s->len);
|
|
53
|
|
54 // Out of memory error
|
|
55 if(!s->buf){
|
|
56 s->len = 0;
|
|
57 free(bt);
|
|
58 return AF_ERROR;
|
|
59 }
|
|
60
|
|
61 // Clear the new buffer
|
|
62 memset(s->buf, 0, s->len);
|
|
63
|
|
64 /* Copy old buffer to avoid click in output
|
|
65 sound (at least most of it) and release it */
|
|
66 if(bt){
|
|
67 memcpy(s->buf,bt,min(lt,s->len));
|
|
68 free(bt);
|
|
69 }
|
|
70 return AF_OK;
|
|
71 }
|
|
72 }
|
|
73 return AF_UNKNOWN;
|
|
74 }
|
|
75
|
|
76 // Deallocate memory
|
|
77 static void uninit(struct af_instance_s* af)
|
|
78 {
|
|
79 if(af->data->audio)
|
|
80 free(af->data->audio);
|
|
81 if(af->data)
|
|
82 free(af->data);
|
|
83 if(((af_delay_t*)(af->setup))->buf)
|
|
84 free(((af_delay_t*)(af->setup))->buf);
|
|
85 if(af->setup)
|
|
86 free(af->setup);
|
|
87 }
|
|
88
|
|
89 // Filter data through filter
|
|
90 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
91 {
|
|
92 af_data_t* c = data; // Current working data
|
|
93 af_data_t* l = af->data; // Local data
|
|
94 af_delay_t* s = (af_delay_t*)af->setup; // Setup for this instance
|
|
95
|
|
96
|
|
97 if(AF_OK != RESIZE_LOCAL_BUFFER(af , data))
|
|
98 return NULL;
|
|
99
|
|
100 if(s->len > c->len){ // Delay bigger than buffer
|
|
101 // Copy beginning of buffer to beginning of output buffer
|
|
102 memcpy(l->audio,s->buf,c->len);
|
|
103 // Move buffer left
|
|
104 memcpy(s->buf,s->buf+c->len,s->len-c->len);
|
|
105 // Save away current audio to end of buffer
|
|
106 memcpy(s->buf+s->len-c->len,c->audio,c->len);
|
|
107 }
|
|
108 else{
|
|
109 // Copy end of previous block to beginning of output buffer
|
|
110 memcpy(l->audio,s->buf,s->len);
|
|
111 // Copy current block except end
|
|
112 memcpy(l->audio+s->len,c->audio,c->len-s->len);
|
|
113 // Save away end of current block for next call
|
|
114 memcpy(s->buf,c->audio+c->len-s->len,s->len);
|
|
115 }
|
|
116
|
|
117 // Set output data
|
|
118 c->audio=l->audio;
|
|
119
|
|
120 return c;
|
|
121 }
|
|
122
|
|
123 // Allocate memory and set function pointers
|
|
124 static int open(af_instance_t* af){
|
|
125 af->control=control;
|
|
126 af->uninit=uninit;
|
|
127 af->play=play;
|
|
128 af->mul.n=1;
|
|
129 af->mul.d=1;
|
|
130 af->data=calloc(1,sizeof(af_data_t));
|
|
131 af->setup=calloc(1,sizeof(af_delay_t));
|
|
132 if(af->data == NULL || af->setup == NULL)
|
|
133 return AF_ERROR;
|
|
134 return AF_OK;
|
|
135 }
|
|
136
|
|
137 // Description of this filter
|
|
138 af_info_t af_info_delay = {
|
|
139 "Delay audio filter",
|
|
140 "delay",
|
|
141 "Anders",
|
|
142 "",
|
|
143 open
|
|
144 };
|
|
145
|
|
146
|