Jump to content

AmbientSource replay


photo

Recommended Posts

We use next pattern: in init() we create an AmbientSource instance. Next we need periodically play it with different delays. If this delay more than sound length all OK (when it must play it already stopped). If this delay less than sound length we need restart sound and play it from start. We do next:

		AmbientSource soundTable[];

		. . .

		if (soundTable[id].isPlaying())
		{
			soundTable[id].setGain(volume);
			soundTable[id].setLoop(loop);
			soundTable[id].stop();

			soundTable[id].setTime(0.0f);
			soundTable[id].play();

			LOG("re-play");
		}

But in this case sound not play. It seems branch stop() / setTime() / play() not working. We hear new created sounds from this array, but old sounds, that already playing not play from start. We used static_wave_01.wav for test.

Link to comment

Usually, the use case scenario of our developers is as follows:

  1. Create all necessary sounds and set their parameters, such as loop, gain, pitch, rolloff (in case of SoundSource), etc.
  2. To rewind the sound, stop() / setTime() / play() are used.

 

Can you provide a test scene for us to reproduce the problem?

Link to comment

UPD. Thank you for a test case. Actually, there is a workaround. The problem in this situation is that play/stop states of the ambient sound are updated only when the engine updates states of world nodes (each frame after script update() function is executed). That is the reason it does not work when you try to change states at one go. Instead, do the following:

 

AmbientSource play(string id, int loop = false, float volume = 1.0f)
{
// In the first update the sound is only stopped and rewound
if (soundTable[id].isPlaying())
{
	soundTable[id].setGain(volume);
	soundTable[id].setLoop(loop);
	soundTable[id].stop();

	soundTable[id].setTime(0.0f);
	//soundTable[id].play();

	log.message("re-play");

	return soundTable[id];
}
// The sound changes its state to playing in the next update
else
{
	soundTable[id].setGain(volume);
	soundTable[id].setLoop(loop);

	soundTable[id].play();

	log.message("play");

	return soundTable[id];
}
}

You will get a one-frame lag before the sound starts to be played, but that's quite a small time interval.

Link to comment
  • 1 month later...

Thanks for explanation.

UPD. Thank you for a test case. Actually, there is a workaround.

I can't use this workaround, because I call play() after some events one times, not in update() method.

 

The problem in this situation is that play/stop states of the ambient sound are updated only when the engine updates states of world nodes (each frame after script update() function is executed).

Strange, AmbientSource isn't inherited from Node class (like SoundSource)...

 

This problem will be solved, or we most try different pattern with continuous update cycle?

Link to comment
×
×
  • Create New...