#encoding = utf8

import sys
import time

from asr import SherpaNcnnAsr
from nlp import PunctuationSplit
from nlp.nlp_doubao import DouBao
from tts import TTSEdge, TTSAudioSaveHandle, TTSEdgeHttp

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")
    handle = TTSAudioSaveHandle(None, None)
    # tts = TTSEdge(handle)
    tts = TTSEdgeHttp(handle)
    split = PunctuationSplit()
    nlp = DouBao(split, tts)
    nlp.ask('你好，你是谁？')
    nlp.ask('可以帮我做什么？')
    nlp.ask('背诵出师表')
    nlp.stop()
    tts.stop()
    print("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")
