How to preload sounds?

Posted By: Schubido

How to preload sounds? - 01/02/11 21:46

Hi,

I'm doing my first try with sound in 3DGS V8.10.
If the sound is played the first time the game jitters a bit. I guess that happens because the sound file is loaded at the first use.
Is there any possibility to automatically preload sounds?
Currently I'm doing a work-around by playing all sounds one time with sound level 0 before the game starts. That works fine, but does not seem to be a smart way to do it.
Any better ideas?
Posted By: Saturnus

Re: How to preload sounds? - 01/02/11 22:26

Do you use media_play()?
As far as I know, you can't preload sounds for media_play().

You could use snd_play() as an alternative, though. It plays wav and ogg files. There shouldn't be any delay when using this function.
You can also create your sound objects to be played back by snd_play() during runtime (snd_create) and destroy them, when you don't need them anymore (ptr_remove).
Posted By: Schubido

Re: How to preload sounds? - 01/03/11 08:03

Thanks for your reply.
I use snd_play and of course there is a delay at the first call of snd_play for each sound (~0.5 sec) regardless if the sound was defined as global <SOUND* = "filename.wav"> or created with snd_create.
Posted By: Saturnus

Re: How to preload sounds? - 01/03/11 08:12

Hmm, according to the manual there shouldn't be a delay: "SOUND* name = "filename"; ... Defined sounds are permanently stored in memory and can be played or looped without initial delay."
I don't know why it doesn't work (for you? for all?), though.
Posted By: DJBMASTER

Re: How to preload sounds? - 01/03/11 09:06

hmm yes this is strange, I thought it was only streaming media that had a delay.
Is it all sounds that have a delay or a select few?
Posted By: Schubido

Re: How to preload sounds? - 01/03/11 10:17

I reproduced the behaviour in a short example.
It uses "fisch1.mdl", "explo.wav", "explosin.wav", "explosion.wav" which I found in the 3DGS samples.
It does a simple animation of the model (could be any model) and plays a sound if you press a number key (1-3).
It turned out, that the delay is only the first time any sound is played, not for each sound. So it should not occur in the final version of the game, if a intro sound is played.

If you want to reproduce the effect, here is the code :


///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <mtlFX.c>
///////////////////////////////

SOUND* Sound1 = "explo.wav";
SOUND* Sound2 = "explosin.wav";
SOUND* Sound3 = "explosion.wav";

// do some silly movements ...
action AnimateFish()
{
var degree = 0;
var movePercent = 0;

while (1)
{
degree+=time_step*5;
movePercent += time_step*30;
me.x = 100*sin(degree);
me.y = 100*cos(degree);
me.pan = -degree;
ent_animate(me,"",movePercent,ANM_CYCLE);
wait(1);
}
}

function Sound()
{
while(1)
{
if (key_1)
{
snd_play(Sound1,50,0);
}
if (key_2)
{
snd_play(Sound2,50,0);
}
if (key_3)
{
snd_play(Sound3,50,0);
}
wait(1);
}
}


function main()
{
video_screen = 1; // full screen
preload_mode = 3; // preload entities
level_load(NULL);
camera.x = -300;
camera.z = 100;
camera.tilt = -20;
ent_create("fisch1.mdl", vector(0,0,0), AnimateFish);
Sound();
}
Posted By: 3dgs_snake

Re: How to preload sounds? - 01/03/11 11:23

Hi!
I think that it's not a problem with the function. It is just that if the keys remain pressed, it will cause the sound to be played again and again until you release the keys. There are many solutions but one example is to wait for the release of a key to play the sound (or play the sound on key press and wait until the release of the key).

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <mtlFX.c>
///////////////////////////////

#define PRAGMA_PATH "C:\\Program Files\\GStudio7\\templates\\sounds";

SOUND* Sound1 = "explo.wav";
SOUND* Sound2 = "explosin.wav";
SOUND* Sound3 = "explosion.wav";

// do some silly movements ...
action AnimateFish()
{
	var degree = 0;
	var movePercent = 0;
	
	while (1)
	{
		degree+=time_step*5;
		movePercent += time_step*30;
		me.x = 100*sin(degree);
		me.y = 100*cos(degree);
		me.pan = -degree;
		ent_animate(me,"",movePercent,ANM_CYCLE);
		wait(1);
	}
}

function Sound()
{
	while(1)
	{
		// Play after a key was released
		if (key_1)
		{
			while (key_1) {wait(1);}
			snd_play(Sound1,50,0);
		}
		// Play on key press and wait until the key is released
		if (key_2)
		{
			snd_play(Sound2,50,0);
			while (key_2) {wait(1);}
		}
		if (key_3)
		{
			snd_play(Sound3,50,0);
		}
		wait(1);
	}
}


function main()
{
	//video_screen = 1;	 // full screen
	preload_mode = 3; // preload entities
	level_load(NULL);
	camera.x = -300;
	camera.z = 100;
	camera.tilt = -20;
	ent_create("fisch1.mdl", vector(0,0,0), AnimateFish);
	Sound();
}


Posted By: Schubido

Re: How to preload sounds? - 01/03/11 13:25

No, it does not matter if the sound is played several times in parallel. Even keepeng the key pressed for some seconds does not result in a further delay - it just creates an enoying sound ;-)
But there is still a delay at the first snd_play call.
The example was just created to show the effect. In the original code the sound is triggered by a hit event.
However - calling snd_play once before the game starts solves the problem.
Posted By: 3dgs_snake

Re: How to preload sounds? - 01/03/11 13:41

Oh! Sorry blush! But here, i think I don't have any delay.
© 2024 lite-C Forums