# Get the real length of Programmer sound

**URL:** https://qa.fmod.com/t/get-the-real-length-of-programmer-sound/16695
**Category:** Unreal Engine
**Tags:** ue4, cpp
**Created:** [January 27, 2021, 1:33pm UTC](https://qa.fmod.com/t/get-the-real-length-of-programmer-sound/16695 "2021-01-27T13:33:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![stormind](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@stormind](https://qa.fmod.com/u/stormind)
#### Post date: [January 27, 2021, 1:33pm UTC](https://qa.fmod.com/t/get-the-real-length-of-programmer-sound/16695/1 "2021-01-27T13:33:50Z")

</div>

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:

> [@Grab Length of AudioClip](https://qa.fmod.com/t/grab-length-of-audioclip/14117):
>
> Hello, so currently I am working on implementing voice lines and want to have my coroutine last only as long as the fmod.studio audio clip and I’m at a complete loss on how to go about that! currently, I’m using c# in unity if that’s any consolation. Please Help

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.

---

<div class="post-metadata">

### Author: ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)
#### Post date: [February 1, 2021, 5:05am UTC](https://qa.fmod.com/t/get-the-real-length-of-programmer-sound/16695/2 "2021-02-01T05:05:56Z")

</div>

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`](https://fmod.com/resources/documentation-api?version=2.1&page=studio-api-eventinstance.html#fmod_studio_programmer_sound_properties).

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

---

<div class="post-metadata">

### Author: ![stormind](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@stormind](https://qa.fmod.com/u/stormind)
#### Post date: [February 8, 2021, 9:49am UTC](https://qa.fmod.com/t/get-the-real-length-of-programmer-sound/16695/3 "2021-02-08T09:49:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)
#### Post date: [February 9, 2021, 3:45am UTC](https://qa.fmod.com/t/get-the-real-length-of-programmer-sound/16695/4 "2021-02-09T03:45:50Z")

</div>

The [`FMOD_STUDIO_EVENT_CALLBACK_SOUND_PLAYED`](https://fmod.com/resources/documentation-api?version=2.1&page=studio-api-eventinstance.html#fmod_studio_event_callback_type) 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`.
