Coverage for src/retrospeedlab/games/hushbeat/clock.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-12-04 00:01 +0000

1from retrospeedlab.games.hushbeat.codes import BAR, TEMPO_BANNER, TEMPO_UP 

2from retrospeedlab.games.hushbeat.effects import banner, signal 

3from retrospeedlab.games.hushbeat.layout import BAR_BEAT, BEAT_LENGTH, BEAT_PHASE, PENDING_TEMPO, SONG_BAR, TEMPO 

4from retrospeedlab.games.hushbeat.tuning import BEAT_LADDER, BEATS_PER_BAR, FRAMES_PER_MINUTE 

5 

6 

7def bpm(tempo: int) -> int: 

8 return FRAMES_PER_MINUTE // BEAT_LADDER[tempo] 

9 

10 

11def apply_tempo(ram: bytearray) -> None: 

12 ram[TEMPO] = ram[PENDING_TEMPO] 

13 ram[BEAT_LENGTH] = BEAT_LADDER[ram[TEMPO]] 

14 signal(ram, BAR) 

15 

16 

17def reset_clock(ram: bytearray) -> None: 

18 ram[BEAT_PHASE], ram[BAR_BEAT], ram[SONG_BAR] = 0, 0, 0 

19 apply_tempo(ram) 

20 

21 

22def downbeat(ram: bytearray) -> None: 

23 if ram[PENDING_TEMPO] > ram[TEMPO]: 

24 signal(ram, TEMPO_UP) 

25 banner(ram, TEMPO_BANNER) 

26 ram[SONG_BAR] = (ram[SONG_BAR] + 1) & 0xFF 

27 apply_tempo(ram) 

28 

29 

30def tick(ram: bytearray) -> bool: 

31 ram[BEAT_PHASE] += 1 

32 if ram[BEAT_PHASE] < ram[BEAT_LENGTH]: 

33 return False 

34 ram[BEAT_PHASE] = 0 

35 ram[BAR_BEAT] = (ram[BAR_BEAT] + 1) % BEATS_PER_BAR 

36 if not ram[BAR_BEAT]: 

37 downbeat(ram) 

38 return True