0

所以我正在将浮点 32 位转换为立体声中的 16 位。由于我自己并不完全理解,遗憾的是它几乎是复制粘贴。

但我想知道它是否可以改进,无论是质量还是速度?并不是说它们中的任何一个都是可怕的或任何东西。

    void SendWaloop(object sender, NAudio.Wave.WaveInEventArgs e)
    {

        byte[] newArray16Bit = new byte[e.BytesRecorded / 2];
        short two;
        float value;
        for (int i = 0, j = 0; i < e.BytesRecorded; i += 4, j += 2)
        {
            value = (BitConverter.ToSingle(e.Buffer, i));
            two = (short)(value * short.MaxValue);

            newArray16Bit[j] = (byte)(two & 0xFF);
            newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF);
        }
        if (connect == true && MuteMic.Checked == false)
        {
            udpSend.Send(newArray16Bit, newArray16Bit.Length, otherPartyIP.Address.ToString(), 1500);
        }

    }

很好,它将缓冲区从 32 位转换为 16 位,并使用 UDP 发送,没什么奇怪的。

虽然对我来说这看起来很复杂,但据我了解,它只是删除每 4 个字节或类似的东西。

编辑:

        unsafe
        {
            byte[] newArray16Bit = new byte[e.BytesRecorded / 2];
            fixed (byte* sourcePtr = e.Buffer)
            fixed (byte* targetPtr = newArray16Bit)
            {
                float* sourceTyped = (float*)sourcePtr;
                short* targetTyped = (short*)targetPtr;

                int count = e.BytesRecorded / 4;
                for (int i = 0; i < count; i++)
                {
                    targetTyped[i] = (short)(sourceTyped[i] * short.MaxValue);
                }
            }

            if (connect == true && MuteMic.Checked == false)
            {
                udpSend.Send(newArray16Bit, newArray16Bit.Length, otherPartyIP.Address.ToString(), 1500);
            }
        }
    }
4

1 回答 1

2

它需要测试,但我可能会尝试一些unsafe

fixed(byte* sourcePtr = e.Buffer)
fixed(byte* targetPtr = newArray16Bit)
{
    float* sourceTyped = (float*)sourcePtr;
    short* targetTyped = (short*)targetPtr;

    int count = e.BytesRecorded / 4;
    for(int i = 0 ; i < count ; i++)
    {
        targetTyped[i] = (short)(sourceTyped[i] * short.MaxValue);
    }
}

显示相同的工作方式:

using System;
static class Program
{
    static void Main()
    {
        byte[] raw1 = new byte[64 * 1024];
        new Random(12345).NextBytes(raw1); // 64k of random data
        var raw2 = (byte[])raw1.Clone(); // just to rule out corruption
        var result1 = OriginalImplFromTopPost(raw1, raw1.Length - 20);
        var result2 = MyImpl(raw2, raw2.Length - 20);

        bool areSame = Convert.ToBase64String(result1) == Convert.ToBase64String(result2);
        Console.WriteLine(areSame); // True
    }

    public static unsafe byte[] MyImpl(byte[] source, int byteCount)
    {
        byte[] newArray16Bit = new byte[byteCount / 2];
        fixed (byte* sourcePtr = source)
        fixed (byte* targetPtr = newArray16Bit)
        {
            float* sourceTyped = (float*)sourcePtr;
            short* targetTyped = (short*)targetPtr;

            int count = byteCount / 4;
            for (int i = 0; i < count; i++)
            {
                targetTyped[i] = (short)(sourceTyped[i] * short.MaxValue);
            }
        }
        return newArray16Bit;
    }

    public static byte[] OriginalImplFromTopPost(byte[] source, int byteCount)
    {
        byte[] newArray16Bit = new byte[byteCount / 2];
        short two;
        float value;
        for (int i = 0, j = 0; i < byteCount; i += 4, j += 2)
        {
            value = (BitConverter.ToSingle(source, i));
            two = (short)(value * short.MaxValue);

            newArray16Bit[j] = (byte)(two & 0xFF);
            newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF);
        }
        return newArray16Bit;
    }
}
于 2013-08-15T12:28:29.747 回答