?? pcm.c
字號:
/* */
/* Copyright 2004 by SEED Electronic Technology LTD. */
/* All rights reserved. SEED Electronic Technology LTD. */
/* Restricted rights to use, duplicate or disclose this code are */
/* granted through contract. */
/* */
/* Authors: david */
/* Data: 2006/10/28 */
/********************************************************************************/
#include "type.h"
#include "codec.h"
#include "sysreg.h"
#include "mcbsp54.h"
#include "memory.h"
/********************************************************************************/
/*數(shù)據(jù)緩沖區(qū)的首址為0x8000,長度為0x8000*/
#define AUDIODATA 0x8000
/********************************************************************************/
#define AUDIOTRY 0xAA0A//音頻試聽
#define AUDIOPCM 0xAA09//音頻PCM
//實驗操控:
//選擇TESTCOMMAND,1為試聽;2為和聲。
//選擇CODECAGAIN,48-127之間。是調整增益的參數(shù)。
//最好在110-127之間,否則聲音太小,難以聽到。
#define TESTCOMMAND 2 //操作命令選擇
unsigned int Command =0;
/////////////////////////////////////////////////////////////////////
HANDLE codec_command =0;
HANDLE codec_data=0;
/////////////////////////////////////////////////////////////////////
int audioleft, audioright;
unsigned int i= 0;
/*********************************************************************/
void pcm();
/*********************************************************************/
main()
{
/*設置系統(tǒng)時鐘*/
sys_clk(CLK160);
#if TESTCOMMAND==1
Command =AUDIOTRY;//試聽 0xAA0A
#endif
#if TESTCOMMAND==2
Command =AUDIOPCM;//PCM 0xAA09
#endif
/*打開codec數(shù)據(jù)接口*/
codec_data = codec_open(CODEC_DATA);
/*打開codec命令接口*/
codec_command = codec_open(CODEC_COMMAND);
/*選擇麥克風作為輸入 0x14,選擇Line in作為輸入 0x12*/
codec_analog_mode(codec_command,0x12);
/*設置波特率,輸入與輸出均為8K*/
codec_sample_rate(codec_command,0xd);
/*配置系統(tǒng)存儲器*/
memory_set(0x80);
//設置增益
codec_headhponeout_gain(codec_command,127);
for(;;)
{
switch(Command)
{
/*音頻試聽*/
case AUDIOTRY:
/* Wait for sample from handset */
while (!MCBSP_RRDY(CODEC_DATA)) {};
/* Read sample from and write back to handset codec */
audioleft = *(volatile u16*)DRR1_ADDR(CODEC_DATA);
while (!MCBSP_RRDY(CODEC_DATA)) {};
/* Read sample from and write back to handset codec */
audioright = *(volatile u16*)DRR1_ADDR(CODEC_DATA);
while (!MCBSP_XRDY(CODEC_DATA)) {};
*(volatile u16*)DXR1_ADDR(CODEC_DATA) = audioleft;
while (!MCBSP_XRDY(CODEC_DATA)) {};
*(volatile u16*)DXR1_ADDR(CODEC_DATA) = audioright;
break;
/*音頻PCM*/
case AUDIOPCM:
pcm();
break;
default:
break;
}
}
}
/***********************************************************************************/
/*函數(shù)名:pcm() */
/***********************************************************************************/
void pcm()
{
int *pAudio;
int sign, segment;
int temp, quant;
char seg;
unsigned int absol, tem;
int *pWork,nAudioCount;
unsigned int uWork;
unsigned char cWork;
int output;
pAudio=pWork=(int *)AUDIODATA;
nAudioCount=0;
for ( i=0;i<1024;i++,pWork++ ) (*pWork)=0;
pWork=pAudio;
do{
/* Wait for sample from handset */
while (!MCBSP_RRDY(CODEC_DATA)) {};
/* Read sample from and write back to handset codec */
audioleft = *(volatile u16*)DRR1_ADDR(CODEC_DATA);
while (!MCBSP_RRDY(CODEC_DATA)) {};
/* Read sample from and write back to handset codec */
audioright= *(volatile u16*)DRR1_ADDR(CODEC_DATA);
/*取右道的數(shù)據(jù)做為運算的數(shù)據(jù)*/
tem=absol=abs(audioleft);
sign=(audioleft >= 0)?1:0;
for(i=0;i<16;i++)
{
output=tem&0x8000;
if(output)
break;
tem<<=1;
}
seg=11-i;
if(seg<=0)
{
seg=0;
quant=(absol>>1)&0x0F;
}
else
quant=(absol>>seg)&0x0F;
seg<<=4;
output=seg+quant;
if(absol>4095)
output=0x7F;
if(sign)
output^=0xD5;
else
output^=0x55;
uWork=(unsigned char)output;
/*inttoa函數(shù)完結*/
uWork<<=8;
/*以下為inttoa函數(shù):uWork|=inttoa(right);*/
tem=absol=abs(audioright);
sign=(audioright >= 0)?1:0;
for(i=0;i<16;i++)
{
output=tem&0x8000;
if(output)
break;
tem<<=1;
}
seg=11-i;
if(seg<=0)
{
seg=0;
quant=(absol>>1)&0x0F;
}
else
quant=(absol>>seg)&0x0F;
seg<<=4;
output=seg+quant;
if(absol>4095)
output=0x7F;
if(sign)
output^=0xD5;
else
output^=0x55;
uWork|=(unsigned char)output;
/*inttoa函數(shù)完結*/
(*pWork)=uWork;
cWork=uWork>>8;
/*以下為atoint函數(shù):left=atoint(cWork);*/
temp=cWork^0xD5;
sign=(temp&0x80)>>7;
segment=(temp&0x70)>>4;
quant=temp&0x0F;
quant<<=1;
if(!segment)
quant+=1;
else
{
quant+=33;
quant<<=segment-1;
}
if ( sign )
audioleft=-quant;
else
audioleft=quant;
/*atoint函數(shù)完結*/
cWork=uWork&0x0ff;
/*以下為atoint函數(shù):right=atoint(cWork); */
temp=cWork^0xD5;
sign=(temp&0x80)>>7;
segment=(temp&0x70)>>4;
quant=temp&0x0F;
quant<<=1;
if(!segment)
quant+=1;
else
{
quant+=33;
quant<<=segment-1;
}
if ( sign )
audioright=-quant;
else
audioright=quant;
/*atoint函數(shù)完結*/
/* Wait for sample from handset */
while (!MCBSP_XRDY(CODEC_DATA)) {};
*(volatile u16*)DXR1_ADDR(CODEC_DATA) = audioright;
while (!MCBSP_XRDY(CODEC_DATA)) {};
*(volatile u16*)DXR1_ADDR(CODEC_DATA) = audioleft;
nAudioCount++; pWork++;
if ( nAudioCount>=1024 )
{
nAudioCount=0;
pWork=pAudio;
}
}
while(Command==0xaa09);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -