请尊重作者劳动成果,如需转载本博客文章请注明出处!谢谢合作!

  inputData是PCM的实时数据,可以通过转码,获取到最后导出的G711u数据(sendData)

NSUInteger datalength = [inputData length];

Byte *byteData = (Byte *)[inputData bytes];

short *pPcm = (short *)byteData;

int outlen = 0;

int len =(int)datalength / 2;

Byte * G711Buff = (Byte *)malloc(len);

memset(G711Buff,0,len);

int i;

for (i=0; i<len; i++) {

//此处修改转换格式(a-law或u-law)

G711Buff[i] = linear2alaw(pPcm[i]);

}

outlen = i;

Byte *sendbuff = (Byte *)G711Buff;

NSData * sendData = [[NSData alloc]initWithBytes:sendbuff length:len];

[self.delegate backVoiceDataWithG711u:sendData];

---------------------------------------------

转码文件

G711.h

#ifndef __G_711_H_

#define __G_711_H_

#include <stdint.h>

enum _e_g711_tp

{

TP_ALAW, //G711A

TP_ULAW //G711U

};

unsigned char linear2alaw(int pcm_val); /* 2's complement (16-bit range) */

int alaw2linear(unsigned char a_val);

unsigned char linear2ulaw(int pcm_val); /* 2's complement (16-bit range) */

int ulaw2linear(unsigned char u_val);

unsigned char alaw2ulaw(unsigned char aval);

unsigned char ulaw2alaw(unsigned char uval);

int g711_decode(void *pout_buf, int *pout_len, const void *pin_buf, const int in_len , int type);

#endif

------------------

G711.cpp文件

/*

* g711.c

*

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

*/

//#include "stdafx.h"

#include <stdint.h>

#include <stdio.h>

#include "g711.h"

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

}

int g711_decode(void *pout_buf, int *pout_len, const void *pin_buf, const int in_len , int type)

{

int16_t *dst = (int16_t *) pout_buf;

uint8_t *src = (uint8_t *) pin_buf;

uint32_t i = 0;

int Ret = 0;

if ((NULL == pout_buf) || \

(NULL == pout_len) || \

(NULL == pin_buf) || \

(0 == in_len))

{

return -1;

}

if (*pout_len < 2 * in_len)

{

return -2;

}

//---{{{

if (TP_ALAW == type)

{

for (i = 0; i < in_len; i++)

{

//*(dst++) = alawtos16[*(src++)];

*(dst++) = (int16_t)alaw2linear(*(src++));

}

}else

{

for (i = 0; i < in_len; i++)

{

//*(dst++) = alawtos16[*(src++)];

*(dst++) = (int16_t)ulaw2linear(*(src++));

}

}

//---}}}

*pout_len = 2 * in_len;

Ret = 2 * in_len;

return Ret;

}

iOS音频格式PCM转G711u(或G711a-law)的更多相关文章

  1. iOS音频学习笔记一:常见音频封装格式及编码格式

    (1) pcm格式    pcm是经过话筒录音后直接得到的未经压缩的数据流    数据大小=采样频率*采样位数*声道*秒数/8     采样频率一般是22k或者44k,位数一般是8位或者16位,声道一 ...

  2. 音频格式RAW和PCM区别和联系

    定义:  RAW:在一些外国品牌的播放机中名为 BitSream,我们通常称为“源码”.意义是把光盘上的音频格式不加处理地.“原汁原味”地从同轴和光纤输出.这就要求用户的功放具备这种音频格式的解码功能 ...

  3. Swift iOS实现把PCM语音转成MP3格式

    最近折腾了swift的语音录制识别和转码,这块还是比较坑的,由于语音识别的准确度实测大概也就80%左右,所以还是需要上传录音文件啊.首先是用讯飞语音SDK实现语音录制和识别(语音听写),第一个坑是讯飞 ...

  4. 一篇对iOS音频比较完善的文章

    转自:http://www.cnblogs.com/iOS-mt/p/4268532.html 感谢作者:梦想通 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改我也 ...

  5. 《转》iOS音频视频初级开发

    代码改变世界 Posts - 73, Articles - 0, Comments - 1539 Cnblogs Dashboard Logout HOME CONTACT GALLERY RSS   ...

  6. iOS音频

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  7. IOS 音频开发文件大小计算

    音频基础知识 音频文件计算大小 音频转码 标签(空格分隔): 调查 IOS音频 https://developer.apple.com/library/ios/documentation/MusicA ...

  8. iOS:音频

    ios中有很多支持音频的控件,如:播放本地音乐(file URL)的AVAudioPlayer和AudioToolbox.Framework.可以播放音乐库音乐的MPMusicPlayerContro ...

  9. 在不同平台上CocosDenshion所支持的音频格式

    在大多数平台上,cocos2d-x调用不同的SDK API来播放背景音乐和音效.CocosDenshion在同一时间只能播放一首背景音乐,但是能同时播放多个音效. 背景音乐 Platform supp ...

随机推荐

  1. 配置VNC SERVER 远程访问

    1.安装软件包 # yum install tigervnc-server -y 2. 配置VNC用户 # vim /etc/sysconfig/vncservers VNCSERVERS=" ...

  2. 命令行利用KVM创建虚拟机

    一,实验环境 OS:CENTOS6.5 X86_64 二,KVM宿主环境配置 1.cat /proc/cpuinfo | egrep 'vmx|svm'  //查看是否支持虚拟技术 2.安装KVM相关 ...

  3. 团队作业4——第一次项目冲刺 SiStH DaY

    项目冲刺--??? 你以为penta kill以后就没事了嘛,就没得写了吗?你还期待我会给你一个六杀?七杀?别逗了,你以为你玩三国杀呢,做项目这么严肃的事情,怎么能玩笑. 那么我就在这里明明白白地告诉 ...

  4. Beta阶段项目复审

    复审人:王李焕 六指神功:http://www.cnblogs.com/teamworkers/ wt.dll:http://www.cnblogs.com/TeamOf/ 六个核桃:http://w ...

  5. 201521123107 《Java程序设计》第7周学习总结

    第7周作业-集合 1.本周学习总结 2.书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: public boolean contains( ...

  6. 控制结构(9) 管道(pipeline)

    // 上一篇:线性化(linearization) // 下一篇:指令序列(opcode) 最近阅读了酷壳上的一篇深度好文:LINUX PID 1 和 SYSTEMD.这篇文章介绍了systemd干掉 ...

  7. 201521123103 《Java程序设计》第三周学习总结

    一.本周学习总结 二.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; pub ...

  8. 201521123063 《Java程序设计》 第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图.Onenote或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 1.1 建立数据库test.表students. ...

  9. 详解go语言的array和slice 【一】

    本篇会详细讲解go语言中的array和slice,和平时开发中使用他样时需要注意的地方,以免入坑. Go语言中array是一组定长的同类型数据集合,并且是连续分配内存空间的. 声明一个数组 var a ...

  10. 多线程:多线程设计模式(二):Future模式

    一.什么是Future模型: 该模型是将异步请求和代理模式联合的模型产物.类似商品订单模型.见下图: 客户端发送一个长时间的请求,服务端不需等待该数据处理完成便立即返回一个伪造的代理数据(相当于商品订 ...