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
1
1
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);
    }
}
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.