File size: 2,734 Bytes
0e27770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fs = require("fs");
const _ = require("lodash");
const Aimastering = require('aimastering');
const program = require('commander');
const srs = require('secure-random-string');

// parse command line arguments
program
    .option('-i, --input <s>', 'Input audio file path')
    .option('-o, --output <s>', 'Output audio file path')
    .parse(process.argv);
if (program.input.length === 0) {
    program.help();
}

// Call API with promise interface
const callApiDeferred = async function (api, method) {
    const apiArgments = Array.prototype.slice.call(arguments, 2);

    return new Promise((resolve, reject) => {
        const callback = (error, data, response) => {
            if (error) {
                reject(error, response);
            } else {
                resolve(data, response);
            }
        };
        const args = _.flatten([
            apiArgments,
            callback
        ]);

        method.apply(api, args);
    });
};

const sleep = async function (ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
};

const main = async function () {
    // configure API client
    const client = Aimastering.ApiClient.instance;
    const bearer = client.authentications['bearer'];
    // bearer.apiKey = process.env.AIMASTERING_ACCESS_TOKEN;

    // API key must be 'guest_' + [arbitrary string]
    // Unless the API key is leaked, the data will not be visible to others.
    bearer.apiKey = 'guest_' + srs({length: 32})

    // create api
    const audioApi = new Aimastering.AudioApi(client);
    const masteringApi = new Aimastering.MasteringApi(client);

    // upload input audio
    const inputAudioData = fs.createReadStream(program.input);
    const inputAudio = await callApiDeferred(audioApi, audioApi.createAudio, {
        'file': inputAudioData,
    });
    console.error(inputAudio);

    // start mastering
    let mastering = await callApiDeferred(masteringApi, masteringApi.createMastering, inputAudio.id, {
        'mode': 'default',
    });
    console.error(mastering);

    // wait for the mastering completion
    while (mastering.status === 'waiting' || mastering.status === 'processing') {
        mastering = await callApiDeferred(masteringApi, masteringApi.getMastering, mastering.id);
        console.error('waiting for the mastering completion progression: '
            + (100 * mastering.progression).toFixed() + '%');
        await sleep(5000);
    }

    // download output audio
    const outputAudioData = await callApiDeferred(audioApi, audioApi.downloadAudio, mastering.output_audio_id);
    fs.writeFileSync(program.output, outputAudioData);

    console.error('the output file was written to ' + program.output);
};

main();