A new try at sound panning with walking.

This commit is contained in:
Storm Dragon 2024-06-20 02:03:06 -04:00
parent 155ed6ec39
commit 58ab5aa854

View File

@ -173,47 +173,42 @@ def cut_scene(sounds, soundName):
pygame.mixer.stop() pygame.mixer.stop()
pygame.event.pump() pygame.event.pump()
def obj_play(sounds, soundName, playerPos, objPos): def obj_play(sounds, sound_name, player_pos, obj_pos):
distance = playerPos - objPos distance = player_pos - obj_pos
if distance > 9 or distance < -9: if abs(distance) > 9:
# The item is out of range, so play it at 0 # The item is out of range, so play it at 0
left = 0 left = 0
right = 0 right = 0
elif distance == 0:
left = 0.9
right = 0.9
else: else:
angle = math.radians(distance * 5) # Calculate the panning based on the distance
left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle)) left = max(0, 1 - (distance / 9))
right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle)) right = max(0, 1 + (distance / 9))
if left < 0: left *= -1 left = min(1, left)
if right < 0: right *= -1 right = min(1, right)
# x is the channel for the sound # Play the sound on a new channel
x = sounds[soundName].play(-1) channel = sounds[sound_name].play(-1)
# Apply the position information to the channel # Apply the position information to the channel
x.set_volume(left, right) channel.set_volume(left, right)
# return the channel so that it can be used in the update and stop functions. # Return the channel so that it can be used in the update and stop functions.
return x return channel
def obj_update(x, playerPos, objPos): def obj_update(channel, player_pos, obj_pos):
distance = playerPos - objPos distance = player_pos - obj_pos
if distance > 9 or distance < -9: if abs(distance) > 9:
left = 0 left = 0
right = 0 right = 0
elif distance == 0:
left = 0.9
right = 0.9
else: else:
angle = math.radians(distance * 5) # Calculate the panning based on the distance
left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle)) left = max(0, 1 - (distance / 9))
right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle)) right = max(0, 1 + (distance / 9))
if left < 0: left *= -1 left = min(1, left)
if right < 0: right *= -1 right = min(1, right)
# Apply the position information to the channel # Apply the position information to the channel
x.set_volume(left, right) channel.set_volume(left, right)
# return the channel # Return the channel
return x return channel
soundData
def obj_stop(x): def obj_stop(x):
# Tries to stop a playing object channel # Tries to stop a playing object channel
try: try: