Mercurial > mplayer.hg
annotate libaf/af_delay.c @ 7691:5381fb575552
10l fix & cleanup of draw_frame(), still NO support for stride :(
author | arpi |
---|---|
date | Wed, 09 Oct 2002 23:39:36 +0000 |
parents | fbd5445cc853 |
children | 9774d0733dfa |
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 | |
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; | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7615
diff
changeset
|
44 af->delay=0.0; |
7568 | 45 return AF_ERROR; |
46 } | |
47 | |
48 // Set new len and allocate new buffer | |
49 s->tlen = *((float*)arg); | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7615
diff
changeset
|
50 af->delay = s->tlen * 1000.0; |
7568 | 51 s->len = af->data->rate*af->data->bps*af->data->nch*(int)s->tlen; |
52 s->buf = malloc(s->len); | |
53 mp_msg(MSGT_AFILTER,MSGL_DBG2,"[delay] Delaying audio output by %0.2fs\n",s->tlen); | |
54 mp_msg(MSGT_AFILTER,MSGL_DBG3,"[delay] Delaying audio output by %i bytes\n",s->len); | |
55 | |
56 // Out of memory error | |
57 if(!s->buf){ | |
58 s->len = 0; | |
59 free(bt); | |
60 return AF_ERROR; | |
61 } | |
62 | |
63 // Clear the new buffer | |
64 memset(s->buf, 0, s->len); | |
65 | |
66 /* Copy old buffer to avoid click in output | |
67 sound (at least most of it) and release it */ | |
68 if(bt){ | |
69 memcpy(s->buf,bt,min(lt,s->len)); | |
70 free(bt); | |
71 } | |
72 return AF_OK; | |
73 } | |
74 } | |
75 return AF_UNKNOWN; | |
76 } | |
77 | |
78 // Deallocate memory | |
79 static void uninit(struct af_instance_s* af) | |
80 { | |
81 if(af->data->audio) | |
82 free(af->data->audio); | |
83 if(af->data) | |
84 free(af->data); | |
85 if(((af_delay_t*)(af->setup))->buf) | |
86 free(((af_delay_t*)(af->setup))->buf); | |
87 if(af->setup) | |
88 free(af->setup); | |
89 } | |
90 | |
91 // Filter data through filter | |
92 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
93 { | |
94 af_data_t* c = data; // Current working data | |
95 af_data_t* l = af->data; // Local data | |
96 af_delay_t* s = (af_delay_t*)af->setup; // Setup for this instance | |
97 | |
98 | |
99 if(AF_OK != RESIZE_LOCAL_BUFFER(af , data)) | |
100 return NULL; | |
101 | |
102 if(s->len > c->len){ // Delay bigger than buffer | |
103 // Copy beginning of buffer to beginning of output buffer | |
104 memcpy(l->audio,s->buf,c->len); | |
105 // Move buffer left | |
106 memcpy(s->buf,s->buf+c->len,s->len-c->len); | |
107 // Save away current audio to end of buffer | |
108 memcpy(s->buf+s->len-c->len,c->audio,c->len); | |
109 } | |
110 else{ | |
111 // Copy end of previous block to beginning of output buffer | |
112 memcpy(l->audio,s->buf,s->len); | |
113 // Copy current block except end | |
114 memcpy(l->audio+s->len,c->audio,c->len-s->len); | |
115 // Save away end of current block for next call | |
116 memcpy(s->buf,c->audio+c->len-s->len,s->len); | |
117 } | |
118 | |
119 // Set output data | |
120 c->audio=l->audio; | |
121 | |
122 return c; | |
123 } | |
124 | |
125 // Allocate memory and set function pointers | |
126 static int open(af_instance_t* af){ | |
127 af->control=control; | |
128 af->uninit=uninit; | |
129 af->play=play; | |
130 af->mul.n=1; | |
131 af->mul.d=1; | |
132 af->data=calloc(1,sizeof(af_data_t)); | |
133 af->setup=calloc(1,sizeof(af_delay_t)); | |
134 if(af->data == NULL || af->setup == NULL) | |
135 return AF_ERROR; | |
136 return AF_OK; | |
137 } | |
138 | |
139 // Description of this filter | |
140 af_info_t af_info_delay = { | |
141 "Delay audio filter", | |
142 "delay", | |
143 "Anders", | |
144 "", | |
7615 | 145 AF_FLAGS_REENTRANT, |
7568 | 146 open |
147 }; | |
148 | |
149 |