转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-audio-playing-pcm-amplitude-array.html

How to play a array of PCM amplitude values (integer or float array) in Java - Steps

Basic Steps :

//initialize source data line - for playback
SourceDataLine line = AudioSystem.getSourceDataLine(audioFormat);
line.open(audioFormat);
line.start(); //play the byteArray
line.write(byteArray, 0, byteArray .length);//(byte[] b, int off, int len)
line.drain();
line.close();

Converting integer array to bytearray :

We need to convert our PCM array to byteArray because the line.write requires byte[] b as parameter.

byte b = (byte)(i*127f);

Full code : This example plays the randomly generated array :

public class PlayAnArray {
private static int sampleRate = 16000;
public static void main(String[] args) {
try {
final AudioFormat audioFormat = new AudioFormat(sampleRate, 8, 1, true, true);
SourceDataLine line = AudioSystem.getSourceDataLine(audioFormat );
line.open(audioFormat );
line.start();
for (int i = 0; i < 5; i++) {//repeat in loop
play(line, generateRandomArray());
}
line.drain();
line.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] generateRandomArray() {
int size = 20000;
System.out.println(size);
byte[] byteArray = new byte[size];
for (int i = 0; i < size; i++) {
byteArray[i] = (byte) (Math.random() * 127f);
}
return byteArray;
}
private static void play(SourceDataLine line, byte[] array) {
int length = sampleRate * array.length / 1000;
line.write(array, 0, array.length);
}
}

You may modify generateRandomArray to play different waveforms like SINE wave, square wave... etc

For playing a wave file in java follow my post on How to play wave file on java

Java Audio : Playing PCM amplitude Array的更多相关文章

  1. Java Sound : audio inputstream from pcm amplitude array

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-making-audio-input-stream.html In ...

  2. Java extract amplitude array from recorded wave

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html Extra ...

  3. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  4. 排查Hive报错:org.apache.hadoop.hive.serde2.SerDeException: java.io.IOException: Start of Array expected

    CREATE TABLE json_nested_test ( count string, usage string, pkg map<string,string>, languages ...

  5. (java oracle)以bean和array为参数的存储过程及dao部分代码

    一.数据库部分 1.创建bean对象 CREATE OR REPLACE TYPE "QUARTZJOBBEAN" as object ( -- Author : Duwc -- ...

  6. Java for LeetCode 189 Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  7. Java SE 基础知识(String,Array)

    String 类: 1. 对于String对象的相等性判断来说,请使用equals()方法,而不是==.String的equals()是判断当前字符串与传进来的字符串的内容是否一致. 2. Strin ...

  8. Java [Leetcode 238]Product of Array Except Self

    题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i]  ...

  9. Java [Leetcode 88]CMerge Sorted Array

    题目描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...

随机推荐

  1. Mac OpenSSL 生成支付宝 2048位密钥

    安装OpenSSL: brew install openssl 然后: OpenSSL> genrsa -out rsa_private_key.pem 2048 #生成私钥 OpenSSL&g ...

  2. jquery中ajax跨域加载

    今天学习ajax跨域加载,先来一段代码,异步加载的链接是爱奇艺的开源,我直接拿来用作测试 <!DOCTYPE html> <html lang="en"> ...

  3. 软件测试技术之可用性测试之WhatsApp Web

    Tag:可行性测试.测试流程.结果分析.案例分析 WhatsApp是一款面向智能手机的网络通讯服务,它可以通过网络传送短信.图片.音频和视频.WhatsApp在全球范围内被广泛使用,是最受欢迎的即时聊 ...

  4. 2019/12/5BJFirstDay--scrum后台+cpp项目前台环境跑起来!!!

    1.配置服务器: 2.进入cd C:\java\25.beijing\06.vuejs\cpp201911221829\cpp 3.运行的命令是:npm run dev 4.先启动 5.然后再启动cp ...

  5. CSS3 新增选择器:伪类选择器和属性选择器

    一.结构(位置)伪类选择器( : ) 1.:first-child 2.:last-child 3.:nth-child(n)或者:nth-child(2n)或者:nth-child(2n+1) &l ...

  6. PHP中使用 Memcached 的测试案例

    <?php class MemcacheController extends ControllerBase { public function indexAction() { session_s ...

  7. 网络请求之get post

    ——http get和post的区别? 1.get用于获取数据,post用于提交数据 2.get提交参数追加在url后面,post参数可以通过http body提交 3.get的url会有长度上的限制 ...

  8. 洛谷P1052过河

    题目 不看数据范围的话是一个很简单的DP,可是加上数据范围之后就之前的做法就不行了. 所以我们考虑一下路径压缩. 小数据Code #include <iostream> #include ...

  9. 利用nc当作备用shell管理方案.

    ssh 有时候真的就是连不上了,然后是没什么然后了呢. 或者手残改错配置然后重新sshd了. 所以这时候需要备用的远程管理工具.nc是最好的选择,一般服务器都是 内网的,如果跳板机也管理不了呢. 安装 ...

  10. C#题(子文章)(持续更新)

    -----> 总文章 入口 文章目录 [-----> 总文章 入口](https://blog.csdn.net/qq_37214567/article/details/90174445) ...