from src.blackbox.audio_to_text import AudioToText
from src.blackbox.text_to_audio import TextToAudio
from runtime.ast.parser import Parser
from runtime.ast.runtime import Runtime

script = """
let text = audio_to_text(audio)
return tts(text)
"""

def version():
    return "0.0.1"

def add(*args):
    return sum(args)

def div(a,b):
    return a/b

def minus(a,b):
    return a-b

def mul(a,b):
    return a*b

if __name__ == "__main__":
    f = open("../test_data/testone.wav", "rb")
    audio_data = f.read()
    f.close()
    tts = TextToAudio()
    audio_to_text = AudioToText()
    # 注入函数
    runtime = Runtime(records={
        "add": add,
        "div": div,
        "minus": minus,
        "mul": mul,
        "audio_to_text": audio_to_text,
        "tts": tts,
        "version": version,
        "print": print
    })
    ast = Parser().parse(script)
    # 注入數據
    script_output = runtime.run(ast, {
        "audio": audio_data,
    })
    f = open("../test_data/tmp.wav", "wb")
    print("script:", type(script_output))
    f.write(script_output.read())
    f.close()
    