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