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

  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. Python虚拟环境virtualenv

    用意 virtualenv可以搭建虚拟且独立的python运行环境, 使得单个项目的运行环境与其它项目独立起来. 安装 virtualenv本质上是个python包, 使用pip安装 pip inst ...

  2. 【深入Java虚拟机】之七:Javac编译与JIT编译

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/18009455 编译过程 不论是物理机还是虚拟机,大部分的程序代码从开始编译到最终转化成物理 ...

  3. 第六次meeting会议

    [Beta] 第六次Daily Scrum Meeting 一.本次会议为第六次meeting会议 二.时间:10:00AM-10:20AM 地点:禹州楼 三.会议站立式照片 四.今日任务安排 成员 ...

  4. 团队作业10——项目复审与事后分析(Beta阶段)

    一.Beta阶段项目复审 http://www.cnblogs.com/womenshuodedoudui/p/7001208.html 二.事后诸葛分析 http://www.cnblogs.com ...

  5. 201521123104 《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 1. List中指定元素的删除(题目4-1) 1.1 实验总结 这道题的关键是如何删除元素.一 ...

  6. java课程设计——猜数游戏

    1.团队课程设计博客链接 http://www.cnblogs.com/springbreezemiles/p/7064135.html 2.个人负责模块或任务说明 本人任务: 编写主界面以及排行榜代 ...

  7. bom是什么?

  8. python并发编程之协程

    ---恢复内容开始--- 一.join方法 (1)开一个主线程 from threading import Thread,currentThread import time def walk(): p ...

  9. Linux中组 与 用户的管理

    在linux中建立组的指令是 groupadd 组名 相应的,删除组的指令: groupdel 组名 查看自己用户的组: groups 一个用户可以在多个组里面,用这个命令可以将用户添加到组: add ...

  10. Java实现CORS跨域请求

    问题 使用前后端分离模式开发项目时,往往会遇到这样一个问题 -- 无法跨域获取服务端数据 这是由于浏览器的同源策略导致的,目的是为了安全.在前后端分离开发模式备受青睐的今天,前端和后台项目往往会在不同 ...