Random sounds

Posted By: Random

Random sounds - 02/08/10 15:12

Does sombody know how to make random sounds?
I meen here is a normal sound code:


SOUND* snd_hurt = "hurt_1.wav";

function hit_obj_meat()
{
if (event_type == EVENT_SHOOT)
{
snd_play(snd_hurt,55,0);
}
}


But what if when you whant to use random sounds:


snd_play(snd_hurt1,55,0); or snd_play(snd_hurt2,55,0);


How can I do that?
I only wright in lite-c.
Posted By: Joey

Re: Random sounds - 02/08/10 15:17

Code:
SOUND** ptr = (SOUND**)malloc(sizeof(SOUND*)*3);
ptr[0] = snd_hurt1;
ptr[1] = snd_hurt2;
ptr[2] = snd_hurt3;
snd_play(ptr[integer(random(3))], 55, 0);
free(ptr);


Posted By: Random

Re: Random sounds - 02/08/10 16:04

Thanks, but it crashes or I maby writen it wrong:

SOUND* snd_hurt = "hurt_1.wav";
SOUND** ptr = (SOUND**)malloc(sizeof(SOUND*)*3);

ptr[0] = snd_hurt1;
ptr[1] = snd_hurt2;
ptr[2] = snd_hurt3;

function hit_obj_meat()
{
if (event_type == EVENT_SHOOT)
{
snd_play(ptr[integer(random(3))], 55, 0);
free(ptr);
}
}


Posted By: Joey

Re: Random sounds - 02/08/10 16:43

well, it won't even compile that way. you first have to declare the snd_hurt1..3 sounds. and the code has to be written inside of a function. i'd advise you to first read some kind of beginners tutorial on coding.
Posted By: Razoron

Re: Random sounds - 02/08/10 20:44

Maybe:

Code:
SOUND*snd1="shdfkdh.wav";
SOUND*snd2="shdfkfsddh.wav";
SOUND*snd3="shdfksdffddh.wav";

function playrandomsound()
{
   random_seed(0);
   wait(1);
   var i=random(2);
   switch(i)
   {
       case 0:
       snd_play(sound1...);
       case 1:
       snd_play(sound2...);
       case 2:
       snd_play(sound3...);
   }
}


Posted By: Widi

Re: Random sounds - 02/08/10 20:51

@Razoron: That don`t will work, for the switch() you need a integer.

var i=integer(random(3));

Now i can be 0,1 or 2.
Posted By: Redeemer

Re: Random sounds - 02/08/10 21:15

Don't you need "break" statements in that switch case? I'm not accustomed to Lite-C, but in real C you have to put break statements at the end of your cases to prevent the program from going through all of them at run time.
Posted By: JibbSmart

Re: Random sounds - 02/08/10 21:19

Yes.
Posted By: Blackchuck

Re: Random sounds - 02/08/10 21:26

Razoro, youre code (with the var from Widi) doesen`t work becose of the function
"random_seed(0);", but without "random_seed(0);" there is no sound.
And when I wright it like this;

_______________________________________________________________
SOUND*snd1="shdfkdh.wav";
SOUND*snd2="shdfkfsddh.wav";
SOUND*snd3="shdfksdffddh.wav";

var i=integer(random(3));

function playrandomsound()
{
//deleted//
wait(1);
var i=random(2);
switch(1)//It was "switch(i)"
{
case 0:
snd_play(sound1...);
case 1:
snd_play(sound2...);
case 2:
snd_play(sound3...);
}
}
______________________________________________________________
There should be no warnings.
Posted By: Random

Re: Random sounds - 02/08/10 21:32

Blackchuck it doesn`t work.
still thanks
Posted By: Widi

Re: Random sounds - 02/08/10 21:50

Code:
SOUND*snd1="shdfkdh.wav";
SOUND*snd2="shdfkfsddh.wav";
SOUND*snd3="shdfksdffddh.wav";

function playrandomsound()
{
   var i=integer(random(3));
   switch(i)
   {
      case 0:
      snd_play(sound1...); break;
      case 1:
      snd_play(sound2...); break;
      case 2:
      snd_play(sound3...); break;
   }
}




Now you have to call the function playrandomsound if you want to hear your Sound.

Edit: if "random_seed(0);" don`t work, make a update, it is the newer function from randomize.
Posted By: Joey

Re: Random sounds - 02/08/10 22:35

that's like:
A: I need something to move heavy objects more easily.
B: Here's the wheel.
A: It crushes.
B: You have to use it with a cart.
C: I've a better solution: Here's the cube.

wink
Posted By: Widi

Re: Random sounds - 02/08/10 22:40

@Joey: Yes, your solution is the best way, i know. But with the "select" it is better to understand for a beginner i think...

@Blackchuck: If random_seed don`t work, then you don`t have the Version 7.80. As i see in other posts, i think you have a Warez Version.
Posted By: Joey

Re: Random sounds - 02/08/10 22:44

i was not complaining about it ^^ - it's really nice of you to help people out.
Posted By: MrGuest

Re: Random sounds - 02/08/10 23:48

why not just use random sounds in an array like Joey said?

Code:
SOUND* snd_random[10]; //create pointer for 10 sounds (0-9)

//...

void play_random_sound(){
   
   int i = random(10);
   snd_play(snd_random[i], 100, 0); //sound, volume, balance
}

void main(){

   snd_random[0] = snd_create("bang.wav");
   snd_random[1] = snd_create("whizz.wav");
   //...
   snd_random[9] = snd_create("pop.wav");



make sure all sounds are in mono, wav or ogg format

*untested* but 'should' work

hope this helps
Posted By: Joey

Re: Random sounds - 02/09/10 10:17

you have to use integer(i) as index.
Posted By: Helghast

Re: Random sounds - 02/09/10 10:25

Code:
void createNewSnd(STRING* sndStr, int maxNmbr) {
	STRING* tempStr = str_create("");
	int randNum = integer(random(maxNmbr)+1);
	
	str_cpy(tempStr, sndStr);
	str_cat(tempStr, "_");
	str_cat(tempStr, str_for_int(NULL, randNum));
	str_cat(tempStr, ".wav");
	
	SOUND* tempSnd = snd_create(tempStr);
	// play the sound
	var sndHandle = snd_play(tempSnd, 100, 0);
	// wait until finished
	while (snd_playing(sndHandle)) { wait(1); }
	
	// remove the sound
	ptr_remove(sndHandle);
}



you have all your sounds named something like "attack_n.wav", where n is an number from 1 - unlimited.

you call the function like:

Code:
createNewSnd("attack", 5);



And it will get you a random sound chosen from attack_1 .. attack_5.

Tested and works (implemented it in my own game)!

regards,
Posted By: MrGuest

Re: Random sounds - 02/09/10 10:56

Originally Posted By: Joey
you have to use integer(i) as index.
I did laugh
Originally Posted By: MrGuest
int i = random(10);
snd_play(snd_random[i], 100, 0); //sound, volume, balance

Posted By: Joey

Re: Random sounds - 02/09/10 10:58

ah damn implicit type conversion wink. with a good compiler you'd at least get a warning here ^^.
Posted By: Random

Re: Random sounds - 02/09/10 15:31

Widi youre code worked the best, thanks it works now.

Thanks you all, I never say a forum that realey helps so much thanks!
Posted By: Widi

Re: Random sounds - 02/09/10 15:40

You`re welcome
© 2024 lite-C Forums