Skip to content

Commit

Permalink
Add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Sep 15, 2022
1 parent 056ce06 commit c41237b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 27 deletions.
8 changes: 2 additions & 6 deletions gambaterm/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ def ratio(self):
return self.output_rate / self.input_rate / self.speed

def send(self, audio):
# Set the right type and shape
(length,) = audio.shape
audio.dtype = np.int16
audio.shape = (length, 2)
# Resample to output rate
data = self.resampler.process(audio, self.ratio).astype(np.int16)
# Loop over data blocks
Expand Down Expand Up @@ -63,8 +59,8 @@ def audio_player(console, speed_factor=1.0):
# Perform late imports
# Those can fail if a linux machine doesn't have portaudio or libsamplerate
# installed
import samplerate
import sounddevice
import samplerate # type: ignore
import sounddevice # type: ignore

input_rate = console.FPS * console.TICKS_IN_FRAME
resampler = samplerate.Resampler("linear", channels=2)
Expand Down
8 changes: 4 additions & 4 deletions gambaterm/keyboard_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def get_xlib_mapping(console):
from Xlib import XK
from Xlib import XK # type: ignore

return {
# Directions
Expand Down Expand Up @@ -59,8 +59,8 @@ def get_keyboard_mapping(console):

@contextmanager
def xlib_key_pressed_context(display=None):
from Xlib.ext import xinput
from Xlib.display import Display
from Xlib.ext import xinput # type: ignore
from Xlib.display import Display # type: ignore

with closing(Display(display)) as xdisplay:
extension_info = xdisplay.query_extension("XInputExtension")
Expand Down Expand Up @@ -134,7 +134,7 @@ def get_pressed():

@contextmanager
def pynput_key_pressed_context(display=None):
from pynput import keyboard
from pynput import keyboard # type: ignore

def on_press(key):
try:
Expand Down
14 changes: 14 additions & 0 deletions gambaterm/libgambatte.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np
import numpy.typing as npt

class GB:
def load(self, rom_file: str, flags: int = 0) -> int: ...
def run_for(
self,
video: npt.NDArray[np.uint32],
pitch: int,
audio: npt.NDArray[np.int16],
samples: int,
) -> tuple[int, int]: ...
def set_input(self, value: int) -> None: ...
def set_save_directory(self, path: str) -> None: ...
15 changes: 9 additions & 6 deletions gambaterm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

import time
import argparse
from typing import Type, Optional, Tuple

from prompt_toolkit.application import create_app_session


from .run import run
from .console import GameboyColor
from .console import GameboyColor, Console
from .audio import audio_player, no_audio
from .colors import detect_local_color_mode
from .keyboard_input import console_input_from_keyboard_context
from .controller_input import combine_console_input_from_controller_context
from .file_input import console_input_from_file_context, write_input_context


def add_base_arguments(parser):
def add_base_arguments(parser: argparse.ArgumentParser):
parser.add_argument("romfile", metavar="ROM", type=str, help="Path to a rom file")
parser.add_argument(
"--input-file", "-i", default=None, help="Path to a bizhawk BK2 file"
)


def add_optional_arguments(parser):
def add_optional_arguments(parser: argparse.ArgumentParser):
parser.add_argument(
"--color-mode",
"-c",
Expand Down Expand Up @@ -80,10 +81,12 @@ def add_optional_arguments(parser):
type=str,
help="Enable game controller support",
)
return parser


def main(args=None, console_cls=GameboyColor):
def main(
parser_args: Optional[Tuple[str, ...]] = None,
console_cls: Type[Console] = GameboyColor,
):
parser = argparse.ArgumentParser(
prog="gambaterm", description="Gambatte terminal front-end"
)
Expand All @@ -93,7 +96,7 @@ def main(args=None, console_cls=GameboyColor):
parser.add_argument(
"--disable-audio", "--da", action="store_true", help="Disable audio entirely"
)
args = parser.parse_args(args)
args: argparse.Namespace = parser.parse_args(parser_args)
console = console_cls(args)

if args.input_file is not None:
Expand Down
6 changes: 3 additions & 3 deletions gambaterm/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def run(
assert color_mode > 0

# Prepare buffers with invalid data
video = np.full((console.HEIGHT, console.WIDTH), -1, np.int32)
audio = np.full(2 * console.TICKS_IN_FRAME, -1, np.int32)
video = np.full((console.HEIGHT, console.WIDTH), -1, np.uint32)
audio = np.full((2 * console.TICKS_IN_FRAME, 2), -1, np.int16)
last_frame = video.copy()

# Print area
Expand Down Expand Up @@ -92,7 +92,7 @@ def run(
# Send audio
with timing(audio_deltas):
if audio_out:
audio_out.send(audio[:samples])
audio_out.send(audio[:samples, :])

# Read keys
for event in app_session.input.read_keys():
Expand Down
8 changes: 4 additions & 4 deletions gambaterm/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from .run import run
from .colors import ColorMode, detect_color_mode
from .file_input import gb_input_from_file_context
from .keyboard_input import gb_input_from_keyboard_context
from .file_input import console_input_from_file_context
from .keyboard_input import console_input_from_keyboard_context
from .main import add_base_arguments, add_optional_arguments

from .ssh_app_session import process_to_app_session
Expand Down Expand Up @@ -138,12 +138,12 @@ async def ssh_process_handler(process):

def thread_target(app_session, app_config, username, display, color_mode):
if app_config.input_file is not None:
gb_input_context = gb_input_from_file_context(
gb_input_context = console_input_from_file_context(
app_config.input_file, app_config.skip_inputs
)
save_directory = tempfile.mkdtemp()
else:
gb_input_context = gb_input_from_keyboard_context(display=display)
gb_input_context = console_input_from_keyboard_context(display=display)
save_directory = Path("ssh_save") / username
save_directory.mkdir(parents=True, exist_ok=True)

Expand Down
14 changes: 14 additions & 0 deletions gambaterm/termblit.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Optional

import numpy as np
import numpy.typing as npt

def blit(
image: npt.NDArray[np.int32],
last: Optional[npt.NDArray[np.int32]],
refx: int,
refy: int,
width: int,
height: int,
color_mode: int,
) -> bytes: ...
4 changes: 2 additions & 2 deletions libgambatte_ext/libgambatte.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ cdef class GB:

def run_for(
self,
np.ndarray[np.int32_t, ndim=2] video,
np.ndarray[np.uint32_t, ndim=2] video,
ptrdiff_t pitch,
np.ndarray[np.int32_t, ndim=1] audio,
np.ndarray[np.int16_t, ndim=2] audio,
size_t samples,
):
cdef unsigned int* video_buffer = <unsigned int*> video.data
Expand Down
4 changes: 2 additions & 2 deletions termblit_ext/termblit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ cdef char* move_from_to(


def blit(
np.ndarray[np.int32_t, ndim=2] image,
np.ndarray[np.int32_t, ndim=2] last,
np.ndarray[np.uint32_t, ndim=2] image,
np.ndarray[np.uint32_t, ndim=2] last,
int refx, int refy, int width, int height,
int color_mode,
):
Expand Down

0 comments on commit c41237b

Please sign in to comment.