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('Bắt đầu phát') t2s(title_audio) self.addMediaPlayerUrl(link_audio) #self.setMediaList() next_key = next_keyword(self.list_title) self.searchInput.setText(next_key) self.getListSearch() title_audio2 = list(self.listAudio_text.keys())[self.idx_audio] link_audio2 = list(self.listAudio_text.values())[self.idx_audio] link_audio2 = getLinkAudio(link_audio2) self.addMediaPlayerUrl(link_audio2) self.setMediaList() print("=" * 20) print("hiện tại:", title_audio) print("tiếp theo", title_audio2) print("=" * 20) self.PlayPause() self.status.setText('Đang phát: {}'.format(title_audio))
def VoiceSearch(self): self.Stop() t2s('Chức năng tìm kiếm bằng giọng nói sẵn sàng') self.status.setText('Tìm kiếm giọng nói') keyword = getVoiceKeyWord() # có xác nhận key self.status.setText('Tìm kiếm cho: "{}"'.format(keyword)) self.searchInput.setText(keyword) self.Search()
def Next(self): self.Pause() self.status.setText("Tiếp theo") t2s("Tiếp theo") self.resetMediaPlayList() #self.idx_audio += 1 self.selectAndPlaySongByIndex() print("sau next:", self.medialist.count())
def getListSearch(self): self.status.setText('Đang tìm') if self.searchInput.text() == '': self.status.setText('Nhập từ khóa hoặc tìm bằng giọng nói') t2s('Vui lòng nhập từ khóa hoặc tìm kiếm bằng giọng nói') else: #t2s(self.searchInput.text()) print('keyword:', self.searchInput.text()) print('searching...') results = YoutubeSearch(self.searchInput.text(), max_results=5).to_json() if len(results) == 0: results = YoutubeSearch(self.searchInput.text(), max_results=3).to_json() results = json.loads(results) results = results["videos"] #print(results) #if self.fornext: # random.shuffle(results) #print(results) if len(results) == 0: if self.fornext: self.idx_audio = self.idx_audio + 1 #pass #getListSearch() else: self.status.setText('Không có kết quả hoặc lỗi') t2s('Tôi không thể tìm kiếm Hãy thử lại') #self.searchInput.setText('') else: #tìm kiếm mới self.listAudio_text = {} self.listAudio.clear() self.list_title = [e["title"] for e in results] for i, item in enumerate(results): itemlink = 'https://www.youtube.com' + item['link'] self.listAudio_text[self.list_title[i]] = itemlink self.status.setText('Kết quả cho: "{}"'.format( self.searchInput.text())) for i, item in enumerate(results): #nếu search gợi ý cho next thì chỉ hiển thị 1 bài tiếp theo if self.fornext: self.listAudio.insertItem( i + 1, 'Tiếp theo: ' + self.list_title[i]) self.idx_audio = 0 self.listAudio.repaint() break else: self.listAudio.insertItem( i + 1, str(i + 1) + ': ' + self.list_title[i]) self.listAudio.repaint() self.listAudio.repaint() print("search done")
def VoiceSearch(self): self.Pause() t2s('Chức năng tìm kiếm bằng giọng nói sẵn sàng') self.status.setText('Tìm kiếm giọng nói') keyword = getVoiceKeyWord() # có xác nhận key # 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 getListSearch(self): self.status.setText('Đang tìm') if self.searchInput.text() == '': self.status.setText('Nhập từ khóa hoặc tìm bằng giọng nói') t2s('Vui lòng nhập từ khóa hoặc tìm kiếm bằng giọng nói') else: print('keyword:', self.searchInput.text()) results = searchYoutube(self.searchInput.text(), offset=1, mode="json", max_results=5).result() results = json.loads(results) results = results["search_result"] #print(results) if len(results) == 0: if self.fornext: self.listAudio.clear() #self.resetMediaPlayList() self.idx_audio = self.idx_audio + 1 self.listAudio.insertItem( 0, 'Tiếp theo: ' + self.list_title[self.idx_audio]) self.listAudio.repaint() else: self.status.setText('Không có kết quả hoặc lỗi') t2s('Tôi không thể tìm kiếm Hãy thử lại') #pass else: #tìm kiếm mới self.listAudio_text = {} self.listAudio.clear() self.list_title = [e["title"] for e in results] print("list_title:", self.list_title[0]) for i, item in enumerate(results): itemlink = item['link'] self.listAudio_text[self.list_title[i]] = itemlink self.status.setText('Kết quả cho: "{}"'.format( self.searchInput.text())) for i, item in enumerate(results): #nếu search gợi ý cho next thì chỉ hiển thị 1 bài tiếp theo if self.fornext: self.listAudio.insertItem( i + 1, 'Tiếp theo: ' + self.list_title[i]) self.idx_audio = 0 self.listAudio.repaint() break else: self.listAudio.insertItem( i + 1, str(i + 1) + ': ' + self.list_title[i]) self.listAudio.repaint() self.listAudio.repaint() print("end search")
def Search(self): self.Pause() self.fornext = False t2s("đang tìm kiếm") self.getListSearch() try: _ = len(self.listAudio_text) t2s("Đây là những kết quả tôi tìm được. Vui lòng chọn một bài") self.idx_audio = select_by_speech() self.fornext = True self.selectAndPlaySongByIndex() except: self.status.setText('Không có kết quả hoặc lỗi')
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('Bắt đầu phát') t2s(title_audio) self.setMediaPlayerUrl(link_audio) next_key = next_keyword(self.list_title) self.searchInput.setText(next_key) self.getListSearch() self.PlayPause() self.status.setText('Đang phát: {}'.format(title_audio))
def getVoiceKeyWord(): match = False while True: #sleep(1) key = recognize('keyword') #t2s('you are want to find: {}'.format(key)) if key != "error": t2s("Có phải bạn muốn tìm kiếm {}".format(key)) cf = recognize('Nói OK hoặc Đồng ý để xác nhận') else: continue if cf in ['yes', 'ok', 'oke', 'đúng', 'phải', 'đồng ý']: t2s('Ok tìm kiếm cho {}'.format(key)) #match = True break #else: #t2s('oh sorry! please speech keyword again') return key
def listen_command(): t2s('Bạn muốn tôi làm gì hãy ra lệnh cho tôi') #t2s('Ví dụ tìm kiếm, kế tiếp, tiếp tục') t2i_en = { 'play': 0, 'next': 1, 'replay': 2, 'search': 3, 'volume up': 4, 'volume down': 6, 'exit': 5 } t2i_vn = { 'phát': 0, 'tiếp tục': 0, 'tiếp theo': 1, 'kế tiếp': 1, 'phát lại từ đầu': 2, 'phát lại': 2, 'nghe lại': 2, 'bắt đầu lại': 2, 'tìm kiếm': 3, 'tăng âm lượng': 4, 'tăng âm': 4, "giảm âm lượng": 6, "giảm âm": 6, 'thoát': 5, 'thoát chương trình': 5 } match = False while match != True: t2s('Đọc lệnh bạn muốn') 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 Search(self): self.Stop() self.resetMediaPlayList() self.fornext = False t2s("Tôi sẽ tìm những kết quả cho") t2s(self.searchInput.text()) self.getListSearch() try: _ = len(self.listAudio_text) t2s("Đây là kết quả Chọn bài muốn phát") self.idx_audio = select_by_speech() self.fornext = True self.selectAndPlaySongByIndex() except: self.status.setText('Không có kết quả hoặc lỗi. Thử lại') t2s('Không có kết quả hoặc lỗi Hãy thử lại')
def recognize(note=""): #with sr.Microphone(device_index=0) as source: with sr.Microphone() as source: r.adjust_for_ambient_noise(source, duration=3) if note != "": t2s(note) t2s('START') try: audio = r.listen(source, timeout=3) text = r.recognize_google(audio, language='vi-VN').lower() except: try: t2s("Thử lại") t2s('START') audio = r.listen(source, timeout=3) text = r.recognize_google(audio, language='vi-VN').lower() except: text = 'error' print('#speech2text:' + text) return text
def Wellcome(self): t2s("Xin chào tôi là Pi")
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 firttime = True while match != True: if firttime: t2s('Mời bạn chọn bài hát bằng cách nói số thứ tự bài') firttime = False else: t2s("Tôi chưa hiểu hãy thử lại") 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 break elif text in t2i_en2.keys(): number = t2i_en2[text] match = True break elif text in t2i_vn.keys(): number = t2i_vn[text] match = True break elif text in t2i_vn2.keys(): number = t2i_vn2[text] match = True break elif text in t2i.keys(): number = t2i[text] match = True break # mặc định trả về 0 nếu không nhận dạng được, nếu không mặc định # thì hỏi đến lúc nhận dạng đúng #else: #match = True #number = 1 #break return int(number) - 1
def Next(self): self.Pause() self.status.setText("Tiếp theo") t2s("Tiếp theo") self.selectAndPlaySongByIndex()
def excCommand(self): self.Pause() command = listen_command() if command == 0: self.PlayPause() elif command == 1: self.Next() elif command == 2: self.Stop() t2s("Phát lại") elif command == 3: self.VoiceSearch() elif command == 4: self.volUp() t2s("đã tăng") self.PlayPause() elif command == 6: self.volDown() t2s("đã giảm") self.PlayPause() elif command == 5: t2s('Bạn chắc chắn muốn thoát không') t2s('Xác nhận bằng cách nói ok') cf_exit = recognize('') if cf_exit == 'yes' or cf_exit == 'ok' or cf_exit == 'oke': t2s("Chào bạn hẹn gặp lại bạn hihi") exit() else: print('no exit')
def waitForSpeech(): t2s('I am listing')