Getting sound names from Event Callbacks

Hi! I’m trying to access sound names through an Event Callback. I have seen this post, but am struggling to get a usable string out of the Sound.getName() function. Right now when I run my code I either get a blank string or gibberish back, seemingly randomly. I’m using the code found in the timeline callback example page as a base, and the code I’m using to attempt to get the name is this (adding a case to the switch in the BeatEventCallback() method):

case FMOD.Studio.EVENT_CALLBACK_TYPE.SOUND_PLAYED:
                    {

                        var parameter = (FMOD.Sound)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Sound));
                        FMOD.Sound sound = parameter;
                        sound.getName(out string name, 512);
                        Debug.Log("Event Name " + name);
                        timelineInfo.soundName = name;

                    }
                    break;

I have tried using FMOD.Studio.SOUND_INFO instead of FMOD.Sound as well to similar results.

I’m trying to display the names of the sound in my Unity project like this: Current bass playing: Bass_1, Current melody playing: Melody_3 etc. Any help regarding this is much appreciated!

The callback’s FMOD.Sound.getName() might require a bit of extra work. Could you try the following:

var parameter = (FMOD.Sound)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Sound));
FMOD.Sound sound = parameter;
int nameLen = 1024;
var name = new System.Text.StringBuilder(nameLen);
sound.getName(out name, nameLen);
Debug.Log("Event Name " + name);
timelineInfo.soundName = name;

Hi! Sorry for the late reply, the notification got sent to my spam folder and I missed it!

This solution still returned garbled strings. I had to modify the code a bit as I had to cast the StringBuilder to a string. This is the code I ended up using:

var parameter = (FMOD.Sound)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Sound));
FMOD.Sound sound = parameter;
int nameLen = 1024;
var name = new System.Text.StringBuilder(nameLen).ToString();
sound.getName(out name, nameLen);
Debug.Log("Event Name " + name);
timelineInfo.soundName = name;

Here is a screenshot of the kind of Debug Logs I get when using this code:

I am getting one log for each multi-instrument in the event, but as you can see the strings are not very usable. The strings are usually different each time the Callback is called, although sometimes they are the same, even across multiple repeats. However, this randomness doesn’t seem to be tied to the sounds themselves, as the chances of these strings being the same or different seems similar regardless if there is one or multiple sounds within each multi-instrument.

This result is similar or the same to my original code.

Thanks for taking your time to help me out!

I’ve spoken with our dev team who said that the PtrToStructure and StringBuilder parts might not be necessary. Could you try the following and see if it helps:

case FMOD.Studio.EVENT_CALLBACK_TYPE.SOUND_PLAYED:
{
    FMOD.Sound sound = new FMOD.Sound(parameter);
    string name;
    sound.getName(out name, 1024);
    Debug.Log("Event Name " + name);
    timelineInfo.soundName = name;
}
break;

How or where should I define the parameter variable?

Ok the code posted in Richard’s last post works, you just need to replace parameter with parameterPtr (if you’re using the example posted in my first post).

Here is the code that works:

case FMOD.Studio.EVENT_CALLBACK_TYPE.SOUND_PLAYED:
{
    FMOD.Sound sound = new FMOD.Sound(parameterPtr);
    string name;
    sound.getName(out name, 1024);
    Debug.Log("Event Name " + name);
    timelineInfo.soundName = name;
}
break;

Thanks for taking the time to help me out!