comparison libao2/pl_format.c @ 3194:1648d11fc36c

commandline configuration of audio plugins now through struct, format conversion plugin added
author anders
date Thu, 29 Nov 2001 12:44:06 +0000
parents
children 62d797a16f72
comparison
equal deleted inserted replaced
3193:53a6d2fc1498 3194:1648d11fc36c
1 /* This is a null audio out plugin it doesnt't really do anything
2 useful but serves an example of how audio plugins work. It delays
3 the output signal by the nuber of samples set by aop_delay n
4 where n is the number of bytes.
5 */
6 #define PLUGIN
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "audio_out.h"
12 #include "audio_plugin.h"
13 #include "audio_plugin_internal.h"
14 #include "afmt.h"
15
16 static ao_info_t info =
17 {
18 "Sample format conversion audio plugin",
19 "format",
20 "Anders",
21 ""
22 };
23
24 LIBAO_PLUGIN_EXTERN(format)
25
26 // local data
27 typedef struct pl_format_s
28 {
29 void* data; // local audio data block
30 int len; // local buffer length
31 int in; // Input fomat
32 int out; // Output fomat
33 double sz_mult; // data size multiplier
34 } pl_format_t;
35
36 static pl_format_t pl_format={NULL,0,0,0,1};
37
38 // Number of bits
39 #define B08 0
40 #define B16 1
41 #define B32 2
42 #define NBITS_MASK 3
43
44 // Endianess
45 #define BE (0<<3) // Big endian
46 #define LE (1<<3) // Little endian
47 #define END_MASK (1<<3)
48
49 // Signed
50 #define US (0<<4)
51 #define SI (1<<4)
52 #define SIGN_MASK (1<<4)
53
54 // to set/get/query special features/parameters
55 static int control(int cmd,int arg){
56 switch(cmd){
57 case AOCONTROL_PLUGIN_SET_LEN:
58 if(pl_format.data)
59 uninit();
60 pl_format.len = ao_plugin_data.len;
61 pl_format.data=(void*)malloc(ao_plugin_data.len);
62 ao_plugin_data.len=(int)((double)ao_plugin_data.len*pl_format.sz_mult);
63 return CONTROL_OK;
64 }
65 return -1;
66 }
67
68 // open & setup audio device
69 // return: 1=success 0=fail
70 static int init(){
71 int i=0;
72 int sign=0;
73 int nbits=8;
74 int be_le=BE;
75
76 // Sheck input format
77 switch(ao_plugin_data.format){
78 case(AFMT_U8):
79 pl_format.in=LE|B08|US; break;
80 case(AFMT_S8):
81 pl_format.in=LE|B08|SI; break;
82 case(AFMT_S16_LE):
83 pl_format.in=LE|B16|US; break;
84 case(AFMT_S16_BE):
85 pl_format.in=BE|B16|SI; break;
86 case(AFMT_U16_LE):
87 pl_format.in=LE|B16|US; break;
88 case(AFMT_U16_BE):
89 pl_format.in=BE|B16|US; break;
90 case(AFMT_S32_LE):
91 pl_format.in=LE|B32|SI; break;
92 case(AFMT_S32_BE):
93 pl_format.in=BE|B32|SI; break;
94 case(AFMT_IMA_ADPCM):
95 case(AFMT_MU_LAW):
96 case(AFMT_A_LAW):
97 case(AFMT_MPEG):
98 case(AFMT_AC3):
99 printf("[pl_format] Audio format not yet suported \n");
100 return 0;
101 default:
102 printf("[pl_format] Unsupported audio format\n"); // Should never happen...
103 return 0;
104 }
105 // Sheck output format
106 switch(ao_plugin_cfg.pl_format_type){
107 case(AFMT_U8):
108 pl_format.in=LE|B08|US; break;
109 case(AFMT_S8):
110 pl_format.in=LE|B08|SI; break;
111 case(AFMT_S16_LE):
112 pl_format.in=LE|B16|US; break;
113 case(AFMT_S16_BE):
114 pl_format.in=BE|B16|SI; break;
115 case(AFMT_U16_LE):
116 pl_format.in=LE|B16|US; break;
117 case(AFMT_U16_BE):
118 pl_format.in=BE|B16|US; break;
119 case(AFMT_S32_LE):
120 pl_format.in=LE|B32|SI; break;
121 case(AFMT_S32_BE):
122 pl_format.in=BE|B32|SI; break;
123 case(AFMT_IMA_ADPCM):
124 case(AFMT_MU_LAW):
125 case(AFMT_A_LAW):
126 case(AFMT_MPEG):
127 case(AFMT_AC3):
128 printf("[pl_format] Audio format not yet suported \n");
129 return 0;
130 default:
131 printf("[pl_format] Unsupported audio format\n"); // Should never happen...
132 return 0;
133 }
134 // We are changing the format
135 ao_plugin_data.format=ao_plugin_cfg.pl_format_type;
136
137 // And perhaps the buffer size
138 pl_format.sz_mult=1;
139 if((pl_format.in&NBITS_MASK) < (pl_format.out&NBITS_MASK))
140 pl_format.sz_mult/=(double)(1<<(pl_format.out-pl_format.in));
141 if((pl_format.in&NBITS_MASK) > (pl_format.out&NBITS_MASK))
142 pl_format.sz_mult*=(double)(1<<(pl_format.out-pl_format.in));
143 ao_plugin_data.sz_mult*=pl_format.sz_mult;
144 return 1;
145 }
146
147 // close plugin
148 static void uninit(){
149 if(pl_format.data)
150 free(pl_format.data);
151 }
152
153 // empty buffers
154 static void reset(){
155 int i = 0;
156 for(i=0;i<pl_format.len;i++)
157 ((char*)pl_format.data)[i]=0;
158 }
159
160 // processes 'ao_plugin_data.len' bytes of 'data'
161 // called for every block of data
162 static int play(){
163 register int i=0;
164 void* in_data=ao_plugin_data.data;
165 void* out_data=pl_format.data;
166 int in_len=((int)(double)pl_format.len*pl_format.sz_mult);
167 in_len>>=pl_format.in&NBITS_MASK;
168
169 if((pl_format.in&END_MASK)!=(pl_format.out&END_MASK)){
170 switch(pl_format.in&NBITS_MASK){
171 case(B16):{
172 register int16_t s;
173 for(i=1;i<in_len;i++){
174 s=((int16_t*)in_data)[i];
175 ((int16_t*)in_data)[i]=(int16_t)(((s&0x00FF)<<8) | (s&0xFF00)>>8);
176 }
177 break;
178 }
179 case(B32):{
180 register int32_t s;
181 for(i=1;i<in_len;i++){
182 s=((int32_t*)in_data)[i];
183 ((int32_t*)in_data)[i]=(int32_t)(((s&0x000000FF)<<24) | ((s&0x0000FF00)<<8) |
184 ((s&0x00FF0000)>>8) | ((s&0xFF000000)>>24));
185 }
186 break;
187 }
188 }
189 }
190
191 return 1;
192 }
193
194
195