Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfinabox committed Dec 8, 2020
1 parent 39ada11 commit 61f9466
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ __pycache__/
*.py[cod]
*$py.class

resources/*exe
*.spec
config.json

# C extensions
*.so

Expand Down
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
25 changes: 25 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
//wolfinabox's PyInstaller Build Task
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
//Available Variables:
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
"version": "2.0.0",
"tasks": [
{
"label": "PyInstaller Build",
"type": "shell",
"command": "${workspaceFolder}/env/scripts/pyinstaller.exe -F --onefile --noupx './Volume Fixer.spec'",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
65 changes: 65 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os,subprocess
from subprocess import CREATE_NO_WINDOW
import time
from infi.systray import SysTrayIcon
from utils import local_path,script_dir
import tkinter as tk
import tkinter.simpledialog
import json
resource_dir=local_path('resources')
NIRCMDC_PATH=os.path.join(resource_dir,'nircmdc.exe')
CONFIG_PATH=os.path.join(script_dir(),'config.json')
CONFIG={}
DEFAULT_CONFIG={
'volume_percent':75,
'time_between_runs':0.5
}
STOP=False
def _on_quit(systray):
global STOP
STOP=True

def _on_change_vol_click(systray):
global CONFIG
root=tk.Tk()
root.overrideredirect(1)
root.withdraw()
tmp=tkinter.simpledialog.askinteger(title="Change Volume",
prompt="Volume to limit to:",minvalue=0,maxvalue=100,initialvalue=CONFIG['volume_percent'])
if tmp is not None:
CONFIG['volume_percent']=tmp
save_config()

root.destroy()

def load_config():
global CONFIG
if not os.path.exists(CONFIG_PATH):
CONFIG=DEFAULT_CONFIG.copy()
with open(CONFIG_PATH,'w') as f:
json.dump(CONFIG,f)
else:
with open(CONFIG_PATH,'r') as f:
CONFIG=json.load(f)

def save_config():
with open(CONFIG_PATH,'w') as f:
json.dump(CONFIG,f)

load_config()
menu_options = (('Change Volume',None, _on_change_vol_click),)
sys_tray=SysTrayIcon(os.path.join(resource_dir,'icon.ico'),'Mic Volume Fixer',menu_options,on_quit=_on_quit)
sys_tray.start()


process:subprocess.Popen=None
while not STOP:
start=time.time()
# print(CONFIG['volume_percent'])
cmd=f'{NIRCMDC_PATH} setsysvolume {int(65535*(CONFIG["volume_percent"]/100))} default_record'
process=subprocess.Popen(cmd,stdout=None,stderr=None,creationflags = CREATE_NO_WINDOW)
end=time.time()
sleep_time=CONFIG['time_between_runs']-(end-start)
if sleep_time>0:
# print(f'Sleeping {sleep_time}')
time.sleep(sleep_time)
Binary file added resources/icon.ico
Binary file not shown.
27 changes: 27 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
def is_executable()->bool:
"""
Determine if the current script is packaged as an executable\n
(EG: If packed into a .exe with PyInstaller)\n
returns : True/False, if the script is an executable
"""
import sys
return getattr(sys,'frozen',False)

def script_dir()->str:
"""
Get the path to the current script's directory, whether running as an executable or in an interpreter.\n
returns : A string containing the path to the script directory.
"""
from os import path
import sys
return path.dirname(sys.executable) if is_executable() else os.path.join(path.dirname(path.realpath(sys.argv[0])))

def local_path(dir_name:str='')->str:
"""
Get the absolute path to a local file/directory __MEIPASS or .), whether running as an executable or in an interpreter.\n
returns : A string containing the path to the local file/directory
"""
from os import path
import sys
return path.join(sys._MEIPASS, dir_name) if is_executable() else path.join(script_dir(),dir_name)

0 comments on commit 61f9466

Please sign in to comment.