Java extract amplitude array from recorded wave
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html
Extract amplitude array from recorded/saved wav : From File , AudioInputStream , ByteArray of File or ByteArrayInputStream - working java source code example
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* saving and extracting amplitude data from wavefile byteArray
*
* @author Ganesh Tiwari
*/
public class WaveData {
private byte[] arrFile;
private byte[] audioBytes;
private int[] audioData;
private ByteArrayInputStream bis;
private AudioInputStream audioInputStream;
private AudioFormat format;
private double durationSec;
private double durationMSec;
public WaveData() {
}
public int[] extractAmplitudeFromFile(File wavFile) {
try {
// create file input stream
FileInputStream fis = new FileInputStream(wavFile);
// create bytearray from file
arrFile = new byte[(int) wavFile.length()];
fis.read(arrFile);
} catch (Exception e) {
System.out.println("SomeException : " + e.toString());
}
return extractAmplitudeFromFileByteArray(arrFile);
}
public int[] extractAmplitudeFromFileByteArray(byte[] arrFile) {
// System.out.println("File : "+wavFile+""+arrFile.length);
bis = new ByteArrayInputStream(arrFile);
return extractAmplitudeFromFileByteArrayInputStream(bis);
}
/**
* for extracting amplitude array the format we are using :16bit, 22khz, 1
* channel, littleEndian,
*
* @return PCM audioData
* @throws Exception
*/
public int[] extractAmplitudeFromFileByteArrayInputStream(ByteArrayInputStream bis) {
try {
audioInputStream = AudioSystem.getAudioInputStream(bis);
} catch (UnsupportedAudioFileException e) {
System.out.println("unsupported file type, during extract amplitude");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException during extracting amplitude");
e.printStackTrace();
}
// float milliseconds = (long) ((audioInputStream.getFrameLength() *
// 1000) / audioInputStream.getFormat().getFrameRate());
// durationSec = milliseconds / 1000.0;
return extractAmplitudeDataFromAudioInputStream(audioInputStream);
}
public int[] extractAmplitudeDataFromAudioInputStream(AudioInputStream audioInputStream) {
format = audioInputStream.getFormat();
audioBytes = new byte[(int) (audioInputStream.getFrameLength() * format.getFrameSize())];
// calculate durations
durationMSec = (long) ((audioInputStream.getFrameLength() * 1000) / audioInputStream.getFormat().getFrameRate());
durationSec = durationMSec / 1000.0;
// System.out.println("The current signal has duration "+durationSec+" Sec");
try {
audioInputStream.read(audioBytes);
} catch (IOException e) {
System.out.println("IOException during reading audioBytes");
e.printStackTrace();
}
return extractAmplitudeDataFromAmplitudeByteArray(format, audioBytes);
}
public int[] extractAmplitudeDataFromAmplitudeByteArray(AudioFormat format, byte[] audioBytes) {
// convert
// TODO: calculate duration here
audioData = null;
if (format.getSampleSizeInBits() == 16) {
int nlengthInSamples = audioBytes.length / 2;
audioData = new int[nlengthInSamples];
if (format.isBigEndian()) {
for (int i = 0; i < nlengthInSamples; i++) {
/* First byte is MSB (high order) */
int MSB = audioBytes[2 * i];
/* Second byte is LSB (low order) */
int LSB = audioBytes[2 * i + 1];
audioData[i] = MSB << 8 | (255 & LSB);
}
} else {
for (int i = 0; i < nlengthInSamples; i++) {
/* First byte is LSB (low order) */
int LSB = audioBytes[2 * i];
/* Second byte is MSB (high order) */
int MSB = audioBytes[2 * i + 1];
audioData[i] = MSB << 8 | (255 & LSB);
}
}
} else if (format.getSampleSizeInBits() == 8) {
int nlengthInSamples = audioBytes.length;
audioData = new int[nlengthInSamples];
if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
// PCM_SIGNED
for (int i = 0; i < audioBytes.length; i++) {
audioData[i] = audioBytes[i];
}
} else {
// PCM_UNSIGNED
for (int i = 0; i < audioBytes.length; i++) {
audioData[i] = audioBytes[i] - 128;
}
}
}// end of if..else
// System.out.println("PCM Returned===============" +
// audioData.length);
return audioData;
}
public byte[] getAudioBytes() {
return audioBytes;
}
public double getDurationSec() {
return durationSec;
}
public double getDurationMiliSec() {
return durationMSec;
}
public int[] getAudioData() {
return audioData;
}
public AudioFormat getFormat() {
return format;
}
}
留言:
Think I found a bug for 8 bit unsigned samples in the code above.
Java regards a byte-variable as a signed variable, so we can't just subtract 128 for all sample-values. For "negative" values we must instead add 128, I think.
E.g. the sampled unsigned value 10000000 (128 unsigned) should mean that we are in the middle of the value-range. It should actually mean 0, but java sees it as -128, and if we subtract 128 we'll get -256, which isn't what we want at all.
And the "highest" sample-value possible with 8 bits, 11111111, means -1 to java if it's in a byte-variable. We'd get the value -129 here with the old method, but we would expect 127.
For all "positive" values 00000000 - 01111111 it works fine to subtract 128 as before, so something like this would work better:
// PCM_UNSIGNED
for (int i = 0; i < audioBytes.length; i++)
{
if (audioBytes[i] >= 0)
_audioData[i] = audioBytes[i] - 128;
else
_audioData[i] = audioBytes[i] + 128;
}
(Or e.g. you could "shift" the byte-value into an int-variable before subtracting 128.)
Java extract amplitude array from recorded wave的更多相关文章
- Java Sound : audio inputstream from pcm amplitude array
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-making-audio-input-stream.html In ...
- Java之数组array和集合list、set、map
之前一直分不清楚java中的array,list.同时对set,map,list的用法彻底迷糊,直到看到了这篇文章,讲解的很清楚. 世间上本来没有集合,(只有数组参考C语言)但有人想要,所以有了集合 ...
- Java – Check if Array contains a certain value?
Java – Check if Array contains a certain value?1. String Arrays1.1 Check if a String Array contains ...
- java 编程基础 Class对象 反射 :数组操作java.lang.reflect.Array类
java.lang.reflect包下还提供了Array类 java.lang.reflect包下还提供了Array类,Array对象可以代表所有的数组.程序可以通过使 Array 来动态地创建数组, ...
- Java Audio : Playing PCM amplitude Array
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-audio-playing-pcm-amplitude-array.html ...
- java中List Array相互转换
List to Array List 提供了toArray的接口,所以可以直接调用,转为object型数组 List<String> list = new ArrayList<Str ...
- Java中对Array数组的常用操作
目录: 声明数组: 初始化数组: 查看数组长度: 遍历数组: int数组转成string数组: 从array中创建arraylist: 数组中是否包含某一个值: 将数组转成set集合: 将数组转成li ...
- JAVA中数组Array与List互转
List<String> list = new ArrayList<String>();String[] array = new String[10]; 1.数组转成Listl ...
- Java List 和 Array 转化
List to Array List 提供了toArray的接口,所以可以直接调用转为object型数组 List<String> list = new ArrayList<Stri ...
随机推荐
- goto语句——慎用,但是可以用
最近使用了goto语句,是因为if嵌套太深了,因此把错误处理同意了,直接使用goto语句. 举例: #include <stdio.h> int main () { /* local va ...
- b、B、KB、MB、GB 的关系?
1. 8bit (位) = 1Byte (字节) 2.1024Byte (字节 ) = 1KB 3.1024KB = 1MB 4.1024MB = 1GB 5.1024GB = 1TB
- 用LinkedList和ArrayList实现自定义栈的异同
//ArrayList已连续的空间进行存储数据 //LinkedList已链表的结构存储数据 //栈 MyStark ms=new MyStark();//new 一个实现栈的类 //压栈 ...
- PostgreSQL 11 新特性之覆盖索引(Covering Index)(转载)
通常来说,索引可以用于提高查询的速度.通过索引,可以快速访问表中的指定数据,避免了表上的扫描.有时候,索引不仅仅能够用于定位表中的数据.某些查询可能只需要访问索引的数据,就能够获取所需要的结果,而不需 ...
- Kafka为什么速度那么快?该怎么回答
Kafka的消息是保存或缓存在磁盘上的,一般认为在磁盘上读写数据是会降低性能的,因为寻址会比较消耗时间,但是实际上,Kafka的特性之一就是高吞吐率.即使是普通的服务器,Kafka也可以轻松支持每秒百 ...
- [代码审计]四个实例递进php反序列化漏洞理解【转载】
原作者:大方子 原文链接:https://blog.csdn.net/nzjdsds/article/details/82703639 0x01 索引 最近在总结php序列化相关的知识,看了好多前辈师 ...
- [线段树]洛谷P5278 算术天才⑨与等差数列
题目描述 算术天才⑨非常喜欢和等差数列玩耍. 有一天,他给了你一个长度为n的序列,其中第i个数为a[i]. 他想考考你,每次他会给出询问l,r,k,问区间[l,r]内的数从小到大排序后能否形成公差为k ...
- 消息队列Rabbit MQ 学习第一篇
1 介绍 1.1RabbitMQ MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队 ...
- 第08组 Alpha冲刺(1/6)
队名:955 组长博客:https://www.cnblogs.com/cclong/p/11841141.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...
- Oracle Audit 功能的使用和说明
http://blog.itpub.net/9399028/viewspace-712457/审计(Audit) 用于监视用户所执行的数据库操作,审计记录可存在数据字典表(称为审计记录:存储在syst ...