【发布时间】:2015-08-04 15:47:16
【问题描述】:
我正在使用 Javascript/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 个字节。
我知道我正在将正确的 PCM WAV 上传到 Glassfish,因为我可以直接从 Javascript 端下载和收听我的录音。
我尝试编码后在服务器端打开文件时出现错误。
我的主要问题是:有/谁能成功使用链接页面中的类从 PCM 编码到 ULAW?
【问题讨论】:
-
是的,我已经成功地将它用于 PCM 到 ULAW。
标签: java audio wav pcm getusermedia