转载自: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

最新文章

  1. MathType应用:批量改变公式格式
  2. Objective-C文章中的生词
  3. python(28)获得网卡的IP地址
  4. CSS核心的几个概念
  5. scala - Map基础
  6. Springmvc整合tiles框架简单入门示例(maven)
  7. web.xml配置bug之提示The content of element type &quot;web-app&quot; must match &quot;(icon?,display- name?,description?,distributable?,
  8. hdoj 5389 Zero Escape
  9. openstack nova数据库计算结点IP地址
  10. Sequence
  11. Java的Date类与Calendar类
  12. PHP的抽象类、接口类的区别和选择【转载】
  13. 只为粗暴看一下ES6的字符串模板的性能
  14. 谈谈CommonsChunkPlugin抽取公共模块
  15. Java爬虫----有道翻译初步
  16. oracle有三种类型的异常错误: 预定义 ( Predefined )错误里面的常见错误
  17. Java+Selenium向文本框输入内容以后模仿键盘的&quot;ENTRY&quot;
  18. 洛谷P1012 拼数 string
  19. AT91RM9200---电源管理控制器(PMC)介绍
  20. spring boot 2.0(二)动态banner的支持

热门文章

  1. 项目后端 - 虚拟环境搭建 | pycharm使用虚拟环境
  2. BasicAuth
  3. Java【基础学习】 之调用构造方法顺序【坑】
  4. React vs Angular vs Vue 2019
  5. Phoenix 简单介绍
  6. Kylin 架构模块简介
  7. jsp+ tinymce粘贴word
  8. HTML5自定义属性操作
  9. 最长升序列 DP
  10. AtCoder Grand Contest 007题解