aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 1706a199da9cc8a9f52541b6b9420469eb75efbf (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>

#include <getopt.h>

#include "pcmengin.h"
#include "wav.h"
#include "aea.h"
#include "atracdenc.h"

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::unique_ptr;
using std::move;
using std::stoi;

using namespace NAtracDEnc;


static void printUsage(const char* myName) {
    cout << "\tusage: " << myName << " <-e|-d> <-i input> <-o output>\n" << endl;
    cout << "-e encode mode (PCM -> ATRAC), -i wav file, -o aea file" << endl;
    cout << "-d decode mode (ATRAC -> PCM), -i aea file, -o wav file" << endl;
    cout << "-h get help" << endl;

}

static void printProgress(int percent) {
    static uint32_t counter;
    counter++;
    const char symbols[4] = {'-', '\\', '|', '/'};
    cout << symbols[counter % 4]<< "  "<< percent <<"% done\r";
    fflush(stdout);
}

static string GetHelp() {
    return "\n--encode -i \t - encode mode"
        "\n--decode -d \t - decode mode"
        "\n -i input file"
        "\n -o output file"
        "\nAdvanced options:\n --bfuidxconst\t Set constant amount of used BFU. WARNING: It is not a lowpass filter! Do not use it to cut off hi frequency."
        "\n --bfuidxfast\t enable fast search of BFU amount"
        "\n --notransient[=mask] disable transient detection and use optional mask to set bands with short MDCT window";
}

int main(int argc, char* const* argv) {
    const char* myName = argv[0];
    static struct option longopts[] = {
        { "encode", no_argument, NULL, 'e' },
        { "decode", no_argument, NULL, 'd' },
        { "help", no_argument, NULL, 'h' },
        { "bfuidxconst", required_argument, NULL, 1},
        { "bfuidxfast", no_argument, NULL, 2},
        { "notransient", optional_argument, NULL, 3},
        { "mono", no_argument, NULL, 'm'},
        { NULL, 0, NULL, 0}
    };

    int ch = 0;
    string inFile;
    string outFile;
    uint32_t mode = 0;
    uint32_t bfuIdxConst = 0; //0 - auto, no const
    bool fastBfuNumSearch = false;
    bool mono = false;
    TAtrac1EncodeSettings::EWindowMode windowMode = TAtrac1EncodeSettings::EWindowMode::EWM_AUTO;
    uint32_t winMask = 0; //all is long
    while ((ch = getopt_long(argc, argv, "edhi:o:m", longopts, NULL)) != -1) {
        switch (ch) {
            case 'e':
                cout << "encode " << endl;
                mode |= E_ENCODE;
                break;
            case 'd':
                cout << "decode" << endl;

                mode |= E_DECODE;
                break;
            case 'i':
                inFile = optarg;
                break;
            case 'o':
                outFile = optarg;
                cout << "out: " << outFile<< endl;
                break;
            case 'm':
                mono = true;
                break;
            case 'h':
                cout << GetHelp() << endl;
                return 0;
                break;
            case 1:
                try {
                    bfuIdxConst = stoi(optarg);
                } catch (std::invalid_argument&) {
                    cerr << "Wrong arg: " << optarg << " should be (0, 8]" << endl;
                    return -1;
                }
                break;
            case 2:
                fastBfuNumSearch = true;
                break;
            case 3:
                windowMode = TAtrac1EncodeSettings::EWindowMode::EWM_NOTRANSIENT;
                if (optarg) {
                    winMask = stoi(optarg);
                }
                cout << "Transient detection disabled, bands: low - " <<
                    ((winMask & 1) ? "short": "long") << ", mid - " <<
                    ((winMask & 2) ? "short": "long") << ", hi - " <<
                    ((winMask & 4) ? "short": "long") << endl;
                break;
			default:
                printUsage(myName);
        }

    }
    argc -= optind;
    argv += optind;

    if (inFile.empty()) {
        cerr << "No input file" << endl;
        return 1;
    }
    if (outFile.empty()) {
        cerr << "No out file" << endl;
        return 1;
    }
    if (bfuIdxConst > 8) {
        cerr << "Wrong bfuidxconst value ("<< bfuIdxConst << "). This is advanced options, use --help to get more information" << endl;
        return 1;
    }

    TWavPtr wavIO;
    TAeaPtr aeaIO;
    TPCMEngine<double>* pcmEngine = nullptr;
    uint64_t totalSamples = 0;
    if (mode == E_ENCODE) {
        wavIO = TWavPtr(new TWav(inFile));
        const int numChannels = wavIO->GetChannelNum();
        totalSamples = wavIO->GetTotalSamples();
        //TODO: recheck it
        aeaIO = TAeaPtr(new TAea(outFile, "test", numChannels, numChannels * totalSamples / 512));
        pcmEngine = new TPCMEngine<double>(4096, numChannels, TPCMEngine<double>::TReaderPtr(wavIO->GetPCMReader<double>()));
        cout << "Input file: " << inFile << "\n Channels: " << numChannels << "\n SampleRate: " << wavIO->GetSampleRate() << "\n TotalSamples: " << totalSamples << endl;
    } else if (mode == E_DECODE) {
        aeaIO = TAeaPtr(new TAea(inFile));
        totalSamples = aeaIO->GetLengthInSamples();
        uint32_t length = aeaIO->GetLengthInSamples();
        cout << "Name: " << aeaIO->GetName() << "\n Channels: " << aeaIO->GetChannelNum() << "\n Length: " << length << endl;
        wavIO = TWavPtr(new TWav(outFile, aeaIO->GetChannelNum(), 44100));
        pcmEngine = new TPCMEngine<double>(4096, aeaIO->GetChannelNum(), TPCMEngine<double>::TWriterPtr(wavIO->GetPCMWriter<double>()));
    } else {
        cout << "Processing mode was not specified" << endl;
        return 1;
    }

    TAtrac1Processor atrac1Processor(move(aeaIO), mono);
    auto atracLambda = (mode == E_DECODE) ? atrac1Processor.GetDecodeLambda() :
        atrac1Processor.GetEncodeLambda(TAtrac1EncodeSettings(bfuIdxConst, fastBfuNumSearch, windowMode, winMask));
    uint64_t processed = 0;
    try {
        while (totalSamples > (processed = pcmEngine->ApplyProcess(512, atracLambda)))
        {
            printProgress(processed*100/totalSamples);
        }
        cout << "\nDone" << endl;
    }
    catch (TAeaIOError err) {
        cerr << "Aea IO fatal error: " << err.what() << endl;
        exit(1);
    }
    catch (TWavIOError err) {
        cerr << "Wav IO fatal error: " << err.what() << endl;
        exit(1);
    }
}