added feature for playing sounds backwards
This commit is contained in:
		
							parent
							
								
									5c9de31835
								
							
						
					
					
						commit
						fad8daf05d
					
				@ -303,8 +303,9 @@ class AudioClip():
 | 
			
		||||
	def __del__(self):
 | 
			
		||||
		self.Logger.info("~AudioClip()")
 | 
			
		||||
 | 
			
		||||
    def Play(self, seconds = None, rubberband = None, dec_params = None, *args):
 | 
			
		||||
        return self.AudioPlayer.PlayURI(self.URI, position = seconds, rubberband = rubberband, dec_params = dec_params, *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, bitrate = bitrate, 
 | 
			
		||||
            backwards = backwards, *args)
 | 
			
		||||
 | 
			
		||||
	def Stop(self):
 | 
			
		||||
		return self.AudioPlayer.Stop()
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,33 @@ import math
 | 
			
		||||
from .Utils import Utils, DataHolder
 | 
			
		||||
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):
 | 
			
		||||
    rubberband = []
 | 
			
		||||
    try:
 | 
			
		||||
@ -548,6 +575,8 @@ class VoiceCommands(BaseCommand):
 | 
			
		||||
            return 0
 | 
			
		||||
        
 | 
			
		||||
        rubberband = get_rubberBand(message)
 | 
			
		||||
        backwards = get_backwards(message)
 | 
			
		||||
        bitrate = get_birtate(message)
 | 
			
		||||
 | 
			
		||||
        if message[0] == "!random":
 | 
			
		||||
            Trigger = self.random.choice(list(self.VoiceTriggers.values()))
 | 
			
		||||
@ -614,7 +643,7 @@ class VoiceCommands(BaseCommand):
 | 
			
		||||
        if not AudioClip:
 | 
			
		||||
            return 1
 | 
			
		||||
 | 
			
		||||
        return AudioClip.Play(rubberband = rubberband)
 | 
			
		||||
        return AudioClip.Play(rubberband = rubberband, bitrate = bitrate, backwards = backwards)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
        dline = ['', line.split(" ", 1)[1]]
 | 
			
		||||
        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):
 | 
			
		||||
    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
 | 
			
		||||
        dline = ['', line.split(" ", 1)[1]]
 | 
			
		||||
        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):
 | 
			
		||||
@ -721,9 +754,17 @@ class Say(BaseCommand):
 | 
			
		||||
        try:
 | 
			
		||||
            dline = ['', message.split(" ", 1)[1]]
 | 
			
		||||
            rubberband = get_rubberBand(dline)
 | 
			
		||||
            backwards = get_backwards(dline)
 | 
			
		||||
        except Exception:
 | 
			
		||||
            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))
 | 
			
		||||
            return 0
 | 
			
		||||
        else:
 | 
			
		||||
 | 
			
		||||
@ -57,7 +57,8 @@ class FFmpegAudioPlayer():
 | 
			
		||||
        self.Master.Logger.debug("~FFmpegAudioPlayer()")
 | 
			
		||||
        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:
 | 
			
		||||
            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]
 | 
			
		||||
@ -68,6 +69,14 @@ class FFmpegAudioPlayer():
 | 
			
		||||
        if 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:
 | 
			
		||||
                Command += ["-filter:a"]
 | 
			
		||||
                rubberCommand = ""
 | 
			
		||||
@ -75,7 +84,14 @@ class FFmpegAudioPlayer():
 | 
			
		||||
                    rubberCommand += rubber + ", "
 | 
			
		||||
                rubberCommand = rubberCommand[:-2]
 | 
			
		||||
                Command += [rubberCommand]
 | 
			
		||||
            if backwards:
 | 
			
		||||
                Command += ["-af"]
 | 
			
		||||
                Command += ["areverse"]
 | 
			
		||||
        if bitrate:
 | 
			
		||||
            Command += ["-ab ", str(bitrate), "k"]
 | 
			
		||||
            self.Master.Logger.debug(f"command: {Command}")
 | 
			
		||||
        Command += ["-"]
 | 
			
		||||
        #self.Master.Logger.debug(f"command: {Command}")
 | 
			
		||||
        asyncio.ensure_future(self._stream_subprocess(Command))
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user