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 :( .

2
  • 1
    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.
    – Max Yankov
    Apr 18, 2013 at 10:38
  • 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 :)
    – Igor
    Apr 18, 2013 at 11:03

2 Answers 2

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;
3
  • And include: using System; Jun 22, 2015 at 16:38
  • What is 0x80000000 ?
    – gameon67
    Feb 8, 2022 at 13:13
  • ...and where will this fit in? I got noise for my base64 mp3. Would you mind posting a more detailed code? Jan 15 at 10:38
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;
}
3
  • 1
    Thanks for this. I wonder why Unity doesn't have a "SoundConversion" class to parallel the "ImageConversion" class??
    – Domarius
    Feb 12 at 1:35
  • 1
    So glad to help developers struggling with the same challenges :). You might want to peek the full implementation here. Hope it helps ;) Feb 13 at 9:55
  • 1
    Thanks man! This helps if you want to get instant refreshes without rebuilding assets.
    – Domarius
    Feb 15 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.