Sound changing with velocity

How can I get the sound to change when the velocity changes? I tried with get.linear velocity but it doesn’t work, maybe because I’m using servo control.

Have you seen this thread?

The code shouldn’t be too hard. This is something I’m writing on the spot, so it might not be perfect.


import math as M
GL = GameLogic
cont = GL.getCurrentController()
obj = cont.owner

#The sound actuator
sound = cont.actuators["roll_sound"]

#The range of speeds your vehicle will be going
speedrange = 3

#The range of pitch you want the sound to be
pitchrange = 1.5
#The minimum pitch
minpitch = .8

#the range of volume the sound will play
volumerange = 1.5
#the minimum volume
minvolume = 0


vspeed = obj.getLinearVelocity(0)

#The current speed of your vehicle
speed = (M.fabs(vspeed[0])) + (M.fabs(vspeed[1])) + (M.fabs(vspeed[2]))

#The new pitch your sound will play at
newpitch = (speed/speedrange)*pitchrange+minpitch

#the new volume the sound will play at
newvolume = (speed/speedrange)*volumerange+minvolume

sound.pitch = newpitch
sound.volume = newvolume
sound.velocity = tuple(vspeed)

cont.activate(sound)

If any of that doesn’t make sense, just say so. If that’s not what you wanted, then… oops:eek:

Edit: Changed script so it actually works.

What is the speedrange? It seems there is nothing to activate the actuator at the end of the script

speedrange is a variable that I made that you can find by taking the maximum velocity - minimum velocity.

By having that number, the pitch won’t go higher than pitchrange + startpitch

Basically, since your minimum speed is going to be 0, just add a script that that says

print GameLogic.getCurrentcontroller().owner.getLinearVelocity(0)

connect that to an always sensor, then make your object go to its maximum velocity. The highest number printed will be the number you put in the speedrange. You can then delete that script.

Does that make sense?

Edit: You edited your post after I posted lol.

You’re correct. You’ll have to add

cont.activate(sound)

at the very end. I forgot that. Whoops.

Thanks a lot but there is a problem

try to apply your scripts to this old blend I uploaded http://www.pasteall.org/blend/2963

Is it only on my pc that getlinearvelocity doesn’t work?

It only displays
[0.0, 0.0, 0.0] throughout all the game

Your problem could have been that you connected it to an always sensor that wasn’t set to true level triggering. I was able to get the linear velocity, and I pretty much implemented the entire script in it. I also added so it adjusts the volume and the speed of the sound, like real sound does.

It now sounds a lot closer to realistic than before. When you first open it, just start the game and let it roll around the dish. That shows most of what I implemented.

There is one thing you need to change with it though: The sound has a blank spot in it, so when it’s set to loop, it randomly stops for a couple seconds, then starts again. You can use Audacity(Link) to trim that out.

.blend file: http://www.pasteall.org/blend/3020

Now, I really better get back to the weekend challenge:p

P.S. I can’t stand that china music. It annoys the heck out of me. I disconnected it when I was testing.

Great!!!

I’m sorry for the theme. It’s been a while now that i’m looking for a free soundtrack, but it’s difficult.

There was something I messed up in the script after I uploaded the .blend. Just replace the entire script with this one:

If you were to go a certain direction at a certain speed, there would be no sound, or it would be playing wrong. This script gives you more control anyway. The volume and pitch minimum and ranges are separate.


import math as M
GL = GameLogic
cont = GL.getCurrentController()
obj = cont.owner

#The sound actuator
sound = cont.actuators["roll_sound"]

#The range of speeds your vehicle will be going
speedrange = 3

#The range of pitch you want the sound to be
pitchrange = 1.5
#The minimum pitch
minpitch = .8

#the range of volume the sound will play
volumerange = 1.5
#the minimum volume
minvolume = 0


vspeed = obj.getLinearVelocity(0)

#The current speed of your vehicle
speed = (M.fabs(vspeed[0])) + (M.fabs(vspeed[1])) + (M.fabs(vspeed[2]))

#The new pitch your sound will play at
newpitch = (speed/speedrange)*pitchrange+minpitch

#the new volume the sound will play at
newvolume = (speed/speedrange)*volumerange+minvolume

sound.pitch = newpitch
sound.volume = newvolume
sound.velocity = tuple(vspeed)

cont.activate(sound)

It’s functioning. Yet there is one little problem. Once it starts it doesn’t stop again even when the sensor is not positive. It will continue making the last sound it was playing when the sensor was positive.

I tried adding
if sensor is postive

to the script but this provocate silence.

Yes, I noticed that too. The reason that is, even though the actuator is not being activated, it has to finish what it was playing.

Here’s what I came up with in a few minutes:

import math as M
GL = GameLogic
cont = GL.getCurrentController()
obj = cont.owner

ground = cont.sensors["touchground"]

#The sound actuator
sound = cont.actuators["roll_sound"]

#The range of speeds your vehicle will be going
speedrange = 3

#The range of pitch you want the sound to be
pitchrange = 1.5
#The minimum pitch
minpitch = .8

#the range of volume the sound will play
volumerange = 1.5
#the minimum volume
minvolume = 0


vspeed = obj.getLinearVelocity(0)

#The current speed of your vehicle
speed = (M.fabs(vspeed[0])) + (M.fabs(vspeed[1])) + (M.fabs(vspeed[2]))

#The new pitch your sound will play at
newpitch = (speed/speedrange)*pitchrange+minpitch

#the new volume the sound will play at
newvolume = (speed/speedrange)*volumerange+minvolume

sound.pitch = newpitch
sound.velocity = tuple(vspeed)
#If the ball is touching the ground, use the dynamic volume level
if ground.positive:
    sound.volume = newvolume
#if the ball isn't touching the ground, make the sound silent
else:
    sound.volume = 0

cont.activate(sound)

P.S. Have you trimmed out the blank spot in the sound?

Yes, i’ve eliminated the black spot. Now it’s perfect. You’ve done a great thing for my game. With this script I can even get the mass of the object and make it influence the picth.

here is is the result
http://blenderartists.org/forum/showthread.php?t=189523

Thanks