音频编码c语言程序

如何用C语言编一个发出声音的程序

这个唱 多来米法少

#include stdio.h

#include stdlib.h

#include windows.h

main()

{

Beep(523,500);

Beep(587,500);

Beep(659,500);

Beep(698,500);

Beep(784,500);

Beep(880,500);

Beep(980,500);

Beep(1060,500);

Sleep(500);

Beep(523,500);

Beep(587,500);

Beep(659,500);

Beep(698,500);

Beep(784,500);

}

如何用C语言实现PCM编码

PCM 脉冲编码调制是Pulse Code Modulation的缩写。脉冲编码调制是数字通信的编码方式之一。主要过程是将话音、图像等模拟信号每隔一定时间进行取样,使其离散化,同时将抽样值按分层单位四舍五入取整量化,同时将抽样值按一组二进制码来表示抽样脉冲的幅值。

模拟信号数字化必须经过三个过程,即抽样、量化和编码,以实现话音数字化的脉冲编码调制(PCM,Pulse Coding Modulation)技术。

抽样(Sampling)

抽样是把模拟信号以其信号带宽2倍以上的频率提取样值,变为在时间轴上离散的抽样信号的过程。例如,话音信号带宽被限制在0.3~3.4kHz内,用 8kHz的抽样频率(fs),就可获得能取代原来连续话音信号的抽样信号。对一个正弦信号进行抽样获得的抽样信号是一个脉冲幅度调制(PAM)信号,如下图对模拟正弦信号的抽样所示。对抽样信号进行检波和平滑滤波,即可还原出原来的模拟信号。

量化(quantizing)

抽样信号虽然是时间轴上离散的信号,但仍然是模拟信号,其样值在一定的取值范围内,可有无限多个值。显然,对无限个样值一一给出数字码组来对应是不可能的。为了实现以数字码表示样值,必须采用“四舍五入”的方法把样值分级“取整”,使一定取值范围内的样值由无限多个值变为有限个值。这一过程称为量化。

量化后的抽样信号与量化前的抽样信号相比较,当然有所失真,且不再是模拟信号。这种量化失真在接收端还原模拟信号时表现为噪声,并称为量化噪声。量化噪声的大小取决于把样值分级“取整”的方式,分的级数越多,即量化级差或间隔越小,量化噪声也越小。

编码(Coding)

量化后的抽样信号在一定的取值范围内仅有有限个可取的样值,且信号正、负幅度分布的对称性使正、负样值的个数相等,正、负向的量化级对称分布。若将有限个 量化样值的绝对值从小到大依次排列,并对应地依次赋予一个十进制数字代码(例如,赋予样值0的十进制数字代码为0),在码前以“+”、“-”号为前缀,来 区分样值的正、负,则量化后的抽样信号就转化为按抽样时序排列的一串十进制数字码流,即十进制数字信号。简单高效的数据系统是二进制码系统,因此,应将十 进制数字代码变换成二进制编码。根据十进制数字代码的总个数,可以确定所需二进制编码的位数,即字长。这种把量化的抽样信号变换成给定字长的二进制码流的 过程称为编码。

例程:

#include iostream

using namespace std;

int main()

{

 const int sect = 8;       //number of segement.

 const int startingVol[sect+1] = {0,16,32,64,128,256,512,1024,2048};

        // the starting value of every segement.

 const int quanIntvl[sect] = {1,1,2,4,8,16,32,64}; 

   //quantity interval of every Segments, 1 equeal to 1/2048.

 int pcmInd = 0;         //pcm code’s index.

 int pcmCode[sect] = {0,0,0,0,0,0,0,0};  // 8 bit of pcm codes.

 int sampleValue = 1270;

 int startPoint; //starting point of the segement starting piont

     //such as startingVol[startPoint] = 16 or 128  etc.

 int finePoint;   //the starting point of inner segement code.

int quanValue; // it’s used to store the final quantity value. 

 int quanError; //error caused by quantity.

//the following four variables is used in geting the segmentCode

 int low = 0;

 

 int high = sect;

 

 int mid;

 int loopInd1 = 0;     //loop index to get segment code

 int loopInd2 = 0;   //loop index to get inner segment codes

//get the first_digit code of polarity

 (sampleValue  0) ? (pcmCode[pcmInd++] = 1) : (pcmCode[pcmInd] = 0);

 

 sampleValue = abs(sampleValue);  //make sure the voltage is positive

    //get the segment code  using modified halve search

 while(loopInd1  3)  //only need 3 loops the segmentCode can be got 

 {

  mid = (low + high)/2;  

   //after 3 loops, sampeValue falls in startingVol[mid] – startingVol[mid] or

    //in startingVol[mid-1] – startingVol[mid]

  if(sampleValue  startingVol[mid])

  {

   pcmCode[pcmInd++] = 0;

   high = mid; 

   startPoint = mid – 1 ;

  }

  else

  {

   pcmCode[pcmInd++] = 1;

   low = mid;

   startPoint = mid;

  }

  loopInd1++;

 }//end while

//get the last four bits codes of pcm 

 low = 0;

 high = 16;  //every segment is split into 16 small segments of the same size

 

 while(loopInd2  4)

 {

  mid = (low + high)/2;

  

  //make the compare progress clear using the following two setences.

  quanValue = startingVol[startPoint] + mid * quanIntvl[startPoint];

  coutstartingVol[startPoint]” + “quanIntvl[startPoint]” * “mid” = “

   quanValue ” ? “sampleValueendl;

     //make the compare progress clear using the above two setences.

  

  if(sampleValue  startingVol[startPoint] + mid * quanIntvl[startPoint])

  {

   pcmCode[pcmInd++] = 0;

   high = mid;

   finePoint = mid -1;

  }

  else

  {

   pcmCode[pcmInd++] = 1;

   low = mid;

   finePoint = mid;

  }

  loopInd2++;

 }//end while

 quanValue = startingVol[startPoint] + finePoint * quanIntvl[startPoint];

 

 quanValue += quanIntvl[startPoint] / 2; //final quantity value.

 quanError = abs( sampleValue – quanValue); // error of quantity.

 cout”Final quantity value is: “quanValueendl;

 cout”Error of quantity  is: “quanErrorendl;

 cout”PCM codes are: “;

for(int i = 0; i  8; i++)

  

 {

  coutpcmCode[i]” “;

 }//end for

 coutendl;

return 0;

}

G711编解码的c语言程序

1、G.711是一种由国际电信联盟(ITU-T)订定音频编码方式,又称为ITU-T G.711.目前G.711有两个编码方式,A-law以及Mu-law.G711A是其中一种编码方式.

2、例程:

/*

 * g711.c

 *

 * u-law, A-law and linear PCM conversions.

 */

#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_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,

       0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};

/* 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,

 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, 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,

 73, 74, 75, 76, 77, 78, 79, 79,

 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 int

search(

 int  val,

 short  *table,

 int  size)

{

 int  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(

 int  pcm_val) /* 2’s complement (16-bit range) */

{

 int  mask;

 int  seg;

 unsigned char aval;

 if (pcm_val = 0) {

  mask = 0xD5;  /* sign (7th) bit = 1 */

 } else {

  mask = 0x55;  /* sign bit = 0 */

  pcm_val = -pcm_val – 8;

 }

 /* 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 maximum value. */

  return (0x7F ^ mask);

 else {

  aval = seg  SEG_SHIFT;

  if (seg  2)

   aval |= (pcm_val  4)  QUANT_MASK;

  else

   aval |= (pcm_val  (seg + 3))  QUANT_MASK;

  return (aval ^ mask);

 }

}

/*

 * alaw2linear() – Convert an A-law value to 16-bit linear PCM

 *

 */

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;

 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. */

/*

 * 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(

 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);

 /*

  * 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);

 }

}

/*

 * 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.

 */

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));

}

/* A-law to u-law conversion */

unsigned char

alaw2ulaw(

 unsigned char aval)

{

 aval = 0xff;

 return ((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 ((uval  0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] – 1)) :

     (0x55 ^ (_u2a[0x7F ^ uval] – 1)));

}

音频编码c语言程序

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月26日 05:07:15
下一篇 2024年3月26日 05:14:24

相关推荐

  • c语言改写模式,c语言实现修改功能

    c语言程序修改? 1、这个程序有4个错误,我都加粗了,第一个是m没有赋初值,第二个是while表达式中的ch=getchar()需要括号括起来,第三个是m=m*10+ch-0中的0也需要用单引号括起来,第四个是第2个while中为m!=0。 2、define容易造成误会,因为不符合一般的编程习惯,false 0, true 1;scanf放在你的那个地方是达…

    2024年5月23日
    4100
  • c语言控制代码的换码序列,c语言交换代码

    求C语言编程大神解答一下下面这个编程代码? k==5,用5去除125余0,所以r=125%5中r为0。由于!0为1,所以执行while循环体:先打印出5(k的值),再n=n/k==125/5=25;由于251则再打印出*号。这一循环结果输出是5*。 下面是我的代码,三个函数分别对应三个问题。 在实现基本要求的前提下,拓展了可以从键盘输入的功能,以下为各题代码…

    2024年5月23日
    5800
  • c语言扫描io脚状态,c语言端口扫描

    求51单片机的上升沿和下降沿C语言检测程序列子,端口就是普通IO口。 上升沿触发是当信号有上升沿时的开关动作,当电位由低变高而触发输出变化的就叫上升沿触发。也就是当测到的信号电位是从低到高也就是上升时就触发,叫做上升沿触发。 单片机怎么计算1s内下降沿的个数的C语言程序或者计算两个下降沿的时间(检测脉冲频率)计算1s内下降沿的个数方法是,一个定时器设置定时1…

    2024年5月23日
    4500
  • c语言mallloc使用的简单介绍

    C语言中使用malloc必须加#includemallo.h? 1、在C语言中使用malloc函数进行动态内存分配。malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void malloc(unsigned int num_bytes);功能:分配长度为num_bytes字节的内存块。 2、你可以看一下C语言那本…

    2024年5月23日
    4500
  • c语言三位小数,C语言三位小数

    怎样用C++语言输出精确到小数点后三位的数? 1、用C++语言输出精确到小数点后三位的数,可以参考下面给出的代码:coutsetiosflags(ios:fixed)setprecision(3)。其中 setiosflags中set是设置的意思。ios是iostream的缩写,即输入输出流。flags是标志的意思。 2、要精确到小数点后若干位,则数据类型为…

    2024年5月23日
    7500
  • c语言21点游戏,二十一点游戏代码c语言

    如何使用C语言编写简单小游戏? 1、数学知识:长方形的面积S=a*b 长方形周长L=2*(a+b)其中a b分别为长方形的宽和高。算法分析:长方形面积及周长均依赖于宽和高,所以先要输入宽高值,然后根据公式计算,输出结果即可。 2、/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++0、turbo…

    2024年5月23日
    6500
  • c语言当中的null,C语言当中的符号

    C/C++中,NULL和null的区别是什么? nul 和 null要看编译器,不同的编译器有所区别。 所以C或者C++中都使用一个特殊定义NULL表示无效值,其本质就是未定义具体数据类型的0值。 null是是什么都没有的意思。在java中表示空对象。 本意是“空的;元素只有零的”意思。计算机中通常表示空值,无结果,或是空集合。\x0d\x0a在ASCII码…

    2024年5月23日
    4700
  • 包含c语言对txt文件命名的词条

    如何在C语言编程里面修改源文件名字 如果你是在WINDOWS的话,简单了,随便用个编辑器,比如记事本,然后写c源程序,保存到你想要保存的位置。如果你在DOS下,可以用edit,写好以后,按alt键,选择文件菜单,然后保存。 用open打开文件,注意操作模式使用“修改”或者“添加” 用write或者fprintf向文件中写入你的内容。 用close关闭文件。 …

    2024年5月23日
    5000
  • 学c语言编程,学c语言编程用什么软件

    编程开发必须要学C语言吗? 1、要学习。编程开发的学习内容主要包括c语言、python和c+语言。C语言作为一种简单灵活的高级编程语言,它是一个面向过程的语言,一般是作为计算机专业的基础入门语言课程。 2、C语言。对于刚接触编程的人来说,先学习C语言是非常重要的。C语言可以说是是计算机编程语言的鼻祖,其他的编程语言几乎全是由C语言变化衍生出来的。 3、不需要…

    2024年5月23日
    3500
  • c语言用string定义字符串,c语言中用string类型来处理字符串类型

    C++怎样定义定义字符串 1、第一是字符数组来表示字符串。用下面的语句声明:char a[10];C语言中字符数组与字符串的唯一区别是字符串末尾有一个结束符\0,而字符数组不需要。 2、在C中定义字符串有下列几种形式:字符串常量,char数组,char指针 字符串常量 即:位于一对双括号中的任何字符。双引号里的字符加上编译器自动提供的结束标志\0字符,作为 …

    2024年5月23日
    4500

发表回复

登录后才能评论



关注微信