From 58ab5aa854268465807d3a3daec7cd39cc9286f9 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Thu, 20 Jun 2024 02:03:06 -0400 Subject: [PATCH] A new try at sound panning with walking. --- __init__.py | 59 ++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/__init__.py b/__init__.py index 901d315..16a4b4c 100755 --- a/__init__.py +++ b/__init__.py @@ -173,47 +173,42 @@ def cut_scene(sounds, soundName): pygame.mixer.stop() pygame.event.pump() -def obj_play(sounds, soundName, playerPos, objPos): - distance = playerPos - objPos - if distance > 9 or distance < -9: +def obj_play(sounds, sound_name, player_pos, obj_pos): + distance = player_pos - obj_pos + if abs(distance) > 9: # The item is out of range, so play it at 0 left = 0 right = 0 - elif distance == 0: - left = 0.9 - right = 0.9 else: - angle = math.radians(distance * 5) - left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle)) - right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle)) - if left < 0: left *= -1 - if right < 0: right *= -1 - # x is the channel for the sound - x = sounds[soundName].play(-1) + # Calculate the panning based on the distance + left = max(0, 1 - (distance / 9)) + right = max(0, 1 + (distance / 9)) + left = min(1, left) + right = min(1, right) + # Play the sound on a new channel + channel = sounds[sound_name].play(-1) # Apply the position information to the channel - x.set_volume(left, right) - # return the channel so that it can be used in the update and stop functions. - return x - -def obj_update(x, playerPos, objPos): - distance = playerPos - objPos - if distance > 9 or distance < -9: + channel.set_volume(left, right) + # Return the channel so that it can be used in the update and stop functions. + return channel + +def obj_update(channel, player_pos, obj_pos): + distance = player_pos - obj_pos + if abs(distance) > 9: left = 0 right = 0 - elif distance == 0: - left = 0.9 - right = 0.9 else: - angle = math.radians(distance * 5) - left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle)) - right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle)) - if left < 0: left *= -1 - if right < 0: right *= -1 + # Calculate the panning based on the distance + left = max(0, 1 - (distance / 9)) + right = max(0, 1 + (distance / 9)) + left = min(1, left) + right = min(1, right) # Apply the position information to the channel - x.set_volume(left, right) - # return the channel - return x - + channel.set_volume(left, right) + # Return the channel + return channel + +soundData def obj_stop(x): # Tries to stop a playing object channel try: