Java Audio : Playing PCM amplitude Array
转载自: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的更多相关文章
- Java Sound : audio inputstream from pcm amplitude array
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-making-audio-input-stream.html In ...
- Java extract amplitude array from recorded wave
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html Extra ...
- 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 + ...
- 排查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 ...
- (java oracle)以bean和array为参数的存储过程及dao部分代码
一.数据库部分 1.创建bean对象 CREATE OR REPLACE TYPE "QUARTZJOBBEAN" as object ( -- Author : Duwc -- ...
- 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 ...
- Java SE 基础知识(String,Array)
String 类: 1. 对于String对象的相等性判断来说,请使用equals()方法,而不是==.String的equals()是判断当前字符串与传进来的字符串的内容是否一致. 2. Strin ...
- 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] ...
- Java [Leetcode 88]CMerge Sorted Array
题目描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...
随机推荐
- c++ templat乱测
该上机实验环境 linux mint IDE:qt5.11 代码复制到windows下vs2017报错,提示char* 类型不能直接赋值字符串 在linux mint下可以运行,测试目的:检验复 ...
- docker的笔记
docker run 命令 docker run ubuntu:15.10 /bin/echo "Hello world" 各个参数解析: docker: Docker 的二进制执 ...
- 学习Spring-Data-Jpa(五)---可嵌入对象和元素集合的使用
1.场景一:地址信息(省.市.县.详细地址)在很多实体中都需要,比如说作者有地址,订单也有地址,但是他们的地址并不能独立与他们存在,所以地址不能映射为实体,那么我们就需要在作者实体和订单实体中都添加这 ...
- RestTemplate 使用中的几个问题
Spring Boot使用RestTemplate消费REST服务的几个问题记录 我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调 ...
- [DUBBO] qos-server can not bind localhost:22222错误解决
问题描述 在启动dubbo-consumer工程时报错,信息如下: 2019-11-29 09:22:18.001 ERROR [RMI TCP Connection(2)-127.0.0.1] [o ...
- rc.local配置
1.让系统默认启动的时候执行rc.local 启动我们想要启动进程:如:nginx ,memcached,或者是 php-fpm等! /usr/local/bin/redis-server /etc/ ...
- codevs 1729 单词查找树
二次联通门 : codevs 1729 单词查找树 /* codevs 1729 单词查找树 Trie树 统计节点个数 建一棵Trie树 插入单词时每新开一个节点就计数器加1 */ #include ...
- Java Redis缓存穿透/缓存雪崩/缓存击穿,Redis分布式锁实现秒杀,限购等
package com.example.redisdistlock.controller; import com.example.redisdistlock.util.RedisUtil; impor ...
- mysql 开放端口 外网访问
mysql 开放端口 外网访问 作者: moyixi 时间: April 24, 2018 分类: 默认分类,数据库,mysql 前提: 如果是云服务器,请先把安全组件相应的开发 查看服务器的端口33 ...
- RabbitMQ入门学习系列(七) 远程调用RPC
快速阅读 生产者和消费者启动以后,都有一个接收事件,消费者是接收事件是处理调用方法以后等待生产者的返回,生产者的接收事件是处理接收生产者发送的消息,进行处理.消费者发送的时候要在回调队列中加入一个标识 ...