将 PCM 录制数据写入 .wav 文件(java android)
我正在使用 AudioRecord 在 android 中录制 16 位 PCM 数据。记录数据并将其保存到文件后,我将其读回以将其保存为 .wav 文件。
问题是 WAV 文件可以被媒体播放器识别,但只播放纯粹的噪音。目前我最好的猜测是我的 wav 文件头不正确,但我一直无法看出问题到底是什么。 (我认为这是因为我可以播放我在 Audacity 中录制的原始 PCM 数据)
这是我用于读取原始 PCM 文件并将其保存为 .wav 的代码:
private void properWAV(File fileToConvert, float newRecordingID){
try {
long mySubChunk1Size = 16;
int myBitsPerSample= 16;
int myFormat = 1;
long myChannels = 1;
long mySampleRate = 22100;
long myByteRate = mySampleRate * myChannels * myBitsPerSample/8;
int myBlockAlign = (int) (myChannels * myBitsPerSample/8);
byte[] clipData = getBytesFromFile(fileToConvert);
long myDataSize = clipData.length;
long myChunk2Size = myDataSize * myChannels * myBitsPerSample/8;
long myChunkSize = 36 + myChunk2Size;
OutputStream os;
os = new FileOutputStream(new File("/sdcard/onefile/assessor/OneFile_Audio_"+ newRecordingID+".wav"));
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream outFile = new DataOutputStream(bos);
outFile.writeBytes("RIFF"); // 00 - RIFF
outFile.write(intToByteArray((int)myChunkSize), 0, 4); // 04 - how big is the rest of this file?
outFile.writeBytes("WAVE"); // 08 - WAVE
outFile.writeBytes("fmt "); // 12 - fmt
outFile.write(intToByteArray((int)mySubChunk1Size), 0, 4); // 16 - size of this chunk
outFile.write(shortToByteArray((short)myFormat), 0, 2); // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
outFile.write(shortToByteArray((short)myChannels), 0, 2); // 22 - mono or stereo? 1 or 2? (or 5 or ???)
outFile.write(intToByteArray((int)mySampleRate), 0, 4); // 24 - samples per second (numbers per second)
outFile.write(intToByteArray((int)myByteRate), 0, 4); // 28 - bytes per second
outFile.write(shortToByteArray((short)myBlockAlign), 0, 2); // 32 - # of bytes in one sample, for all channels
outFile.write(shortToByteArray((short)myBitsPerSample), 0, 2); // 34 - how many bits in a sample(number)? usually 16 or 24
outFile.writeBytes("data"); // 36 - data
outFile.write(intToByteArray((int)myDataSize), 0, 4); // 40 - how big is this data chunk
outFile.write(clipData); // 44 - the actual data itself - just a long string of numbers
outFile.flush();
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static byte[] intToByteArray(int i)
{
byte[] b = new byte[4];
b[0] = (byte) (i & 0x00FF);
b[1] = (byte) ((i >> 8) & 0x000000FF);
b[2] = (byte) ((i >> 16) & 0x000000FF);
b[3] = (byte) ((i >> 24) & 0x000000FF);
return b;
}
// convert a short to a byte array
public static byte[] shortToByteArray(short data)
{
/*
* NB have also tried:
* return new byte[]{(byte)(data & 0xff),(byte)((data >> 8) & 0xff)};
*
*/
return new byte[]{(byte)(data & 0xff),(byte)((data >>> 8) & 0xff)};
}
我没有包含 getBytesFromFile() 因为它占用了太多空间这是一种久经考验的方法。 无论如何,这是执行实际记录的代码:
public void run() {
Log.i("ONEFILE", "Starting main audio capture loop...");
int frequency = 22100;
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);
audioRecord.startRecording();
ByteArrayOutputStream recData = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(recData);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
while (!stopped) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for(int i = 0; i < bufferReadResult;i++) {
try {
dos.writeShort(buffer[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
audioRecord.stop();
try {
dos.flush();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
audioRecord.stop();
byte[] clipData = recData.toByteArray();
File file = new File(audioOutputPath);
if(file.exists())
file.delete();
file = new File(audioOutputPath);
OutputStream os;
try {
os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream outFile = new DataOutputStream(bos);
outFile.write(clipData);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
请提出可能出现问题的建议。
I'm using AudioRecord to record 16 bit PCM data in android. After recording the data and saving it to a file, I read it back to save it as .wav file.
The problem is that the WAV files are recognized by media players but play nothing but pure noise. My best guess at the moment is that my wav file headers are incorrect but I have been unable to see what exactly the problem is. (I think this because I can play the raw PCM data that I recorded in Audacity)
Here's my code for reading the raw PCM file and saving it as a .wav:
private void properWAV(File fileToConvert, float newRecordingID){
try {
long mySubChunk1Size = 16;
int myBitsPerSample= 16;
int myFormat = 1;
long myChannels = 1;
long mySampleRate = 22100;
long myByteRate = mySampleRate * myChannels * myBitsPerSample/8;
int myBlockAlign = (int) (myChannels * myBitsPerSample/8);
byte[] clipData = getBytesFromFile(fileToConvert);
long myDataSize = clipData.length;
long myChunk2Size = myDataSize * myChannels * myBitsPerSample/8;
long myChunkSize = 36 + myChunk2Size;
OutputStream os;
os = new FileOutputStream(new File("/sdcard/onefile/assessor/OneFile_Audio_"+ newRecordingID+".wav"));
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream outFile = new DataOutputStream(bos);
outFile.writeBytes("RIFF"); // 00 - RIFF
outFile.write(intToByteArray((int)myChunkSize), 0, 4); // 04 - how big is the rest of this file?
outFile.writeBytes("WAVE"); // 08 - WAVE
outFile.writeBytes("fmt "); // 12 - fmt
outFile.write(intToByteArray((int)mySubChunk1Size), 0, 4); // 16 - size of this chunk
outFile.write(shortToByteArray((short)myFormat), 0, 2); // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
outFile.write(shortToByteArray((short)myChannels), 0, 2); // 22 - mono or stereo? 1 or 2? (or 5 or ???)
outFile.write(intToByteArray((int)mySampleRate), 0, 4); // 24 - samples per second (numbers per second)
outFile.write(intToByteArray((int)myByteRate), 0, 4); // 28 - bytes per second
outFile.write(shortToByteArray((short)myBlockAlign), 0, 2); // 32 - # of bytes in one sample, for all channels
outFile.write(shortToByteArray((short)myBitsPerSample), 0, 2); // 34 - how many bits in a sample(number)? usually 16 or 24
outFile.writeBytes("data"); // 36 - data
outFile.write(intToByteArray((int)myDataSize), 0, 4); // 40 - how big is this data chunk
outFile.write(clipData); // 44 - the actual data itself - just a long string of numbers
outFile.flush();
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static byte[] intToByteArray(int i)
{
byte[] b = new byte[4];
b[0] = (byte) (i & 0x00FF);
b[1] = (byte) ((i >> 8) & 0x000000FF);
b[2] = (byte) ((i >> 16) & 0x000000FF);
b[3] = (byte) ((i >> 24) & 0x000000FF);
return b;
}
// convert a short to a byte array
public static byte[] shortToByteArray(short data)
{
/*
* NB have also tried:
* return new byte[]{(byte)(data & 0xff),(byte)((data >> 8) & 0xff)};
*
*/
return new byte[]{(byte)(data & 0xff),(byte)((data >>> 8) & 0xff)};
}
I haven't included getBytesFromFile() since it takes up too much space and its a tried and tested method.
Anyway, here's the code that does the actual recording:
public void run() {
Log.i("ONEFILE", "Starting main audio capture loop...");
int frequency = 22100;
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);
audioRecord.startRecording();
ByteArrayOutputStream recData = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(recData);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
while (!stopped) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for(int i = 0; i < bufferReadResult;i++) {
try {
dos.writeShort(buffer[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
audioRecord.stop();
try {
dos.flush();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
audioRecord.stop();
byte[] clipData = recData.toByteArray();
File file = new File(audioOutputPath);
if(file.exists())
file.delete();
file = new File(audioOutputPath);
OutputStream os;
try {
os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream outFile = new DataOutputStream(bos);
outFile.write(clipData);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Please suggest what could be going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我几个小时以来一直在努力解决这个完全相同的问题,我的问题主要是在以 16 位进行录制时,您必须非常小心写入输出的内容。 WAV 文件需要 Little Endian 格式的数据,但使用 writeShort 会将其作为 Big Endian 写入输出。在使用其他函数时,我也得到了有趣的结果,因此我返回以正确的顺序写入字节并且有效。
我在调试时广泛使用了十六进制编辑器。我可以建议你也这样做。另外,上面答案中的标头有效,我用它来检查我自己的代码,并且这个标头相当万无一失。
I've been wrestling with this exact same question for hours now, and my issue was mostly that when recording in 16 bits you have to be very careful about what you write to the output. The WAV file expects the data in Little Endian format, but using writeShort writes it to the output as Big Endian. I also got interesting results when using the other functions so I returned to writing bytes in the correct order and that works.
I used a Hex editor extensively while debugging this. I can recommend you do the same. Also, the header in the answer above works, I used it to check versus my own code and this header is rather foolproof.
根据标题,我遵循了这段代码(如果它对您有帮助的话)。
As per the header is concern, I had followed this code (if it's helps you some way).
您确定字节顺序吗? “RIFF”、“WAV”、“fmt”和“data”看起来不错,但标头中的数字可能需要不同的顺序(小端序与大端序)。您也不需要使用
intToByteArray
方法手动转换为字节。您可以使用DataOutputStream
的writeInt
和writeShort
方法。对于第一个,这看起来像:outFile.writeInt(Integer.reverseBytes((int)myChunkSize));
对于短裤,这看起来像:
outFile.writeShort(Short .reverseBytes((short)myFormat))
这样您也不需要提供偏移量和长度
(0, 4)
数字。很好。Are you certain of the byte order? "RIFF", "WAV", "fmt", and "data" look fine but the numbers in the header may need to be a different order (little endian vs. big endian). You also don't need to convert to bytes manually using your
intToByteArray
method. You could use thewriteInt
andwriteShort
methods ofDataOutputStream
. For the first one, this would look something like:outFile.writeInt(Integer.reverseBytes((int)myChunkSize));
For the shorts it'd be like:
outFile.writeShort(Short.reverseBytes((short)myFormat))
This way you also don't need to provide the offset and length
(0, 4)
numbers. It's nice.正如 Ronald Kunenborg 正确指出的那样,问题是小端/大端转换。
最简单的方法是编写一个像这样的短助手:
如果您使用 Android 将音频录制到波形文件并且您也需要短数组,这将非常有用。
(来源:https://stackoverflow.com/a/1394839/1686216)
As Ronald Kunenborg correctly states the problem is the Litte Endian / Big Endian conversion.
The simplest way is to write a short helper like this:
This is very helpful if you record audio to a wave file with Android and you're in need of the short array, too.
(Credits: https://stackoverflow.com/a/1394839/1686216)
以下是创建具有正确标头的 Little Endian 格式的 .wav 文件的示例。然后它会附加指定持续时间(以秒为单位)的空音频数据。在您的情况下,您想附加录制的音频。
The following is an example that creates a .wav file in Little Endian format with a proper header. It then appends empty audio data for a specified duration in seconds. In your case you want to append you recorded audio.