added support for pitch= and tempo= to yt yts and sayen

This commit is contained in:
jenz 2024-08-06 14:42:15 +02:00
parent 2fbe20740f
commit 4b658e90d5
2 changed files with 133 additions and 111 deletions

View File

@ -87,6 +87,10 @@ class CommandHandler():
try:
if RMatch:
Ret = await Command._rfunc(line, RMatch, player)
else:
#self.Logger.debug(f"_rfunc line: {line}")
if line.startswith("!yt"): #transfering the line if its !yt or !yts in case people want to include pitch or tempo.
Ret = await Command._func(Message, player, line)
else:
Ret = await Command._func(Message, player)
except Exception as e:

View File

@ -8,6 +8,28 @@ import math
from .Utils import Utils, DataHolder
import traceback
def get_rubberBand(message):
rubberband = []
try:
for msg in message[1].split(" "): #checking if pitch= or tempo= is specified
if "tempo=" in msg:
tempo = float(msg.split("tempo=",1)[1])
if tempo < 0.0:
tempo = 0.01
if tempo > 20:
tempo = 20
rubberband.append(f"rubberband=tempo={tempo}")
elif "pitch=" in msg:
pitch = float(msg.split("pitch=",1)[1])
if pitch < 0.0:
pitch = 0.1
if pitch > 5.0:
pitch = 5.0
rubberband.append(f"rubberband=pitch={pitch}")
except Exception:
pass
return rubberband
class BaseCommand():
Order = 0
def __init__(self, torchlight):
@ -525,25 +547,8 @@ class VoiceCommands(BaseCommand):
elif Level < 2:
return 0
rubberband = []
try:
for msg in message[1].split(" "): #checking if pitch= or tempo= is specified
if "tempo=" in msg:
tempo = float(msg.split("tempo=",1)[1])
if tempo < 0.0:
tempo = 0.01
if tempo > 20:
tempo = 20
rubberband.append(f"rubberband=tempo={tempo}")
elif "pitch=" in msg:
pitch = float(msg.split("pitch=",1)[1])
if pitch < 0.0:
pitch = 0.1
if pitch > 99:
pitch = 99
rubberband.append(f"rubberband=pitch={pitch}")
except Exception:
pass
rubberband = get_rubberBand(message)
if message[0] == "!random":
Trigger = self.random.choice(list(self.VoiceTriggers.values()))
if isinstance(Trigger, list):
@ -561,6 +566,7 @@ class VoiceCommands(BaseCommand):
if isinstance(Sounds, list):
if Num and Num > 0 and Num <= len(Sounds):
Sound = Sounds[Num - 1]
elif message[1] and not message[1].startswith("tempo=") and not message[1].startswith("pitch="): #it does not start with pitch or with tempo, so must be a number or alias.
searching = message[1].startswith('?')
search = message[1][1:] if searching else message[1].split(" ")[0]
@ -617,7 +623,7 @@ class YouTube(BaseCommand):
self.Triggers = ["!yt"]
self.Level = 6
async def _func(self, message, player):
async def _func(self, message, player, line = None):
self.Logger.debug(sys._getframe().f_code.co_name + ' ' + str(message))
if self.check_disabled(player):
@ -638,7 +644,10 @@ class YouTube(BaseCommand):
if not AudioClip:
return 1
return AudioClip.Play(Time)
#turning the string into a list where get_rubberband just picks the second element to search in
dline = ['', line.split(" ", 1)[1]]
rubberband = get_rubberBand(dline)
return AudioClip.Play(Time, rubberband = rubberband)
class YouTubeSearch(BaseCommand):
import json
@ -648,7 +657,7 @@ class YouTubeSearch(BaseCommand):
self.Triggers = ["!yts"]
self.Level = 6 #adjusting to new levels
async def _func(self, message, player):
async def _func(self, message, player, line = None):
self.Logger.debug(sys._getframe().f_code.co_name + ' ' + str(message))
if self.check_disabled(player):
@ -663,7 +672,8 @@ class YouTubeSearch(BaseCommand):
Time = Utils.ParseTime(TimeStr)
message[1] = message[1][:Temp.value]
Proc = await asyncio.create_subprocess_exec("yt-dlp", "--dump-json", "--username", "oauth2", "--password", "''", "-xg", "ytsearch:" + message[1],
search_term = message[1].split("pitch=")[0].split("tempo=")[0]
Proc = await asyncio.create_subprocess_exec("yt-dlp", "--dump-json", "--username", "oauth2", "--password", "''", "-xg", "ytsearch:" + search_term,
stdout = asyncio.subprocess.PIPE)
Out, _ = await Proc.communicate()
@ -679,7 +689,11 @@ class YouTubeSearch(BaseCommand):
if not AudioClip:
return 1
self.Torchlight().LastUrl = url
return AudioClip.Play(Time)
#turning the string into a list where get_rubberband just picks the second element to search in
dline = ['', line.split(" ", 1)[1]]
rubberband = get_rubberBand(dline)
return AudioClip.Play(Time, rubberband = rubberband)
class Say(BaseCommand):
@ -692,7 +706,8 @@ class Say(BaseCommand):
self.Level = 2
async def Say(self, player, language, message):
GTTS = self.gtts.gTTS(text = message, lang = language)
actual_message = message.split("pitch=")[0].split("tempo=")[0]
GTTS = self.gtts.gTTS(text = actual_message, lang = language)
TempFile = self.tempfile.NamedTemporaryFile(delete = False)
GTTS.write_to_fp(TempFile)
@ -702,8 +717,11 @@ class Say(BaseCommand):
if not AudioClip:
os.unlink(TempFile.name)
return 1
#turning the string into a list where get_rubberband just picks the second element to search in
dline = ['', message.split(" ", 1)[1]]
rubberband = get_rubberBand(dline)
if AudioClip.Play():
if AudioClip.Play(rubberband = rubberband):
AudioClip.AudioPlayer.AddCallback("Stop", lambda: os.unlink(TempFile.name))
return 0
else: