Skip to content

Commit 03c36da

Browse files
committed
Use precise QtTimer types.
Default coarse only tried to keep accuracy within 5% of the desired interval Also use constants instead of magic number 1000
1 parent 80bf03c commit 03c36da

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/AutoSplit.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
AUTOSPLIT_VERSION,
4242
BGRA_CHANNEL_COUNT,
4343
FROZEN,
44+
ONE_SECOND,
4445
auto_split_directory,
4546
decimal,
4647
flatten,
@@ -76,7 +77,9 @@ class AutoSplit(QMainWindow, design.Ui_MainWindow):
7677

7778
# Timers
7879
timer_live_image = QtCore.QTimer()
80+
timer_live_image.setTimerType(QtCore.Qt.TimerType.PreciseTimer)
7981
timer_start_image = QtCore.QTimer()
82+
timer_start_image.setTimerType(QtCore.Qt.TimerType.PreciseTimer)
8083

8184
# Widgets
8285
AboutWidget: about.Ui_AboutAutoSplitWidget | None = None
@@ -205,7 +208,7 @@ def _update_checker_widget_signal_slot(latest_version: str, check_on_open: bool)
205208

206209
# live image checkbox
207210
self.timer_live_image.timeout.connect(lambda: self.__update_live_image_details(None, True))
208-
self.timer_live_image.start(int(1000 / self.settings_dict["fps_limit"]))
211+
self.timer_live_image.start(int(ONE_SECOND / self.settings_dict["fps_limit"]))
209212

210213
# Automatic timer start
211214
self.timer_start_image.timeout.connect(self.__start_image_function)
@@ -296,7 +299,7 @@ def __load_start_image(self, started_by_button: bool = False, wait_for_delay: bo
296299
self.highest_similarity = 0.0
297300
self.reset_highest_similarity = 0.0
298301
self.split_below_threshold = False
299-
self.timer_start_image.start(int(1000 / self.settings_dict["fps_limit"]))
302+
self.timer_start_image.start(int(ONE_SECOND / self.settings_dict["fps_limit"]))
300303

301304
QApplication.processEvents()
302305

@@ -341,7 +344,7 @@ def __start_image_function(self):
341344
if self.start_image.get_delay_time(self) > 0:
342345
self.start_image_status_value_label.setText("delaying start...")
343346
delay_start_time = time()
344-
start_delay = self.start_image.get_delay_time(self) / 1000
347+
start_delay = self.start_image.get_delay_time(self) / ONE_SECOND
345348
time_delta = 0.0
346349
while time_delta < start_delay:
347350
delay_time_left = start_delay - time_delta
@@ -564,7 +567,7 @@ def __auto_splitter(self): # noqa: PLR0912,PLR0915
564567
while self.split_image_number < number_of_split_images:
565568
# Check if we are not waiting for the split delay to send the key press
566569
if self.waiting_for_split_delay:
567-
time_millis = int(round(time() * 1000))
570+
time_millis = int(round(time() * ONE_SECOND))
568571
if time_millis < split_time:
569572
QApplication.processEvents()
570573
continue
@@ -583,9 +586,9 @@ def __auto_splitter(self): # noqa: PLR0912,PLR0915
583586
if not self.split_image.check_flag(DUMMY_FLAG):
584587
# If it's a delayed split, check if the delay has passed
585588
# Otherwise calculate the split time for the key press
586-
split_delay = self.split_image.get_delay_time(self) / 1000
589+
split_delay = self.split_image.get_delay_time(self) / ONE_SECOND
587590
if split_delay > 0 and not self.waiting_for_split_delay:
588-
split_time = round(time() + split_delay * 1000)
591+
split_time = round(time() + split_delay * ONE_SECOND)
589592
self.waiting_for_split_delay = True
590593
buttons_to_disable = [
591594
self.next_image_button,
@@ -668,7 +671,7 @@ def __similarity_threshold_loop(self, number_of_split_images: int, dummy_splits_
668671
# Limit the number of time the comparison runs to reduce cpu usage
669672
frame_interval = 1 / self.settings_dict["fps_limit"]
670673
# Use a time delta to have a consistant check interval
671-
wait_delta_ms = int((frame_interval - (time() - start) % frame_interval) * 1000)
674+
wait_delta_ms = int((frame_interval - (time() - start) % frame_interval) * ONE_SECOND)
672675

673676
below_flag = self.split_image.check_flag(BELOW_FLAG)
674677
# if the b flag is set, let similarity go above threshold first,

src/menu_bar.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323
from gen import about, design, settings as settings_ui, update_checker
2424
from hotkeys import HOTKEYS, Hotkey, set_hotkey
25-
from utils import AUTOSPLIT_VERSION, GITHUB_REPOSITORY, decimal, fire_and_forget
25+
from utils import AUTOSPLIT_VERSION, GITHUB_REPOSITORY, ONE_SECOND, decimal, fire_and_forget
2626

2727
if TYPE_CHECKING:
2828
from AutoSplit import AutoSplit
@@ -231,7 +231,7 @@ def __capture_device_changed(self):
231231
def __fps_limit_changed(self, value: int):
232232
value = self.fps_limit_spinbox.value()
233233
self._autosplit_ref.settings_dict["fps_limit"] = value
234-
self._autosplit_ref.timer_live_image.setInterval(int(1000 / value))
234+
self._autosplit_ref.timer_live_image.setInterval(int(ONE_SECOND / value))
235235

236236
@fire_and_forget
237237
def __set_all_capture_devices(self):

src/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
T = TypeVar("T")
2626

27-
27+
ONE_SECOND = 1000
28+
"""1000 milliseconds in 1 second"""
2829
DWMWA_EXTENDED_FRAME_BOUNDS = 9
2930
MAXBYTE = 255
3031
BGR_CHANNEL_COUNT = 3

0 commit comments

Comments
 (0)