📄 sound_g721.c
字号:
/*****************程序描述********************************************/
/*
硬件资源:AD转换器AD50,多通道缓冲串口McBSP
编序原理:使用基于CCITT721的A律压缩标准和U率压缩标准对输入的语音信号进行压缩和解压
功能描述:(1)开机亮灯三次,表示系统正常工作
(2)LED0亮表示输入、压缩语音信号
(3)LED0灭,LED1亮表示解压、输出语音信号
(4)LED0灭,LED1灭一次压缩、回放过程结束
作者:舒光华 刘霖
编程日期:2007年1月9号
版本号:V1.1
*********************************************************************/
//包含头文件
#include <type.h> //数据类型头文件
#include <board.h> //DSK板头文件
#include <codec.h> //AD50C头文件
#include <mcbsp54.h> //多通道缓冲串口头文件
#include "g72x.h"//CCITT721语音压缩标准的头文件
//宏定义
#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. */
#define BIAS (0x84) //在u律压缩中使用/* Bias for linear code. */
//子函数声明
void delay(s16 period);
void led(s16 cnt);
void initcodec(void);
unsigned char linear2alaw(s16 pcm_val);
int alaw2linear(unsigned char a_val);
void update(
int code_size, /* distinguish 723_40 with others */
int y, /* quantizer step size */
int wi, /* scale factor multiplier */
int fi, /* for long/short term energies */
int dq, /* quantized prediction difference */
int sr, /* reconstructed signal */
int dqsez, /* difference from 2-pole predictor */
struct g72x_state *state_ptr);/* coder state pointer */
HANDLE hHandset;
s16 data;
s16 data1;
s32 i=0;
u16 temp1;
u16 j=0;
u16 a=0;
s32 k,l=0;
u8 temp2;
//u16 buffer[21000];
static short seg_end[8]={0x1F,0x3F,0x7F,0xFF,0x1FF,0x3FF,0x7FF,0xFFF};
u8 temp_G721; // edit: for doing the CCITT G721, 32kbps ADPCM codings...
u16 buffer_G721[12000]; // edit: for doing the CCITT G721, 32kbps ADPCM codings...
struct g72x_state state; // edit: for doing the CCITT G721, 32kbps ADPCM codings...
u8 dec_G721; // edit: for doing the CCITT G721, 32kbps ADPCM codings...
void main()
{
if (brd_init(100))
return;
led(2); //闪灯两次
initcodec(); //初始化codec
while (1)
{ brd_led_toggle(BRD_LED0);
again: while (!MCBSP_RRDY(HANDSET_CODEC)) {}; //等待接收handset处的采样
data = *(volatile u16*)DRR1_ADDR(HANDSET_CODEC); //从handset处读取采样
// temp1=linear2alaw(data); //对采样进行a律压缩
// temp1=linear2ulaw(data); //对采样进行u律压缩
temp_G721=g721_encoder( (int)data, AUDIO_ENCODING_ALAW, &state ); // G721, 32kbps ADPCM codings...
i=i+1;
// buffer_G721[j1]= buffer_G721[j]&0x0000;
switch(i%4)
{
//
case 1:
buffer_G721[j]=(temp_G721<<12);
break;
case 2:
buffer_G721[j]=(buffer_G721[j]|(temp_G721<<8));
break;
case 3:
buffer_G721[j]=(buffer_G721[j]|(temp_G721<<4));
break;
default:
buffer_G721[j]=(buffer_G721[j]|(temp_G721));
j++;
break;
}
if(i>=48000)//84000)
{
i=0;
}
if(j==12000)
{
j=0;
brd_led_toggle(BRD_LED1);
//点亮二极管1 表示放音开始
play: for(k=0;k<=48000;k++)
{
switch(k%4)
{
case 1:
dec_G721=(buffer_G721[l]>>12)&0x0f;
break;
/*奇数号数据左移4位 temp_G721=abcd0000*/
case 2:
dec_G721=(buffer_G721[l]>>8)&0x0f;
break;
case 3:
dec_G721=(buffer_G721[l]>>4)&0x0f;
break;
default:
dec_G721=buffer_G721[l]&0x0f;
l++;
break;
}
// data1=alaw2linear(temp2);
// data1=ulaw2linear(temp2);
data1=g721_decoder(dec_G721, AUDIO_ENCODING_ALAW, &state);
while (!MCBSP_XRDY(HANDSET_CODEC)) {};
*(volatile u16*)DXR1_ADDR(HANDSET_CODEC) = data1;
if(l>=12000)
l=0;
if(k==48000)
{
brd_led_toggle(BRD_LED1);
a++;
delay(10);
if(a<=2)
goto play;
else
a=0;
goto again;
}
}
}
}
}
/*******延时******/
void delay(s16 period)
{
int m, n;
for(m=0; m<period; m++)
{
for(n=0; n<period>>1; n++);
}
}
/*******闪灯******/
void led(s16 cnt)
{
while ( cnt-- )
{
brd_led_toggle(BRD_LED0);
delay(1000);
brd_led_toggle(BRD_LED1);
delay(1000);
brd_led_toggle(BRD_LED2);
delay(1000);
}
}
/*****初始化codec**/
void initcodec(void)
{
/* Open Handset Codec */
hHandset = codec_open(HANDSET_CODEC); // Acquire handle to codec
/* Set codec parameters */
codec_dac_mode(hHandset, CODEC_DAC_15BIT); // DAC in 15-bit mode
codec_adc_mode(hHandset, CODEC_ADC_15BIT); // ADC in 15-bit mode
codec_ain_gain(hHandset, CODEC_AIN_6dB); // 6dB gain on analog input to ADC
codec_aout_gain(hHandset, CODEC_AOUT_MINUS_6dB);
// -6dB gain on analog output from DAC
codec_sample_rate(hHandset,SR_16000); // 16KHz sampling rate
}
/*****a律压缩******/
unsigned char linear2alaw(s16 pcm_val)
{
int mask;
int seg;
unsigned char aval;
if (pcm_val >= 0)
{
mask = 0xD5; // 标记 (7th) bit = 1
}
else
{
mask = 0x55; // 标记 bit = 0
pcm_val = -pcm_val;
}
// Convert the scaled magnitude to segment number.
seg = search(pcm_val, seg_end, 8);
//将符号位,段码和段内码组合起来 Combine the sign, segment, and quantization bits.
if (seg >= 8) // out of range, 返回最大数.
return (0x7F ^ mask);
else
{
aval = seg << SEG_SHIFT;//段码左移四位
if (seg < 2)
aval |= (pcm_val >> 1) & QUANT_MASK;
else
aval |= (pcm_val >>seg) & QUANT_MASK;
return (aval ^ mask);
}
}
/**********************************alaw的子程序*************************/
static int search(int val,short *table,int size)
{
int i;
for (i = 0; i < size; i++)
{
if (val <= *table++)
return (i);
}
return (size);
}
/**********************************a律解压*************************************/
int alaw2linear(unsigned char a_val)
{
int t;
int seg;
a_val ^= 0x55;
t = (a_val & QUANT_MASK) << 4;
seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;//SEG_SHIFT=4
if(seg==0)
{
t += 8;
t=(t>>3);
}
if((seg<4)&&(seg>0))
{
t +=0x108;
t=(t>>(4-seg));
}
if(seg>3)
{
t+=0x108;
t=(t<<=(seg-4));
}
return ((a_val & SIGN_BIT) ? t : -t);
}
#define BIAS (0x84) /* Bias for linear code. */
/******************u律压缩*****************************/
unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */
{
int mask;
int seg;
unsigned char uval;
/* Get the sign and the magnitude of the value. */
if (pcm_val < 0) {
pcm_val = BIAS - pcm_val;
mask = 0x7F;
} else {
pcm_val += BIAS;
mask = 0xFF;
}
/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_end, 8);//seg_end的地址传给形参table
/*
* Combine the sign, segment, quantization bits;
* and complement the code word.
*/
if (seg >= 8) /* out of range, return maximum value. */
return (0x7F ^ mask);
else {
uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
return (uval ^ mask);
}
}
/*****u律解压******/
int ulaw2linear(unsigned char u_val)
{
int 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));
}
#include "g72x.h"
static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
/*
* Maps G.721 code word to reconstructed scale factor normalized log
* magnitude values.
*/
static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
425, 373, 323, 273, 213, 135, 4, -2048};
/* Maps G.721 code word to log of scale factor multiplier. */
static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
1122, 355, 198, 112, 64, 41, 18, -12};
/*
* Maps G.721 code words to a set of values whose long and short
* term averages are computed and then compared to give an indication
* how stationary (steady state) the signal is.
*/
static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
/*****G721压缩******/
/*
* g721_encoder()
*
* Encodes the input vale of linear PCM, A-law or u-law data sl and returns
* the resulting code. -1 is returned for unknown input coding value.
*/
int g721_encoder(
int sl,
int in_coding,
struct g72x_state *state_ptr)
{
short sezi, se, sez; /* ACCUM */
short d; /* SUBTA */
short sr; /* ADDB */
short y; /* MIX */
short dqsez; /* ADDC */
short dq, i;
switch (in_coding) { /* linearize input sample to 14-bit PCM */
case AUDIO_ENCODING_ALAW://进行a律压缩
sl = alaw2linear(sl) >> 2;
break;
case AUDIO_ENCODING_ULAW://进行u律压缩
sl = ulaw2linear(sl) >> 2;
break;
case AUDIO_ENCODING_LINEAR://不压缩
sl >>= 2; /* 14-bit dynamic range */
break;
default:
return (-1);
}
sezi = predictor_zero(state_ptr);
sez = sezi >> 1;
se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
d = sl - se; /* estimation difference */
/* quantize the prediction difference */
y = step_size(state_ptr); /* quantizer step size */
i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
dqsez = sr + sez - se; /* pole prediction diff. */
update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
return (i);
}
/*****G721解压******/
/*
* g721_decoder()
*
* Description:
*
* Decodes a 4-bit code of G.721 encoded data of i and
* returns the resulting linear PCM, A-law or u-law value.
* return -1 for unknown out_coding value.
*/
int g721_decoder(
int i,
int out_coding,
struct g72x_state *state_ptr)
{
short sezi, sei, sez, se; /* ACCUM */
short y; /* MIX */
short sr; /* ADDB */
short dq;
short dqsez;
i &= 0x0f; /* mask to get proper bits */
sezi = predictor_zero(state_ptr);
sez = sezi >> 1;
sei = sezi + predictor_pole(state_ptr);
se = sei >> 1; /* se = estimated signal */
y = step_size(state_ptr); /* dynamic quantizer step size */
dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
dqsez = sr - se + sez; /* pole prediction diff. */
update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
switch (out_coding) {
case AUDIO_ENCODING_ALAW://a率解压
return (tandem_adjust_alaw(sr, se, y, i, 8, qtab_721));
case AUDIO_ENCODING_ULAW://u率解压
return (tandem_adjust_ulaw(sr, se, y, i, 8, qtab_721));
case AUDIO_ENCODING_LINEAR://左移两位
return (sr << 2); /* sr was 14-bit dynamic range */
default:
return (-1);
}
}
/*
* g72x.c
*
* Common routines for G.721 and G.723 conversions.
*/
static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
//段内码权重数据
/*
* quan()
*
* quantizes the input val against the table of size short integers.
* It returns i if table[i - 1] <= val < table[i].
* Using linear search for simple coding.
*/
static int quan(
int val,
short *table,
int size)
{
int i;
for (i = 0; i < size; i++)
if (val < *table++)
break;
return (i);
}
/*
* fmult()
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -