转换字节序后,将16BitPCM转换为.wav,.wav文件将向后播放 [英] Converting 16BitPCM to .wav, after switching endianness, the .wav file plays backwards

查看:84
本文介绍了转换字节序后,将16BitPCM转换为.wav,.wav文件将向后播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个可记录PCM音频并将其导出为wav文件的Android应用.

I am trying to build an Android app that records PCM audio and exports it as a wav file.

对于8BitPCM来说,它工作正常,但是当我切换到16BitPCM时,出现白噪声.

It worked fine for 8BitPCM, but when I switched to 16BitPCM I got white noise.

我终于弄清楚这是字节数组的字节序,但是现在,从Little Endian转换为Big Endian之后,我的音频清晰了,但是反转了!

I finally figured out it was the endianness of the byte array, but now, after converting from Little Endian to Big Endian, I get my audio crystal clear, but reversed!

这是我调用方法的方式:

Here is how I call the method:

byte [] inputByteArray = convertLittleEndianToBig(readToByte(input));

,然后将该byte []附加到我的.wav标头中:

and then that byte[] is appended to my .wav header here:

        OutputStream os;
        os = new FileOutputStream(output);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        DataOutputStream outFile = new DataOutputStream(bos);

        // Adding header here...

        outFile.write(inputByteArray);

convertLittleEndianToBig():

convertLittleEndianToBig():

   public static byte[] convertLittleEndianToBig(byte[] value) {
    final int length = value.length;
    byte[] res = new byte[length];
    for(int i = 0; i < length; i++) {
        res[length - i - 1] = value[i];
    }
    return res;
}

和.... readToByte():

and.... readToByte():

public static byte[] readToByte(File file) throws IOException, FileNotFoundException {
    if (file.length() < MAX_FILE_SIZE && file.length() != 0L) {
        ByteArrayOutputStream ous = null;
        InputStream ios = null;
        try {
            byte[] buffer = new byte[4096];
            ous = new ByteArrayOutputStream();
            ios = new FileInputStream(file);
            int read = 0;
            while ((read = ios.read(buffer)) != -1) {
                ous.write(buffer, 0, read);
            }
        } finally {
            try {
                if (ous != null)
                    ous.close();
            } catch (IOException e) {
            }

            try {
                if (ios != null)
                    ios.close();
            } catch (IOException e) {
            }
        }
        return ous.toByteArray();
    }
    else {
    return new byte[0];
    }

太奇怪了,声音听起来完全正确,但向后倒.

So weird that the audio sounds exactly right, but backwards.

如果我删除了对"convertLittleEndianToBig()"的调用,我又回到了白噪声的静止状态.

If I remove the call to "convertLittleEndianToBig()" I am back to white noise static.

感谢您的帮助.这是我的第一个真实项目.

Thanks for any help. This is my first real project.

推荐答案

我是个白痴-16位!=一个字节.

I'm an idiot - 16 bits != a byte.

当我应该反转一个短数组时,我正在反转字节数组.

I was reversing the byte array when I should have been reversing a short array.

我最终将LittleEndianToBig替换为:

I ended up replacing LittleEndianToBig with:

public static short[] convertLittleBytesToBigShorts(byte[] value) {

    short[] shorts = new short[value.length/2];
    ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);

    return shorts;
}

以及带有以下内容的写命令:

and the write command with:

for (int i = 0; i < inputByteArray.length; i++)
        {
            outFile.writeShort(inputByteArray[i]);
        }

我会清理的,但这就是问题所在.我的音频现在是正确的.

I'll clean it up, but that was the issue. My audio is correct now.

这篇关于转换字节序后,将16BitPCM转换为.wav,.wav文件将向后播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆