Skip to content

Commit

Permalink
STT: Improve example, add fields for partials and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyWeeUsr committed Dec 1, 2018
1 parent fda922a commit 6d137a6
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions examples/speech/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
from plyer import speech

Builder.load_string('''
#:import speech plyer.speech
<SpeechInterface>:
orientation: 'vertical'
Label:
size_hint_y: None
height: sp(40)
text: 'Is supported: %s' % root.speech.exist()
text: 'Is supported: %s' % speech.exist()
Label:
size_hint_y: None
height: sp(40)
text: 'Possible Matches'
Label:
TextInput:
id: results
size_hint_y: 0.7
height: sp(40)
text: ''
hint_text: 'results (auto stop)'
size_hint_y: 0.3
TextInput:
id: partial
hint_text: 'partial results (manual stop)'
size_hint_y: 0.3
TextInput:
id: errors
size_hint_y: None
height: sp(20)
Button:
id: start_button
text: 'Start Listening'
Expand All @@ -34,41 +43,38 @@
class SpeechInterface(BoxLayout):
'''Root Widget.'''

speech = speech
state = StringProperty()

def start_listening(self):
if self.speech.state == 'listening':
if speech.listening:
self.stop_listening()
return

start_button = self.ids['start_button']
start_button = self.ids.start_button
start_button.text = 'Stop'

label_results = self.ids['results']
label_results.text = ''
self.ids.results.text = ''
self.ids.partial.text = ''

self.speech.start()
self.state = self.speech.state
speech.start()

Clock.schedule_interval(self.check_state, 1/5)

def stop_listening(self):
start_button = self.ids['start_button']
start_button = self.ids.start_button
start_button.text = 'Start Listening'

self.speech.stop()
speech.stop()
self.update()

Clock.unschedule(self.check_state)

def check_state(self, dt):
if self.state != self.speech.state:
# if the recognizer service stops, change UI
if not speech.listening:
self.stop_listening()

def update(self):
label_results = self.ids['results']
label_results.text = '\n'.join(set(self.speech.results))
self.ids.partial.text = '\n'.join(speech.partial_results)
self.ids.results.text = '\n'.join(speech.results)


class SpeechApp(App):
Expand Down

0 comments on commit 6d137a6

Please sign in to comment.