Get the real length of Programmer sound

,

Hello everyone,

I have an issue with getting the real length of a programmers sound with FMOD in Unreal Engine.

The blueprint node “GetLength” and the “Sound->getLength()” always return 2000 ms even if the sound is longer than this.

so I searched in to the forum and I found this:

So I decided to do this:

in MyClass.cpp I wrote this:
void MyClass::PlayLineSound()
{

	if(FMODAudioComponent)
	{
		FMODAudioComponent->Stop();
		FMODAudioComponent->SetEvent(GetFMODEvent());
		FMODAudioComponent->SetProgrammerSoundName(GetProgrammerSoundName());
		FMODAudioComponent->StudioInstance->setCallback(&MyClass::MyCallback, FMOD_STUDIO_EVENT_CALLBACK_SOUND_PLAYED);
		FMODAudioComponent->Play();
	}
}

and then this is the callback:

FMOD_RESULT F_CALLBACK MyClass::MyCallback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters)
{
	unsigned int length;

	FMOD::Sound* Sound = static_cast<FMOD::Sound*>(parameters);
	Sound->getLength(&length, FMOD_TIMEUNIT_MS);

	CurrentSoundDuration = static_cast<int32>(length);
	return FMOD_OK;
}

but when I print out my “CurrentSoundDuration” I always get 2000 ms, even if the sound is 5 seconds long.

Am I doing something wrong? Can you help me?

Thanks.

It looks like you are casting the callbacks parameters directly to an FMOD::Sound where you should be casting it to FMOD_STUDIO_PROGRAMMER_SOUND_PROPERTIES.

There is a programmer sound example included in the API download (available from the download page), and you can also see an example in our UE4 Integration in function EventCallbackCreateProgrammerSound of the FMODAudioComponent.cpp.

Hello again,

I gave a look at the FMODAudioComponent.cpp, so I modified my code like so:

void MyClass::PlayLineSound()
{
	if(FMODAudioComponent)
	{
		FMODAudioComponent->Stop();
		FMODAudioComponent->SetEvent(GetFMODEvent());
		FMODAudioComponent->SetProgrammerSoundName(GetProgrammerSoundName());
		FMODAudioComponent->StudioInstance->setCallback(&MyClass::MyCallback, FMOD_STUDIO_EVENT_CALLBACK_SOUND_PLAYED);
		FMODAudioComponent->Play();
	}
}

and then this is the callback (in the .cpp file):

FMOD_RESULT F_CALLBACK MyClass::MyCallback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters)
{
	unsigned int length;

	GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Blue, "Callback Called");
	
	FMOD_STUDIO_PROGRAMMER_SOUND_PROPERTIES* SoundProps = static_cast<FMOD_STUDIO_PROGRAMMER_SOUND_PROPERTIES*>(parameters);
	FString SoundName = SoundProps->name;
	FMOD::Studio::System *System = GetStudioModule().GetStudioSystem(EFMODSystemContext::Max);

	FMOD::Sound *Sound = nullptr;
	FMOD::System *LowLevelSystem = nullptr;
	System->getCoreSystem(&LowLevelSystem);
	FString SoundPath = SoundName;
	if (FPaths::IsRelative(SoundPath))
	{
		SoundPath = FPaths::ProjectContentDir() / SoundPath;
	}
	FMOD_MODE SoundMode = FMOD_LOOP_NORMAL | FMOD_CREATECOMPRESSEDSAMPLE | FMOD_NONBLOCKING;
	LowLevelSystem->createSound(TCHAR_TO_UTF8(*SoundPath), SoundMode, nullptr, &Sound);
	Sound->getLength(&length, FMOD_TIMEUNIT_MS);
	CurrentSoundDuration = static_cast<int32>(length);
	return FMOD_OK;
}

this is the declaration in the .h file

static FMOD_RESULT F_CALLBACK MyCallback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters);

The problem I noticed now is that the callback never gets called.

What is happening?

Thanks

The FMOD_STUDIO_EVENT_CALLBACK_SOUND_PLAYED callback does not get triggered until a sound is played in the event, but you are trying to create the sound in the callback. You will want to use another callback type, like FMOD_STUDIO_EVENT_CALLBACK_CREATE_PROGRAMMER_SOUND.