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
|
#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";
}
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},
{ "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;
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;
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 TWavHeader& wavHeader = wavIO->GetWavHeader();
const int numChannels = wavHeader.NumOfChan;
totalSamples = wavHeader.ChunkSize;
aeaIO = TAeaPtr(new TAea(outFile, "test", numChannels, totalSamples / 2 / 512));
pcmEngine = new TPCMEngine<double>(4096, numChannels, TPCMEngine<double>::TReaderPtr(wavIO->GetPCMReader<double>()));
cout << "Input file: " << inFile << "Channels: " << numChannels << "SampleRate: " << wavHeader.SamplesPerSec << "TotalSamples: " << totalSamples << endl;
} else if (mode == E_DECODE) {
aeaIO = TAeaPtr(new TAea(inFile));
totalSamples = 4 * aeaIO->GetLengthInSamples();
uint32_t length = aeaIO->GetLengthInSamples();
cout << "Name: " << aeaIO->GetName() << "\n Channels: " << aeaIO->GetChannelNum() << "\n Length: " << length << endl;
wavIO = TWavPtr(new TWav(outFile, TWav::CreateHeader(aeaIO->GetChannelNum(), length)));
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, TAtrac1EncodeSettings::EWindowMode::EWM_LONG_ONLY));
uint64_t processed = 0;
try {
while (totalSamples/4 > (processed = pcmEngine->ApplyProcess(512, atracLambda)))
{
printProgress(processed*100/(totalSamples/4));
}
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);
}
}
|