gpt4 book ai didi

java - 在JAVA中将PCM WAV转换为ULAW WAV

转载 作者:行者123 更新时间:2023-12-03 00:42:32 25 4
gpt4 key购买 nike

我正在使用Java / HTML5 / JSF前端和Glassfish(Java)后端编写基于Web的录音机。

我需要使用ULAW编码保存记录的.WAV文件。但是据我所知,以HTML5 / Javascript(带有getUserMedia())录制音频的唯一方法是采用PCM编码。我希望有一种简单的方法来捕获ULAW中的客户端记录,但是一直找不到任何方法。

所以现在我一直想做的是:

Record in PCM wav (client side)
Upload to server using JSP
Pass the received FileItem into a JAVA converter method that returns a byte array in ULAW

我发现有人尝试在Android中执行以下操作:

Android PCM to Ulaw encoding wav file

但是,本文引用的类无法正常工作,或者我没有正确使用它。

我当前的 convertPCMtoULAW(FileItem file) Java方法:
//read the FileItem's InputStream into a byte[]
InputStream uploadedStream = file.getInputStream();
byte[] pcmBytes = new byte[(int) file.getSize()];
uploadedStream.read(pcmBytes);

//extract the number of PCM Samples from the header
int offset =40;
int length =4;
int pcmSamples = 0;
for (int i = 0; i < length; i++)
{
pcmSamples += ((int) pcmBytes[offset+i] & 0xffL) << (8 * i);
}

//create the UlawEncoderInputStream (class in link above)
is = new UlawEncoderInputStream(
file.getInputStream(),
UlawEncoderInputStream.maxAbsPcm(pcmBytes, 44, pcmSamples/2)
);

//read from the created InputStream into another byte[]
byteLength = is.read(ulawBytes);

//I then add the ULAW header to the beginning of the byte[]
//and pass the entire byte[] through a pre-existing Wav Verifier method
//which serializes the byte[] later on

(所有代码都会编译,上面的代码已简化为包括必要的部分)

我一直在找回从byteLength变量中读取的512个字节。

我知道我正在向Glassfish上传正确的PCM WAV,因为我可以直接从Javascript一侧下载并收听录音。

尝试对文件进行编码后,在服务器端打开文件时出现错误。

我的主要问题是: 是否有人可以成功使用链接页面中的类从PCM编码为ULAW?

最佳答案

我相信您面临的问题与转码无关,而是与Java的InputStream.read的契约(Contract)无关。从documentation:

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.



换句话说,此函数返回的数字是在此方法的特定调用中实际读取的字节数 。契约(Contract)不保证在关闭流之前将读取所有字节,仅保证调用时可用的字节数。

您将不得不循环调用其重载read(byte[] b, int off, int len)直到关闭流,或者将流包装为 DataInputStream 并使用 readFully

关于java - 在JAVA中将PCM WAV转换为ULAW WAV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31813753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com