gpt4 book ai didi

java - Xuggler 中的 BigEndian、LittleEndian 混淆

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:52 26 4
gpt4 key购买 nike

之前我提出了一个关于将 byte[] 转换为 short[] 的问题,我遇到的一个新问题是转换/不转换来自 byte 的数据[] 到 BigEndian。
这是正在发生的事情:

  • 我正在使用 TargetDataLine 将数据读入 byte[10000]
  • AudioFormat 对象将 BigEndian 任意设置为 true
  • 这个 byte[] 需要转换成 short[] 才能使用 Xuggler 编码
  • 我不知道 AudioFormat BigEndian 应该设置为 true 还是 false。
    我已经尝试了这两种情况,并且在这两种情况下都出现了异常。
    要将 byte[] 转换为 short[],我这样做:

    fromMic.read(tempBufferByte, 0, tempBufferByte.length);
    for(int i=0;i<tempBufferShort.length;i++){
    tempBufferShort[i] = (short) tempBufferByte[i];
    }

    地点:
    fromMicTargetDataLine
    tempBufferbytebyte[10000]
    tempBufferShortshort[10000]
    我得到异常:

       java.lang.RuntimeException: failed to write packet: com.xuggle.xuggler.IPacket@90098448[complete:true;dts:12;pts:12;size:72;key:true;flags:1;stream index:1;duration:1;position:-1;time base:9/125;]

    可能需要的杂项信息:

  • 我如何在 Xuggler 中设置流以添加音频:
  • writer.addAudioStream(0,1,fmt.getChannels(),(int)fmt.getSampleRate());

  • 我如何执行编码
  • writer.encodeAudio(1,tempBufferShort,timeStamp,TimeUnit.NANOSECONDS);

    关于 AudioFormat 的 Java 文档

    ...In addition to the encoding, the audio format includes other properties that further specify the exact arrangement of the data. These include the number of channels, sample rate, sample size, byte order, frame rate, and frame size...

    For 16-bit samples (or any other sample size larger than a byte), byte order is important; the bytes in each sample are arranged in either the "little-endian" or "big-endian" style.

    问题:

  • 我是否需要在 javax.sound.sampled.AudioFormat 对象中将 BigEndian 保持为 true

  • 是什么导致了错误?是格式吗?



  • 我想我得到了由 AudioFormat 对象预格式化的 BigEndian 数据。

    最佳答案

    如果你的数据确实是big endian,你可以像这样直接将它转换成一个(big endian)短数组:

    ByteBuffer buf = ByteBuffer.wrap(originalByteArray);
    short[] shortArray = buf.asShortBuffer().array();

    生成的 short 数组将直接包含所有原始 byte 数组,并且正确映射,前提是您的数据是大端。所以,一个原始数组,例如:

    // bytes
    [00], [ae], [00], [7f]

    将转换为:

    // shorts
    [00ae], [007f]

    关于java - Xuggler 中的 BigEndian、LittleEndian 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14033778/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com