summaryrefslogtreecommitdiffstats
path: root/Scripts/cpp_transcribe.py
blob: c4997699c4be14b874b8fa0de77d30d8dcc92fd3 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3

# The app loop does 2 things:
#   1. Read lines from stdin and send them into the game via OSC.
#   2. Write control info to stdout.
# The app exits when stdin closes.

from playsound import playsound

import argparse
import dataclasses
import generate_utils
import os
import osc_ctrl
import steamvr
import sys
import threading
import time

@dataclasses.dataclass
class AudioState:
    text: str
    osc_state: osc_ctrl.OscState
    enable_local_beep: int
    use_builtin: int
    button: str

    send_transcript: bool
    run_app: bool

def writeControlMessage(run: bool):
    msg = ""
    if run:
        msg += "1"
    else:
        msg += "0"
    print(f"{msg}")

def readControllerInput(audio_state: AudioState):
    session = None
    first = True
    while session == None and audio_state.run_app == True:
        try:
            session = steamvr.SessionState()
        except:
            print("steamvr is off, no controller input", file=sys.stderr)
            session = None
            time.sleep(5)

    RECORD_STATE = 0
    PAUSE_STATE = 1
    state = PAUSE_STATE
    osc_ctrl.indicateSpeech(audio_state.osc_state.client, False)
    osc_ctrl.indicatePaging(audio_state.osc_state.client, False)

    hand_id = steamvr.hands[audio_state.button.split()[0]]
    button_id = steamvr.buttons[audio_state.button.split()[1]]

    last_rising = time.time()
    while audio_state.run_app == True:
        time.sleep(0.05)

        event = steamvr.pollButtonPress(session, hand_id=hand_id,
                button_id=button_id)

        if event == steamvr.EVENT_RISING_EDGE:
            last_rising = time.time()
        elif event == steamvr.EVENT_FALLING_EDGE:
            now = time.time()
            if now - last_rising > 0.3:
                # Long hold
                state = PAUSE_STATE
                if not audio_state.use_builtin:
                    osc_ctrl.indicateSpeech(audio_state.osc_state.client, False)
                    osc_ctrl.toggleBoard(audio_state.osc_state.client, False)

                osc_ctrl.send_transcript = False
                osc_ctrl.clear(audio_state.osc_state)
            else:
                # Short hold
                if state == RECORD_STATE:
                    state = PAUSE_STATE
                    if not audio_state.use_builtin:
                        osc_ctrl.indicateSpeech(audio_state.osc_state.client, False)
                        osc_ctrl.lockWorld(audio_state.osc_state.client, True)

                    osc_ctrl.send_transcript = False

                    if audio_state.enable_local_beep == 1:
                        playsound(os.path.abspath("Resources/Sounds/Noise_Off_Quiet.wav"))
                elif state == PAUSE_STATE:
                    state = RECORD_STATE
                    if not audio_state.use_builtin:
                        osc_ctrl.indicateSpeech(audio_state.osc_state.client, True)
                        osc_ctrl.toggleBoard(audio_state.osc_state.client, True)
                        osc_ctrl.lockWorld(audio_state.osc_state.client, False)

                    osc_ctrl.send_transcript = True
                    osc_ctrl.clear(audio_state.osc_state)

                    audio_state.drop_transcription = True
                    audio_state.audio_paused = False

                    if audio_state.enable_local_beep == 1:
                        playsound(os.path.abspath("Resources/Sounds/Noise_On_Quiet.wav"))

def drainStdin(audio_state: AudioState):
    while True:
        try:
            line = input()
        except EOFError:
            # Invoking process closes the write end of their stdin to signal us
            # to exit.
            # TODO(yum) merge all threads
            audio_state.run_app = False
            return
        if len(line) > 0:
            print(f"stdin get: {line}", file=sys.stderr)

def mainLoop(audio_state: AudioState):
    steamvr_input_thd = threading.Thread(target = readControllerInput,
            args = [audio_state])
    steamvr_input_thd.daemon = True
    steamvr_input_thd.start()

    drain_stdin_thd = threading.Thread(target = drainStdin,
            args = [audio_state])
    drain_stdin_thd.daemon = True
    drain_stdin_thd.start()

    writeControlMessage(False)

    while audio_state.run_app:
        time.sleep(0.01)
        writeControlMessage(audio_state.send_transcript)

if __name__ == "__main__":
    print("args: {}".format(" ".join(sys.argv)), file=sys.stderr)

    # Set cwd to TaSTT/
    abspath = os.path.abspath(__file__)
    dname = os.path.dirname(abspath)
    dname = os.path.dirname(dname)
    dname = os.path.dirname(dname)
    os.chdir(dname)
    print(f"Set cwd to {os.getcwd()}", file=sys.stderr)

    parser = argparse.ArgumentParser()
    parser.add_argument("--bytes_per_char", type=str, help="The number of bytes to use to represent each character")
    parser.add_argument("--chars_per_sync", type=str, help="The number of characters to send on each sync event")
    parser.add_argument("--rows", type=int, help="The number of rows on the board")
    parser.add_argument("--cols", type=int, help="The number of columns on the board")
    parser.add_argument("--enable_local_beep", type=int,
            help=("Whether to play a local auditory indicator when "
                "transcription starts/stops."))
    parser.add_argument("--use_builtin", type=int,
            help=("If set to 1, use the text box built into the game."))
    parser.add_argument("--button", type=str,
            help=("The controller button used to start/stop transcription. "
                "E.g. \"left joystick\""))
    args = parser.parse_args()

    if args.bytes_per_char is None or args.chars_per_sync is None:
        print("--bytes_per_char and --chars_per_sync required", file=sys.stderr)
        sys.exit(1)
    if args.rows is None or args.cols is None:
        print("--rows and --cols required", file=sys.stderr)
        sys.exit(1)
    if args.button is None:
        print("--button required", file=sys.stderr)
        sys.exit(1)
    if args.enable_local_beep is None:
        print("--enable_local_beep required", file=sys.stderr)
        sys.exit(1)
    if args.use_builtin is None:
        print("--use_builtin required", file=sys.stderr)
        sys.exit(1)

    generate_utils.config.BYTES_PER_CHAR = int(args.bytes_per_char)
    generate_utils.config.CHARS_PER_SYNC = int(args.chars_per_sync)
    generate_utils.config.BOARD_ROWS = int(args.rows)
    generate_utils.config.BOARD_COLS = int(args.cols)

    audio_state = AudioState(
            text = "",
            osc_state = osc_ctrl.OscState(
                generate_utils.config.CHARS_PER_SYNC,
                generate_utils.config.BOARD_ROWS,
                generate_utils.config.BOARD_COLS),
            button = args.button,
            enable_local_beep = args.enable_local_beep,
            use_builtin = args.use_builtin,
            send_transcript = False,
            run_app = True)

    mainLoop(audio_state)