-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtel.py
86 lines (76 loc) · 2.66 KB
/
rtel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from api import Tscb
from recorder import Recorder
'''
Parameters used for settings on how the exitcommand will be handeled.
0 indicates exiting immediately.
1 indicates doing the passto action first and then exiting.
'''
EXIT_IMMEDIATELY = 0
PASS_THEN_EXIT = 1
class Rtel:
def __init__(self):
'''
Makes a Rtel application.
'''
self.num = 0
self.passto = print #function
self.exitcommand = None #string
self.exitcmdhandlesetting = 0
self.exitinput = None #function
self.lengthinput = input #function
self.recorder = Recorder(44100, 10)
self.transcriber = Tscb()
def passTo(self, function):
"""
Sets the function to pass the transcribed string to.
:param function: The function to execute with the transcribed string as the parameter. The default passTo is print.
"""
self.passto = function
def setExitCommand(self, exitcommand):
"""
Sets the exit command. It is optional.
:param exitcommand: If the transcribed result is ever this, the loop exits.
"""
self.exitcommand = exitcommand
def exitComandHandelingMethodSetting(self, setting):
self.exitcmdhandlesetting = setting
def lengthInput(self, function):
"""
Sets the input source of the length.
:param function: The function that is called every loop. int(function()) will be then length of the source. The default lengthInput is input.
"""
self.lengthinput = function
def exitInput(self, function):
"""
Sets a function to exit the program externally. It is optional.
:param function: The function that is called every loop. function() should return a boolean value, which if is true, the loop will stuff.
"""
self.exitinput = function
def injectTranscriber(self, transcriber):
'''
The transcriber instance to use. A default instance is used if not specified.
'''
self.transcriber = transcriber
def injectRecorder(self, recorder):
'''
The recorder instance to use. A default instance is used if not specified.
'''
self.recorder = recorder
def run(self):
while True:
if self.exitinput is not None:
if self.exitinput():
break
length = int(self.lengthinput())
self.recorder.setRecordingLength(length)
f = self.recorder.record()
self.transcriber.setup(f)
s = self.transcriber.transcribe()
if self.exitcommand is not None and self.exitcmdhandlesetting == EXIT_IMMEDIATELY:
if s == self.exitcommand:
break
self.passto(s)
if self.exitcommand is not None and self.exitcmdhandlesetting == PASS_THEN_EXIT:
if s == self.exitcommand:
break
self.transcriber.purge()