-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcnetmd.cpp
More file actions
557 lines (486 loc) · 15.1 KB
/
Copy pathcnetmd.cpp
File metadata and controls
557 lines (486 loc) · 15.1 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
* 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 <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QTextCodec>
#include <iomanip>
#include "cnetmd.h"
#include "defines.h"
#include "helpers.h"
CNetMD::CNetMD(QObject *parent)
: QThread(parent), mCurrJob(NetMDCmd::UNKNWON),
mpApi(nullptr), mMono(false)
{
mpApi = new netmd::netmd_pp;
mNameFLog = QString("%1/cd2netmd_transfer_log.tmp").arg(QDir::tempPath());
mTReadLog.setInterval(1000);
mTReadLog.setSingleShot(false);
mfLog.setFileName(mNameFLog);
if ((mLogStream = std::ofstream(mNameFLog.toStdString(), std::ios_base::out | std::ios_base::trunc)))
{
qInfo() << "Log File / Stream" << mNameFLog << "created.";
mpApi->setLogStream(mLogStream);
}
else
{
qCritical() << "Can't open Log File / Stream";
}
if (!mfLog.open(QIODevice::ReadOnly | QIODevice::Text))
{
qCritical() << "Can't share Log File / Stream";
}
connect(&mTReadLog, &QTimer::timeout, this, &CNetMD::extractPercent);
connect(this, &CNetMD::finished, this, &CNetMD::procEnded);
}
CNetMD::~CNetMD()
{
mTReadLog.stop();
mfLog.close();
if (mLogStream)
{
mLogStream.close();
}
if (mpApi != nullptr)
{
delete mpApi;
}
}
void CNetMD::start(NetMDStartup startup)
{
mLog.clear();
mCurrJob = startup;
mTReadLog.start();
QThread::start();
}
//--------------------------------------------------------------------------
//! @brief store data, start thread
//!
//! @param[in] tocData TOC data for manipulation
//! @param[in] resetDev reset device after TOC edit
//! @param[in] mono mono flag for TOC edit
//--------------------------------------------------------------------------
void CNetMD::start(const TocData& tocData, bool resetDev, bool mono)
{
mLog.clear();
mTocData = tocData;
mCurrJob.mCmd = NetMDCmd::TOC_MANIP;
mCurrJob.miFirst = resetDev ? 1 : 0;
mMono = mono;
mTReadLog.start();
QThread::start();
}
//--------------------------------------------------------------------------
//! @brief init the NetMD device
//--------------------------------------------------------------------------
void CNetMD::initNetMdDevice()
{
// set log level for netmd++
switch(g_LogFilter)
{
case c2n::LogLevel::DEBUG:
mpApi->setLogLevel(netmd::DEBUG);
break;
case c2n::LogLevel::INFO:
mpApi->setLogLevel(netmd::INFO);
break;
case c2n::LogLevel::WARN:
mpApi->setLogLevel(netmd::WARN);
break;
case c2n::LogLevel::CRITICAL:
case c2n::LogLevel::FATAL:
mpApi->setLogLevel(netmd::CRITICAL);
break;
}
mpApi->initDevice();
mDevName = QString::fromStdString(mpApi->getDeviceName());
}
//--------------------------------------------------------------------------
//! @brief get MD disc info
//!
//! @return 0
//--------------------------------------------------------------------------
int CNetMD::getDiscInfo()
{
using namespace netmd;
qInfo() << "getting MD disc / device info ...";
initNetMdDevice();
int i;
uint16_t tc = 0;
std::string s;
std::ostringstream os;
// helper function to clear std::ostringstream
auto clrOs = [&]()->void{os.clear(); os.str("");};
// Construct JSON object
QJsonObject tree;
if (mpApi->discTitle(s) == NETMDERR_NO_ERROR)
{
if (s.empty())
{
s = "<untitled>";
}
tree.insert("title", mdToUtf8(s.c_str()));
}
tree.insert("otf_enc", mpApi->otfEncodeSupported() ? 1 : 0);
tree.insert("toc_manip", mpApi->tocManipSupported() ? 1 : 0);
tree.insert("device", mpApi->getDeviceName().c_str());
tree.insert("sp_upload", mpApi->spUploadSupported() ? 1 : 0);
tree.insert("pcm2mono", mpApi->pcm2MonoSupported() ? 1 : 0);
if ((i = mpApi->trackCount()) > -1)
{
tree.insert("trk_count", i);
tc = i;
}
if ((i = mpApi->discFlags()) > -1)
{
clrOs();
os << "0x" << std::hex << std::setw(2) << std::setfill('0') << i;
tree.insert("disc_flags", os.str().c_str());
}
DiscCapacity capacity;
if (mpApi->discCapacity(capacity) == NETMDERR_NO_ERROR)
{
tree.insert("t_used", toSec(&capacity.recorded));
tree.insert("t_total", toSec(&capacity.total));
tree.insert("t_free", toSec(&capacity.available));
}
Groups mdGroups = mpApi->groups();
QJsonArray groups;
for (const auto& g : mdGroups)
{
if (g.mFirst > 0)
{
int first = g.mFirst;
int last = (g.mLast == -1) ? first : g.mLast;
QJsonObject group;
group.insert("name", mdToUtf8(g.mName.c_str()));
group.insert("first", first);
group.insert("last", last);
groups.append(group);
}
}
tree.insert("groups", groups);
QJsonArray tracks;
AudioEncoding aenc;
TrackProtection tprot;
TrackTime ttime;
uint8_t channel;
for(i = 0; i < tc; i++)
{
QJsonObject track;
s = "";
tprot = TrackProtection::UNKNOWN;
aenc = AudioEncoding::UNKNOWN;
ttime = {0, 0, 0};
mpApi->trackTitle(i, s);
mpApi->trackBitRate(i, aenc, channel);
mpApi->trackFlags(i, tprot);
mpApi->trackTime(i, ttime);
// Create JSON track object and add to array
track.insert("no", i);
// protection
clrOs();
os << tprot;
track.insert("protect", os.str().c_str());
// bitrate
clrOs();
os << aenc;
if (channel == 1)
{
os << " Mono";
}
track.insert("bitrate", os.str().c_str());
// time
clrOs();
os << ttime;
track.insert("time", os.str().c_str());
// title
if (s.empty())
{
s = "<untitled>";
}
else if (s.find("LP:") == 0)
{
s = s.substr(3);
}
track.insert("name", mdToUtf8(s.c_str()));
tracks.append(track);
}
tree.insert("tracks", tracks);
QJsonDocument jdoc(tree);
QByteArray ba = jdoc.toJson(QJsonDocument::Indented);
if (ba.isEmpty())
{
ba = EMPTY_JSON_RESP;
}
emit jsonOut(static_cast<const char*>(ba));
qInfo() << static_cast<const char*>(ba);
return 0;
}
//--------------------------------------------------------------------------
//! @brief write audio track to MD
//!
//! @param[in] cmd write command
//! @param[in] fName file name of source file
//! @param[in] title track title
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::writeTrack(const NetMDCmd& cmd, const QString& fName, const QString& title)
{
qInfo() << "send track:" << title << "file:" << fName << "to" << mDevName;
int ret = netmd::NETMDERR_NO_ERROR;
netmd::DiskFormat onTheFlyConvert;
bool spMono = false;
switch(cmd)
{
case NetMDCmd::WRITE_TRACK_LP2:
onTheFlyConvert = netmd::NETMD_DISKFORMAT_LP2;
ret = netmd::NETMDERR_NO_ERROR;
break;
case NetMDCmd::WRITE_TRACK_LP4:
onTheFlyConvert = netmd::NETMD_DISKFORMAT_LP4;
ret = netmd::NETMDERR_NO_ERROR;
break;
case NetMDCmd::WRITE_TRACK_SP:
case NetMDCmd::WRITE_TRACK_SP_PREENC: // patch will be automatically applied
onTheFlyConvert = netmd::NO_ONTHEFLY_CONVERSION;
ret = netmd::NETMDERR_NO_ERROR;
break;
case NetMDCmd::WRITE_TRACK_SP_MONO:
qInfo() << "Enable PCM to Mono patch." << Qt::endl;
spMono = true;
onTheFlyConvert = netmd::NETMD_DISKFORMAT_SP_MONO;
ret = mpApi->enablePcm2Mono();
break;
default:
qCritical() << "Wrong command given:" << static_cast<int>(cmd);
ret = netmd::NETMDERR_OTHER;
break;
}
if (ret == netmd::NETMDERR_NO_ERROR)
{
ret = mpApi->sendAudioFile(fName.toStdString(),
static_cast<const char*>(utf8ToMd(title)),
onTheFlyConvert);
if (spMono)
{
qInfo() << "Disable PCM to Mono patch." << Qt::endl;
mpApi->disablePcm2Mono();
}
}
return ret;
}
//--------------------------------------------------------------------------
//! @brief add MD group
//!
//! @param[in] name group name
//! @param[in] first first track in group
//! @param[in] last last track in group
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::addGroup(const QString& name, int first, int last)
{
qInfo() << "add group" << name << "first track:" << first << "last track:" << last << "to" << mDevName;
return mpApi->createGroup(static_cast<const char*>(utf8ToMd(name)), first, last);
}
//--------------------------------------------------------------------------
//! @brief rename MD
//!
//! @param[in] name new MD name
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::renameDisc(const QString& name)
{
qInfo() << "rename MD to" << name << "on" << mDevName;
return mpApi->setDiscTitle(static_cast<const char*>(utf8ToMd(name)));
}
//--------------------------------------------------------------------------
//! @brief rename MD track
//!
//! @param[in] name new MD track name
//! @param[in] trackNo track number
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::renameTrack(const QString& name, int trackNo)
{
qInfo() << "rename track" << trackNo << "to" << name << "on" << mDevName;
return mpApi->setTrackTitle(trackNo, static_cast<const char*>(utf8ToMd(name)));
}
//--------------------------------------------------------------------------
//! @brief rename MD group
//!
//! @param[in] name new MD group name
//! @param[in] groupNo group number
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::renameGroup(const QString& name, int groupNo)
{
qInfo() << "rename group" << groupNo << "to" << name << "on" << mDevName;
return mpApi->setGroupTitle(groupNo, static_cast<const char*>(utf8ToMd(name)));
}
//--------------------------------------------------------------------------
//! @brief erase disc
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::eraseDisc()
{
qInfo() << "erase disc on" << mDevName;
return mpApi->eraseDisc();
}
//--------------------------------------------------------------------------
//! @brief delete MD group
//!
//! @param[in] groupNo group number
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::delGroup(int groupNo)
{
qInfo() << "delete group" << groupNo << "on" << mDevName;
return mpApi->deleteGroup(groupNo);
}
//--------------------------------------------------------------------------
//! @brief delete MD track
//!
//! @param[in] trackNo track number
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::delTrack(int trackNo)
{
qInfo() << "delete track" << trackNo << "on" << mDevName;
return mpApi->deleteTrack(trackNo);
}
//--------------------------------------------------------------------------
//! @brief do TOC manipulation
//!
//! @param[in] resetDev reset device after TOC edit
//!
//! @return 0 -> success; else -> error
//--------------------------------------------------------------------------
int CNetMD::doTocManip(bool devReset)
{
CTocManip manip(mpApi);
return manip.manipulateTOC(mTocData, devReset, mMono);
}
void CNetMD::run()
{
int ret = 0;
switch(mCurrJob.mCmd)
{
case NetMDCmd::DISCINFO:
ret = getDiscInfo();
break;
case NetMDCmd::WRITE_TRACK_SP:
case NetMDCmd::WRITE_TRACK_SP_MONO:
case NetMDCmd::WRITE_TRACK_SP_PREENC:
case NetMDCmd::WRITE_TRACK_LP2:
case NetMDCmd::WRITE_TRACK_LP4:
ret = writeTrack(mCurrJob.mCmd, mCurrJob.msTrack, mCurrJob.msTitle);
break;
case NetMDCmd::ADD_GROUP:
ret = addGroup(mCurrJob.msGroup, mCurrJob.miFirst, mCurrJob.miLast);
break;
case NetMDCmd::RENAME_DISC:
ret = renameDisc(mCurrJob.msTitle);
break;
case NetMDCmd::RENAME_TRACK:
ret = renameTrack(mCurrJob.msTrack, mCurrJob.miFirst - 1);
break;
case NetMDCmd::RENAME_GROUP:
ret = renameGroup(mCurrJob.msGroup, mCurrJob.miGroup);
break;
case NetMDCmd::ERASE_DISC:
ret = eraseDisc();
break;
case NetMDCmd::DEL_GROUP:
ret = delGroup(mCurrJob.miGroup);
break;
case NetMDCmd::DEL_TRACK:
ret = delTrack(mCurrJob.miFirst);
break;
case NetMDCmd::TOC_MANIP:
if (((ret = doTocManip(!!mCurrJob.miFirst)) == 0) && !!mCurrJob.miFirst)
{
ret = TOCMANIP_DEV_RESET;
}
break;
default:
ret = -1;
break;
}
if (ret < 0)
{
qCritical() << "libnetmd action returned with error: " << ret;
}
if ((mCurrJob.mCmd == NetMDCmd::ERASE_DISC)
|| (mCurrJob.mCmd == NetMDCmd::DEL_TRACK)
|| (ret == TOCMANIP_DEV_RESET)) // TOC edit successful, dev reset done
{
getDiscInfo();
}
emit finished(false, ret);
}
bool CNetMD::busy()
{
return QThread::isRunning();
}
void CNetMD::extractPercent()
{
if (mfLog.isOpen())
{
mLog += QString::fromUtf8(mfLog.readAll());
int pos;
if ((pos = mLog.lastIndexOf(QChar('%'))) > 0)
{
int startPos = mLog.lastIndexOf(QRegExp("[^0-9]+"), pos - 1);
if (startPos > -1)
{
bool ok;
int length = (pos - 1) - startPos;
int percent = mLog.mid(startPos + 1, length).toInt(&ok);
if (ok)
{
emit progress(percent);
}
}
}
}
}
void CNetMD::procEnded(bool, int)
{
mTReadLog.stop();
// flush log stream
mLogStream << std::flush;
// read all content from log
if (mfLog.isOpen())
{
mLog += QString::fromUtf8(mfLog.readAll());
}
if (!mLog.isEmpty())
{
qInfo().noquote() << Qt::endl << static_cast<const char*>(mLog.toUtf8());
}
}