create AudioClip from byte[]
Asked Answered
T

2

18

I have problem. I use sqlite to store sounds. I get sound from it in byte[]. Then convert byte[] to 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);

Then create AudioClip:

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

And then play it

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

But result is noise :( .

Translatable answered 18/4, 2013 at 8:40 Comment(2)
May be going the other way would help you determine the source of the problem? If I was debugging this, I would create the inverse conversion, from audioClip.GetData to byte array. If you'll load up the exact same sample in Unity and use this reverse conversion, you may get a hint at what's going wrong here.Monzon
Thanks, I wanted to try it) I will do that. There is maybe another solution - save bytes[] into file, and then use WWW instance to load AudioClip, but I don't like it :)Translatable
G
9

floatArr needs to be scaled to be within the range of -1.0f to 1.0f.

floatArr[i] = BitConverter.ToSingle(array, i*4) / 0x80000000;
Grommet answered 23/4, 2013 at 23:17 Comment(3)
And include: using System;Aloes
What is 0x80000000 ?Instant
...and where will this fit in? I got noise for my base64 mp3. Would you mind posting a more detailed code?Haft
S
1

In my case, BitConverter.ToSingle didn't work. I had first to read the PCM (*.wav) header (pcmHeader) and then use:

  • Direct cast for 8bit samples.
  • BitConverter.ToInt16 for 16bit samples
  • BitConverter.ToInt32 for 32bit samples
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]
}

Where NormalizeSample scales rawSample using the corresponding max and min values for the given bit depth:

// _negativeDepth = Mathf.Pow(2f, BitDepth - 1f);
// _positiveDepth = _negativeDepth - 1f;
public float NormalizeSample(float rawSample)
{
    float sampleDepth = rawSample < 0 ? _negativeDepth : _positiveDepth;
    return rawSample / sampleDepth;
}
Soft answered 28/8, 2021 at 14:4 Comment(3)
Thanks for this. I wonder why Unity doesn't have a "SoundConversion" class to parallel the "ImageConversion" class??Logogriph
So glad to help developers struggling with the same challenges :). You might want to peek the full implementation here. Hope it helps ;)Soft
Thanks man! This helps if you want to get instant refreshes without rebuilding assets.Logogriph

© 2022 - 2024 — McMap. All rights reserved.