# shellcheck shell=dash

# https://platform.openai.com/docs/guides/speech-to-text

___x_cmd_openai_audio(){
    local X_help_cmd='___x_cmd help -m openai audio';   help:arg-null:parse
    param:scope     ___x_cmd_openai
    param:subcmd    ___x_cmd_openai_audio                               \
        generate          "Generates audio from the input text"         \
        translate         "Translates audio into English"               \
        transcribe        "Transcribes audio into the input language"

    param:subcmd:try
    param:run

    ___x_cmd_openai_audio _param_help_doc
    return 1
}

___x_cmd_openai_audio_generate(){
    local X_help_cmd='___x_cmd help -m openai audio generate';   help:arg-null:parse
    param:scope     ___x_cmd_openai
    param:dsl       '
options:
    --model             "One of the available TTS models"               <>:String    = tts-1 tts-1-hd
    --input             "The text to generate audio for"                <>:String
    --voice             "The voice to use when generating the audio"    <>:String    = alloy echo fable onyx nova shimmer
    --response_format   "The format to audio in"                        <>="mp3"     = mp3 opus aac flac
    --speed             "The speed of the generated audio"              <>:Number="1"
    --output_path       "The path to the resulting audio file"          <>=""
'
    param:run

    local gen_openai_json="";   gen_openai_json="$(param:option2json -output_path)"
    openai:debug  --data "$gen_openai_json"
    local output_path="${output_path:-"${PWD}/audio.${response_format}"}"
    ___x_cmd_openai_curl post "/audio/speech" gen_openai_json
}

___x_cmd_openai_audio_transcribe(){
    local X_help_cmd='___x_cmd help -m openai audio transcribe';   help:arg-null:parse
    param:scope     ___x_cmd_openai
    param:dsl       '
options:
    #1                  "The audio file path to transcribe"                                                 <>:String
    --model             "ID of the model to use. Only whisper-1 is currently available"                     <>:String    = whisper-1
    --language          "The language of the input audio"                                                   <>=""
    --prompt            "An optional text to guide the model style or continue a previous audio segment"    <>=""
    --response_format   "The format of the transcript output"                                               <>="json"   = json text srt vtt verbose_json
    --temperature       "The sampling temperature, between 0 and 1"                                         <>:Number="0"
'
    param:run

    local _file_path="$1"
    local prompt="\"$prompt\""
    ___x_cmd_openai_curl upload /audio/transcriptions _file_path model language prompt response_format temperature
}

___x_cmd_openai_audio_translate(){
    local X_help_cmd='___x_cmd help -m openai audio translate';   help:arg-null:parse
    param:scope     ___x_cmd_openai
    param:dsl       '
options:
    #1                  "The audio file path to translate"                                                  <>:String
    --model             "ID of the model to use. Only whisper-1 is currently available"                     <>:String    = whisper-1
    --prompt            "An optional text to guide the model style or continue a previous audio segment"    <>=""
    --response_format   "The format of the transcript output"                                               <>="json"   = json text srt vtt verbose_json
    --temperature       "The sampling temperature, between 0 and 1"                                         <>:Number="0"
'
    param:run

    local _file_path="$1"
    local prompt="\"$prompt\""
    ___x_cmd_openai_curl upload /audio/translations _file_path model prompt response_format temperature
}
