comparison libao2/ao_plugin.c @ 3096:15abd9121737

ao_plugin.c and plugin headers added
author anders
date Sat, 24 Nov 2001 05:24:06 +0000
parents
children ef2287ccc42b
comparison
equal deleted inserted replaced
3095:981a9e5118ce 3096:15abd9121737
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "../config.h"
5
6 #include "audio_out.h"
7 #include "audio_out_internal.h"
8
9 #include "audio_plugin.h"
10
11 static ao_info_t info =
12 {
13 "Plugin audio output",
14 "plugin",
15 "Anders",
16 ""
17 };
18
19 LIBAO_EXTERN(plugin)
20
21 #define plugin(i) (ao_plugin_local_data.ao_plugins[i])
22 #define driver() (ao_plugin_local_data.ao_driver)
23
24 /* local data */
25 typedef struct ao_plugin_local_data_s
26 {
27 ao_plugin_functions_t** ao_plugins; /* List of all plugins */
28 ao_functions_t* ao_driver; /* ao driver used by ao_plugin */
29 } ao_plugin_local_data_t;
30
31 ao_plugin_local_data_t ao_plugin_local_data;
32
33 /* gloabal data */
34 ao_plugin_data_t ao_plugin_data;
35
36 // to set/get/query special features/parameters
37 static int control(int cmd,int arg){
38 return driver()->control(cmd,arg);
39 }
40
41 // open & setup audio device and plugins
42 // return: 1=success 0=fail
43 static int init(int rate,int channels,int format,int flags){
44 int ok=1;
45
46 /* FIXME these are cfg file parameters */
47 int i=0;
48 ao_plugin_local_data.ao_plugins=malloc((i+1)*sizeof(ao_plugin_functions_t*));
49 plugin(i)=NULL;
50 ao_plugin_local_data.ao_driver=audio_out_drivers[1];
51
52 /* Set input parameters and itterate through plugins each plugin
53 changes the parameters according to its output */
54 ao_plugin_data.rate=rate;
55 ao_plugin_data.channels=channels;
56 ao_plugin_data.format=format;
57 ao_plugin_data.sz_mult=1;
58 ao_plugin_data.sz_fix=0;
59 ao_plugin_data.delay_mult=1;
60 ao_plugin_data.delay_fix=0;
61 i=0;
62 while(plugin(i)&&ok)
63 ok=plugin(i++)->init();
64
65 if(!ok) return 0;
66
67 ok = driver()->init(ao_plugin_data.rate,
68 ao_plugin_data.channels,
69 ao_plugin_data.format,
70 flags);
71 if(!ok) return 0;
72
73 /* Now that the driver is initialized we can calculate and set the
74 input and output buffers for each plugin */
75 ao_plugin_data.len=driver()->get_space();
76
77 return 1;
78 }
79
80 // close audio device
81 static void uninit(){
82 int i=0;
83 driver()->uninit();
84 while(plugin(i))
85 plugin(i++)->uninit();
86 if(ao_plugin_local_data.ao_plugins)
87 free(ao_plugin_local_data.ao_plugins);
88 }
89
90 // stop playing and empty buffers (for seeking/pause)
91 static void reset(){
92 int i=0;
93 driver()->reset();
94 while(plugin(i))
95 plugin(i++)->reset();
96 }
97
98 // stop playing, keep buffers (for pause)
99 static void audio_pause(){
100 driver()->pause();
101 }
102
103 // resume playing, after audio_pause()
104 static void audio_resume(){
105 driver()->resume();
106 }
107
108 // return: how many bytes can be played without blocking
109 static int get_space(){
110 double sz=(double)(driver()->get_space());
111 sz*=ao_plugin_data.sz_mult;
112 sz+=ao_plugin_data.sz_fix;
113 return (int)(sz);
114 }
115
116 // plays 'len' bytes of 'data'
117 // return: number of bytes played
118 static int play(void* data,int len,int flags){
119 int i=0;
120 /* Due to constant buffer sizes in plugins limit length */
121 int tmp = get_space();
122 int ret_len =(tmp<len)?tmp:len;
123 /* Filter data */
124 ao_plugin_data.len=ret_len;
125 ao_plugin_data.data=data;
126 while(plugin(i))
127 plugin(i++)->play();
128 /* Send data to output */
129 len=driver()->play(ao_plugin_data.data,ao_plugin_data.len,flags);
130
131 if(len!=ao_plugin_data.len)
132 printf("Buffer over flow in sound plugin ");
133
134 return ret_len;
135 }
136
137 // return: delay in seconds between first and last sample in buffer
138 static float get_delay(){
139 float delay=driver()->get_delay();
140 delay*=ao_plugin_data.delay_mult;
141 delay+=ao_plugin_data.delay_fix;
142 return delay;
143 }
144
145