-
-
Save BjoernSchilberg/9fb5a6784d9501b59b979ac63c0a9617 to your computer and use it in GitHub Desktop.
Adds volume control to PICO-8 on the gameshell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you need to `sudo pip install pyxhook` as it's used to intercept the volume presses | |
# you will also want to update your launcher to call this script (I just have mine in the pico-8 directory | |
# and have my PICO-8.sh run: | |
# SDL_VIDEODRIVER=x11 DISPLAY=:0 python /home/cpi/pico-8/pico-8-volume.py | |
# video of what it looks like: https://www.youtube.com/watch?v=BAoYapdMiPM | |
import pygame | |
import os | |
import pyxhook | |
import alsaaudio | |
import subprocess | |
import time | |
from threading import Timer | |
PROCESS = None | |
PROCESS_CHECK = None | |
HOOK = None | |
VOLUME_SCREEN = None | |
VOLUME_TIMER = None | |
def kill_listener(): | |
HOOK.cancel() | |
def keypress_handler(e): | |
global VOLUME_TIMER, PROCESS | |
volume = None | |
if e.Key == 'P_Subtract': | |
volume = get_volume() - 3 | |
elif e.Key == 'P_Add': | |
volume = get_volume() + 3 | |
if volume is not None: | |
set_volume(volume) | |
if VOLUME_TIMER: | |
update_volume() | |
hide_volume_timer() | |
else: | |
show_volume() | |
def normalize_volume(value): | |
return min(100, max(0, value)) | |
def get_volume(): | |
m = alsaaudio.Mixer() | |
return int(m.getvolume()[0]) | |
def set_volume(value): | |
m = alsaaudio.Mixer() | |
value = normalize_volume(value) | |
m.setvolume(value) | |
def show_volume(): | |
global VOLUME_TIMER, VOLUME_SCREEN | |
pygame.init() | |
os.environ['SDL_VIDEO_WINDOW_POS'] = "0,200" | |
VOLUME_SCREEN = pygame.display.set_mode((320, 40), pygame.NOFRAME) | |
update_volume() | |
hide_volume_timer() | |
def update_volume(): | |
VOLUME_SCREEN.fill(pygame.Color("black")) | |
if get_volume(): | |
width = (320 - 60) * get_volume() / 100 | |
pygame.draw.rect(VOLUME_SCREEN, (0, 255, 128), pygame.Rect(30, 10, width, 20)) | |
pygame.display.flip() | |
def hide_volume_timer(): | |
global VOLUME_TIMER | |
if VOLUME_TIMER: | |
VOLUME_TIMER.cancel() | |
update_volume() | |
VOLUME_TIMER = Timer(2.0, hide_volume) | |
VOLUME_TIMER.start() | |
def check_application(): | |
if PROCESS.poll() is 0: | |
print("killing application") | |
kill_listener() | |
exit() | |
PROCESS_CHECK = Timer(0.5, check_application) | |
PROCESS_CHECK.start() | |
def hide_volume(): | |
global VOLUME_TIMER | |
VOLUME_TIMER = None | |
pygame.quit() | |
PROCESS = subprocess.Popen(["/home/cpi/pico-8/pico8_dyn -splore -draw_rect 32,0,256,240"], shell=True) | |
check_application() | |
HOOK = pyxhook.HookManager() | |
HOOK.KeyDown = keypress_handler | |
HOOK.HookKeyboard | |
HOOK.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment