Matching Sound Effect with Animation Speed

kR1pt0n1t3

Veteran
Veteran
Joined
Dec 24, 2014
Messages
53
Reaction score
7
First Language
Croatian
Primarily Uses
RMMV
Hi.

So, as the thread name says want to match a certain sound effect to my character sprite animation.

Under main I got: Graphics.frame_rate = 30
I have smooth mode ON.
I'm using a standard 4x4 character sprite file that has 4 frames for animation.

What I want is to match a certain sound effect to start just as the 4th frame of the animation is playing.

I can edit the sound file and make it start at any time I want but I don't know the exact time because, in general, I don't really understand how the game calculates the speed at which it plays each frame of the sprite animation. How does move_speed affect the animation speed? It must increase in certain increments?

What are the formulas for calculating the frame rate based on my settings?

In short;

What I exactly need is to calculate how much pause I need to add before a sound effect so it starts to play exactly at the time as the 4th frame of a character sprite animation is displayed on the screen.

I need to calculate that for move_speed = 2,3,4,5,6

The length of the sound effect is 0,250s.

Thanks.
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
169
Reaction score
62
First Language
English
Primarily Uses
RMXP
You're needlessly over-complicating things. If the end-goal is to "play SE when sprite is on frame X", you just play the SE if frame == X.

What are you trying to do anyways? It kinda sounds like a terrain step sound or a flapping, wing beat.
 

kR1pt0n1t3

Veteran
Veteran
Joined
Dec 24, 2014
Messages
53
Reaction score
7
First Language
Croatian
Primarily Uses
RMMV
So how would I go around doing that?

Via script? And what's the exact command and condition for that?

if anime_count == 4?

What I want to do is similar to the wing beat but not exactly.
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
169
Reaction score
62
First Language
English
Primarily Uses
RMXP
I would need to know more information on how you're going to utilize this in game. I could give you a very simple solution, which would be...

in Game_Character 2, under the update method, you locate this line and immediately place beneath it:
Code:
      # Clear animation count
      @anime_count = 0
      Audio.se_play("Audio/SE/filename", 80, 100) if @character_name == 'filename' && @pattern == 3
Adding your filenames accordingly.
 

kR1pt0n1t3

Veteran
Veteran
Joined
Dec 24, 2014
Messages
53
Reaction score
7
First Language
Croatian
Primarily Uses
RMMV
I have an event with a graphic file loaded and stop-animation checked.
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
169
Reaction score
62
First Language
English
Primarily Uses
RMXP
Here's a quick script then. Just configure it down below and play; you don't need to do anything extra with your events (besides setting the graphic, stop animation to ON, and speed).
Code:
#==============================================================================
# Step Sound for Specific Character Frames
# by KK20
#------------------------------------------------------------------------------
# Plays a sound effect for a given character graphic on a specific frame of its
# walk animation.
#
# Configuration instructions:
#
# when FILENAME then [SOUND_EFFECT, VOLUME, PITCH, ANIM_FRAME(S)]
#
#  - FILENAME: the character graphic filename (no file extension needed)
#  - SOUND_EFFECT: the sound effect filename (no file extension needed)
#  - VOLUME: the volume of the sound, between 0 and 100
#  - PITCH: the pitch bend of the sound, between 50 and 150 (100 for default)
#  - ANIM_FRAME(S): specify which frame(s) of the step animation plays the sound
#      You can configure this as one single number, an Array of numbers, or
#      a Range of numbers, as shown below
#    
#      3      -> Plays only on frame 3 (remember frames start at index 0)
#      [0, 2] -> Plays on frames 0 and 2
#      1..2   -> Plays on frames 1 and 2
#      0..3   -> Plays on frames 0 through 3 (i.e every step)
#      
#==============================================================================

module StepSoundConfigure
  def self.for_graphic(filename)
    case filename
    # -- BEGIN CONFIGURATION -------------------------------------------------
    when '001-Fighter01' then ['016-Jump02', 80, 100, 3]
    when '002-Fighter02' then ['016-Jump02', 30, 150, [1,3]]
    # -- END CONFIGURATION ---------------------------------------------------
    end
  end
end


class Game_Character
  alias play_step_sound_update update
  def update
    play_step_sound_update
    if step_sound = StepSoundConfigure.for_graphic(@character_name)
      if @anime_count == 0 && Array(step_sound[3]).include?(@pattern)
        Audio.se_play("Audio/SE/#{step_sound[0]}", step_sound[1], step_sound[2])
      end
    end
  end
end
 

kR1pt0n1t3

Veteran
Veteran
Joined
Dec 24, 2014
Messages
53
Reaction score
7
First Language
Croatian
Primarily Uses
RMMV
Works like a charm.

It always amazes me when people come up with solutions like this.
I wish I had such an understanding of RMXP code structure so I could write something like this.

Thanks man!

EDIT:

Is it possible to increase animation speed more than 6 - Fastest as you can set in the edit event window?

Also, do you know if there's a command to change X events speed via event script?

I tried using $game_map.events[x].move_speed = y but I keep getting undefined method move_speed
I assume it's because I need to define inside which event page I want to change speed or I'm using a totally wrong function.
 
Last edited:

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
169
Reaction score
62
First Language
English
Primarily Uses
RMXP
You're right, it's move_speed. The problem is that the variable is not accessible outside of the Game_Character class. You can easily change that by adding
Code:
attr_accessor :move_speed
to Game_Character.

Just be aware that move_speed is used in formulas that assume it is an integer. While you can set it as a floating point value, you may witness odd behaviors/results. I suggest you CTRL + SHIFT + F for "@move_speed" to get an idea.
 
Last edited:

kR1pt0n1t3

Veteran
Veteran
Joined
Dec 24, 2014
Messages
53
Reaction score
7
First Language
Croatian
Primarily Uses
RMMV
I still keep getting error even when I added "attr_reader :move_speed" to Game_Character
Untitled.png

Could you add a control switch to the script so I can enable/disable it at will?
 
Last edited:

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
169
Reaction score
62
First Language
English
Primarily Uses
RMXP
Whoops I typed too fast. I meant attr_accessor
 

kR1pt0n1t3

Veteran
Veteran
Joined
Dec 24, 2014
Messages
53
Reaction score
7
First Language
Croatian
Primarily Uses
RMMV
Whoops I typed too fast. I meant attr_accessor
:=)

Works!

One last thing is that I need help with being able to disable the script with a switch.

i tried putting

case filename
if $game_switches[850] == true
# -- BEGIN CONFIGURATION -------------------------------------------------
when '001-Fighter01' then ['016-Jump02', 80, 100, 3]
when '002-Fighter02' then ['016-Jump02', 30, 150, [1,3]]
end
# -- END CONFIGURATION ---------------------------------------------------
end

but I keep getting some weird error and sometimes the script runs even though I turned on the switch which is freaking weird.

I tried then
if $game_switches[850] == true
Audio.se_play("Audio/SE/#{step_sound[0]}", step_sound[1], step_sound[2])
end

This works but not right away. It has some weird delays. In the first animation it continued to play sound and I think it was because I parallel process in events.

Is there some other more elegant way to put a turn on/off switch? I'm sure there's a more easier way that I just can come up with on my own.
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
169
Reaction score
62
First Language
English
Primarily Uses
RMXP
Your second solution should work fine, and without really knowing your eventing I can't explain why you may be having issues. There really isn't an alternative other than creating a dedicated variable for toggling true/false, e.g.
Ruby:
module StepSoundConfigure
  @@enabled = true
 
  def self.enable
    @@enabled = true
  end
 
  def self.disable
    @@enabled = false
  end
 
  def self.enabled?
    @@enabled
  end
end

...

if StepSoundConfigure.enabled?
 Audio.se_play(...)
end
Your first solution is invalid syntax. Frankly, I'm not sure how you claimed it worked--it's not possible. You had to have done something else during your test trials.
 

kR1pt0n1t3

Veteran
Veteran
Joined
Dec 24, 2014
Messages
53
Reaction score
7
First Language
Croatian
Primarily Uses
RMMV
I got it to work. Because of the parallel process in events the switch didn't get turned on when it should and that's why the script was running. After adding the wait command after the turn switch on command all works fine now.

Thanks for the help.

EDIT:

Would it be possible to add another condition to exclude certain rows in animation?

Since each character sprite has 4 animations, and some have different animation that don't need sound.
Instead of separating them could you add another condition inside the script to check the direction so I have the option to exclude the sound being played depending on the direction.

I saw there's a variable @direction so it should be possible to implement but I have no clue as to where to put it.

I guess I could send the filename inside the array and then use it as a condition...;

when '001-Fighter01' then ['016-Jump02', 80, 100, 3, '001-Fighter01']


if @direction == 2 && Array(step_sound[4]) == '001-Fighter01'

else
Audio.se_play("Audio/SE/#{step_sound[0]}", step_sound[1], step_sound[2])
end

edit2:

Nvm. It's just easier to sort out the animation that doesn't have sound in another file.

@bgillisp
close thread since it's solved.
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

made a bio for the protag in my project!
Beggar : "I haven't eat for days..."
Kid : "Just do crime! You will get daily necessity for free in prison~"
Stream is a bit late but will be live shortly with some game development! Feel free to drop by!
omg, why persona 5 is so good?

Forum statistics

Threads
99,155
Messages
962,402
Members
130,724
Latest member
Cryp2o
Top