Example #1
0
def play_music1():
    global PLAYING_MUSIC1
    while True:
        while not PLAYING_MUSIC1:
            time.sleep(0.00001)
        ps.playsound(
            "/Users/wangan/Documents/calhacks2017/temp_wav/tempViolin1.wav")
Example #2
0
def play_music2():
    global PLAYING_MUSIC2
    while True:
        while not PLAYING_MUSIC2:
            time.sleep(0.00001)
        ps.playsound(
            "/Users/wangan/Documents/calhacks2017/temp_wav/tempBass2.wav")
Example #3
0
def speak(audio_string):
    tts = gTTS(text=audio_string, lang='en')  # text to speech(voice)
    r = random.randint(1, 20000000)
    audio_file = 'audio' + str(r) + '.mp3'
    tts.save(audio_file)  # save as mp3
    playsound.playsound(audio_file)  # play the audio file
    print(f"Elpie: {audio_string}")  # print what app said
    os.remove(audio_file)  # remove audio file
def wakeup():
    """
    弃用
    :return:
    """
    playsound.playsound("../SystemSpeech/service/do.mp3")
    command_path="command.wav"
    record_vad.record_vad(filePath=command_path,speechCount=50)  #当前文件夹   命令时长应比较长
    command_path=command_path.split('wav')[0]+"pcm"
    SpeechRecognition.Recognition(command_path)     #识别并做处理
def recordAndtrain():
    playsound.playsound("../SystemSpeech/trainVoice/start_set.mp3")
    playsound.playsound("../SystemSpeech/trainVoice/start_set1.mp3")
    record.record_vad("wav1.wav")
    playsound.playsound("../SystemSpeech/trainVoice/start_set2.mp3")
    record.record_vad("wav2.wav")
    playsound.playsound("../SystemSpeech/trainVoice/start_set3.mp3")
    record.record_vad("wav3.wav")
    playsound.playsound("../SystemSpeech/trainVoice/training.mp3")
    trainHotWord(wav1="wav1.wav",
                 wav2="wav2.wav",
                 wav3="wav3.wav",
                 out="ye11.pmdl")
Example #6
0
 def onEvent(self, event):
     if isinstance(event, SkypeNewMessageEvent) \
       and not event.msg.userId == self.userId :
         message_describe = event.msg.user.raw
         friend_name = message_describe['display_name']  # 获取好友的名称
         message_detail = "你的好友" + friend_name + "发来一条消息," + event.msg.content
         SpeechSynthesis.Synthesis(message_detail,
                                   "../SystemSpeech/Skype/newMessage.mp3")
         # if(self.Queue.full()):
         #     self.Queue.get()    # 暂时没有使用消息队列的需要
         # self.Queue.put(friend_name)
         try:
             playsound.playsound("../SystemSpeech/Skype/newMessage.mp3")
             #存在有音频占用的可能性
         except Exception as e:
             print(e)
             playsound.playsound("../SystemSpeech/Skype/newMessage.mp3")
def sendQuestion(question=""):
    data = {}
    data['app_id'] = app_id
    data["time_stamp"] = str(int(time.time()))
    data['nonce_str'] = "".join(
        random.sample(string.ascii_letters + string.digits, 10))  #随机生成20个字符
    data['session'] = 10000  #暂时不明白这个是什么唯一,应该是对于一个应用内部的多个客户端而言
    data['sign'] = ""
    data['question'] = question
    sign = getSign(data)
    data['sign'] = sign
    response = doHttpGet(data)
    print(response)
    if (not response['ret'] == 0):
        playsound.playsound('../SystemSpeech/service/sorry.mp3')
    else:
        return response['data']['answer']
Example #8
0
    def execute(self, command: str):
        if 'navegar para' in command:
            url = command.split()[2:]
            self.voice(f'Abrindo {url}.')
            self.open_url(path=''.join(url))

        if 'executar' in command:
            executable = command.split()[1:]
            self.voice("Executando {}".format(executable))
            try:
                self.run_file(term=' '.join(executable))
            except NotFoundErr:
                self.voice('Infelizmente não foi encontrado nenhum caminho para a requisição solicitada.')

        if 'pesquisar' in command:
            searchable = command.replace('pesquisar', '')
            self.voice("Pesquisando {}".format(searchable))
            self.search(to_search=searchable)

        if 'me conte uma piada' in command or 'contar piada' in command:
            self.tell_joke()
        
        if 'reproduzir no' in command:
            local_to_reproduce = command.split()[2]
            if local_to_reproduce == 'youtube':
                url = Youtube(token=os.getenv('YT_TOKEN')).most_viewed(query=' '.join(command.split()[3:]))
                if url is not None:
                    self.voice(f'Reproduzindo em {command.split()[2]} {" ".join(command.split()[3:])}')
                    self.open_url(path=url)
                else:
                    self.voice(f'Nenhum resultado encontrado para: {" ".join(command.split()[3:])}')
            else:
                self.voice(f'Infelizmente {local_to_reproduce} ainda não está disponível.')
        
        if 'desligar computador' in command:
            self.voice('Desligando o computador... Boa noite, mestre!')
            os.system('shutdown -s')

        # if 'volume' in x:
        #     audio_controller = AudioController('firefox.exe')
        #     audio_controller.set_volume(float(path)/100)

        playsound.playsound('./resources/sfx/sleep.mp3')
    def wakeup(self):
        """
        通过小瓜小瓜的唤醒词进入的状态有两种
        1、命令模式:要符合命令的格式才会有响应
        2、闲聊模式:随便聊

        如果过程中出现网络错误,将会被此处劫住,然后进行语音提示
        :return:None
        """
        try:
            self.controled = 0
            self.oldValue = 0
            if (self.player.is_playing()):
                # 如果还在播放音乐
                self.player.pause()  # 先暂停播放
                self.controled = 1  # 记录:发生过暂停
                self.oldValue = 3  # 记录:每次修改时纪录旧值,如果不等于3,代表后面使用的其他关键的控制
            playsound.playsound("../SystemSpeech/service/do.mp3")
            if self.status == 0:
                command_path = "command.wav"
                record_vad.record_vad(filePath=command_path,
                                      speechCount=50)  # 当前文件夹   命令时长应比较长
                command_path = command_path.split('wav')[0] + "pcm"
                self.Recognition(command_path)  # 识别并做处理
            else:
                # 闲聊的时长限制多一些
                self.smallchat()
            # 产生语音控制时,会停止播放音乐
            # 只有oldValue==3才会继续播放
            if (self.controled == 1 and self.oldValue == 3):
                # 当发生了暂停、终止等命令后,不会进入此处
                print('触发恢复')
                self.player.resume()
                self.controled = 0
                self.oldValue = 0
        except ConnectionError as e:
            playsound.playsound("../SystemSpeech/service/netCheck.mp3")
 def smallchat(self):
     """
     进行闲聊,包含比较多的错误处理
     利用了百度语音识别和腾讯闲聊API
     :return:
     """
     try:
         code = record_vad.record_vad("smallchat.wav",
                                      50)  # 产生一个smallchat.pcm文件在底层
         if (code == -2):
             return  #认定识别错误,结束
         speech = AipSpeech(APP_ID, APP_KEY, SECRET_KEY)
         p = speech.asr(get_file_content('smallchat.pcm'), 'pcm', 16000, {
             'dev_pid': 1537,
         })
         command = p['result'][0]  #从json中提取识别出来的文字
         print(command)
         if (command == ""):
             playsound.playsound("../SystemSpeech/service/sorry.mp3")
             print("无法明白")
             return
         elif (command == "退出闲聊模式。"):
             playsound.playsound("../SystemSpeech/service/end_chat.mp3")
             print("退出闲聊模式")
             self.status = 0
             return
         else:
             # 封装好的闲聊函数
             response = SmallChat.sendQuestion(command)
             if (response == None):
                 return
             print(response)
             SpeechSynthesis.Synthesis(response, "smallchatResponse.mp3")
             playsound.playsound("smallchatResponse.mp3")
     except Exception as e:
         playsound.playsound("../SystemSpeech/service/netCheck.mp3")
Example #11
0
def playSound(_soundName, _useMainRegister):
    filePath = "assets/sfx/" + _soundName
    if os.path.isfile(filePath):
        playsound.playsound(filePath, _useMainRegister)
    else:
        print("Missing file: " + filePath)
Example #12
0
from playsound import playsound
playsound.playsound('fart-01.mp3', True)
Example #13
0
# play the converted audio 
import os 
  
# The text that you want to convert to audio 
mytext = 'দুর্নীতি দমন কমিশনের (দুদক) মামলায় সাবেক প্রধান বিচারপতি এস কে সিনহাসহ ১১ জন পলাতক আসামির নামে জাতীয় পত্রিকায় বিজ্ঞপ্তি প্রকাশের নির্দেশ দিয়েছেন আদালত। ঢাকা মহানগরের সিনিয়র স্পেশাল জজ কে এম ইমরুল কায়েস আজ বুধবার এ আদেশ দেন।আদালত সূত্র বলছে, এর আগে ৫ জানুয়ারি সাবেক প্রধান বিচারপতি এস কে সিনহাসহ ১১ জনের বিরুদ্ধে গ্রেপ্তারি পরোয়ানা জারি করেন আদালত। আজ ওই গ্রেপ্তারি পরোয়ানার তামিল প্রতিবেদন জমা দেওয়ার দিন ধার্য ছিল। পুলিশের পক্ষ থেকে আদালতে প্রতিবেদন দিয়ে বলা হয়, এস কে সিনহাসহ অন্যদের গ্রেপ্তার করা যায়নি। আদালত ওই প্রতিবেদন দিয়ে তাঁদের বিরুদ্ধে পত্রিকায় বিজ্ঞপ্তি প্রকাশের নির্দেশ দেন। এ–সংক্রান্ত প্রতিবেদন ও মামলার শুনানির জন্য নতুন দিন ঠিক করা হয়েছে আগামী ২০ ফেব্রুয়ারি।'
# Language in which you want to convert 
language = 'bn'
  
# Passing the text and language to the engine,  
# here we have marked slow=False. Which tells  
# the module that the converted audio should  
# have a high speed 
myobj = gTTS(text=mytext, lang=language, slow=False) 
  
# Saving the converted audio in a mp3 file named 
# welcome  
myobj.save("welcome.mp3") 
  
# Playing the converted file 
os.system("mpg321 welcome.mp3")

!pip install playsound

from gtts import gTTS
import playsound
import os
tts = gTTS(text='দর্শকেরা আমি তানভীর ইসলাম ইস্ত্রিম । চাইতেছি বাংলা  সহকারি বানাতে ? আর সেটা উন্মুক্ত করে দিতে । আপনারা কি এই প্রোজেক্ট এ কাজ করবেন ? ', lang='bn')
tts.save("good.mp3")
os.system("mpg321 good.mp3")
playsound.playsound('good.mp3', True)
Example #14
0
def gameloop():
    #game processing variables
    exitgame = False
    gameover = False
    score = 0
    velocity_x = 0
    velocity_y = 0
    #fps = 30

    snake_x = 100
    snake_y = 150
    food_x = r.randint(0, box_width)
    food_y = r.randint(0, box_height)

    #snake length
    snake_length = 1
    snake_list = []

    #game loop
    while exitgame != True:
        if gameover == True:
            #w.PlaySound("The Rowdy Tavern.wav", w.SND_ASYNC)                #gives sound effects in windows
            ps.playsound('The Rowdy Tavern.mp3', True)
            boxwindow.fill(grey)
            text_screen("GAME OVER!", "press ENTER to play again...", blue,
                        yellow, 250, 160, 230, 200)
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    exitgame = True
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_RETURN:
                        gameloop()
        else:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    exitgame = True
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_RIGHT:
                        velocity_x = speed
                        velocity_y = 0
                        #event.key != pg.K_LEFT
                    if event.key == pg.K_LEFT:
                        velocity_x = -speed
                        velocity_y = 0
                        #event.key != pg.K_RIGHT
                    if event.key == pg.K_UP:
                        velocity_x = 0
                        velocity_y = -speed
                        #event.key != pg.K_DOWN
                    if event.key == pg.K_DOWN:
                        velocity_x = 0
                        velocity_y = speed
                        #event.key != pg.K_UP
            ''' for event in pg.event.get():
                #if event.type == pg.KEYDOWN:
                if velocity_x == speed:
                    pg.K_LEFT = False 
                elif velocity_x == -speed:
                    pg.K_RIGHT = False
                elif velocity_y == -speed:
                    pg.K_DOWN = False
                else:
                    pg.K_UP = False '''

            #snake movement
            snake_x += velocity_x
            snake_y += velocity_y

            #collision or eating food
            #c = 10
            if abs(snake_x - food_x) < 10 and abs(snake_y - food_y) < 10:
                score += 10
                snake_length += 2
                food_x = r.randint(0, box_width)
                food_y = r.randint(0, box_height)
                #c += 20

            gamewindow.fill(grey)
            boxwindow.fill(white)
            text_screen("sNake GaMe", "score: " + str(score), green, red, 10,
                        5, 800, 5)
            pg.draw.rect(boxwindow, red, [food_x, food_y, 10, 10])

            #snake head for length
            snake_head = []
            snake_head.append(snake_x)
            snake_head.append(snake_y)

            snake_list.append(snake_head)
            #print(snake_list)
            if len(
                    snake_list
            ) > snake_length:  #if no. of lists of snake is more than snake length:
                del (snake_list[0])

            #snake eating itself (game over)
            ''' for x in snake_list:
                if abs(x[1] - snake_head[1])<5 and abs(x[2] - snake_head[2])<5:
                    exitgame = True '''

            if (snake_head in snake_list[:-1]
                ) or (snake_x or snake_y) < 0 or (snake_x > box_width) or (
                    snake_y >
                    box_height):  #list[-1]or[:-1] -> starting from last item
                gameover = True

            snake(boxwindow, black, snake_list, snake_size)
            """ e=[i*i for i in range(1,6)]
            #print("e is " + str(e))
            #print("e is " + str(e[-3:]))
            print("e is " + str(e[ :-1])) """

        pg.display.update()
        clock.tick(fps)
    pg.quit()
    quit()
def sound_alarm(path):  # play an alarm sound
    playsound.playsound(path)
Example #16
0
def wakeup():
    playsound.playsound("../SystemSpeech/do.mp3")
    record.record_vad("Command.wav")
    SpeechRecognition()
    def Recognition(self, Path='Hardware/speech.pcm'):
        """
        命令模式下的识别,编写的规则的主体
        通过正则表达式的匹配和match进行匹配,通过match的返回值进行处理
        :param Path:
        :return:
        """
        speech = AipSpeech(APP_ID, APP_KEY, SECRET_KEY)
        p = speech.asr(get_file_content(Path), 'pcm', 16000, {
            'dev_pid': 1537,
        })
        code = matchCode()
        # 一旦匹配就会立即返回不会再进行匹配
        if not p["err_no"] == 0:  #发生错误时,提示网络问题同时返回
            playsound.playsound("../SystemSpeech/service/netCheck.mp3")
            return
        command = p['result'][0]
        print(command)
        if ("天气" in command):  #天气模块
            imformation = match(
                p['result'][0],
                re.compile("^今天天气怎么样"),
                code.setWeatherCode(num=1),
                re.compile("^明天天气怎么样"),
                code.setWeatherCode(num=2, date=1, location=""),
                re.compile("^(\S+)今天天气怎么样"),
                lambda x: code.setWeatherCode(4, date=0, location=x),
                re.compile("^(\S+)明天天气怎么样"),
                lambda x: code.setWeatherCode(8, date=1, location=x),
                strict=False)
            print(imformation)
            if (not imformation):
                playsound.playsound('../SystemSpeech/service/sorry.mp3')
                return
            information = getWeather.getWeather(date=imformation[0],
                                                city=imformation[1])
            SpeechSynthesis.Synthesis(information, "response.mp3")
            playsound.playsound("response.mp3")
        elif ("现在" in command):  #  时间匹配
            imformation = match(command,
                                re.compile("现在是几点钟"),
                                code.setTimeCode(5),
                                re.compile("现在的时间是多少"),
                                code.setTimeCode(6),
                                strict=False)
            if (not imformation):
                playsound.playsound("../SystemSpeech/service/sorry.mp3")
                return
            nowTime = ""
            if (imformation == 5):
                nowTime = getTime.getTime()
            elif imformation == 6:
                nowTime = getTime.getFullTime()
            SpeechSynthesis.Synthesis(nowTime, "response.mp3")
            playsound.playsound("response.mp3")
        elif ("进入闲聊模式" in command):  # 闲聊只需要切换status变量即可
            print("进入闲聊模式")
            self.status = 1
        elif ("几点钟" in command):  #  时间,只是命令匹配不同关键字
            nowTime = ""
            nowTime = getTime.getFullTime()
            SpeechSynthesis.Synthesis(nowTime, "response.mp3")
            playsound.playsound("response.mp3")
        elif ("发送消息" in command):  # 设定从10 开始设定为发送消息的code
            imformation = match(
                command,
                re.compile("^发送消息给(\S+)。"),
                lambda x: code.setChatCode(code=10, contract=x),
                re.compile("^给(\S+)发送消息"),
                lambda x: code.setChatCode(code=10, contract=x),
                re.compile("^给(\S+)发送一条消息"),
                lambda x: code.setChatCode(code=10, contract=x),
                strict=False)
            if not imformation:  # 上述匹配代码匹配的话,如果失败会返回false,false即没有匹配上任何规则
                playsound.playsound("../SystemSpeech/service/sorry.mp3")
                return
            else:
                if len(self.friends) == 0:  # 如果由于网络原因,还未获得联系人字典的话,重新获取
                    playsound.playsound('../SystemSpeech/service/retry.mp3')
                    self.friends = self.SkpyeEvent.getFriends()
                    if len(self.friends) == 0:
                        playsound.playsound(
                            "../SystemSpeech/service/netCheck.mp3")
                        return
                whatMesssage = "发什么消息给" + imformation[1]
                SpeechSynthesis.Synthesis(
                    whatMesssage,
                    "../SystemSpeech/service/whatMessage.mp3")  # 根据联系人实时生成语音提示
                playsound.playsound("../SystemSpeech/service/whatMessage.mp3")
                record_vad.record_vad("message_contents.mp3", speechCount=50)
                message2text = speech.asr(
                    get_file_content("message_contents.mp3"), 'pcm', 16000, {
                        'dev_pid': 1537,
                    })
            # 语音发送失败会返回-1
            if (self.sendMessage(
                    message=message2text['result'][0],
                    contract=imformation[1]) == -1):  # imfotmation[1]是朋友的名称
                # 发送的消息如果失败了
                playsound.playsound(
                    "../SystemSpeech/service/contractNotFound.mp3")
            else:
                playsound.playsound("../SystemSpeech/Skype/sendSuccess.mp3")

        elif "回复消息" in command:  # 发送Skype消息,不一样的匹配规则
            imformation, friend = match(command, re.compile("给(\S+)回复消息"),
                                        lambda x: code.setChatCode(10, x),
                                        re.compile("回复消息"),
                                        code.setChatCode(code=11))
            if (not imformation == 10):
                playsound.playsound("../SystemSpeech/Skype/chooseWho.mp3")
                record_vad.record_vad("../SystemSpeech/Skype/who.wav", 40)
                friend = speech.asr(
                    get_file_content('../SystemSpeech/Skype/who.pcm'), 'pcm',
                    16000, {
                        'dev_pid': 1537,
                    })['result'][0]

            playsound.playsound("../SystemSpeech/Skype/whatMessage.mp3")
            record_vad.record_vad("../SystemSpeech/Skype/whatMessage.wav", 40)
            message = speech.asr(
                get_file_content('../SystemSpeech/Skype/whatMessage.pcm'),
                'pcm', 16000, {
                    'dev_pid': 1537,
                })['result'][0]
            if (self.sendMessage(
                    message=message,
                    contract=friend) == -1):  # imfotmation[1]是朋友的名称
                # 发送的消息如果失败了
                playsound.playsound(
                    "../SystemSpeech/service/contractNotFound.mp3")
            else:
                playsound.playsound("../SystemSpeech/Skype/sendSuccess.mp3")
        elif "播放音乐" in command or "播放歌曲" in command:
            self.oldValue = 0
            playsound.playsound("../SystemSpeech/music/musicNameRequired.mp3")
            record_vad.record_vad("../SystemSpeech/music/musicName.wav", 40)
            musicName = speech.asr(
                get_file_content("../SystemSpeech/music/musicName.pcm"), 'pcm',
                16000, {"dev_pid": 1537})['result'][0]
            print("歌曲名:", musicName.split("。")[0])
            self.player.set_uri(music.searchNetMusic(
                musicName.split("。")[0]))  #搜索同时播放音乐,去除识别产生的句号,实际也可以不去除
            self.player.play()
            return
        elif "暂停播放" in command:
            #音乐进程不为空 音乐进程仍然在播放
            if not self.player.playing_uri == "":
                #仍然在播放音乐的话
                playsound.playsound("../SystemSpeech/service/Idoit.mp3")
                self.oldValue = 0  # 告知,已修改
        elif "继续播放" in command:
            # 音乐进程不为空,音乐进程没有被杀死 音乐暂停播放
            if not self.player.playing_uri == "":  # 仍然在播放音乐的话
                playsound.playsound("../SystemSpeech/service/Idoit.mp3")
                self.player.resume()
                self.oldValue = 0
        elif "停止播放" in command:

            if not self.player.playing_uri == "":
                playsound.playsound("../SystemSpeech/service/Idoit.mp3")
                self.player.stop()
                self.oldValue = 0

        elif "增大音量" in command or "大声点" in command:
            # 如果在播放音乐,等等,实际上似乎用不上消息队列
            # 如果在播放音乐,增加音乐音量
            playsound.playsound("../SystemSpeech/service/Idoit.mp3")
            if self.player.is_playing():
                self.player.add_volume()
            else:
                #  TODO 如果没有播放音乐,则增加音箱音量  MAC端 迁移到树莓派时需要修正
                pass
        elif "减小音量" in command or "小声点" in command:
            # 如果在播放音乐,等等,实际上似乎用不上消息队列
            # 如果在播放音乐,减小音乐音量
            playsound.playsound("../SystemSpeech/service/Idoit.mp3")  # 好的
            if not self.player.playing_uri == "":
                self.player.sub_volume()  # 此处测试不通过消息队列处理
            else:
                pass