-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (90 loc) · 2.9 KB
/
Copy pathmain.cpp
File metadata and controls
110 lines (90 loc) · 2.9 KB
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
/*
* at3plusenc - ATRAC3plus encoder
* Simplified entry point, no external dependencies.
*
* Based on AtracDEnc. SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <iostream>
#include <string>
#include <memory>
#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <cstring>
#include <fenv.h>
#include "wav_io.h"
#include "core/riff_writer.h"
#include "resampler.h"
#include "core/encoder.h"
using namespace std;
using namespace NAtracDEnc;
typedef unique_ptr<TPCMEngine> TPcmEnginePtr;
typedef unique_ptr<IProcessor> TAtracProcessorPtr;
static string GetOutputFilename(const string& in) {
filesystem::path p = in;
p.replace_extension(".at3");
return p.string();
}
static TWavPtr OpenWavFile(const string& p) {
return make_unique<TWav>(p);
}
static void PrepareAtrac3PEncoder(
const string& outPath, int ch, uint64_t* total,
const TWavPtr& wav, TPcmEnginePtr* pcm, TAtracProcessorPtr* atr)
{
uint32_t sr = (uint32_t)wav->GetSampleRate();
uint32_t dsr = 44100;
*total = (sr != dsr) ? ResampledLength(wav->GetTotalSamples(), sr, dsr) : wav->GetTotalSamples();
TCompressedOutputPtr ao(new TAt3Riff(outPath, ch, 2048));
TPCMEngine::TReaderPtr pr(wav->GetPCMReader());
if (sr != dsr)
pr = CreateResamplingReader(move(pr), sr, dsr, ch);
pcm->reset(new TPCMEngine(4096, ch, move(pr)));
TAt3PEnc::TSettings s;
atr->reset(new TAt3PEnc(move(ao), ch, s));
}
static void print_usage() {
printf("Usage: at3plusenc -e input.wav\n");
printf("\n");
printf("Encodes a WAV file to ATRAC3plus (.at3) format.\n");
printf("Input must be 44100 Hz (auto-resampled if not).\n");
}
int main(int argc, char* argv[]) {
// Set floating-point rounding mode for codec precision
fesetround(FE_TONEAREST);
if (argc < 3 || strcmp(argv[1], "-e") != 0) {
print_usage();
return 1;
}
string inputPath = argv[2];
string outputPath = GetOutputFilename(inputPath);
if (!filesystem::exists(inputPath)) {
fprintf(stderr, "Error: File not found: %s\n", inputPath.c_str());
return 1;
}
try {
TWavPtr wav = OpenWavFile(inputPath);
TPcmEnginePtr pcm;
TAtracProcessorPtr atr;
uint64_t total = 0;
printf("Input: %s\n", inputPath.c_str());
printf("Rate: %zu Hz\n", wav->GetSampleRate());
printf("Ch: %zu\n", wav->GetChannelNum());
PrepareAtrac3PEncoder(outputPath, (int)wav->GetChannelNum(), &total, wav, &pcm, &atr);
auto f = atr->GetLambda();
uint64_t d = 0;
while (total > (d = pcm->ApplyProcess(2048, f)))
;
printf("Output: %s\n", outputPath.c_str());
printf("Done.\n");
}
catch (const exception& ex) {
fprintf(stderr, "Error: %s\n", ex.what());
return 1;
}
catch (...) {
fprintf(stderr, "Error: Encode failed\n");
return 1;
}
return 0;
}