Mercurial > mplayer.hg
annotate libaf/af_delay.c @ 8154:3dd948f5d012
size optim
author | pontscho |
---|---|
date | Mon, 11 Nov 2002 18:31:13 +0000 |
parents | d48a06d07afb |
children | e8832e66babd |
rev | line source |
---|---|
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 | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7715
diff
changeset
|
33 return af->control(af,AF_CONTROL_DELAY_SET_LEN,&((af_delay_t*)af->setup)->tlen); |
7568 | 34 } |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7745
diff
changeset
|
35 case AF_CONTROL_COMMAND_LINE:{ |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7745
diff
changeset
|
36 float d = 0; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7745
diff
changeset
|
37 sscanf((char*)arg,"%f",&d); |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7745
diff
changeset
|
38 return af->control(af,AF_CONTROL_DELAY_SET_LEN,&d); |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7745
diff
changeset
|
39 } |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7715
diff
changeset
|
40 case AF_CONTROL_DELAY_SET_LEN:{ |
7568 | 41 af_delay_t* s = (af_delay_t*)af->setup; |
42 void* bt = s->buf; // Old buffer | |
43 int lt = s->len; // Old len | |
44 | |
45 if(*((float*)arg) > 30 || *((float*)arg) < 0){ | |
46 mp_msg(MSGT_AFILTER,MSGL_ERR,"Error setting delay length in af_delay. Delay must be between 0s and 30s\n"); | |
47 s->len=0; | |
48 s->tlen=0.0; | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7615
diff
changeset
|
49 af->delay=0.0; |
7568 | 50 return AF_ERROR; |
51 } | |
52 | |
53 // Set new len and allocate new buffer | |
54 s->tlen = *((float*)arg); | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7615
diff
changeset
|
55 af->delay = s->tlen * 1000.0; |
7568 | 56 s->len = af->data->rate*af->data->bps*af->data->nch*(int)s->tlen; |
57 s->buf = malloc(s->len); | |
58 mp_msg(MSGT_AFILTER,MSGL_DBG2,"[delay] Delaying audio output by %0.2fs\n",s->tlen); | |
59 mp_msg(MSGT_AFILTER,MSGL_DBG3,"[delay] Delaying audio output by %i bytes\n",s->len); | |
60 | |
61 // Out of memory error | |
62 if(!s->buf){ | |
63 s->len = 0; | |
64 free(bt); | |
65 return AF_ERROR; | |
66 } | |
67 | |
68 // Clear the new buffer | |
69 memset(s->buf, 0, s->len); | |
70 | |
71 /* Copy old buffer to avoid click in output | |
72 sound (at least most of it) and release it */ | |
73 if(bt){ | |
74 memcpy(s->buf,bt,min(lt,s->len)); | |
75 free(bt); | |
76 } | |
77 return AF_OK; | |
78 } | |
79 } | |
80 return AF_UNKNOWN; | |
81 } | |
82 | |
83 // Deallocate memory | |
84 static void uninit(struct af_instance_s* af) | |
85 { | |
86 if(af->data->audio) | |
87 free(af->data->audio); | |
88 if(af->data) | |
89 free(af->data); | |
90 if(((af_delay_t*)(af->setup))->buf) | |
91 free(((af_delay_t*)(af->setup))->buf); | |
92 if(af->setup) | |
93 free(af->setup); | |
94 } | |
95 | |
96 // Filter data through filter | |
97 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
98 { | |
99 af_data_t* c = data; // Current working data | |
100 af_data_t* l = af->data; // Local data | |
101 af_delay_t* s = (af_delay_t*)af->setup; // Setup for this instance | |
102 | |
103 | |
104 if(AF_OK != RESIZE_LOCAL_BUFFER(af , data)) | |
105 return NULL; | |
106 | |
107 if(s->len > c->len){ // Delay bigger than buffer | |
108 // Copy beginning of buffer to beginning of output buffer | |
109 memcpy(l->audio,s->buf,c->len); | |
110 // Move buffer left | |
7715 | 111 memmove(s->buf,s->buf+c->len,s->len-c->len); |
7568 | 112 // Save away current audio to end of buffer |
113 memcpy(s->buf+s->len-c->len,c->audio,c->len); | |
114 } | |
115 else{ | |
116 // Copy end of previous block to beginning of output buffer | |
117 memcpy(l->audio,s->buf,s->len); | |
118 // Copy current block except end | |
119 memcpy(l->audio+s->len,c->audio,c->len-s->len); | |
120 // Save away end of current block for next call | |
121 memcpy(s->buf,c->audio+c->len-s->len,s->len); | |
122 } | |
123 | |
124 // Set output data | |
125 c->audio=l->audio; | |
126 | |
127 return c; | |
128 } | |
129 | |
130 // Allocate memory and set function pointers | |
131 static int open(af_instance_t* af){ | |
132 af->control=control; | |
133 af->uninit=uninit; | |
134 af->play=play; | |
135 af->mul.n=1; | |
136 af->mul.d=1; | |
137 af->data=calloc(1,sizeof(af_data_t)); | |
138 af->setup=calloc(1,sizeof(af_delay_t)); | |
139 if(af->data == NULL || af->setup == NULL) | |
140 return AF_ERROR; | |
141 return AF_OK; | |
142 } | |
143 | |
144 // Description of this filter | |
145 af_info_t af_info_delay = { | |
146 "Delay audio filter", | |
147 "delay", | |
148 "Anders", | |
149 "", | |
7615 | 150 AF_FLAGS_REENTRANT, |
7568 | 151 open |
152 }; | |
153 | |
154 |