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

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
Igor
  • 376
  • 1
  • 6
  • 14
  • 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 '13 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 '13 at 11:03

2 Answers2

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;
jaket
  • 9,140
  • 2
  • 25
  • 44
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;
}
fibriZo raZiel
  • 894
  • 11
  • 10
  • 1
    Thanks for this. I wonder why Unity doesn't have a "SoundConversion" class to parallel the "ImageConversion" class?? – Domarius Feb 12 '23 at 01:35
  • 1
    So glad to help developers struggling with the same challenges :). You might want to peek the full implementation [here](https://stackoverflow.com/a/68965193/1934546). Hope it helps ;) – fibriZo raZiel Feb 13 '23 at 09:55
  • 1
    Thanks man! This helps if you want to get instant refreshes without rebuilding assets. – Domarius Feb 15 '23 at 13:39