Normalizing audio, how to convert a float array to a byte array?

后端 未结 5 1838
南旧
南旧 2021-02-04 21:52

Hi all, I am playing an audio file. I read it as a byte[] and then I need to normalize the audio by putting values into range of [-1,1]. I want to then put each flo

5条回答
  •  轮回少年
    2021-02-04 22:29

    You can change temp to a list of byte arrays to avoid overwriting it all the time.

        byte[] data = new byte[] { 1, 3, 5, 7, 9 };  // sample data
        IList temp = new List(data.Length);
        float biggest = 0; ;
    
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] > biggest)
                biggest = data[i];
        }
    
        for (int i = 0; i < data.Length; i++)
        {
            temp.Add(BitConverter.GetBytes(data[i] * (1 / biggest)));
        }
    

提交回复
热议问题