#encoding = utf8

import sys
import time

from asr import SherpaNcnnAsr
from nlp import PunctuationSplit
from nlp.nlp_doubao import DouBao

try:
    import sounddevice as sd
except ImportError as e:
    print("Please install sounddevice first. You can use")
    print()
    print("  pip install sounddevice")
    print()
    print("to install it")
    sys.exit(-1)


def main():
    print("Started! Please speak")
    split = PunctuationSplit()
    nlp = DouBao(split)
    asr = SherpaNcnnAsr()
    asr.attach(nlp)
    time.sleep(20)
    print("Stop! ")
    asr.stop()
    asr.detach(nlp)
    nlp.stop()


if __name__ == "__main__":
    devices = sd.query_devices()
    print(devices)
    default_input_device_idx = sd.default.device[0]
    print(f'Use default device: {devices[default_input_device_idx]["name"]}')

    try:
        main()
    except KeyboardInterrupt:
        print("\nCaught Ctrl + C. Exiting")
