play random sounds at certain viewframes using an audiochannel*...?

Started by daniel, Fri 19/05/2017 20:23:08

Previous topic - Next topic

daniel

hey guys,

another day another question:P

i'm creating audio for a character who's animating in the background.
what i want is to play one of a couple of random sounds at certain frames in the characters view.
this is what i have:

Code: ags
void PlayMomSounds(){

  int RandomHackSound=Random(2);
    
  ViewFrame* MomHackFrame1 = Game.GetViewFrame(26, 0, 2);
  ViewFrame* MomHackFrame2 = Game.GetViewFrame(26, 0, 6);
  ViewFrame* MomHackFrame3 = Game.GetViewFrame(26, 0, 10);
  
  if (RandomHackSound==0){
    MomHackFrame1.LinkedAudio = aKnifeHack1;
    MomHackFrame2.LinkedAudio = aKnifeHack1;
    MomHackFrame3.LinkedAudio = aKnifeHack1;
  }
  else if (RandomHackSound==1){
    MomHackFrame1.LinkedAudio = aKnifeHack2;
    MomHackFrame2.LinkedAudio = aKnifeHack2;
    MomHackFrame3.LinkedAudio = aKnifeHack2;
  }
  else if (RandomHackSound==2){
    MomHackFrame1.LinkedAudio = aKnifeHack3;
    MomHackFrame2.LinkedAudio = aKnifeHack3;
    MomHackFrame3.LinkedAudio = aKnifeHack3;
  }
}


this actually works nicely but:

a) is there a smarter way to assign multiple frames to the same group of random sounds? (i.e. if i have a really long animation with 30 frames that all use the same random sounds i would end up with a huge big block of code and lots of copy/paste...)
b) i can't set the ViewFrame.LinkedAudio to an Audiochannel*...how can i control panning, volume, position, etc. of the random sounds?

cheers

dayowlron

Whenever you have a lot of code that you think will be repeated see if you can do it in a function.
With the below you could create a function that you pass in a sound and it gets applied to all 3 frames.

I am not sure of how to correctly define the parameter. I think it is "AudioClip*"
Code: ags

void AssignSounds(AudioClip* Snd){
  ViewFrame* MomHackFrame1 = Game.GetViewFrame(26, 0, 2);
  ViewFrame* MomHackFrame2 = Game.GetViewFrame(26, 0, 6);
  ViewFrame* MomHackFrame3 = Game.GetViewFrame(26, 0, 10);
  MomHackFrame1.LinkedAudio = Snd;
  MomHackFrame2.LinkedAudio = Snd;
  MomHackFrame3.LinkedAudio = Snd;
}

void PlayMomSounds(){
  int RandomHackSound=Random(2);
  if (RandomHackSound==0){ AssignSounds(aKnifeHack1); }
  if (RandomHackSound==1){ AssignSounds(aKnifeHack2); }
  if (RandomHackSound==2){ AssignSounds(aKnifeHack3); }
}
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

daniel

thanks dayowlron,

i'll give this a try later. any ideas on how i could achieve stuff like "Panning", "SetRoomLocation" etc. for the sounds?
the character is all the way to the right on a scrolling background and i don't want the sounds to play when the play walks over to the left (at least not at full volume). also the sounds playing slightly from the right the right would make sense...

Slasher

You could use the players x position to determine sound level.

daniel

Quote from: Slasher on Sat 20/05/2017 08:26:47
You could use the players x position to determine sound level.

how? i can't find any function to adjust the volume for an audioclip directly...only for audiochannels. ???

Snarky

The AudioChannel/AudioClip model was discussed in another thread not too long ago. The way I like to think of it is that each AudioChannel is an mp3 player with a speaker, and each AudioClip is a track. You have to adjust volume etc. on the player, not the track.

The general pattern (taken directly from the manual entries on AudioChannel.Panning and AudioChannel.Volume) is to store the channel when you play the clip, and immediately adjust the channel properties:

Code: ags

  AudioChannel *channel = Snd.Play();
  channel.Panning = 20; // Slightly to the right
  channel.Volume = (player.x * 100 / Room.Width); // Goes from 0 at left edge to 100 at right edge of room


Note that this will not automatically update if the character moves while the sounds are playing, so if the sound-effects are long, you may want to update them dynamically (which means storing the audio channel and updating it in one of the RepExec/repeatedly_execute functions; then there's extra work making sure that the audio channel is still playing the sound effect in question).

As for controlling the properties of the clips played by ViewFrame.LinkedAudio, I don't think that's possible. I believe there is a game-wide option somewhere for whether they should be panned according to the player position (they're basically meant to be used for footsteps), but I don't think there's any configurability beyond that. (Edit: It's a character property called Character.ScaleVolume, and apparently if it's turned on the sound will be panned depending on the character x-position and the volume set depending on the character scaling.)

For a solution to your issue, you can adapt the code here: http://www.adventuregamestudio.co.uk/forums/index.php?topic=36284.msg538902#msg538902

daniel

thanks snarky,

it seems the Character.ScaleVolume property only adjusts the volume but not the panning of the sounds. which is too bad because it would be awesome if it could do that.

i simply worked around the problem by setting the default volume of the clips in the client and using stereo files that are already panned slightly to the right.
i also set the LinkedAudio to "null" if "GetViewport<=100"...so the sounds only play when the screen is scrolled to the right. not perfect but it's good enough for now.

SMF spam blocked by CleanTalk