C++ Set3DPosition for Channel does not alter the channel. (nullptr)

Hello!

I have created a channel and applied it to a sound that gets played on a loop basis.
However, if I change the 3D coordinates for the channel, nothing happens.
I believe that this is due to the fact that there is no channel “creation”.
Every documentation I have found for Fmod simply just uses a channel, they never set it to anything, initializes it to anything. It’s just null.
I can’t find a constructor, I can’t find anything.
However, during my play of the sound, it should retrieve a channel from the audio function, but it doesn’t change the value.

void SoundManager::PlaySound(SoundClass aSound, FMOD::Channel *aChannel, bool isLooping)
{
	if (!isLooping)
	{
		aSound->setMode(FMOD_LOOP_OFF);
	}
	else
	{
		aSound->setMode(FMOD_LOOP_NORMAL);
		aSound->setLoopCount(-1);
	}

	mySystem->playSound(aSound, nullptr, false, &aChannel);
}


void SoundManager::SetChannelAttributes(FMOD::Channel *aChannel, int aX, int aY, int aZ)
{
	FMOD_VECTOR positionVector;
	FMOD_VECTOR velocityVector; // default 0
	FMOD_VECTOR alt_pan_posVector;

	positionVector.x = aX;
	positionVector.y = aY;
	positionVector.z = aZ;

	aChannel->set3DAttributes(&positionVector, &velocityVector, &alt_pan_posVector);
}


void SoundManager::CreateChannel(std::string &aName) // choose group to attach to instead
{
	//mySystem->setOutput(FMOD_OUTPUTTYPE_WAVWRITER);
	//mySystem->setSoftwareFormat(48000, FMOD_SPEAKERMODE_STEREO, 2);

	FMOD::Channel *aChannel = nullptr;
	aChannel->setPaused(true); // start paused

	ChannelObject aChannelObject(aName, aChannel);
	myChannels.Add(aChannelObject);
}

100% new to Fmod.

There is no creation function for FMOD::Channel, internally FMOD has a pool of them, it will return you a handle to one of these objects via System::playSound.

If you change your SoundManager::PlaySound function to take a FMOD::Channel** you can return the pointer System::playSound returns.
After that you can call any Channel APIs you need.

Make sure you regularly call System::update, this should be performed once per game frame.

Also, please check the error codes returned from all FMOD functions.
Calling Channel::setPaused on a nullptr channel will fail.

e.g.
FMOD::Channel *channel = nullptr;
SoundManager::PlaySound(…, &channel, …);
SoundManager::SetChannelAttributes(channel, …);

Thanks a lot! I’ve changed my structure now but it still does not work. However, it does not return any errors (it says OK).
I’ve tried panning, and directly changing the 3D attributes, but always I hear the same audio at the same place. Do I have to create a listener?
It’s only for a 2D game so if SetPan could work that would be great.

void SoundManager::SetChannelAttributes(FMOD::Channel **aChannel, int aX, int aY, int aZ)
{
FMOD_VECTOR positionVector;
FMOD_VECTOR velocityVector; // default 0
FMOD_VECTOR alt_pan_posVector;

positionVector.x = aX;
positionVector.y = aY;
positionVector.z = aZ;

FMOD_RESULT result = (*aChannel)->set3DAttributes(&positionVector, 0, nullptr);
std::cout << result << std::endl;

}

You should leave SetChannelAttributes as FMOD::Channel*, only change PlaySound to FMOD::Channel**. Also I noticed you are passing in SoundClass to System::playSound, you should be passing in an FMOD::Sound*. I highly recommend you take a look at our examples and start with those, copying and modifying as necessary.

This is what my playsound looks like:
FMOD::Channel* SoundManager::PlaySound(SoundClass aSound, bool isLooping)
{
if (!isLooping)
{
aSound->setMode(FMOD_LOOP_OFF);
}
else
{
aSound->setMode(FMOD_LOOP_NORMAL);
aSound->setLoopCount(-1);
}
FMOD::Channel* tempChannel = nullptr;

mySystem->playSound(aSound, nullptr, false, &tempChannel);
return tempChannel;

}

I send in a SoundClass which is typedef for FMOD::Sound* and then retrieve a channel and return it to the user of the function. I then alter that channel with error code “OK”. Still ordinary 1D sound though.

It works now! It was actually just missing a function call to system->update… My bad! However, what settings for the listener is correct if i want to center the listener at the middle of the screen? (in my engine 0.5f, 0.5f)

That was about to be my next suggestion, glad you got it working. As for Channel and Listener positions, they can be set with any units or positions you like, everything is relative.