How to mix audio played with FMOD.System.playSound()?

Hey,

In my Unity project, I’m attempting to download a piece of music at runtime and play it. I’ve gotten that part to work, however the playing audio doesn’t seem to be affected by the connected FMOD Studio Mixer like other sounds in the project are. Given the following code, I’d expect the volume of the playing music to be affected when I change the gain on the master bus, but isn’t. Any insight would be appreciated.

Thanks!

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;

public class FMODAudioStreamTest : MonoBehaviour
{
    private FileInfo file;

    // Start is called before the first frame update
    async void Start()
    {
        file = new FileInfo("test.mp3");

        using (var client = new WebClient())
        {
            var uri = new System.Uri("http://www.hyperion-records.co.uk/audiotest/14%20Clementi%20Piano%20Sonata%20in%20D%20major,%20Op%2025%20No%206%20-%20Movement%202%20Un%20poco%20andante.MP3");
            client.DownloadFile(uri, file.FullName);
        }

        FMODUnity.RuntimeManager.CoreSystem.createSound(file.FullName, FMOD.MODE.LOOP_NORMAL, out var sound);
        FMODUnity.RuntimeManager.CoreSystem.getMasterChannelGroup(out var group);
        //FMODUnity.RuntimeManager.GetBus("bus:/Music").getChannelGroup(out var group);
        //FMODUnity.RuntimeManager.CoreSystem.createChannelGroup("ASDFLOL", out var group);
        FMODUnity.RuntimeManager.CoreSystem.playSound(sound, group, false, out var channel);
        channel.setChannelGroup(group);
    }

    private void OnDestroy()
    {
        file?.Delete();
    }
}

Hi,

What version of the FMOD integration are you using?

How are you changing the gain? It may be an issue with using the Core API to play the sound and then using Studio API the adjust the volume.

It looks like it’s version 2.02.20

I’m attempting to change the gain by using the slider in FMOD Studio’s Mixer panel while it’s connected to the Unity game.

Full disclosure, I’m not sure if what I’m trying to do should be working or not. I’m open to any other method to play a downloaded sound, but have it participate in the mix.

1 Like

Hi,

Thank you for the screenshot.

When you create a Sound with the Core API it will not be part of the Studio API master bus shown here using the FMOD Core Profiler:
image.

An alternative solution would be using a Programmer instrument to play the audio file instead. We have a scripting example using a programmer instrument to play an audio file from an Audio Table: Unity Integration | Scripting Examples: Programmer Sounds.

You will have to change the CallBack like this:

[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
static FMOD.RESULT DialogueEventCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)
{
	FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(instancePtr);

	IntPtr stringPtr;
	instance.getUserData(out stringPtr);

	GCHandle stringHandle = GCHandle.FromIntPtr(stringPtr);
	String key = stringHandle.Target as String;

	switch (type)
	{
		case FMOD.Studio.EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND:
			{
				FMOD.MODE soundMode = FMOD.MODE.LOOP_NORMAL | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.NONBLOCKING;
				var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));

				FMOD.Sound dialogueSound;
				var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(key, soundMode, out dialogueSound);
				if (soundResult == FMOD.RESULT.OK)
				{
					parameter.sound = dialogueSound.handle;
					parameter.subsoundIndex = -1;
					Marshal.StructureToPtr(parameter, parameterPtr, false);
				}
				break;
			}
		case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROY_PROGRAMMER_SOUND:
			{
				var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
				var sound = new FMOD.Sound(parameter.sound);
				sound.release();

				break;
			}
		case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROYED:
			{
				stringHandle.Free();
				break;
			}
	}
	return FMOD.RESULT.OK;
}

Now the key is just the path to the audio file.

As the programmer sound is created as part of the Studio API you should be able to control its volume through Live Update.

Hope this helps!

Yep, this ended up working for me. Faffed w/ it a bit to fit my needs. Thanks for the pointer.

1 Like