Baidu NLP - Generate audio files

$ npm install baidu-aip-sdk
$ npm install sprintf-js
#!/usr/bin/node

let AipSpeechClient = require('baidu-aip-sdk').speech
let sprintf = require('sprintf-js').sprintf
let fs = require('fs')

let client = new AipSpeechClient(
    /* AppID     */ '11642631', 
    /* ApiKey    */ '2BbrDXlDOjwL822kwRhS02LH', 
    /* SecretKey */ 'M2BjEjwtBUiwtVBqprGF7Q6lGMrXx497'
);

class TtsParam {
    constructor(arr) {
        // 待处理的文本集合

        this.text_array = arr
        // 待处理的文本编号

        this.index = 0
        // 生成的语音文件前缀

        this.prefix = 'tts'
        // 语速,取值0-15,默认为5中语速

        this.speed = 5
        // 音调,取值0-15,默认为5中语调

        this.pitch = 5
        // 音量,取值0-15,默认为5中音量

        this.volume = 5
        // 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫

        this.person = 0
    }
}

function load_text_to_arr(filename) {
    if (!fs.existsSync(filename)) {
        throw "File not exist: " + filename
    }
    else {
        var data = fs.readFileSync(filename).toString().split('\n').filter(s => s.trim() !== '')
        return data
    }
}

function save_text_to_audio(text, filename) {
    console.log(`${filename}: ${text}`)
    client.text2audio(text, { spd:3 }).then((result) => {
        if (result.data) {
            fs.writeFileSync(filename, result.data)
        }
        else {
            console.log(result)
        }
    });
}

function download(param) {
    if (param.text_array.length > 0 && param.index < param.text_array.length) {
        var basename = sprintf('%s-%05d.%02d%02d%d',
            param.prefix,
            param.index,
            param.person,
            param.speed,
            param.volume
        )
        save_text_to_audio(param.text_array[param.index], `${basename}.mp3`)
        param.index++;
        setTimeout(download, 150, param)
    }
}


function main() {
    let program = require('commander')
    program.version('0.0.1')
        .option('--speed [SPEED]', '0-15, default is 5', 5)
        .option('--pitch [PITCH]', '0-15, default is 5', 5)
        .option('--volume [VOLUME]', '0-15, default is 5',  5)
        .option('--person [PERSON]', '0:female, 1:male, 3:度逍遥, 4:度丫丫', 0)
        .option('--input [FILE]', 'Text file to speech', 'text2speech.txt' )
        .parse(process.argv)

    var arr = load_text_to_arr(program.input)
    var param = new TtsParam(arr)
    param.speed = program.speed
    param.pitch = program.pitch
    param.volume = program.volume
    param.person = program.person
    param.text_arr = arr

    download(param)
}

main()