|
编译环境为keilourdev_457307.rar(文件大小:33K) (原文件名:Wireless_Audio.rar)
以下为该压缩包中关于DPCM 编解码的例程。
/**************************************************************************
DPCM 编解码例程,文件名为DpcmCodec.c
***************************************************************************
* Description: *
* DPCM 编解码 *
***************************************************************************
* Input arguments: *
* byte sample: Unsigned speech sample (0-255). *
* bit returncode: Function returns an encoded difference (4 bit) if set,*
* or a predicted sample (8 bit) if not set. *
* Return value: *
* byte: An encoded 4-bit DPCM code or a predicted sample. *
**************************************************************************/
byte DpcmEncoder (byte sample, bit returncode)
{
// Compute the differense between the actual- and predicted sample
diff = sample - encoderpredsample;
// Quantize the difference
if (diff >= 0)
dpcmcodeout = 8;// Set the first bit
else // (diff < 0)
{
dpcmcodeout = 0;
diff = -diff;// Use the absolute value of the difference
}// End else
if (diff >= 8)
{
dpcmcodeout |= 4;// Set the second bit
if (diff >= 32)
{
dpcmcodeout |= 2;// Set the third bit
if (diff >= 64)
dpcmcodeout |= 1;// Set the fourth bit (LSB)
}// End if
else// (diff < 32)
{
if (diff >= 16)
dpcmcodeout |= 1;// Set the fourth bit (LSB)
}// End else
}// End if
else// (diff < 8)
{
if (diff >= 2)
{
dpcmcodeout |= 2;// Set the third bit
if (diff >= 4)
dpcmcodeout |= 1;// Set the fourth bit(LSB)
}// End if
else // (diff < 2)
{
if (diff >= 1)
dpcmcodeout |= 1;// Set the fourth bit(LSB)
}// End else
}// End else
/* Compute new predicted sample by adding the
old predicted sample to the quantzed difference*/
encoderpredsample += StepSize[dpcmcodeout];
// Return the new DPCM code or the new predicted sample
if (returncode)
return (dpcmcodeout);
else
return (encoderpredsample);
}// End function
/**************************************************************************
* DpcmDecoder() - DPCM decoder routine *
***************************************************************************
* Description: *
* Performs DPCM speech decompression (1:2). *
***************************************************************************
* Input arguments: *
* byte dpcmcodein: Contains one or two encoded differences. *
* bit msbits: The 4 msb of dpcmcodein are decoded if set, the 4 lsb are *
* decoded if not set. *
* Return value: *
* byte: A decoded predicted sample (8 bit). *
**************************************************************************/
byte DpcmDecoder (byte dpcmcodein, bit msbits)
{
// Decide if the four MSB or LSB should be decoded
if (msbits)
// Shift dpcmcodein four bits to the right
dpcmcodein >>= 4;
/* Compute new predicted sample by adding the
old predicted sample to the quantzed difference*/
decoderpredsample += StepSize[dpcmcodein & 0x0f];
// Return the decoded sample
return (decoderpredsample);
} // End function |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|