def listen_command():

    t2i_en = {
        'play': 0,
        'next': 1,
        'stop': 2,
        'search': 3,
        'select song': 4,
        'exit': 5
    }
    t2i_vn = {
        'phát': 0,
        'tiếp tục': 0,
        'tiếp theo': 1,
        'kế tiếp': 1,
        'dừng lại': 2,
        'tìm kiếm': 3,
        'chọn bài hát': 4,
        'chọn bài': 4,
        'thoát': 5,
        'thoát chương trình': 5
    }

    match = False
    while match != True:
        t2s('speech command')
        text = recognize()
        if text in t2i_en.keys() or text in t2i_vn.keys():
            if text in t2i_en.keys():
                command = t2i_en[text]
                match = True
            elif text in t2i_vn.keys():
                command = t2i_vn[text]
                match = True
    return int(command)
    def selectAndPlaySongByIndex(self):
        title_audio = list(self.listAudio_text.keys())[self.idx_audio]
        link_audio = list(self.listAudio_text.values())[self.idx_audio]
        link_audio = getLinkAudio(link_audio)

        t2s('Start play: {}'.format(self.idx_audio + 1))

        self.setMediaPlayerUrl(link_audio)
        self.PlayPause()
        self.status.setText('Đang phát: {}'.format(title_audio))
    def VoiceSearch(self):
        self.Pause()
        t2s('Start Voice Search')

        self.status.setText('Nói từ khóa')
        keyword = getVoiceKeyWord()  # có xác nhận key
        #t2s('')
        # keyword = recognize('keyword')# không cần xác nhận
        t2s('search for {}'.format(keyword))
        self.status.setText('Tìm kiếm cho: "{}"'.format(keyword))
        self.searchInput.setText(keyword)
        self.Search()
def getVoiceKeyWord():
    match = False
    while match != True:
        key = recognize('keyword')
        t2s('you are want to find: {}'.format(key))
        cf = recognize('YES or OK to confirm and NO to try again')
        if cf in ['yes', 'ok', 'oke', 'đúng', 'phải', 'đồng ý']:
            t2s('ok')
            match = True
        else:
            t2s('oh sorry! please speech keyword again')
    return key
 def excCommand(self):
     self.Pause()
     t2s('OK. Im here')
     t2s('Please speech a command')
     command = listen_command()
     if command == 0:
         self.PlayPause()
     elif command == 1:
         self.voiceNext()
     elif command == 2:
         self.Stop()
     elif command == 3:
         self.VoiceSearch()
     elif command == 4:
         self.voiceSelect()
     elif command == 5:
         t2s('You are sure to exit!')
         t2s('Speech Yes or OK to comfirm!')
         cf_exit = recognize('')
         if cf_exit == 'yes' or cf_exit == 'ok' or cf_exit == 'oke':
             exit()
         else:
             print('no exit')
示例#6
0
def recognize(note=''):
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        t2s('start speech ' + note)
        t2s('START')
        #print('start speech')
        # while True:
        audio = r.listen(source)
        t2s('DONE')
        #print('done')
        try:
            #print('infering...')
            text = r.recognize_google(audio, language='vi-VN').lower()
        except:
            print('speech recognition error')
            text = 'error'
        print('#speech2text:' + text)
    return text
    def Search(self, get_idx_by_user=False):
        self.Pause()
        self.listAudio.clear()
        if self.searchInput.text() != '':
            print('keyword:', self.searchInput.text())
            print('searching...')
            results = YoutubeSearch(self.searchInput.text(),
                                    max_results=10).to_json()
            results = json.loads(results)
            results = results["videos"]

            if len(results) == 0:
                self.status.setText('Không có kết quả hoặc lỗi')
                t2s('try again')
                #self.searchInput.setText('')
            else:
                self.listAudio_text = {}
                self.list_title = [e["title"] for e in results]
                print(results)
                for i, item in enumerate(results):
                    itemtitle = item['title']
                    itemlink = 'https://www.youtube.com' + item['link']
                    self.listAudio.insertItem(
                        i + 1,
                        str(i + 1) + ': ' + itemtitle + '#link#' + itemlink)
                    self.listAudio_text[itemtitle] = itemlink
                    self.status.setText('Kết quả cho: "{}"'.format(
                        self.searchInput.text()))

                #self.searchInput.setText('')
                t2s('seach done')
                if get_idx_by_user:
                    #sau khi search, cho người dùng chọn bài hát bằng voice
                    self.idx_audio = select_by_speech()
                else:
                    #cho chế độ tự động phát ramdom. phát bài đầu tiên trong list. không cần chọn bằng voice
                    self.idx_audio = 0
                sleep(1)
                self.selectAndPlaySongByIndex()
        else:
            self.status.setText('Nhập từ khóa hoặc tìm bằng giọng nói')
            t2s('enter keyword or voice search')
 def Wellcome(self):
     #sleep(1)
     t2s('Hello! Wellcome to music player')
     t2s('Let search a song!')
     self.VoiceSearch()
def select_by_speech():
    t2i_en = {
        'one': 1,
        'two': 2,
        'three': 3,
        'four': 4,
        'five': 5,
        'six': 6,
        'seven': 7,
        'eight': 8,
        'nine': 9,
        'ten': 10
    }
    t2i_en2 = {
        'number one': 1,
        'number two': 2,
        'number three': 3,
        'number four': 4,
        'number five': 5,
        'number six': 6,
        'number seven': 7,
        'number eight': 8,
        'number nine': 9,
        'number ten': 10
    }
    t2i_vn = {
        'một': 1,
        'hai': 2,
        'ba': 3,
        'bốn': 4,
        'năm': 5,
        'sáu': 6,
        'bẩy': 7,
        'tám': 8,
        'chín': 9,
        'mười': 10
    }
    t2i_vn2 = {
        'số 1': 1,
        'số 2': 2,
        'số 3': 3,
        'số 4': 4,
        'số 5': 5,
        'số 6': 6,
        'số 7': 7,
        'số 8': 8,
        'số 9': 9,
        'số 10': 10
    }
    t2i = {
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9,
        '10': 10
    }
    match = False
    while match != True:
        t2s('select song')
        text = recognize()
        if text in t2i_en.keys() or text in t2i_en2.keys(
        ) or text in t2i_vn.keys() or t2i_vn2.keys() or text in t2i.keys():
            if text in t2i_en.keys():
                number = t2i_en[text]
                match = True
            elif text in t2i_en2.keys():
                number = t2i_en2[text]
                match = True
            elif text in t2i_vn.keys():
                number = t2i_vn[text]
                match = True
            elif text in t2i_vn2.keys():
                number = t2i_vn2[text]
                match = True
            elif text in t2i.keys():
                number = t2i[text]
                match = True
    return int(number) - 1
def waitForSpeech():
    t2s('I am listing')