首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JAVA将PCM WAV转换为ULAW WAV

用JAVA将PCM WAV转换为ULAW WAV
EN

Stack Overflow用户
提问于 2015-08-04 15:47:16
回答 1查看 1.9K关注 0票数 0

我正在编写一个基于web的语音记录器,带有Javascript/HTML5 5/JSF前端和一个Glassfish (Java)后端。

我需要用ULAW编码保存记录的.WAV文件。然而,据我所知,用HTML5 5/Javascript(用getUserMedia())记录音频的唯一方法是PCM编码。我希望有一种简单的方法来捕获ULAW中的客户端记录,但是一直无法找到任何方法来实现这一点。

所以现在我想做的是:

代码语言:javascript
复制
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到Ulaw编码wav文件

然而,本文引用的类要么不起作用,要么我没有正确地使用它。

我当前的convertPCMtoULAW(FileItem file) Java方法:

代码语言:javascript
复制
//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上传到Glassfish,因为我可以从Javascript端直接下载和听我的录音。

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

我的主要问题是:已经/谁能成功地使用链接页面中的类从PCM编码到ULAW?

EN

回答 1

Stack Overflow用户

发布于 2015-08-04 16:16:57

我相信您所面临的问题与代码转换无关,而是与Java的InputStream.read协议有关。来自文档

从输入流中读取一定数量的字节并将其存储到缓冲区数组b中。实际读取的字节数作为整数返回。此方法将阻塞,直到输入数据可用、检测到文件结束或抛出异常为止。

换句话说,这个函数返回的数字是在这个方法的特定调用中实际读取了多少字节。契约不能保证在流关闭之前读取所有字节,只有调用时可用的字节数。

要么必须在循环中调用它的重载read(byte[] b, int off, int len),直到流关闭,要么将流包装到一个DataInputStream中并使用readFully

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31813753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档