【问题标题】:create AudioClip from byte[]从 byte[] 创建 AudioClip
【发布时间】:2013-04-18 08:40:55
【问题描述】:

我有问题。我使用 sqlite 来存储声音。我在 byte[] 中得到声音。 然后将byte[]转换为float[]:

            private float[] ConvertByteToFloat(byte[] array) 
            {
                float[] floatArr = new float[array.Length / 4];
                for (int i = 0; i < floatArr.Length; i++) 
                {
                    if (BitConverter.IsLittleEndian) 
                        Array.Reverse(array, i * 4, 4);
                    floatArr[i] = BitConverter.ToSingle(array, i * 4);
                }
                return floatArr;
            } 


            float[] f = ConvertByteToFloat(bytes);

然后创建 AudioClip:

    AudioClip audioClip = AudioClip.Create("testSound", f.Length, 1, 44100, false, false);
    audioClip.SetData(f, 0);

然后播放它

AudioSource.PlayClipAtPoint(audioClip, new Vector3(100, 100, 0), 1.0f);

但结果是噪音:( .

【问题讨论】:

  • 可能会反过来帮助您确定问题的根源?如果我正在调试它,我将创建从 audioClip.GetData 到字节数组的逆转换。如果您将在 Unity 中加载完全相同的示例并使用此反向转换,您可能会在此处得到错误提示。
  • 谢谢,我想试试)我会的。可能还有另一种解决方案 - 将 bytes[] 保存到文件中,然后使用 WWW 实例加载 AudioClip,但我不喜欢它:)

标签: c# arrays unity3d endianness


【解决方案1】:

floatArr 需要缩放到 -1.0f 到 1.0f 的范围内。

floatArr[i] = BitConverter.ToSingle(array, i*4) / 0x80000000;

【讨论】:

  • 并包括:using System;
  • 0x80000000 是什么?
【解决方案2】:

就我而言,BitConverter.ToSingle 不起作用。我必须先阅读 PCM (*.wav) 标头 (pcmHeader),然后使用:

  • 直接转换为 8 位样本。
  • BitConverter.ToInt16 用于 16 位样本
  • BitConverter.ToInt32 用于 32 位样本
float[] samples       = new float[pcmHeader.AudioSampleCount];
for (int i = 0; i < samples.Length; ++i)
{
    int   byteIndex = pcmHeader.AudioStartIndex + i * pcmHeader.AudioSampleSize;
    float rawSample;
    switch (pcmHeader.BitDepth)
    {
        case 8:
            rawSample = bytes[byteIndex];
            break;

        case 16:
            rawSample = BitConverter.ToInt16(bytes, byteIndex);
            break;

        case 32:
            rawSample = BitConverter.ToInt32(bytes, byteIndex);
            break;

        default: throw new ArgumentOutOfRangeException(nameof(pcmHeader.BitDepth), pcmHeader.BitDepth, "Supported values are: 8, 16, 32");
    }
    
    samples[i] = pcmHeader.NormalizeSample(rawSample); // normalize sample between [-1f, 1f]
}

其中NormalizeSample 使用给定位深度的相应最大值和最小值缩放rawSample

// _negativeDepth = Mathf.Pow(2f, BitDepth - 1f);
// _positiveDepth = _negativeDepth - 1f;
public float NormalizeSample(float rawSample)
{
    float sampleDepth = rawSample < 0 ? _negativeDepth : _positiveDepth;
    return rawSample / sampleDepth;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2012-01-11
    相关资源
    最近更新 更多