Last active
May 7, 2020 08:22
-
-
Save NP-chaonay/09d21512c04f05ca80556fa35e1683df to your computer and use it in GitHub Desktop.
My specialized music player (MPlayer front-end)
This file contains 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
#!/usr/bin/env python3 | |
import os,glob,random,subprocess,signal,threading,time,sys,curses | |
IsProcessRunning=True | |
Is_cli_win_display_Active=False | |
Has_cli_win_display_Active=False | |
play_no=0 | |
args=['<Not playing>','<Not playing>'] | |
os.chdir('/home/np-chaonay/') | |
songs=[] | |
def curses_print(msg,scr): | |
scr.addstr(msg) | |
scr.refresh() | |
def cli_win_display(): | |
global Is_cli_win_display_Active,Has_cli_win_display_Active,stdscr | |
Is_cli_win_display_Active=True | |
while True: | |
try: | |
stdscr.clear() | |
curses_print('MPlayer specialized Front-end',stdscr) | |
curses_print('\nThe #'+str(play_no)+' song :',stdscr) | |
curses_print('\n\tName : '+args[-1],stdscr) | |
curses_print('\n\tDefault Speed and Pitch : '+args[-2],stdscr) | |
curses_print('\n\nKey : Ctrl+C:Exit, Ctrl+Z:Pause_And_Move_To_BG, q:Next_Song, Left+Enter,Right+Enter:Move_Cursor (And other key to control MPlayer)',stdscr) | |
break | |
except: | |
msg='[WARNING] Console resolution is too low. Program\'s keyboard operation is ignored. Please resize the console.' | |
stdscr.clear() | |
stdscr.refresh() | |
print(msg) | |
stdscr.refresh() | |
while True: | |
key=stdscr.getch() | |
if key==curses.KEY_RESIZE: break | |
stdscr.refresh() | |
Is_cli_win_display_Active=False | |
Has_cli_win_display_Active=True | |
def init_cli(): | |
global stdscr | |
stdscr=curses.initscr() | |
curses.noecho() | |
curses.cbreak() | |
stdscr.clear() | |
stdscr.refresh() | |
def end_cli(): | |
curses.nocbreak() | |
curses.echo() | |
curses.endwin() | |
def player_func(): | |
global play_no,args,MPlayer_process,Is_cli_win_display_Active,cli_win_display_thread | |
while IsProcessRunning: | |
random.seed() | |
play_no+=1 | |
args=['mplayer','-vo','null','-speed',str(random.randint(500,2000)/1000),random.choice(songs)] | |
MPlayer_process=subprocess.Popen(executable='mplayer',args=args,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,stdin=subprocess.PIPE) | |
if not Is_cli_win_display_Active: | |
cli_win_display_thread=threading.Thread(target=cli_win_display,daemon=True) | |
cli_win_display_thread.start() | |
MPlayer_process.wait() | |
player_thread=threading.Thread(target=player_func,daemon=True) | |
try: | |
init_cli() | |
player_thread.start() | |
# Prevent double getch() called. | |
while not Has_cli_win_display_Active: | |
try: cli_win_display_thread.wait() | |
except (NameError,AttributeError): continue | |
else: break | |
while True: | |
if not Is_cli_win_display_Active: | |
key=stdscr.getch() | |
else: | |
try: cli_win_display_thread.wait() | |
except (NameError,AttributeError): pass | |
continue | |
if key==curses.KEY_RESIZE and not Is_cli_win_display_Active: | |
cli_win_display() | |
elif not Is_cli_win_display_Active: | |
try: | |
MPlayer_process.stdin.raw.write(bytes([key])) | |
MPlayer_process.stdin.raw.flush() | |
except (IOError,NameError,AttributeError): pass | |
except KeyboardInterrupt: | |
IsProcessRunning=False | |
try: MPlayer_process.kill() | |
except: pass | |
end_cli() | |
exit() | |
except Exception: | |
IsProcessRunning=False | |
try: MPlayer_process.kill() | |
except: pass | |
end_cli() | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment