-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcxenc.cpp
413 lines (364 loc) · 12.7 KB
/
cxenc.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* Copyright (C) 2021 Jo2003 (olenka.joerg@gmail.com)
* This file is part of cd2netmd_gui
*
* cd2netmd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cd2netmd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*/
#include <QFileInfo>
#include <QRegExp>
#include <QApplication>
#include <QtEndian>
#include <QDir>
#include "cxenc.h"
#include "helpers.h"
#include "audio.h"
#include <cmath>
CXEnc::CXEnc(QObject *parent)
: CCliProcess(parent), mCurrCmd(XEncCmd::NONE), mLength(0), mbAltEnc(false), mProgressIt(0)
{
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &CXEnc::finishCopy);
mProgUpd.setInterval(1100);
mProgUpd.setSingleShot(false);
connect(&mProgUpd, &QTimer::timeout, this, &CXEnc::extractPercent);
}
int CXEnc::start(XEncCmd cmd, const QString& tmpFileName, double trackLength, const QString &at3tool)
{
QStringList params;
mLog.clear();
mCurrCmd = cmd;
mLength = trackLength;
mbAltEnc = (!at3tool.isEmpty() && (mCurrCmd != XEncCmd::DAO_SP_ENCODE));
mAtracFileName = tmpFileName + (mbAltEnc ? ".at3" : ".aea");
switch (cmd)
{
case XEncCmd::DAO_LP2_ENCODE:
case XEncCmd::LP2_ENCODE:
if (mbAltEnc)
{
params << "-e" << "-br" << "132" << QDir::toNativeSeparators(tmpFileName) << QDir::toNativeSeparators(mAtracFileName);
}
else
{
params << "-e" << "atrac3" << "--bitrate=128" << "-i" << tmpFileName << "-o" << mAtracFileName;
}
break;
case XEncCmd::LP4_ENCODE:
if (mbAltEnc)
{
params << "-e" << "-br" << "66" << QDir::toNativeSeparators(tmpFileName) << QDir::toNativeSeparators(mAtracFileName);
}
else
{
params << "-e" << "atrac3" << "--bitrate=64" << "-i" << tmpFileName << "-o" << mAtracFileName;
}
break;
case XEncCmd::DAO_SP_ENCODE:
params << "-e" << "atrac1" << "-i" << tmpFileName << "-o" << mAtracFileName;
break;
default:
return -1;
}
if (mbAltEnc)
{
mProgressIt = 0;
mProgUpd.start();
qInfo() << "Using alternate encoder.";
qInfo() << at3tool << params;
run(at3tool, params);
}
else
{
#ifdef Q_OS_MAC
// app folder
QString sAppDir = QApplication::applicationDirPath();
// find bundle dir ...
QRegExp rx("^(.*)/MacOS");
if (rx.indexIn(sAppDir) > -1)
{
// found section --> create path names ...
QString encTool = QString("%1/%2").arg(sAppDir).arg(XENC_CLI);
qInfo() << encTool << params;
run(encTool, params);
}
#else
qInfo() << XENC_CLI << params;
run(XENC_CLI, params);
#endif
}
return 0;
}
int CXEnc::start(CXEnc::XEncCmd cmd, const c2n::TransferQueue &queue, double discLength, const QString& at3tool)
{
mQueue = queue;
return start(cmd, queue.at(0).mFileName, discLength, at3tool);
}
int CXEnc::atrac3WaveHeader(QFile& waveFile, XEncCmd cmd, size_t dataSz, int length)
{
int ret = -1;
if ((waveFile.isOpen())
&& ((cmd == XEncCmd::LP2_ENCODE) || (cmd == XEncCmd::LP4_ENCODE) || (cmd == XEncCmd::DAO_LP2_ENCODE))
&& (dataSz > 192)) // 1x lp4 frame size
{
// heavily inspired by atrac3tool and completed through
// reverse engineering of ffmpeg output ...
uint32_t i = 0xC + 8 + 0x20 + 8 + dataSz - 8;
waveFile.write("RIFF", 4);
putNum(i, waveFile, 4);
waveFile.write("WAVEfmt ", 8);
putNum(0x20, waveFile, 4);
putNum(WAVE_FORMAT_SONY_SCX, waveFile, 2); ///< format tag
putNum(2, waveFile, 2); ///< channels
putNum(44100, waveFile, 4); ///< sample rate
putNum(static_cast<uint32_t>(ceil(dataSz / length)), waveFile, 4); ///< bytes per sec
if ((cmd == XEncCmd::LP2_ENCODE) || (cmd == XEncCmd::DAO_LP2_ENCODE))
{
putNum(ATRAC3_LP2_BLOCK_ALIGN, waveFile, 4); ///< block align
}
else if (cmd == XEncCmd::LP4_ENCODE)
{
putNum(ATRAC3_LP4_BLOCK_ALIGN, waveFile, 4); ///< block align
}
putNum(0, waveFile, 2); ///< bits per sample
putNum(0xE, waveFile, 2); ///< cb size
if ((cmd == XEncCmd::LP2_ENCODE) || (cmd == XEncCmd::DAO_LP2_ENCODE))
{
waveFile.write("\x01\x00\x44\xAC\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00", 0xE);
}
else if (cmd == XEncCmd::LP4_ENCODE)
{
waveFile.write("\x01\x00\x44\xAC\x00\x00\x01\x00\x01\x00\x01\x00\x00\x00", 0xE);
}
waveFile.write("data", 4);
putNum(dataSz, waveFile, 4);
ret = 0;
}
return ret;
}
//--------------------------------------------------------------------------
//! @brief create atrac1 (SP) header
//!
//! @param aeaFile The target file
//! @param[in] cmd The command
//! @param[in] dataSz The data size
//! @param[in] length The length
//!
//! @return 0 on success
//--------------------------------------------------------------------------
int CXEnc::atrac1Header(QFile& aeaFile, XEncCmd cmd, size_t dataSz, int length)
{
Q_UNUSED(length)
Q_UNUSED(cmd)
int ret = -1;
uint8_t header[ATRAC_SP_HEADER_SIZE] = {0,};
header[1] = 8;
header[264] = 2;
char* pTitle = reinterpret_cast<char*>(&header[4]);
strncpy(pTitle, "Temp AEA Track by NetMD Wizard", 31);
uint32_t blockCount = dataSz / ATRAC_SP_BLOCK_ALIGN;
*reinterpret_cast<uint32_t*>(&header[260]) = qToLittleEndian(blockCount);
if (aeaFile.isOpen())
{
size_t written = 0;
do { written += aeaFile.write(reinterpret_cast<char*>(header + written), ATRAC_SP_HEADER_SIZE - written); } while(written < ATRAC_SP_HEADER_SIZE);
ret = (written == ATRAC_SP_HEADER_SIZE) ? 0 : -1;
}
return ret;
}
int CXEnc::splitAtrac3()
{
// open atrac file for size check
QFile fAtrac(mAtracFileName, this);
if (fAtrac.open(QIODevice::ReadOnly))
{
// get file size
size_t sz;
if (mbAltEnc)
{
audio::stripWaveHeader(fAtrac, sz);
}
else
{
QFileInfo atracInfo(fAtrac);
sz = atracInfo.size() - ATRAC3_HEADER_SIZE;
fAtrac.seek(ATRAC3_HEADER_SIZE);
}
// average blocks per second
double blocksPerSec = (static_cast<double>(sz) / static_cast<double>(ATRAC3_LP2_BLOCK_ALIGN)) / mLength;
int trkCount = 0;
size_t copied = 0;
uint32_t copyBlocks;
size_t cpSz;
for (const auto& t : mQueue)
{
trkCount ++;
// overwrite original wave file
QFile waveFile(t.mFileName);
if (waveFile.open(QIODevice::Truncate | QIODevice::WriteOnly))
{
// copy atrac data
if (trkCount < mQueue.size())
{
copyBlocks = static_cast<uint32_t>(ceil(t.mLength * blocksPerSec));
cpSz = copyBlocks * ATRAC3_LP2_BLOCK_ALIGN;
copied += cpSz;
}
else
{
// last track -> read all
cpSz = sz - copied;
}
// create wave header
atrac3WaveHeader(waveFile, mCurrCmd, cpSz, t.mLength);
waveFile.write(fAtrac.read(cpSz));
waveFile.close();
qInfo() << "Copied " << cpSz << "B ATRAC3 data to " << t.mFileName;
}
}
fAtrac.close();
qInfo() << "Delete temp. file" << fAtrac.fileName();
fAtrac.remove();
}
return 0;
}
//--------------------------------------------------------------------------
//! @brief Splits an atrac 1 (SP).
//!
//! @return 0 on success
//--------------------------------------------------------------------------
int CXEnc::splitAtrac1()
{
// open atrac file for size check
QFile fAtrac(mAtracFileName, this);
if (fAtrac.open(QIODevice::ReadOnly))
{
// get file size
QFileInfo atracInfo(fAtrac);
size_t sz = atracInfo.size() - ATRAC_SP_HEADER_SIZE;
// drop atrac 1 header
fAtrac.seek(ATRAC_SP_HEADER_SIZE);
// average blocks per second
double blocksPerSec = (static_cast<double>(sz) / static_cast<double>(AT_SP_STEREO_BLOCK_SIZE)) / mLength;
int trkCount = 0;
for (const auto& t : mQueue)
{
trkCount ++;
// overwrite original wave file
QFile aeaFile(t.mFileName);
if (aeaFile.open(QIODevice::Truncate | QIODevice::WriteOnly))
{
uint32_t copyBlocks = static_cast<uint32_t>(ceil(t.mLength * blocksPerSec));
size_t sz = copyBlocks * AT_SP_STEREO_BLOCK_SIZE;
// create file header
atrac1Header(aeaFile, mCurrCmd, sz, t.mLength);
// copy atrac data
if (trkCount < mQueue.size())
{
aeaFile.write(fAtrac.read(sz));
}
else
{
// last track -> read all
aeaFile.write(fAtrac.readAll());
}
aeaFile.close();
qInfo() << "Copied " << sz << "B ATRAC1 (SP) data to " << t.mFileName;
}
}
fAtrac.close();
qInfo() << "Delete temp. file" << fAtrac.fileName();
fAtrac.remove();
}
return 0;
}
//--------------------------------------------------------------------------
//! @brief extract percentage from log file
//--------------------------------------------------------------------------
void CXEnc::extractPercent()
{
if (mbAltEnc)
{
mLog += QString::fromUtf8(readAllStandardOutput());
// show some progress even if we don't know it
emit progress(mProgressIt);
mProgressIt += 10;
if (mProgressIt > 90)
{
mProgressIt = 10;
}
}
else
{
CCliProcess::extractPercent();
}
}
void CXEnc::finishCopy(int exitCode, ExitStatus exitStatus)
{
if ((exitCode == 0) && (exitStatus == ExitStatus::NormalExit))
{
switch(mCurrCmd)
{
case XEncCmd::LP2_ENCODE:
case XEncCmd::LP4_ENCODE:
{
// open atrac file for size check
QFile fAtrac(mAtracFileName, this);
if (fAtrac.open(QIODevice::ReadOnly))
{
// get file size
size_t sz;
if (mbAltEnc)
{
audio::stripWaveHeader(fAtrac, sz);
}
else
{
QFileInfo atracInfo(fAtrac);
sz = atracInfo.size() - ATRAC3_HEADER_SIZE;
fAtrac.seek(ATRAC3_HEADER_SIZE);
}
// overwrite original wave file
QFile waveFile(mAtracFileName.mid(0, mAtracFileName.lastIndexOf(QChar('.'))));
if (waveFile.open(QIODevice::Truncate | QIODevice::WriteOnly))
{
// create wave header
atrac3WaveHeader(waveFile, mCurrCmd, sz, mLength);
waveFile.write(fAtrac.read(sz));
waveFile.close();
qInfo() << "Copied " << sz << "B ATRAC3 data to " << waveFile.fileName();
}
fAtrac.close();
qInfo() << "Delete temp. file" << fAtrac.fileName();
fAtrac.remove();
}
}
break;
case XEncCmd::DAO_LP2_ENCODE:
splitAtrac3();
break;
case XEncCmd::DAO_SP_ENCODE:
splitAtrac1();
break;
default:
break;
}
}
if (mbAltEnc)
{
mProgUpd.stop();
}
if (!mLog.isEmpty())
{
qDebug().noquote() << Qt::endl << static_cast<const char*>(mLog.toUtf8());
}
emit fileDone(false);
}