Low Level API on Unity

Hi,
I am trying to use the Low Level API on Unity. However when I play there is no sound at all.

  • I have disabled Unity Audio in Project Sett

private void InitFMODLowLevel()
{
FMOD.Factory.System_Create(out FMODsystem);
FMODsystem.setOutput(FMOD.OUTPUTTYPE.WASAPI);
FMODsystem.init(512, FMOD.INITFLAGS.NORMAL, System.IntPtr.Zero);
}

void PlaySoundLowLevel(float noteFreq, float duration )
{

    FMOD.CREATESOUNDEXINFO sInfo = new FMOD.CREATESOUNDEXINFO();
    sInfo.length = (uint)duration; //sound.Length
    sInfo.format = FMOD.SOUND_FORMAT.PCMFLOAT;
    sInfo.defaultfrequency = 44100;
    sInfo.numchannels = 1;
    sInfo.cbsize = Marshal.SizeOf(typeof(FMOD.CREATESOUNDEXINFO));

    FMOD.Sound FMODSound;
    byte[] Signal= CreateSound( noteFreq, duration);
    FMODsystem.createSound(Signal, FMOD.MODE.DEFAULT,ref sInfo,out FMODSound);


    FMOD.ChannelGroup masterChannelGroup;
    FMODsystem.getMasterChannelGroup(out masterChannelGroup);

    FMODsystem.playSound(FMODSound, masterChannelGroup, false, out FMODChannel);
     
}

Could you please tell me where could be the problem? I receive no error on the console but there is no sound.

Firstly, I’d recommend you check the error codes returned from each FMOD function, that should give you an idea where your error is.

Looking at your sample, I’d say createSound is failing because you need to pass in FMOD.MODE.OPEN_MEMORY.

I tried OPEN_MEMORY and OPENUSER, I get errors with all different combinations from createSound

FmodERRCheck(FMODsystem.createSound(Signal, (FMOD.MODE.OPENUSER| FMOD.MODE.OPENMEMORY), ref sInfo, out FMODSound));

Signal is a byte[] generated sinewave.

void PlaySoundLowLevel(float noteFreq, float duration )
    {

        FMOD.CREATESOUNDEXINFO sInfo = new FMOD.CREATESOUNDEXINFO();
        sInfo.length = (uint)duration; //sound.Length
        sInfo.format = FMOD.SOUND_FORMAT.PCMFLOAT;
        sInfo.defaultfrequency = 44100;
        sInfo.numchannels = 1;
        sInfo.cbsize = Marshal.SizeOf(typeof(FMOD.CREATESOUNDEXINFO));

        FMOD.Sound FMODSound;
        byte[] Signal= CreateSoundSignal( noteFreq, duration);



        FmodERRCheck(FMODsystem.createSound(Signal, (FMOD.MODE.OPENUSER| FMOD.MODE.OPENMEMORY), ref sInfo, out FMODSound));



        ///Channel configuration
        FMOD.ChannelGroup masterChannelGroup;
        FmodERRCheck(FMODsystem.getMasterChannelGroup(out masterChannelGroup));
        

        FMOD.Channel FMODChannel; 
        FmodERRCheck(FMODsystem.playSound(FMODSound, masterChannelGroup, false, out FMODChannel));

    }

    public static byte[] CreateSoundSignal(float noteFreq, float time)
    {
        int sampleFreq = 44100;
        int sampleTime = Mathf.RoundToInt(sampleFreq * time);

        float[] samples = new float[sampleTime];
        for (int i = 0; i < samples.Length; i++)
        {
            samples[i] = Mathf.Sin(Mathf.PI * 2 * i * noteFreq / sampleFreq);
        }
        byte[] byteSamples = new byte[samples.Length * sizeof(float)];

        Buffer.BlockCopy(samples, 0, byteSamples, 0, byteSamples.Length);

        return byteSamples;
    }

Instead of OPENUSER, try OPENRAW.
So FMOD.MODE.OPENRAW | FMOD.MODE.OPENMEMORY