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

  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. 个人作业2——必应词典APP分析

    第一部分:调研.评测 1.刚刚打开必应词典的时候,它给我的第一反应就是界面美观,最上面是一个查询框,下面有一些经典的句子.单词以及一些精选的文章,所有的功能都可以一目了然,看一眼就知道要怎么去使用,这 ...

  2. 团队作业八——第二次团队冲刺(Beta版本)第3天

    一.每个人的工作 (1) 昨天已完成的工作 对界面进行完善,并增加简单界面(包含简单界面内含的界面),简单模式与复杂模式的选择界面. (2) 今天计划完成的工作 做一下用户注册的功能和登录功能. (3 ...

  3. 团队作业4——第一次项目冲刺(Alpha版本)7th day

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 在计时模式下能够记录用户的用户名和成绩,没有弄登录功能, 将程序定义为单机的 未完成的卡片为登录功能和使用QQ登录. 四.困难 ...

  4. 201521123075 《Java程序设计》第9周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避 ...

  5. 201521123113《Java程序设计》第10周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集异常.多线程 Q1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? fi ...

  6. Eclipse rap 富客户端开发总结(2):rap项目目前的进度和存在的问题

    1. Eclipse rap项目目前的进度 目前rap 的最新版本是 rap 1.5M4,最稳定版本是 rap 1.4.1,我们项目目前使用的版本是rap 1.4.1,Rap目前已经支持的部分是 sw ...

  7. vim基础详解

    目录: 什么是vim Vim能做什么 如何学习vim 如何用vim打开一个文件 Vim的三种模式 插入模式 命令模式 扩展命令模式 光标移动 在命令模式下 删除,复制,粘贴 扩展命令模式 可视化模式 ...

  8. Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class

    如果在使用SpringMVC中使用文件上传的MultipartFile对象时,出现了以下的错误: Could not instantiate bean class [org.springframewo ...

  9. 上传文件复用代码【fileUpload】

    这是使用了FileUpload上传组件的,解决了中文乱码问题了,并且删除了临时文件的. 使用了一个Book对象做示范 private Book uploadData(HttpServletReques ...

  10. 查找Oracle数据库中的重复记录

    本文介绍了几种快速查找ORACLE数据库中的重复记录的方法. 下面以表table_name为例,介绍三种不同的方法来确定库表中重复的记录 方法1:利用分组函数查找表中的重复行:按照某个字段分组,找出行 ...