The issue you're experiencing with the new code stems from a change in how it calculates the next speed step and decides whether to update the playback speed. In your old code, you had additional logic that determined which speed step (speed_step_next or speed_step_prev) is closer to the calculated speed_drag_current and then used that value to update the playback speed. This ensured smooth and responsive adjustments as you dragged.
In the new code, this comparison logic was omitted, which is why you need to drag further before the speed updates, leading to less responsive behavior when decreasing the speed.
Here's a detailed explanation of the problem and how to fix it:
Problem Explanation:
In the old code, after calculating speed_drag_current, you have a loop that increments speed_step_next using self:speed_step(speed_step_next, drag_dir_up) until the difference between speed_step_next and speed_current is no longer less than speed_drag_diff. After the loop, you calculate speed_step_prev by stepping in the opposite direction. You then compare speed_step_next and speed_step_prev to speed_drag_current and choose the one that's closer. This way, you ensure that the speed adjustment is as close as possible to where you've dragged.
In the new code, you removed the calculation of speed_step_prev and the comparison, and you only break the loop when math.abs(speed_step - speed_current) >= speed_drag_diff. This approach doesn't account for which speed step is actually closer to speed_drag_current, which can result in less responsive adjustments, particularly when decreasing the speed.
Solution:
To fix the issue, you need to reintroduce the logic that compares speed_step_next and speed_step_prev to speed_drag_current and selects the one that's closest. Here's how you can modify your new code:
function Speed:on_global_mouse_move()
if not self.dragging then return end
local drag = self.dragging
drag.distance = cursor.x - drag.start_x
drag.speed_distance = (-drag.distance / self.notch_spacing) * self.notch_every
local speed_drag_current = clamp(0.01, drag.start_speed + drag.speed_distance, 100)
local speed_current = state.speed
local drag_dir_up = speed_drag_current > speed_current
local speed_step_next = speed_current
local speed_drag_diff = math.abs(speed_drag_current - speed_current)
while true do
local speed_step = self:speed_step(speed_step_next, drag_dir_up)
if math.abs(speed_step - speed_current) >= speed_drag_diff then
break
end
speed_step_next = speed_step
end
local speed_step_prev = self:speed_step(speed_step_next, not drag_dir_up)
local speed_new = speed_step_prev
local speed_next_diff = math.abs(speed_drag_current - speed_step_next)
local speed_prev_diff = math.abs(speed_drag_current - speed_step_prev)
if speed_next_diff < speed_prev_diff then
speed_new = speed_step_next
end
if speed_new ~= speed_current then
mp.set_property_native('speed', speed_new)
end
end
Explanation of Changes:
-
Reintroduced speed_step_prev Calculation:
After the loop, we calculate speed_step_prev by stepping once in the opposite direction (not drag_dir_up). This gives us the adjacent speed step in the other direction.
-
Comparison Logic Added:
We compare the differences between speed_drag_current and both speed_step_next and speed_step_prev:
local speed_next_diff = math.abs(speed_drag_current - speed_step_next)
local speed_prev_diff = math.abs(speed_drag_current - speed_step_prev)
Then, we select the speed step that is closer to speed_drag_current:
local speed_new = speed_step_prev
if speed_next_diff < speed_prev_diff then
speed_new = speed_step_next
end
-
Update Playback Speed with the Closest Step:
We check if the new speed is different from the current speed before updating:
if speed_new ~= speed_current then
mp.set_property_native('speed', speed_new)
end
By reintroducing this logic, the code now correctly selects the speed step that is closest to where you've dragged, resulting in more intuitive and responsive behavior when adjusting the speed.
Additional Notes:
-
Why the Issue Occurred:
The removal of the comparison between speed_step_next and speed_step_prev meant the code couldn't accurately determine the optimal speed step based on the drag distance.
-
Understanding drag_dir_up:
This variable determines the direction of the drag (increasing or decreasing speed), which is crucial for stepping in the correct direction during the loop.
-
Importance of Clamping:
The clamp function ensures speed_drag_current stays within acceptable bounds (0.01 to 100), preventing invalid speed values.
Conclusion:
By modifying your new code to include the comparison logic from your old code, you restore the smooth and responsive speed adjustment behavior. This should resolve the issue where you had to drag further before the speed updated, and ensure that small drag distances result in immediate and accurate speed changes.