annotate libao2/pl_delay.c @ 3274:ac7ded58b6df

mpeg subtitle flickering patch by Evgeny Chukreev <codedj@echo.ru>
author arpi
date Mon, 03 Dec 2001 01:03:17 +0000
parents 1648d11fc36c
children 4a5d56e89735
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
1 /* This is a null audio out plugin it doesnt't really do anything
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
2 useful but serves an example of how audio plugins work. It delays
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
3 the output signal by the nuber of samples set by aop_delay n
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
4 where n is the number of bytes.
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
5 */
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
6 #define PLUGIN
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
7
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
8 #include <stdio.h>
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
9 #include <stdlib.h>
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
10
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
11 #include "audio_out.h"
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
12 #include "audio_plugin.h"
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
13 #include "audio_plugin_internal.h"
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
14 #include "afmt.h"
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
15
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
16 static ao_info_t info =
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
17 {
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
18 "Delay audio plugin",
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
19 "delay",
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
20 "Anders",
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
21 ""
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
22 };
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
23
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
24 LIBAO_PLUGIN_EXTERN(delay)
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
25
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
26 // local data
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
27 typedef struct pl_delay_s
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
28 {
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
29 void* data; // local audio data block
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
30 void* delay; // data block used for delaying audio signal
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
31 int len; // local buffer length
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
32 int rate; // local data rate
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
33 int channels; // local number of channels
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
34 int format; // local format
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
35
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
36 } pl_delay_t;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
37
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
38 static pl_delay_t pl_delay={NULL,NULL,0,0,0,0};
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
39
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
40 // to set/get/query special features/parameters
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
41 static int control(int cmd,int arg){
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
42 switch(cmd){
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
43 case AOCONTROL_PLUGIN_SET_LEN:
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
44 if(pl_delay.data)
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
45 uninit();
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
46 pl_delay.len = ao_plugin_data.len;
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
47 pl_delay.data=(void*)malloc(ao_plugin_data.len);
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
48 return CONTROL_OK;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
49 }
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
50 return -1;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
51 }
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
52
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
53 // open & setup audio device
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
54 // return: 1=success 0=fail
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
55 static int init(){
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
56 int i=0;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
57 float time_delay; // The number of tsamples this plugin delays the output data
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
58 /* if the output format of any of the below parameters differs from
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
59 what is give it should be changed. See ao_plugin init() */
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
60 pl_delay.rate=ao_plugin_data.rate;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
61 pl_delay.channels=ao_plugin_data.channels+1; //0=mono 1=stereo
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
62 pl_delay.format=ao_plugin_data.format;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
63
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
64 // Tell ao_plugin how much this plugin adds to the overall time delay
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
65 time_delay=-1*(float)ao_plugin_cfg.pl_delay_len/((float)pl_delay.channels*(float)pl_delay.rate);
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
66 if(pl_delay.format != AFMT_U8 && pl_delay.format != AFMT_S8)
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
67 time_delay/=2;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
68 ao_plugin_data.delay_fix+=time_delay;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
69
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
70 pl_delay.delay=(void*)malloc(ao_plugin_cfg.pl_delay_len);
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
71 if(!pl_delay.delay)
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
72 return 0;
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
73 for(i=0;i<ao_plugin_cfg.pl_delay_len;i++)
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
74 ((char*)pl_delay.delay)[i]=0;
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
75 printf("[pl_delay] Output sound delayed by %i bytes\n",ao_plugin_cfg.pl_delay_len);
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
76 return 1;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
77 }
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
78
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
79 // close plugin
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
80 static void uninit(){
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
81 if(pl_delay.delay)
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
82 free(pl_delay.delay);
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
83 if(pl_delay.data)
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
84 free(pl_delay.data);
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
85 ao_plugin_cfg.pl_delay_len=0;
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
86 }
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
87
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
88 // empty buffers
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
89 static void reset(){
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
90 int i = 0;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
91 for(i=0;i<pl_delay.len;i++)
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
92 ((char*)pl_delay.data)[i]=0;
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
93 for(i=0;i<ao_plugin_cfg.pl_delay_len;i++)
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
94 ((char*)pl_delay.delay)[i]=0;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
95 }
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
96
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
97 // processes 'ao_plugin_data.len' bytes of 'data'
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
98 // called for every block of data
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
99 static int play(){
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
100 int i=0;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
101 int j=0;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
102 int k=0;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
103 // Copy end of prev block to begining of buffer
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
104 for(i=0;i<ao_plugin_cfg.pl_delay_len;i++,j++)
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
105 ((char*)pl_delay.data)[j]=((char*)pl_delay.delay)[i];
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
106 // Copy current block except end
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
107 for(i=0;i<ao_plugin_data.len-ao_plugin_cfg.pl_delay_len;i++,j++,k++)
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
108 ((char*)pl_delay.data)[j]=((char*)ao_plugin_data.data)[k];
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
109 // Save away end of current block for next call
3194
1648d11fc36c commandline configuration of audio plugins now through struct, format conversion plugin added
anders
parents: 3107
diff changeset
110 for(i=0;i<ao_plugin_cfg.pl_delay_len;i++,k++)
3107
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
111 ((char*)pl_delay.delay)[i]=((char*)ao_plugin_data.data)[k];
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
112 // Set output data block
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
113 ao_plugin_data.data=pl_delay.data;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
114 return 1;
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
115 }
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
116
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
117
ef2287ccc42b Changes to audio out plugin, first plugin added
anders
parents:
diff changeset
118