added feature for playing sounds backwards

This commit is contained in:
jenz 2024-10-15 19:04:18 +02:00
parent 5c9de31835
commit fad8daf05d
3 changed files with 178 additions and 120 deletions

View File

@ -303,8 +303,9 @@ class AudioClip():
def __del__(self): def __del__(self):
self.Logger.info("~AudioClip()") self.Logger.info("~AudioClip()")
def Play(self, seconds = None, rubberband = None, dec_params = None, *args): def Play(self, seconds = None, rubberband = None, dec_params = None, bitrate = None, backwards = None, *args):
return self.AudioPlayer.PlayURI(self.URI, position = seconds, rubberband = rubberband, dec_params = dec_params, *args) return self.AudioPlayer.PlayURI(self.URI, position = seconds, rubberband = rubberband, dec_params = dec_params, bitrate = bitrate,
backwards = backwards, *args)
def Stop(self): def Stop(self):
return self.AudioPlayer.Stop() return self.AudioPlayer.Stop()

View File

@ -8,6 +8,33 @@ import math
from .Utils import Utils, DataHolder from .Utils import Utils, DataHolder
import traceback import traceback
def get_birtate(message):
bitrate = []
try:
for msg in message[1].split(" "): #checking if
if "bitrate=" in msg:
bitrate = int(msg.split("bitrate=",1)[1])
if bitrate < 0.0:
bitrate = 0.01
if bitrate > 2000:
bitrate = 20
bitrate.append(f"bitrate=tempo={bitrate}")
except Exception:
pass
return bitrate
def get_backwards(message):
backwards = None
try:
for msg in message[1].split(" "): #checking if pitch= or tempo= is specified
if "backward=" in msg.lower():
backwards = True
elif "backwards=" in msg.lower():
backwards = True
except Exception:
pass
return backwards
def get_rubberBand(message): def get_rubberBand(message):
rubberband = [] rubberband = []
try: try:
@ -548,6 +575,8 @@ class VoiceCommands(BaseCommand):
return 0 return 0
rubberband = get_rubberBand(message) rubberband = get_rubberBand(message)
backwards = get_backwards(message)
bitrate = get_birtate(message)
if message[0] == "!random": if message[0] == "!random":
Trigger = self.random.choice(list(self.VoiceTriggers.values())) Trigger = self.random.choice(list(self.VoiceTriggers.values()))
@ -614,7 +643,7 @@ class VoiceCommands(BaseCommand):
if not AudioClip: if not AudioClip:
return 1 return 1
return AudioClip.Play(rubberband = rubberband) return AudioClip.Play(rubberband = rubberband, bitrate = bitrate, backwards = backwards)
class YouTube(BaseCommand): class YouTube(BaseCommand):
@ -647,7 +676,9 @@ class YouTube(BaseCommand):
#turning the string into a list where get_rubberband just picks the second element to search in #turning the string into a list where get_rubberband just picks the second element to search in
dline = ['', line.split(" ", 1)[1]] dline = ['', line.split(" ", 1)[1]]
rubberband = get_rubberBand(dline) rubberband = get_rubberBand(dline)
return AudioClip.Play(Time, rubberband = rubberband) backwards = get_backwards(dline)
bitrate = get_birtate(dline)
return AudioClip.Play(Time, rubberband = rubberband, bitrate = bitrate, backwards = backwards)
class YouTubeSearch(BaseCommand): class YouTubeSearch(BaseCommand):
import json import json
@ -693,7 +724,9 @@ class YouTubeSearch(BaseCommand):
#turning the string into a list where get_rubberband just picks the second element to search in #turning the string into a list where get_rubberband just picks the second element to search in
dline = ['', line.split(" ", 1)[1]] dline = ['', line.split(" ", 1)[1]]
rubberband = get_rubberBand(dline) rubberband = get_rubberBand(dline)
return AudioClip.Play(Time, rubberband = rubberband) backwards = get_backwards(dline)
bitrate = get_birtate(dline)
return AudioClip.Play(Time, rubberband = rubberband, bitrate = bitrate, backwards = backwards)
class Say(BaseCommand): class Say(BaseCommand):
@ -721,9 +754,17 @@ class Say(BaseCommand):
try: try:
dline = ['', message.split(" ", 1)[1]] dline = ['', message.split(" ", 1)[1]]
rubberband = get_rubberBand(dline) rubberband = get_rubberBand(dline)
backwards = get_backwards(dline)
except Exception: except Exception:
rubberband = None rubberband = None
if AudioClip.Play(rubberband = rubberband): backwards = None
try:
dline = ['', message.split(" ", 1)[1]]
bitrate = get_birtate(dline)
except Exception:
bitrate = None
if AudioClip.Play(rubberband = rubberband, bitrate = bitrate, backwards = backwards):
AudioClip.AudioPlayer.AddCallback("Stop", lambda: os.unlink(TempFile.name)) AudioClip.AudioPlayer.AddCallback("Stop", lambda: os.unlink(TempFile.name))
return 0 return 0
else: else:

View File

@ -57,7 +57,8 @@ class FFmpegAudioPlayer():
self.Master.Logger.debug("~FFmpegAudioPlayer()") self.Master.Logger.debug("~FFmpegAudioPlayer()")
self.Stop() self.Stop()
def PlayURI(self, uri, position, rubberband = None, dec_params = None, *args): def PlayURI(self, uri, position, rubberband = None, dec_params = None, bitrate = None,
backwards = None, *args):
if position: if position:
PosStr = str(datetime.timedelta(seconds = position)) PosStr = str(datetime.timedelta(seconds = position))
Command = ["/usr/bin/ffmpeg", "-ss", PosStr, "-i", uri, "-acodec", "pcm_s16le", "-ac", "1", "-ar", str(int(self.SampleRate)), "-f", "s16le", "-vn", *args] Command = ["/usr/bin/ffmpeg", "-ss", PosStr, "-i", uri, "-acodec", "pcm_s16le", "-ac", "1", "-ar", str(int(self.SampleRate)), "-f", "s16le", "-vn", *args]
@ -68,6 +69,14 @@ class FFmpegAudioPlayer():
if dec_params: if dec_params:
Command += dec_params Command += dec_params
if rubberband and backwards:
Command += ["-filter:a"]
rubberCommand = ""
for rubber in rubberband:
rubberCommand += rubber + ", "
rubberCommand = rubberCommand[:-2]
Command += [rubberCommand + "[reversed];[reversed]areverse"] #[reversed] is intermediate stream label so reverse knows what stream label to reverse
else:
if rubberband: if rubberband:
Command += ["-filter:a"] Command += ["-filter:a"]
rubberCommand = "" rubberCommand = ""
@ -75,7 +84,14 @@ class FFmpegAudioPlayer():
rubberCommand += rubber + ", " rubberCommand += rubber + ", "
rubberCommand = rubberCommand[:-2] rubberCommand = rubberCommand[:-2]
Command += [rubberCommand] Command += [rubberCommand]
if backwards:
Command += ["-af"]
Command += ["areverse"]
if bitrate:
Command += ["-ab ", str(bitrate), "k"]
self.Master.Logger.debug(f"command: {Command}")
Command += ["-"] Command += ["-"]
#self.Master.Logger.debug(f"command: {Command}")
asyncio.ensure_future(self._stream_subprocess(Command)) asyncio.ensure_future(self._stream_subprocess(Command))
return True return True