如何将音频文件转换为字节数组

时间:2017-03-26 21:12:57

标签: java arrays file audio

我想将音频文件转换为字节数组。我目前这样做了,我想知道它是否有效:

    private static AudioFormat getFormat() {
    float sampleRate = 44100;
    int sampleSizeInBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = true;
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
            bigEndian);
}

public static byte[] listenSound(File f) {

    AudioInputStream din = null;
    AudioInputStream outDin = null;
    PCM2PCMConversionProvider conversionProvider = new PCM2PCMConversionProvider();

    try {
        AudioInputStream in = AudioSystem.getAudioInputStream(f);
        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    16,
                    baseFormat.getChannels(),
                    baseFormat.getChannels() * 2,
                    baseFormat.getSampleRate(),
                    false);

        din = AudioSystem.getAudioInputStream(decodedFormat, in);

        if (!conversionProvider.isConversionSupported(getFormat(), decodedFormat)) {
            System.out.println("Conversion Not Supported.");
            System.exit(-1);
        }

        outDin = conversionProvider.getAudioInputStream(getFormat(), din);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int n = 0;
        byte[] buffer = new byte[1024];

        while (true) {
            n++;
            if (n > 1000)
                break;

            int count = 0;
            count = outDin.read(buffer, 0, 1024);

            if (count > 0) {
                out.write(buffer, 0, count);
            }
        }
        in.close();
        din.close();
        outDin.close();
        out.flush();
        out.close();

        //byte[] b=out.toByteArray();
        //for(int i=0; i<b.length; i++)
        //System.out.println("b = "+b[i]);
        return out.toByteArray();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

该字节数组实际上称为时域,我需要确定它是否有效,然后才能使用离散傅立叶将此数据转换为频域。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

通常,处理的实际字节数是从

之类的调用中返回的
count = outDin.read(buffer, 0, 1024);

所以除了你处理1000个块之后当前的硬中断,如果API实际上返回了一个字节数,你应该检查它:

int size_chunk = 1024
byte[] buffer = new byte[size_chunk];
boolean keep_streaming = true;
while (keep_streaming) {
    n++;
    if (n > 1000) {  // troubleshooting ONLY remove later

        keep_streaming = false;
    }

    int count = 0;
    count = outDin.read(buffer, 0, size_chunk);

    if (count > 0) {
        out.write(buffer, 0, count);
    }

    if (count < size_chunk) {  //  input stream has been consumed
        keep_streaming = false;
    }
}

您没有提供API文档的链接,所以我无法确认,但假设outDin.read将输出实际处理的字节数,上面的代码将正确输出仅匹配输入的字节,因此将导致如果输入小于1兆的数据,则输出较小(原始逻辑盲目生成1 meg输出仅在看到1000个块后停止...它还假设您打算在按照行的1兆数据后截断输入

    if (n > 1000) {  // troubleshooting ONLY remove later

        keep_streaming = false;
    }