Sundpood/main.py

307 lines
9.9 KiB
Python
Raw Normal View History

2020-09-07 15:32:38 +03:00
import os
import sys
import json
import threading
import keyboard
from pynput.keyboard import Listener
2020-09-07 15:32:38 +03:00
import soundfile as sf
import sounddevice as sd
2020-10-17 23:51:55 +03:00
from PyQt5 import QtWidgets, QtGui, QtCore
import ui_sundpood
import ui_overlay
import keys
2020-10-17 23:51:55 +03:00
class OverlayUi(QtWidgets.QMainWindow, ui_overlay.Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_F1:
self.hide()
win.show()
class MainUi(QtWidgets.QMainWindow, ui_sundpood.Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_F1:
self.hide()
over.show()
2020-09-07 15:32:38 +03:00
2020-09-07 15:32:38 +03:00
###! JSON !###
def jsonread(file): ## Чтение JSON
with open(file, "r", encoding='utf-8') as read_file:
data = json.load(read_file)
return data
def jsonwrite(file, data): ## Запись JSON
with open(file, 'w', encoding='utf-8') as write_file:
write_file.write(json.dumps(data))
###! FUNCTIONS !###
def found_device(list_): # Поиск микшера VoiceMeeter
index = 0
for i in list_:
if 'VoiceMeeter Input' in i['name']:
break
index += 1
return index
def sound_get(mode): # Сбор файлов
def check_format(name, format_): # Проверка слова до точки с конца строки
suf = ''
while name[-1] != '.':
suf += name[-1]
name = name[:-1]
suf = suf[::-1]
if type(format_) is list:
if suf in format_:
return True
else:
return False
elif type(format_) is str:
if suf == format_:
return True
else:
return False
def sound_convert(path, name, format_): # Конвертация из форматов 'mp3' 'm4a' в format_
old_name = name
if check_format(name, ['mp3', 'm4a']):
while name[-1] != '.':
name = name[:-1]
name += format_
2020-10-17 23:51:55 +03:00
os.system(f'''ffmpeg.exe -i "{os.path.join(path, old_name)}"
"{os.path.join(path, name)}"''')
os.remove(f'{os.path.join(path, old_name)}')
return name
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Critical)
msg.setText("You don't have any sounds in 'sound' folder")
msg.setInformativeText('download sound in .wav / .mp3 / .m4a format')
msg.setWindowTitle('Error')
2020-10-17 23:51:55 +03:00
if os.path.exists('settings.json') and mode == False:
sounds_list = jsonread('settings.json')
elif not os.path.exists('settings.json') or mode == True:
2020-09-07 15:32:38 +03:00
if os.path.exists('sound'):
if len(os.listdir('sound')) == 0:
2020-09-07 15:32:38 +03:00
msg.exec_()
exit()
2020-10-17 23:51:55 +03:00
menu = []
sounds_list = ['sound\\']
for i in os.listdir('sound'):
if os.path.isfile(os.path.join('sound', i)):
i = sound_convert('sound', i, 'wav')
sounds_list.append(i)
2020-10-17 23:51:55 +03:00
else:
sounds_list_cat = [os.path.join('sound', i)]
for x in os.listdir(os.path.join('sound', i)):
if os.path.isfile(os.path.join('sound', i, x)):
x = sound_convert(os.path.join('sound', i), x, 'wav')
sounds_list_cat.append(x)
menu.append(sounds_list_cat)
menu.append(sounds_list)
2020-10-17 23:51:55 +03:00
sounds = []
for i in os.listdir('sound'):
if os.path.isfile(os.path.join('sound', i)):
if check_format(i, 'wav'):
sounds.append(i)
2020-09-07 15:32:38 +03:00
if os.path.exists('settings.json'):
hotkeys = jsonread('settings.json')['hotkeys']
sounds_list = {'sound':sounds,
'hotkeys':hotkeys,
'menu':menu}
2020-09-07 15:32:38 +03:00
else:
sounds_list = {'sounds':sounds,
'hotkeys':{'Push button':''},
'menu':menu}
2020-09-07 15:32:38 +03:00
for i in COMBOS:
i.addItems(sounds)
2020-10-17 23:51:55 +03:00
jsonwrite('settings.json', sounds_list)
2020-09-07 15:32:38 +03:00
else:
msg.exec_()
exit()
return sounds_list
2020-09-12 13:44:23 +03:00
def save(): # Сохранение списка хоткеев
hotkeys = {}
2020-10-17 23:51:55 +03:00
sounds = sound_get(False)
for i in range(len(COMBOS)):
hotkeys.setdefault(HOTKEYS[i].text(), COMBOS[i].currentText())
sounds_list = {'sounds':sounds, 'hotkeys':hotkeys, 'menu':menu}
jsonwrite('settings.json', sounds_list)
2020-09-07 15:32:38 +03:00
sounds = None
hotkeys = None
2020-09-12 13:44:23 +03:00
def play_sound(index): # Проигрываение звука
print(index)
2020-10-17 23:51:55 +03:00
try:
filename = COMBOS[index].currentText()
2020-10-20 23:41:25 +03:00
try:
data, fs = sf.read(os.path.join('sound', filename), dtype='float32')
sd.play(data, fs)
keyboard.wait(sd.play())
sd.wait()
except:
pass
2020-10-17 23:51:55 +03:00
except:
filename = menu[select[0]][select[1]]
2020-10-20 23:41:25 +03:00
try:
data, fs = sf.read(os.path.join(menu[select[0]][0], filename), dtype='float32')
sd.play(data, fs)
keyboard.wait(sd.play())
sd.wait()
except:
pass
2020-09-07 15:32:38 +03:00
def hotkey_remap(btn): # Переназначение хоткеев
def check(key):
button = HOTKEYS[btn]
key = str(key)
if key not in keys.forbidden:
print(key)
COMMANDS[COMMANDS.index(button.text())] = key.replace("'",'')
button.setText(key.replace("'",''))
for i in HOTKEYS:
if i != HOTKEYS[btn]:
i.setEnabled(True)
return False
for i in HOTKEYS:
if i != HOTKEYS[btn]:
i.setEnabled(False)
hotkey_remap_Listener = Listener(
on_release=check)
hotkey_remap_Listener.start()
2020-09-12 13:44:23 +03:00
2020-10-17 23:51:55 +03:00
###! CONTROL !###
def key_check(key): # Хоткеи
2020-10-17 23:51:55 +03:00
def select_move(mode):
select[1] += mode[1]
select[0] += mode[0]
if select[0] > len(menu)-1 or select[0] < -len(menu)+1:
select[0] = 0
if select[1] > len(menu[select[0]])-1 or select[1] < -len(menu[select[0]])+1:
select[1] = 0
if mode[0] != 0:
2020-10-17 23:51:55 +03:00
select[1] = 0
over.label.setText(menu[select[0]][select[1]])
win.select_label.setText(menu[select[0]][select[1]])
key = str(key)
if key.replace("'",'') in COMMANDS:
play_sound(COMMANDS.index(key.replace("'",'')))
2020-10-17 23:51:55 +03:00
keyboard.add_hotkey(72, select_move, args=[[0, -1]])
keyboard.add_hotkey(80, select_move, args=[[0, 1]])
keyboard.add_hotkey(77, select_move, args=[[1, 0]])
keyboard.add_hotkey(75, select_move, args=[[-1, 0]])
keyboard.add_hotkey(76, play_sound, args=[''])
2020-09-12 13:44:23 +03:00
keyboard.add_hotkey(73, sd.stop)
2020-09-07 15:32:38 +03:00
def main(): # Интерфейс
win.show()
key_check_Listener.start()
2020-10-17 23:51:55 +03:00
win.save_button.clicked.connect(save)
win.hotkey_1.clicked.connect(lambda: hotkey_remap(0))
win.hotkey_2.clicked.connect(lambda: hotkey_remap(1))
win.hotkey_3.clicked.connect(lambda: hotkey_remap(2))
win.hotkey_4.clicked.connect(lambda: hotkey_remap(3))
win.hotkey_5.clicked.connect(lambda: hotkey_remap(4))
win.hotkey_6.clicked.connect(lambda: hotkey_remap(5))
win.hotkey_7.clicked.connect(lambda: hotkey_remap(6))
win.hotkey_8.clicked.connect(lambda: hotkey_remap(7))
win.hotkey_9.clicked.connect(lambda: hotkey_remap(8))
win.hotkey_10.clicked.connect(lambda: hotkey_remap(9))
win.hotkey_11.clicked.connect(lambda: hotkey_remap(10))
win.hotkey_12.clicked.connect(lambda: hotkey_remap(11))
2020-09-07 15:32:38 +03:00
if __name__ == '__main__':
2020-10-17 23:51:55 +03:00
### Поиск устроства ввода ###
2020-09-07 15:32:38 +03:00
list_ = list(sd.query_devices())
index = found_device(list_)
sd.default.device = list_[index]['name']
2020-10-17 23:51:55 +03:00
### Создание окна ###
2020-09-07 15:32:38 +03:00
app = QtWidgets.QApplication([])
2020-10-17 23:51:55 +03:00
over = OverlayUi()
win = MainUi()
2020-09-07 15:32:38 +03:00
COMBOS = [
win.combo0,
win.combo1,
win.combo2,
win.combo3,
win.combo4,
win.combo5,
win.combo6,
win.combo7,
win.combo8,
win.combo9,
win.combo10,
win.combo11,]
HOTKEYS = [
win.hotkey_1,
win.hotkey_2,
win.hotkey_3,
win.hotkey_4,
win.hotkey_5,
win.hotkey_6,
win.hotkey_7,
win.hotkey_8,
win.hotkey_9,
win.hotkey_10,
win.hotkey_11,
win.hotkey_12,]
sound_get_dict = sound_get(True)
### Глобальные переменные ###
sounds = sound_get_dict['hotkeys']
menu = sound_get_dict['menu']
select = [0, 0]
combo = 0
for i in sounds.items():
index = COMBOS[combo].findText(i[1])
COMBOS[combo].setCurrentIndex(index)
HOTKEYS[combo].setText(i[0])
combo += 1
combo = None
2020-09-07 15:32:38 +03:00
COMMANDS = [
HOTKEYS[0].text(),
HOTKEYS[1].text(),
HOTKEYS[2].text(),
HOTKEYS[3].text(),
HOTKEYS[4].text(),
HOTKEYS[5].text(),
HOTKEYS[6].text(),
HOTKEYS[7].text(),
HOTKEYS[8].text(),
HOTKEYS[9].text(),
HOTKEYS[10].text(),
HOTKEYS[11].text(),]
key_check_Listener = Listener(
on_release=key_check)
2020-10-17 23:51:55 +03:00
2020-09-07 15:32:38 +03:00
main()
sys.exit(app.exec())