Java Sound : audio inputstream from pcm amplitude array
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-making-audio-input-stream.html
In this post, i am going to show the code for creating the AudioInputStream from an PCM - amplitude array.
It basically converts the int [] array to byte array according to AudioFormat.
The code for the reverse operation (extract amplitude array from recorded wave file or AudioStream )is in my
earlier post : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html
The code for converting PCM amplitude array to AudioStream is follows :
public static AudioInputStream getAudioStreamFromPCMArray(int[] audioDataIntArr, AudioFormat format) {
byte[] data = null;
int nlengthInSamples = audioDataIntArr.length * 2;
if (format.getSampleSizeInBits() == 16) {
// FIXME: debug at full length, signed/unsigned problem
data = new byte[nlengthInSamples];
if (format.isBigEndian()) {
for (int i = 0; i < audioDataIntArr.length; i++) {
int temp = Math.abs((short) (audioDataIntArr[i] * 255));
data[2 * i + 1] = (byte) temp;
data[2 * i + 0] = (byte) (temp >> 8);
}
} else {
for (int i = 0; i < audioDataIntArr.length; i++) {
int temp = Math.abs((short) (audioDataIntArr[i] * 255));
data[2 * i + 0] = (byte) temp;
data[2 * i + 1] = (byte) (temp >> 8);
}
}
} else if (format.getSampleSizeInBits() == 8) {
nlengthInSamples = audioDataIntArr.length;
data = new byte[nlengthInSamples];
if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
// PCM_SIGNED
for (int i = 0; i < nlengthInSamples; i++) {
data[i] = (byte) audioDataIntArr[i];
}
} else {
// PCM_UNSIGNED
for (int i = 0; i < nlengthInSamples; i++) {
data[i] = (byte) (audioDataIntArr[i] + 128);
}
}
}// end of if..else
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
AudioInputStream ais = new AudioInputStream(bais, format, audioDataIntArr.length);
return ais;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//## Required Testing -- Comments are much welcomed ...... Thanks
Java Sound : audio inputstream from pcm amplitude array的更多相关文章
- Java Audio : Playing PCM amplitude Array
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-audio-playing-pcm-amplitude-array.html ...
- Java Sound : generate play sine wave - source code
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-generate-play-sine-wave.html Work ...
- Java extract amplitude array from recorded wave
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html Extra ...
- Sound (audio file) player in java - working source code example
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/sound-audio-file-player-in-java-working.html ...
- java sound初探
网上关于java sound的正规资源讲解的非常好,本文不再给出示例,主要提供一些好的资源,并说说我的一些理解,用于形成对java sound的整体认识. 一.几个词汇 TTS:text-to-spe ...
- Java IO (1) - InputStream
Java IO (1) - InputStream 前言 JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包.Java 流在处理上 ...
- Java学习之InputStream中read()与read(byte[] b)
Java学习之InputStream中read()与read(byte[] b) 这两个方法在抽象类InputStream中都是作为抽象方法存在的, JDK API中是这样描述两者的: read() ...
- java 实现往oracle存储过程中传递array数组类型的参数
注:本文来源于 < java 实现往oracle存储过程中传递array数组类型的参数 >最近项目中遇到通过往存储过程传递数组参数的问题, 浪费了N多个小时,终于有点头绪. 具体的代码 ...
- Java – How to convert String to Char Array
Java – How to convert String to Char ArrayIn Java, you can use String.toCharArray() to convert a Str ...
随机推荐
- 远程连接Linux mysql报错:Access denied for user ‘root’@‘localhost’(using password: YES)的解决方法
在新安装好的Centos7上刚安装好mysql,准备进去看看,但是登陆的时候,发现报错啦: ERROR 1045 (28000): Access denied for user 'root'@'loc ...
- vue的组件名称问题
如果组件名称中带有大写字母,那么会报错
- [React] Write a Custom State Hook in React
Writing your own custom State Hook is not as a daunting as you think. To keep things simple, we'll r ...
- greenplum 下载地址
一.推荐使用下面下载地址 https://network.pivotal.io/products/pivotal-gpdb#/releases/158026/file_groups/1083 二.官网 ...
- 繁繁的游戏 Floyd
繁繁的游戏 Floyd 繁繁想和小伙伴们打游戏,游戏在一个山庄进行,这个山庄有N座山,编号为\(1\)到\(N\),为了方便大 家在不同的山之间移动,繁繁建了一些桥,由于技术的原因,桥连接的两座山的高 ...
- CF1163E Magical Permutation【线性基,构造】
题目描述:输入一个大小为\(n\)的正整数集合\(S\),求最大的\(x\),使得能构造一个\(0\)到\(2^x-1\)的排列\(p\),满足\(p_i\oplus p_{i+1}\in S\) 数 ...
- 深度学习面试题18:网中网结构(Network in Network)
目录 举例 参考资料 网中网结构通过多个分支的运算(卷积或池化),将分支上的运算结果在深度上连接 举例 一个3*3*2的张量, 与3个1*1*2的卷积核分别same卷积,步长=1, 与2个2*2*2的 ...
- 第2课第3节_Java面向对象编程_继承性_P【学习笔记】
摘要:韦东山android视频学习笔记 面向对象程序的三大特性之继承性:继承性的主要作用就是复用代码.继承性也有一定的限制,如图一 图一 1.我们在第2课第2节_Java面向对象编程_封装性_P 中 ...
- 自定义alert弹框,title不显示域名(重写alert)
问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...
- GIS地理处理工具案例教程-成本距离
GIS地理处理工具案例教程-成本距离 关键词:最短路径,成本路径,最佳路径,最优路径,路径分析,选线分析 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq ...