Search Unity

CreateAudioClip

Discussion in 'Audio & Video' started by shakil055, Dec 8, 2014.

  1. shakil055

    shakil055

    Joined:
    Oct 4, 2012
    Posts:
    3
    Hi,
    I am trying to create a audioclip through bytes of array converting to float of array via
    AudioClip.Create function but all i am getting noise of that sound And sound is in the Hard Disk.
    Any Help , Please .
    Thanks.
     
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Please post some code, so we can see where things go wrong.

    Also, if what you need is to load a clip at runtime from the hard drive, it'll be much easier to use the WWW class.

    Cheers,

    Gregzo
     
  3. shakil055

    shakil055

    Joined:
    Oct 4, 2012
    Posts:
    3
    Thanks For Your Answer
    But I already Did that using www and it worked but I want to try this in a different way like below code.



    using UnityEngine;
    using System.IO;
    using System;
    using System.Collections;

    public class LoadMp3 : MonoBehaviour {
    public string FilePathToMp3;
    public AudioClip OwnAudioClip;
    private byte[] bytesArray;
    private float[] floatArray;

    void Start()
    {
    if (!FilePathToMp3.Equals(""))
    {
    bytesArray = File.ReadAllBytes(FilePathToMp3);
    print("Total Bytes: " + bytesArray.Length);
    floatArray = ConvertByteToFloat(bytesArray);
    print("Total Float: " + floatArray.Length);

    OwnAudioClip = AudioClip.Create(Path.GetFileName(FilePathToMp3), floatArray.Length, 1, 44100, false, false);
    OwnAudioClip.SetData(floatArray, 0);
    print("Fre: " + OwnAudioClip.frequency + " Chan: " + OwnAudioClip.channels);
    }
    else
    {
    Debug.LogWarning("File Path Set First");
    }
    if (OwnAudioClip.isReadyToPlay)
    {
    GetComponent<AudioSource>().clip = OwnAudioClip;
    GetComponent<AudioSource>().Play();
    }
    }


    private float[] ConvertByteToFloat(byte[] ArrayOfBytes)
    {
    float[] FloatArrayOf = new float[ArrayOfBytes.Length / 4];
    for (int i = 0; i < FloatArrayOf.Length; i++)
    {
    if (BitConverter.IsLittleEndian)
    {
    Array.Reverse(ArrayOfBytes, i * 4, 4);
    }
    FloatArrayOf = BitConverter.ToSingle(ArrayOfBytes, i * 4) / 0x80000000;
    //if (i < 10)
    //{
    // print("Float Before: " + FloatArrayOf);
    //}
    //FloatArrayOf = Mathf.Clamp(FloatArrayOf, -1.0f, 1.0f);
    //if (i < 10)
    //{
    // print("Float After: " + FloatArrayOf);
    //}
    }
    return FloatArrayOf;
    }

    }
     
  4. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi again,

    Do insert code using the Insert button, it'll be a lot more readable!

    When you use SetData, it is implicit that the data you set is raw pcm data, a float[] where values range from -1f to 1f. If you load a compressed file as a byte array and convert that to a float array, of course you'll get little else than noise.

    Cheers,

    Gregzo
     
  5. shakil055

    shakil055

    Joined:
    Oct 4, 2012
    Posts:
    3
    Hi ,
    Can you help me how to get raw pcm data from mp3 , I just read byte [] array from mp3 and decode to float [] array.
    Thank you for helping me.
     
  6. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    You can't - MP3 is a compressed format. Unless you can get your hands on an MP3 library, you're better off just loading WAV files.
     
  7. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    281