Answered
0
0

Hey guys,

is there any way to load and Unity AudioClip as an fmod sound like we do with FMOD.System.createSound(“sound.wav”) ? I was trying to convert the audio clip to a bytearray and get just the raw data, but it hasn’t work.

Any other approaches suggested?

Thanks.

  • You must to post comments
Best Answer
2
2
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;

public class clip_to_sound : MonoBehaviour 
{
    public AudioClip audioclip;
    FMOD.Channel channel;

    void Start ()
    {
        float[] samples = new float[audioclip.samples * audioclip.channels];
        audioclip.GetData(samples, 0);

        FMOD.System lowlevel = null;
        FMOD_StudioSystem.instance.System.getLowLevelSystem(out lowlevel);

        uint lenbytes = (uint)(audioclip.samples * audioclip.channels * sizeof(float));

        FMOD.CREATESOUNDEXINFO soundinfo = new FMOD.CREATESOUNDEXINFO();
        soundinfo.length = lenbytes;
        soundinfo.format = FMOD.SOUND_FORMAT.PCMFLOAT;
        soundinfo.defaultfrequency = audioclip.frequency;
        soundinfo.numchannels = audioclip.channels;

        FMOD.RESULT result;
        FMOD.Sound sound;
        result = lowlevel.createSound("", FMOD.MODE.OPENUSER, ref soundinfo, out sound);

        IntPtr ptr1, ptr2;
        uint len1, len2;
        result = sound.@lock(0, lenbytes, out ptr1, out ptr2, out len1, out len2);
        Marshal.Copy(samples, 0, ptr1, (int)(len1 / sizeof(float)));
        if (len2 > 0)
        {
            Marshal.Copy(samples, (int)(len1 / sizeof(float)), ptr2, (int)(len2 / sizeof(float)));
        }
        result = sound.unlock(ptr1, ptr2, len1, len2);

        result = sound.setMode(FMOD.MODE.LOOP_NORMAL);

        result = lowlevel.playSound(sound, null, false, out channel);
    }
}
  • Cameron Baron

    The StudioSystem will already contain a LowLevel System and this will give you a reference to it, System_Create will make a new LowLevel System separate from the one you are already using.

  • Dic
    • Dic
    • 4 months ago

    Thanks!But I can’t create the current sound,it’s handle’s value always equals 0,I don’t know why.

  • Dic
    • Dic
    • 4 months ago

    ps:The parametric “name” of createSound is the path of music likes “d:/AAA/touch.mp3”, Is’t correct ?

  • Cameron Baron

    That is correct.
    This Q&A is for the older legacy Unity Integration for FMOD Ex and will be different for the latest versions, although not a lot will need to change.
    To get the LowLevel system use:

    FMOD.System lowlevelsystem = FMODUnity.RuntimeManager.LowlevelSystem;

  • Dic
    • Dic
    • 4 months ago

    THANKS!!!

Showing 1 result
Your Answer

Please first to submit.