PCM与G711A互转

server/2025/2/12 11:10:45/

PCM与G711A互转

  • 工具类(Java)
  • 调用方法(Kotlin)

工具类(Java)

public class G711Code {private final static int SIGN_BIT = 0x80;private final static int QUANT_MASK = 0xf;private final static int SEG_SHIFT = 4;private final static int SEG_MASK = 0x70;static short[] seg_end = {0xFF, 0x1FF, 0x3FF, 0x7FF,0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};static short search(short val,short[] table,short size){for (short i = 0 ; i < size; i++) {if(val <= table[i]){return i;}}return size;}static byte linear2alaw(short pcm_val){short mask;short seg;char aval;if(pcm_val >= 0){mask = 0xD5;}else{mask = 0x55;pcm_val = (short) (-pcm_val - 1);if(pcm_val < 0){pcm_val = 32767;}}/* Convert the scaled magnitude to segment number. */seg = search(pcm_val, seg_end, (short) 8);/* Combine the sign, segment, and quantization bits. */if (seg >= 8)       /* out of range, return maximum value. */return (byte) (0x7F ^ mask);else {aval = (char) (seg << SEG_SHIFT);if (seg < 2)aval |= (pcm_val >> 4) & QUANT_MASK;elseaval |= (pcm_val >> (seg + 3)) & QUANT_MASK;return (byte) (aval ^ mask);}}static short alaw2linear(byte a_val){short       t;short       seg;a_val ^= 0x55;t = (short) ((a_val & QUANT_MASK) << 4);seg = (short) ((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) != 0 ? t : (short) -t;}/*** pcmg711a* @param pcm* @param code* @param size*/public static void G711aEncoder(short[] pcm,byte[] code,int size){for(int i=0;i<size;i++){code[i]=linear2alaw(pcm[i]);
//            Log.e("-------------","数据编码:"+code[i]);}}/*** g711a 转 pcm* @param pcm* @param code* @param size*/public static void G711aDecoder(short[] pcm,byte[] code,int size){for(int i=0;i<size;i++){pcm[i]=alaw2linear(code[i]);}}/*** byte转short* @param data* @return*/public static short[] byteToShort(byte[] data) {short[] shortValue = new short[data.length / 2];for (int i = 0; i < shortValue.length; i++) {shortValue[i] = (short) ((data[i * 2] & 0xff) | ((data[i * 2 + 1] & 0xff) << 8));}return shortValue;}/*** short转byte* @param data* @return*/public static byte[] shortToByte(short[] data) {byte[] byteValue = new byte[data.length * 2];for (int i = 0; i < data.length; i++) {byteValue[i * 2] = (byte) (data[i] & 0xff);byteValue[i * 2 + 1] = (byte) ((data[i] & 0xff00) >> 8);}return byteValue;}}

调用方法(Kotlin)

	/*** ffmpeg -f s16le -i customAudio1.pcm -f alaw customAudio1.pcm.g711a* ffplay -f s16le -ar 8000 customAudio1.pcm* ffplay -f alaw -ar 8000 customAudio1.pcm.g711a** 可以用进制软件比如hexviewer查看对比ffmpeg转化的g711a文件* 或者使用ffplay播放显示波形*/fun pcm2g711a(){val inputStream: InputStream = assets.open("customAudio1.pcm")val available = inputStream.available()Log.e("TAG","available:"+available)val bytes: ByteArray = ByteArray(available)val byteCount = inputStream.read(bytes)Log.e("TAG","byteCount:"+byteCount)//文件大可能需要循环读取
//        val bytes: ByteArray = ByteArray(1024)
//        var byteCount = inputStream.read(bytes)
//        Log.e("TAG","start byteCount:"+byteCount)
//        while (byteCount != -1){
//            byteCount = inputStream.read(bytes)
//            Log.e("TAG","byteCount:"+byteCount)
//        }
//        Log.e("TAG","end byteCount:"+byteCount)val shortPCM: ShortArray = G711Code.byteToShort(bytes)//一定要转!!!val bytesG711A: ByteArray = ByteArray(available)Log.e("TAG","shortPCM.size:"+shortPCM.size)G711Code.G711aEncoder(shortPCM,bytesG711A,shortPCM.size)Log.e("TAG","bytesG711A.size:"+bytesG711A.size)val file = File(getExternalFilesDir(null)?.absolutePath+"/customAudio1_"+System.currentTimeMillis()+".g711a")val fos = FileOutputStream(file)fos.write(bytesG711A)Log.e("TAG","write end")inputStream.close()fos.flush()fos.close()}fun g711a2pcm(){val inputStream: InputStream = assets.open("customAudio1.pcm.g711a")val available = inputStream.available()Log.e("TAG","available:"+available)val bytes: ByteArray = ByteArray(available)val byteCount = inputStream.read(bytes)Log.e("TAG","byteCount:"+byteCount)//文件大可能需要循环读取
//        val bytes: ByteArray = ByteArray(1024)
//        var byteCount = inputStream.read(bytes)
//        Log.e("TAG","start byteCount:"+byteCount)
//        while (byteCount != -1){
//            byteCount = inputStream.read(bytes)
//            Log.e("TAG","byteCount:"+byteCount)
//        }
//        Log.e("TAG","end byteCount:"+byteCount)//        val bytesPCMEmpty: ByteArray = ByteArray(available)
//        Log.e("TAG","bytesPCMEmpty.size:"+bytesPCMEmpty.size)
//        val shortPCM: ShortArray = G711Code.byteToShort(bytesPCMEmpty)//会丢失后面一段数据!!!val shortPCM: ShortArray = ShortArray(available)Log.e("TAG","shortPCM.size:"+shortPCM.size)G711Code.G711aDecoder(shortPCM,bytes,bytes.size)Log.e("TAG","shortPCM.size:"+shortPCM.size)val file = File(getExternalFilesDir(null)?.absolutePath+"/customAudio1_"+System.currentTimeMillis()+".pcm")val fos = FileOutputStream(file)val bytesPCM: ByteArray = G711Code.shortToByte(shortPCM)fos.write(bytesPCM)Log.e("TAG","write end")inputStream.close()fos.flush()fos.close()}

http://www.ppmy.cn/server/167040.html

相关文章

ZooKeeper 的典型应用场景:从概念到实践

引言 在分布式系统的生态中&#xff0c;ZooKeeper 作为一个协调服务框架&#xff0c;扮演着至关重要的角色。它的设计目的是提供一个简单高效的解决方案来处理分布式系统中常见的协调问题。本文将详细探讨 ZooKeeper 的典型应用场景&#xff0c;包括但不限于配置管理、命名服务…

机器学习数学公式推导笔记

正定方程是凸函数证明 范数 向量的范数与内积 范数例子

Spring Boot 3.4 中 MockMvcTester 的新特性解析

引言 在 Spring Boot 3.4 版本中&#xff0c;引入了一个全新的 MockMvcTester 类&#xff0c;使 MockMvc 测试可以直接支持 AssertJ 断言。本文将深入探讨这一新特性&#xff0c;分析它如何优化 MockMvc 测试并提升测试的可读性。 Spring MVC 示例 为了演示 MockMvcTester 的…

【含文档+PPT+源码】基于python爬虫的豆瓣电影、音乐、图书数据分析系统

项目介绍 本课程演示的是一款基于python爬虫的豆瓣电影、音乐、图书数据分析系统&#xff0c;主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的 Python学习者。 1.包含&#xff1a;项目源码、项目文档、数据库脚本、软件工具等所有资料 2.带你从零开始部署运行…

将DeepSeek接入Excel实现交互式对话

引言 将DeepSeek接入Excel&#xff0c;为你带来前所未有的交互体验&#xff01;“哪里不懂&#xff0c;选中哪里”&#xff0c;然后直接在侧边栏对话框向DeepSeek发问&#xff0c;非常地方便&#xff01; 案例演示 设置接入方式 既可以通过本地部署的DeepSeek接入Excel&#…

Flink-序列化

一、概述 几乎每个Flink作业都必须在其运算符之间交换数据&#xff0c;由于这些记录不仅可以发送到同一JVM中的另一个实例&#xff0c;还可以发送到单独的进程&#xff0c;因此需要先将记录序列化为字节。类似地&#xff0c;Flink的堆外状态后端基于本地嵌入式RocksDB实例&…

Spring Boot接入Deep Seek的API

1&#xff0c;首先进入deepseek的官网&#xff1a;DeepSeek | 深度求索&#xff0c;单击右上角的API开放平台。 2&#xff0c;单击API keys&#xff0c;创建一个API&#xff0c;创建完成务必复制&#xff01;&#xff01;不然关掉之后会看不看api key&#xff01;&#xff01;&…

使用deepseek快速创作ppt

目录 1.在DeekSeek生成PPT脚本2.打开Kimi3.最终效果 DeepSeek作为目前最强大模型&#xff0c;其推理能力炸裂&#xff0c;但是DeepSeek官方没有提供生成PPT功能&#xff0c;如果让DeepSeek做PPT呢&#xff1f; 有个途径&#xff1a;在DeepSeek让其深度思考做出PPT脚本&#xf…