19 #include <ucommon/ucommon.h>
20 #include <ucommon/export.h>
34 using namespace UCOMMON_NAMESPACE;
37 #define M_PI 3.14159265358979323846
40 #if !defined(__BIG_ENDIAN)
41 #define __LITTLE_ENDIAN 1234
42 #define __BIG_ENDIAN 4321
43 #define __PDP_ENDIAN 3412
44 #define __BYTE_ORDER __LITTLE_ENDIAN
47 #if _MSC_VER > 1400 // windows broken dll linkage issue...
49 const unsigned Audio::ndata = (unsigned)(-1);
52 Audio::level_t Audio::tolevel(
float dbm)
54 double l = pow(10.0, (dbm -
M_PI)/20.0)*(32768.0*0.70711);
55 return (level_t)(l*l);
58 float Audio::todbm(level_t l)
60 return (
float)(log10(sqrt((
float)l)/(32768.0*0.70711))*20.0 +
M_PI);
63 const char *Audio::getExtension(encoding_t encoding)
110 Audio::encoding_t Audio::getMono(encoding_t encoding)
126 Audio::encoding_t Audio::getStereo(encoding_t encoding)
143 return unknownEncoding;
147 Audio::encoding_t Audio::getEncoding(
const char *name)
149 if(!stricmp(name,
"ulaw") || !stricmp(name,
"mulaw") || !stricmp(name,
"pcmu"))
151 else if(!stricmp(name,
"alaw") || !stricmp(name,
"pcma"))
153 else if(!stricmp(name,
"linear") || !stricmp(name,
"pcm16") || !stricmp(name,
"pcm") || !stricmp(name,
"l16"))
155 else if(!stricmp(name,
"stereo"))
157 else if(!stricmp(name,
"cda"))
159 else if(!stricmp(name,
"gsm"))
161 else if(!stricmp(name,
"msgsm"))
163 else if(!stricmp(name,
"pcm8") || !stricmp(name,
"l8"))
165 else if(!stricmp(name,
"pcm32"))
167 else if(!stricmp(name,
"adpcm"))
169 else if(!stricmp(name,
"g721") || !stricmp(name,
"g.721"))
171 else if(!stricmp(name,
"g726-32"))
173 else if(!stricmp(name,
"g729") || !stricmp(name,
"g.729"))
175 else if(!stricmp(name,
"ilbc"))
177 else if(!stricmp(name,
"mp1"))
179 else if(!stricmp(name,
"mp2"))
181 else if(!stricmp(name,
"mp3"))
183 else if(!stricmp(name,
"oki"))
185 else if(!stricmp(name,
"vox"))
187 else if(!stricmp(name,
"sx73"))
189 else if(!stricmp(name,
"sx96"))
191 else if(!stricmp(name,
"spx") || !stricmp(name,
"speex"))
193 else if(!stricmp(name,
"g723-16") || !stricmp(name,
"g.723-16"))
195 else if(!stricmp(name,
"g723-24") || !stricmp(name,
"g.723-24"))
197 else if(!stricmp(name,
"g723-40") || !stricmp(name,
"g.723-40"))
199 else if(!stricmp(name,
".al") || !stricmp(name,
".alaw"))
201 else if(!stricmp(name,
".ul") || !stricmp(name,
".ulaw") || !stricmp(name,
".mulaw"))
203 else if(!stricmp(name,
".sw") || !stricmp(name,
".raw") || !stricmp(name,
".pcm"))
205 else if(!stricmp(name,
".vox") || !stricmp(name,
"vox"))
207 else if(!stricmp(name,
".adpcm"))
209 else if(!stricmp(name,
".g721"))
211 else if(!stricmp(name,
".a32") || !stricmp(name,
"a32"))
213 else if(!stricmp(name,
".a24") || !stricmp(name,
"a24"))
215 else if(!stricmp(name,
".a16") || !stricmp(name,
"a16"))
217 else if(!stricmp(name,
".a40") || !stricmp(name,
"a40"))
219 else if(!stricmp(name,
".g723"))
221 else if(!stricmp(name,
".g729"))
223 else if(!stricmp(name,
".ilbc"))
225 else if(!stricmp(name,
".a24"))
227 else if(!stricmp(name,
".a40"))
229 else if(!stricmp(name,
".cda"))
231 else if(!stricmp(name,
".sx"))
233 else if(!stricmp(name,
".gsm"))
235 else if(!stricmp(name,
".mp1"))
237 else if(!stricmp(name,
".mp2"))
239 else if(!stricmp(name,
".mp3"))
242 return unknownEncoding;
245 const char *Audio::getMIME(Info &info)
247 if(info.format == wave)
248 return "audio/x-wav";
250 if(info.format == snd) {
251 switch(info.encoding) {
253 return "audio/x-adpcm";
255 return "audio/basic";
259 if(info.format == riff)
260 return "audio/x-riff";
262 switch(info.encoding) {
265 return "application/x-spx";
269 return "audio/x-mpeg";
273 return "audio/x-vox";
275 return "audio/x-gsm";
285 const char *Audio::getName(encoding_t encoding)
343 bool Audio::is_buffered(encoding_t encoding)
355 bool Audio::is_linear(encoding_t encoding)
368 Audio::level_t Audio::impulse(encoding_t encoding,
void *buffer,
unsigned samples)
370 unsigned long sum = 0;
374 unsigned char *sv = (
unsigned char *)&mb, *s1, *s2;
377 samples = getCount(encoding);
387 sp = (linear_t)buffer;
394 return (level_t)(sum/count);
397 s1 = (
unsigned char *)buffer;
409 return (level_t)(sum / count);
415 Audio::level_t Audio::impulse(Info &info,
void *buffer,
unsigned samples)
417 unsigned long sum = 0;
421 unsigned char *sv = (
unsigned char *)&mb, *s1, *s2;
422 unsigned char *uv = (
unsigned char *)&ub;
425 samples = info.framecount;
428 samples = getCount(info.encoding);
430 switch(info.encoding) {
437 if(info.format == snd && (info.order ==
__BYTE_ORDER || !info.order)) {
439 up = (snd16_t *)buffer;
442 return (level_t)(sum / count);
444 if(info.format == snd) {
446 s1 = (
unsigned char *)buffer;
455 return (level_t)(sum / count);
458 sp = (linear_t)buffer;
465 return (level_t)(sum/count);
468 s1 = (
unsigned char *)buffer;
480 return (level_t)(sum / count);
486 Audio::level_t Audio::peak(encoding_t encoding,
void *buffer,
unsigned samples)
488 level_t max = 0, value;
490 unsigned char *sv = (
unsigned char *)&mb, *s1, *s2;
493 samples = getCount(encoding);
502 sp = (linear_t)buffer;
513 s1 = (
unsigned char *)buffer;
531 Audio::level_t Audio::peak(Info &info,
void *buffer,
unsigned samples)
533 level_t max = 0, value;
537 unsigned char *sv = (
unsigned char *)&mb, *s1, *s2;
538 unsigned char *uv = (
unsigned char *)&ub;
541 samples = info.framecount;
544 samples = getCount(info.encoding);
546 switch(info.encoding) {
553 if(info.format == snd && (info.order ==
__BYTE_ORDER || !info.order)) {
555 up = (snd16_t *)buffer;
557 value = (level_t)(*(up++) / 2);
563 if(info.format == snd) {
565 s1 = (
unsigned char *)buffer;
573 if((level_t)ub > max)
579 sp = (linear_t)buffer;
590 s1 = (
unsigned char *)buffer;
608 void Audio::swapEncoded(Info &info, encoded_t buffer,
size_t bytes)
612 if(!is_linear(info.encoding))
629 bool Audio::swapEndian(encoding_t encoding,
void *buffer,
unsigned samples)
632 unsigned char *s1, *s2, *s3, *s4;
633 if(is_stereo(encoding))
644 s1 = (
unsigned char *)buffer;
659 s1 = (
unsigned char *)buffer;
681 bool Audio::swapEndian(Info &info,
void *buffer,
unsigned samples)
684 unsigned char *s1, *s2, *s3, *s4;
685 if(is_stereo(info.encoding))
688 switch(info.encoding) {
696 s1 = (
unsigned char *)buffer;
711 s1 = (
unsigned char *)buffer;
734 bool Audio::is_endian(encoding_t encoding)
750 bool Audio::is_endian(Info &info)
752 switch(info.encoding) {
766 timeout_t Audio::getFraming(Info &info, timeout_t timeout)
768 timeout_t fa = info.framing;
769 unsigned long frames;
777 frames = timeout / fa;
781 timeout_t Audio::getFraming(encoding_t encoding, timeout_t timeout)
784 unsigned long frames;
821 frames = timeout / fa;
825 int Audio::getCount(encoding_t encoding)
850 case unknownEncoding:
866 int Audio::getFrame(encoding_t encoding,
int samples)
903 case unknownEncoding:
923 return (samples / framing) * framing;
926 void Audio::fill(
unsigned char *addr,
int samples, encoding_t encoding)
928 int frame = getFrame(encoding);
929 int count = getCount(encoding);
934 while(samples >= count) {
943 memset(addr, 0, frame);
951 Audio::rate_t Audio::getRate(encoding_t encoding, rate_t request)
953 if((
long)request == (
long)0)
954 request = getRate(encoding);
967 if(request == rate8khz)
973 return getRate(encoding);
976 Audio::rate_t Audio::getRate(encoding_t encoding)
985 case unknownEncoding:
1001 unsigned long Audio::toSamples(encoding_t encoding,
size_t bytes)
1003 unsigned long sf = getFrame(encoding);
1006 unsigned long frames = (
unsigned long)(bytes / sf);
1007 return frames * getCount(encoding);
1010 unsigned long Audio::toSamples(Info &info,
size_t bytes)
1015 unsigned long frames = (
unsigned long)(bytes / info.framesize);
1016 return frames * info.framecount;
1019 size_t Audio::toBytes(encoding_t encoding,
unsigned long samples)
1021 unsigned long sc = getCount(encoding);
1024 unsigned long frames = samples / sc;
1025 return frames * getFrame(encoding);
1028 size_t Audio::maxFramesize(Info &info)
1030 switch(info.encoding) {
1037 return info.framesize;
1041 size_t Audio::toBytes(Info &info,
unsigned long samples)
1045 unsigned long frames = samples / info.framecount;
1046 return frames * info.framesize;
1049 bool Audio::is_mono(encoding_t encoding)
1062 bool Audio::is_stereo(encoding_t encoding)
1064 return !is_mono(encoding);
1068 #define snprintf _snprintf
1071 void Audio::toTimestamp(timeout_t duration,
char *buf,
size_t len)
1073 timeout_t msec = duration % 1000;
1074 timeout_t secs = (duration / 1000) % 60;
1075 timeout_t mins = (duration / 60000) % 60;
1076 timeout_t hours = (duration /3600000);
1078 snprintf(buf, len,
"%ld:%02ld:%02ld.%03ld", hours, mins, secs, msec);
1081 timeout_t Audio::toTimeout(
const char *buf)
1083 const char *cp, *sp;
1087 cp = strchr(buf,
'.');
1089 msec = atol(cp + 1);
1093 sp = strrchr(buf,
':');
1097 while(*sp && isdigit(*sp))
1099 if(*sp && tolower(*sp) ==
'm' && tolower(sp[1] ==
's'))
1101 if(tolower(*sp) ==
'h')
1102 return atol(buf) * 3600000;
1103 if(tolower(*sp) ==
'm')
1104 return atol(buf) * 60000;
1105 return atol(buf) * 1000l;
1107 while(*sp !=
':' && sp > buf)
1111 return atol(buf) * 1000 + msec;
1113 sec = atol(sp + 1) * 1000;
1115 while(*sp !=
':' && sp > buf)
1119 return atol(buf) * 60000 + sec + msec;
1121 return atol(buf) * 3600000 + atol(++sp) * 60000 + sec + msec;
#define BAYONNE_NAMESPACE
GNU Bayonne library namespace.