alting
驱动牛犊
驱动牛犊
  • 注册日期2004-04-03
  • 最后登录2005-03-10
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:2689回复:11

大虾救命啊!!! usb audio device firmware设计不可或缺 G711.16位PCM 转 8位a/u-law 源程序

楼主#
更多 发布于:2004-04-06 15:08
各位大虾 好啊

小弟有一事想请教  小弟正在开发 USB phone 用EZ-USB AN2131自己写firmware 现在碰到非常棘手的问题 想向各位大虾请教

SOF中断是1ms一次 声音的采样频率是125us一次
如果我用一个timer来控制发声和录音采样 而在SOF中断服务程序将他们传送到主机 这样做对吗 因为我在示波器上看到采样频率达不到8KHz 是不是因为在SOF中断中花太多时间 (在中断程序处理传输时我把timer 停止了)

我端点是设成 ISO 16Byte (我参考过其他设计大都这样)主机传到USB设备是16位的uniform PCM, codec 是用8位 一次ISO传输(1ms)刚好可以产生8个采样 但16位PCM 转 8位a-law格式很费时间吧 应该放在哪好呢 下面有一段源码 是不是那样做的

#define SIGN_BIT (0x80)  /* Sign bit for a A-law byte. */
#define QUANT_MASK (0xf)   /* Quantization field mask. */
#define NSEGS (8)         /* Number of A-law segments. */
#define SEG_SHIFT (4)       /* Left shift for segment number. */
#define SEG_MASK (0x70) /* Segment field mask. */

static short seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
  0x1FF, 0x3FF, 0x7FF, 0xFFF};

/* copy from CCITT G.711 specifications */
unsigned char _u2a[128] = { /* u- to A-law conversions */
1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 29, 31, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44,
46, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62,
64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79,
/* corrected:
81, 82, 83, 84, 85, 86, 87, 88,
   should be: */
80, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128};

unsigned char _a2u[128] = { /* A- to u-law conversions */
1, 3, 5, 7, 9, 11, 13, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 32, 33, 33, 34, 34, 35, 35,
36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 48, 49, 49,
50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 64,
65, 66, 67, 68, 69, 70, 71, 72,
/* corrected:
73, 74, 75, 76, 77, 78, 79, 79,
   should be: */
73, 74, 75, 76, 77, 78, 79, 80,

80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127};

static short
search(
short val,
short *table,
short size)
{
short i;

for (i = 0; i < size; i++) {
if (val <= *table++)
return (i);
}
return (size);
}

/*
 * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
 *
 * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
 *
 * Linear Input Code Compressed Code
 * ------------------------ ---------------
 * 0000000wxyza 000wxyz
 * 0000001wxyza 001wxyz
 * 000001wxyzab 010wxyz
 * 00001wxyzabc 011wxyz
 * 0001wxyzabcd 100wxyz
 * 001wxyzabcde 101wxyz
 * 01wxyzabcdef 110wxyz
 * 1wxyzabcdefg 111wxyz
 *
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 * John Wiley & Sons, pps 98-111 and 472-476.
 */
unsigned char
linear2alaw(
short pcm_val) /* 2's complement (16-bit range) */
{
short mask;
short seg;
unsigned char aval;

pcm_val = pcm_val >> 3;

if (pcm_val >= 0) {
mask = 0xD5; /* sign (7th) bit = 1 */
} else {
mask = 0x55; /* sign bit = 0 */
pcm_val = -pcm_val - 1;
}

/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_aend, 8);

/* Combine the sign, segment, and quantization bits. */

if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
aval = (unsigned char) seg << SEG_SHIFT;
if (seg < 2)
aval |= (pcm_val >> 1) & QUANT_MASK;
else
aval |= (pcm_val >> seg) & QUANT_MASK;
return (aval ^ mask);
}
}

多谢大虾赐教 在下感激不尽!!!!!!
咳现在已经过了期限了老板发怒就要饭碗不保 救命啊!!!


源码:

#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
#define QUANT_MASK (0xf) /* Quantization field mask. */
#define NSEGS (8) /* Number of A-law segments. */
#define SEG_SHIFT (4) /* Left shift for segment number. */
#define SEG_MASK (0x70) /* Segment field mask. */

static short seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
   0x1FF, 0x3FF, 0x7FF, 0xFFF};
static short seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
   0x3FF, 0x7FF, 0xFFF, 0x1FFF};

/* copy from CCITT G.711 specifications */
unsigned char _u2a[128] = { /* u- to A-law conversions */
1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 29, 31, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44,
46, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62,
64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79,
/* corrected:
81, 82, 83, 84, 85, 86, 87, 88,
   should be: */
80, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128};

unsigned char _a2u[128] = { /* A- to u-law conversions */
1, 3, 5, 7, 9, 11, 13, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 32, 33, 33, 34, 34, 35, 35,
36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 48, 49, 49,
50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 64,
65, 66, 67, 68, 69, 70, 71, 72,
/* corrected:
73, 74, 75, 76, 77, 78, 79, 79,
   should be: */
73, 74, 75, 76, 77, 78, 79, 80,

80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127};

static short
search(
short val,
short *table,
short size)
{
short i;

for (i = 0; i < size; i++) {
if (val <= *table++)
return (i);
}
return (size);
}

/*
 * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
 *
 * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
 *
 * Linear Input Code Compressed Code
 * ------------------------ ---------------
 * 0000000wxyza 000wxyz
 * 0000001wxyza 001wxyz
 * 000001wxyzab 010wxyz
 * 00001wxyzabc 011wxyz
 * 0001wxyzabcd 100wxyz
 * 001wxyzabcde 101wxyz
 * 01wxyzabcdef 110wxyz
 * 1wxyzabcdefg 111wxyz
 *
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 * John Wiley & Sons, pps 98-111 and 472-476.
 */
unsigned char
linear2alaw(
short pcm_val) /* 2's complement (16-bit range) */
{
short mask;
short seg;
unsigned char aval;

pcm_val = pcm_val >> 3;

if (pcm_val >= 0) {
mask = 0xD5; /* sign (7th) bit = 1 */
} else {
mask = 0x55; /* sign bit = 0 */
pcm_val = -pcm_val - 1;
}

/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_aend, 8);

/* Combine the sign, segment, and quantization bits. */

if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
aval = (unsigned char) seg << SEG_SHIFT;
if (seg < 2)
aval |= (pcm_val >> 1) & QUANT_MASK;
else
aval |= (pcm_val >> seg) & QUANT_MASK;
return (aval ^ mask);
}
}

/*
 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
 *
 */
short
alaw2linear(
unsigned char a_val)
{
short t;
short seg;

a_val ^= 0x55;

t = (a_val & QUANT_MASK) << 4;
seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
switch (seg) {
case 0:
t += 8;
break;
case 1:
t += 0x108;
break;
default:
t += 0x108;
t <<= seg - 1;
}
return ((a_val & SIGN_BIT) ? t : -t);
}

#define BIAS (0x84) /* Bias for linear code. */
#define CLIP            8159

/*
 * linear2ulaw() - Convert a linear PCM value to u-law
 *
 * In order to simplify the encoding process, the original linear magnitude
 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
 * (33 - 8191). The result can be seen in the following encoding table:
 *
 * Biased Linear Input Code Compressed Code
 * ------------------------ ---------------
 * 00000001wxyza 000wxyz
 * 0000001wxyzab 001wxyz
 * 000001wxyzabc 010wxyz
 * 00001wxyzabcd 011wxyz
 * 0001wxyzabcde 100wxyz
 * 001wxyzabcdef 101wxyz
 * 01wxyzabcdefg 110wxyz
 * 1wxyzabcdefgh 111wxyz
 *
 * Each biased linear code has a leading 1 which identifies the segment
 * number. The value of the segment number is equal to 7 minus the number
 * of leading 0's. The quantization interval is directly available as the
 * four bits wxyz.  * The trailing bits (a - h) are ignored.
 *
 * Ordinarily the complement of the resulting code word is used for
 * transmission, and so the code word is complemented before it is returned.
 *
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 * John Wiley & Sons, pps 98-111 and 472-476.
 */
unsigned char
linear2ulaw(
short pcm_val) /* 2's complement (16-bit range) */
{
short mask;
short seg;
unsigned char uval;

/* Get the sign and the magnitude of the value. */
pcm_val = pcm_val >> 2;
if (pcm_val < 0) {
pcm_val = -pcm_val;
mask = 0x7F;
} else {
mask = 0xFF;
}
        if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
pcm_val += (BIAS >> 2);

/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_uend, 8);

/*
* Combine the sign, segment, quantization bits;
* and complement the code word.
*/
if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
return (uval ^ mask);
}

}

/*
 * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
 *
 * First, a biased linear code is derived from the code word. An unbiased
 * output can then be obtained by subtracting 33 from the biased code.
 *
 * Note that this function expects to be passed the complement of the
 * original code word. This is in keeping with ISDN conventions.
 */
short
ulaw2linear(
unsigned char u_val)
{
short t;

/* Complement to obtain normal u-law value. */
u_val = ~u_val;

/*
* Extract and bias the quantization bits. Then
* shift up by the segment number and subtract out the bias.
*/
t = ((u_val & QUANT_MASK) << 3) + BIAS;
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;

return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
}

/* A-law to u-law conversion */
unsigned char
alaw2ulaw(
unsigned char aval)
{
aval &= 0xff;
return (unsigned char) ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) :
   (0x7F ^ _a2u[aval ^ 0x55]));
}

/* u-law to A-law conversion */
unsigned char
ulaw2alaw(
unsigned char uval)
{
uval &= 0xff;
return (unsigned char) ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) :
   (unsigned char) (0x55 ^ (_u2a[0x7F ^ uval] - 1)));
}
  
alting
驱动牛犊
驱动牛犊
  • 注册日期2004-04-03
  • 最后登录2005-03-10
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-04-07 17:33
给为大虾

有没有人知道怎样将 16位线性PCM 转成 线性8位PCM 格式啊

拜托
alting
驱动牛犊
驱动牛犊
  • 注册日期2004-04-03
  • 最后登录2005-03-10
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-04-09 16:34
怎么美人给我建议啊  郁闷啊

各位大虾给电子把
pz771230
驱动牛犊
驱动牛犊
  • 注册日期2002-12-25
  • 最后登录2008-12-29
  • 粉丝0
  • 关注0
  • 积分1分
  • 威望1点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-04-13 03:00
嘿,现在在线?我也开发usb audio 设备。我想我们可以交流一下。
我的msn:pz771230@msn.com
kermit
驱动小牛
驱动小牛
  • 注册日期2002-11-29
  • 最后登录2010-03-29
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望2点
  • 贡献值0点
  • 好评度2点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2004-04-13 11:11
你的说法是对的,当然格式转换比较费时间,无论如何你现在的思路是对的。
我已经成功开发了这个项目,我网站上有一些介绍,和一些数据转换方法,希望对你有帮助。
www.eyeteck.com
谢谢!
alting
驱动牛犊
驱动牛犊
  • 注册日期2004-04-03
  • 最后登录2005-03-10
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2004-04-13 12:10
多谢 keimet 大虾

我的firmware可以出声音了但很多噪音 因为只能出7个采样每1ms,在SOF中断程序的开头我把timer启动(R0=1)中断间隔是125us

但我不知道为什么timer中断 总在SOF中断处理完才开始。 我用示波器看过SOF共花了 194us 来执行, 第一个timer 中断是在 194us 之后才发生的 但我把 timer 设成 125us 中断一次的啊

照理说timer的优先级高过 USBINT, timer 可以中断 SOF 的阿
头疼 郁闷啊 !!
kermit
驱动小牛
驱动小牛
  • 注册日期2002-11-29
  • 最后登录2010-03-29
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望2点
  • 贡献值0点
  • 好评度2点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2004-04-14 14:14
我没有很仔细去看MCU的中断结构。好像MCU的中断结构是进了中断就不能再进第2级中断了。--只是我的印象,我没去考证。
所以,如果是这样的话,你一个中断已经进去了,另一个再来的话,它也无法相应啊。

为什么你1ms只能做7次啊,做8次是没有问题的啊。

杂音是因为你的波形不稳定造成的。
alting
驱动牛犊
驱动牛犊
  • 注册日期2004-04-03
  • 最后登录2005-03-10
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2004-04-15 09:31
kermit 大虾

中断的问题终于解决了原来是要设定优先级才可以的
你所说的波形不稳定是不是指输入的波形啊

我在SOF中断里处理 16->8, 8->16 的转换很费时间啊(因为我同时做声音的输入和输出) 是不是用查表的方法快点?

缓冲区是不是也用乒乓形式啊 因为要在每8个timer中断后输入声音才采样完毕 要等到下一个SOF才能传输出去 而下一个SOF同时也要用8个时间中断继续采样声音 所以要用乒乓形式的缓冲区  不知道我这样想对不对呢

多谢指教了
alting
驱动牛犊
驱动牛犊
  • 注册日期2004-04-03
  • 最后登录2005-03-10
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
8楼#
发布于:2004-04-15 11:56
现在可以出声音了8个采样每1ms 但很大的稳定的噪音 音乐的声音很小噪声很大  不知为什么  为什么????
mensan2004
驱动牛犊
驱动牛犊
  • 注册日期2004-04-05
  • 最后登录2004-05-27
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
9楼#
发布于:2004-04-15 16:45
实在不行,就先把老板炒掉,不要着急,凡事都有解决的办法.呵呵
alting
驱动牛犊
驱动牛犊
  • 注册日期2004-04-03
  • 最后登录2005-03-10
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
10楼#
发布于:2004-04-15 19:00
我发觉可能是 an2131传送数据是一字节一字节的传 就是把16位的PCM 拆开成2字节 我在firmware里需要把他们再合并 可能就是这里出错了 由谁能告诉我要怎么 合并才正确

while (byteCount-=2) {
  seg = OUT8DATA;
  pcm_val = OUT8DATA;

   pcm_val >>=3;
   if (pcm_val >= 0) {
        mask = 0xD5;
   } else {
        mask = 0x55;
        pcm_val = -pcm_val - 1;
   }......

这样做(其实把第一字节给丢了)可以出声音 但很多噪音

但试过其他组合方法 却连声音也没有 惨啊
adaye2003
驱动牛犊
驱动牛犊
  • 注册日期2002-12-31
  • 最后登录2016-01-22
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
11楼#
发布于:2004-10-08 21:55
这位老兄,你最后调通了么?
游客

返回顶部