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