1 /*=============================================================================
3 // This software has been released under the terms of the GNU General Public
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
6 // Copyright 2005 Alex Beregszaszi
8 //=============================================================================
20 // Convert from string to format
21 int af_str2fmt(const char* str
)
24 // Scan for endianness
25 if(strstr(str
,"be") || strstr(str
,"BE"))
26 format
|= AF_FORMAT_BE
;
27 else if(strstr(str
,"le") || strstr(str
,"LE"))
28 format
|= AF_FORMAT_LE
;
30 format
|= AF_FORMAT_NE
;
32 // Scan for special formats
33 if(strstr(str
,"mulaw") || strstr(str
,"MULAW")){
34 format
|= AF_FORMAT_MU_LAW
; return format
;
36 if(strstr(str
,"alaw") || strstr(str
,"ALAW")){
37 format
|= AF_FORMAT_A_LAW
; return format
;
39 if(strstr(str
,"ac3") || strstr(str
,"AC3")){
40 format
|= AF_FORMAT_AC3
; return format
;
42 if(strstr(str
,"mpeg2") || strstr(str
,"MPEG2")){
43 format
|= AF_FORMAT_MPEG2
; return format
;
45 if(strstr(str
,"imaadpcm") || strstr(str
,"IMAADPCM")){
46 format
|= AF_FORMAT_IMA_ADPCM
; return format
;
50 if(strstr(str
,"float") || strstr(str
,"FLOAT")){
51 format
|= AF_FORMAT_F
; return format
;
54 format
|= AF_FORMAT_I
;
56 // Scan for signed/unsigned
57 if(strstr(str
,"unsigned") || strstr(str
,"UNSIGNED"))
58 format
|= AF_FORMAT_US
;
60 format
|= AF_FORMAT_SI
;
65 int af_fmt2bits(int format
)
67 return (format
& AF_FORMAT_BITS_MASK
)+8;
68 // return (((format & AF_FORMAT_BITS_MASK)>>3)+1) * 8;
70 switch(format
& AF_FORMAT_BITS_MASK
)
72 case AF_FORMAT_8BIT
: return 8;
73 case AF_FORMAT_16BIT
: return 16;
74 case AF_FORMAT_24BIT
: return 24;
75 case AF_FORMAT_32BIT
: return 32;
76 case AF_FORMAT_48BIT
: return 48;
82 int af_bits2fmt(int bits
)
84 return (bits
/8 - 1) << 3;
87 /* Convert format to str input str is a buffer for the
88 converted string, size is the size of the buffer */
89 char* af_fmt2str(int format
, char* str
, int size
)
95 size
--; // reserve one for terminating 0
98 if(AF_FORMAT_LE
== (format
& AF_FORMAT_END_MASK
))
99 i
+=snprintf(str
,size
-i
,"little-endian ");
101 i
+=snprintf(str
,size
-i
,"big-endian ");
103 if(format
& AF_FORMAT_SPECIAL_MASK
){
104 switch(format
& AF_FORMAT_SPECIAL_MASK
){
105 case(AF_FORMAT_MU_LAW
):
106 i
+=snprintf(&str
[i
],size
-i
,"mu-law "); break;
107 case(AF_FORMAT_A_LAW
):
108 i
+=snprintf(&str
[i
],size
-i
,"A-law "); break;
109 case(AF_FORMAT_MPEG2
):
110 i
+=snprintf(&str
[i
],size
-i
,"MPEG-2 "); break;
112 i
+=snprintf(&str
[i
],size
-i
,"AC3 "); break;
113 case(AF_FORMAT_IMA_ADPCM
):
114 i
+=snprintf(&str
[i
],size
-i
,"IMA-ADPCM "); break;
116 i
+=snprintf(&str
[i
],size
-i
,MSGTR_AF_FORMAT_UnknownFormat
);
121 i
+=snprintf(&str
[i
],size
-i
,"%d-bit ", af_fmt2bits(format
));
124 if(AF_FORMAT_F
== (format
& AF_FORMAT_POINT_MASK
))
125 i
+=snprintf(&str
[i
],size
-i
,"float ");
128 if(AF_FORMAT_US
== (format
& AF_FORMAT_SIGN_MASK
))
129 i
+=snprintf(&str
[i
],size
-i
,"unsigned ");
131 i
+=snprintf(&str
[i
],size
-i
,"signed ");
133 i
+=snprintf(&str
[i
],size
-i
,"int ");
136 // remove trailing space
137 if (i
> 0 && str
[i
- 1] == ' ')
139 str
[i
] = 0; // make sure it is 0 terminated.
146 } af_fmtstr_table
[] = {
147 { "mulaw", AF_FORMAT_MU_LAW
},
148 { "alaw", AF_FORMAT_A_LAW
},
149 { "mpeg2", AF_FORMAT_MPEG2
},
150 { "ac3", AF_FORMAT_AC3
},
151 { "imaadpcm", AF_FORMAT_IMA_ADPCM
},
153 { "u8", AF_FORMAT_U8
},
154 { "s8", AF_FORMAT_S8
},
155 { "u16le", AF_FORMAT_U16_LE
},
156 { "u16be", AF_FORMAT_U16_BE
},
157 { "u16ne", AF_FORMAT_U16_NE
},
158 { "s16le", AF_FORMAT_S16_LE
},
159 { "s16be", AF_FORMAT_S16_BE
},
160 { "s16ne", AF_FORMAT_S16_NE
},
161 { "u24le", AF_FORMAT_U24_LE
},
162 { "u24be", AF_FORMAT_U24_BE
},
163 { "u24ne", AF_FORMAT_U24_NE
},
164 { "s24le", AF_FORMAT_S24_LE
},
165 { "s24be", AF_FORMAT_S24_BE
},
166 { "s24ne", AF_FORMAT_S24_NE
},
167 { "u32le", AF_FORMAT_U32_LE
},
168 { "u32be", AF_FORMAT_U32_BE
},
169 { "u32ne", AF_FORMAT_U32_NE
},
170 { "s32le", AF_FORMAT_S32_LE
},
171 { "s32be", AF_FORMAT_S32_BE
},
172 { "s32ne", AF_FORMAT_S32_NE
},
173 { "floatle", AF_FORMAT_FLOAT_LE
},
174 { "floatbe", AF_FORMAT_FLOAT_BE
},
175 { "floatne", AF_FORMAT_FLOAT_NE
},
180 const char *af_fmt2str_short(int format
)
184 for (i
= 0; af_fmtstr_table
[i
].name
; i
++)
185 if (af_fmtstr_table
[i
].format
== format
)
186 return af_fmtstr_table
[i
].name
;
191 int af_str2fmt_short(const char* str
)
195 for (i
= 0; af_fmtstr_table
[i
].name
; i
++)
196 if (!strcasecmp(str
, af_fmtstr_table
[i
].name
))
197 return af_fmtstr_table
[i
].format
;