- Home /
About Array to AudioClip
I am trying to convert byte array / float array to an AudioClip but i'm getting in trouble. I got a byte array after decryption and I successfully got a float array after conversion.
Here is my code:
public AudioClip getAudioClip(byte[] b)
{
File.WriteAllBytes("test.mp3", b); // for testing
float[] f = ConvertByteToFloat(b);
AudioClip ac = AudioClip.Create("test", f.Length, 1, 44100, false, false);
ac.SetData(f, 0);
return ac;
}
Finally, I got an AudioClip with noise...
I have tried to extract what i got by using "File.WriteAllBytes", the output sound is same with the original mp3 file. Also I have compared the float array with the original one and they are no different. So I think there must have been some kind of mistake on creating the AudioClip... I'm not sure...
Please Help
perhaps i'm missing something, but you don't seem to specify what the problem is. is it that the AudioClip
contains noise when it shouldn't?
what does ConvertByteToFloat()
do?
your byte data is 0-255, but the floats required for SetData()
are -1.0f to 1.0f - do you transpose them?
Thanks for your comment. I am new on using AudioClip. Yes, the AudioClip is just contain noise.
About the "ConvertByteToFloat" :
public float[] ConvertByteToFloat(byte[] array)
{
float[] data = new float[array.Length / 4];
System.Buffer.BlockCopy(array, 0, data, 0, array.Length);
return data;
}
How to modify it to achieve the require? Sorry for bothering you.
typically, if it's an 8bit sample, it's likely to be signed so you need to map values from -7F thru 7F to -1.0f thru 1.0f - not just convert the byte numbers into floats.
ultimately, it depends on the format of your byte array and where you get that data...
Your answer
![](https://koobas.hobune.stream/wayback/20220612062054im_/https://answers.unity.com/themes/thub/images/avi.jpg)