AudioClip.GetData Equivalent in FMOD?

Hi there :slightly_smiling_face:

I’m currently trying to convert a speech recognition solution work with FMOD audio, rather than the built-in Unity Audio. I’m stumped at a point in the script where the Unity method AudioClip.GetData is used to receive some data. I’m wondering if there is a way to extract the same data from FMOD sounds?
FMOD Help

I’m aware of a method called ReadData in FMOD, but I was, unfortunately, unable to apply it to get the desired values.
I’m a bit new to working this deeply with FMOD, so any help is appreciated :slightly_smiling_face:

Hi,

There is a way to get the sample data from an audio source but there is a bit of setup to do, we will need to create a Sound to read the data from. Here is an example of how:

GetSampleData(string filePath)
    /// <summary>
    /// Return sample data in a byte array from an audio source using its file path 
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
	private byte[] GetSampleData(string filePath)
    {
        // Very useful tool for debugging FMOD function calls
        FMOD.RESULT result;

        // Sound variable to retrieve the sample data from
        FMOD.Sound sound; 

        // Creating the sound using the file path of the audio source 
        // Make sure to create the sound using the MODE.CREATESAMEPLE | MDOE.OPENONLY so the sample data can be retrieved
		result = FMODUnity.RuntimeManager.CoreSystem.createSound(filePath, FMOD.MODE.CREATESAMPLE | FMOD.MODE.OPENONLY, out sound);

        // Debug the results of the FMOD function call to make sure it got called properly
        if (result != FMOD.RESULT.OK)
        {
            Debug.Log("Failed to create sound with the result of: " + result);
            return null; 
        }

        // Retrieving the length of the sound in milliseconds to size the arrays correctly
		result = sound.getLength(out uint length, FMOD.TIMEUNIT.MS);

        if (result != FMOD.RESULT.OK)
        {
            Debug.Log("Failed to retrieve the length of the sound with result: " + result);
            return null; 
        }

        // Buffer which the sample data will be copied too 
        System.IntPtr buffer; 

        // Creating a pointer to newly allocated memory
        // This pointer must be released at the end of the function! 
        buffer = Marshal.AllocHGlobal((int)length * sizeof(byte));
		
        // Creating the return array which will have the sample data is a readable variable type 
        // Using the length of the sound to create it to the right size 
		byte[] byteArray = new byte[(int)length];

        // Retrieving the sample data to the pointer using the full length of the sound 
		result = sound.readData(buffer, length, out uint read);

        if (result != FMOD.RESULT.OK)
        {
            Debug.Log("Failed to retrieve data from sound: " + result);
            return null; 
        }

        // Make sure the pointer has been populated 
		if (_buffer != System.IntPtr.Zero)
		{			
            // Coping the data from our pointer to our usable array 
			Marshal.Copy(buffer, byteArray, 0, (int)length);
		}

        //Releasing the pointer 
        Marshal.FreeHGlobal(buffer); 

        //Returning the array populated with the sample data to be used
		return byteArray;
	}

Hopefully, calling Sound::readData this way will get you the values you are looking for!

Hope this helps!