-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3gnormalizer.cpp
More file actions
141 lines (132 loc) · 4.62 KB
/
mp3gnormalizer.cpp
File metadata and controls
141 lines (132 loc) · 4.62 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
#include "mp3gnormalizer.h"
#include <QDebug>
#include <QFile>
#include <QTemporaryDir>
#include <QProcess>
#include <QFileInfo>
#include "ziphandler.h"
Mp3gNormalizer::Mp3gNormalizer(QObject *parent) : QObject(parent)
{
force = false;
zipLvl = 9;
}
void Mp3gNormalizer::setFilename(QString fname)
{
this->fname = fname;
}
void Mp3gNormalizer::setForce(bool force)
{
this->force = force;
}
void Mp3gNormalizer::run()
{
qWarning() << "Processing file: " << fname;
qWarning() << "Force: " << force;
if (!QFile::exists(fname))
{
qWarning() << "Specified file does not exist!";
emit finished();
return;
}
processZipFileRg(fname);
emit finished();
}
void Mp3gNormalizer::aboutToQuitApp()
{
}
void Mp3gNormalizer::processZipFileRg(QString fileName)
{
QString mp3gainPath = "mp3gain";
qWarning() << "Processing - File: " << fileName;
QFileInfo info(fileName);
QString baseName = info.completeBaseName();
QTemporaryDir tmpDir;
ZipHandler zipper;
zipper.setZipFile(fileName);
if ((zipper.containsRGMarkerFile()) && (!force))
{
qWarning() << "File contains marker, already processed. Skipping";
zipper.close();
return;
}
qWarning() << "Processing - Extracting mp3 file";
if (!zipper.extractAudio(QDir(tmpDir.path())))
{
qWarning() << "Bad file contents - error extracting mp3";
zipper.close();
return;
}
QFile::copy(tmpDir.path() + QDir::separator() + "tmp.mp3", "/storage/KaraokeRGTest/PreRG.mp3");
qWarning() << "Processing - Extracting cdg file";
if (!zipper.extractCdg(QDir(tmpDir.path())))
{
qWarning() << "Processing - Bad file contents - error extracting cdg";
zipper.close();
return;
}
zipper.close();
QString program = mp3gainPath;
QStringList arguments;
arguments << "-c";
arguments << "-r";
arguments << "-T";
arguments << "-s" << "r";
arguments << tmpDir.path() + QDir::separator() + "tmp.mp3";
QProcess process;
qWarning() << "Doing mp3gain processing...";
process.start(program, arguments);
process.waitForFinished();
int exitCode = process.exitCode();
QFile::copy(tmpDir.path() + QDir::separator() + "tmp.mp3", "/storage/KaraokeRGTest/PostRG.mp3");
//qWarning() << process.readAllStandardOutput();
//qWarning() << process.readAllStandardError();
if (exitCode != 0)
{
qWarning() << "Error occurred while running mp3gain, aborting";
qWarning() << "mp3gain error output: " << process.readAllStandardError();
return;
}
qWarning() << "Processing - Creating zip file";
QFile::rename(tmpDir.path() + QDir::separator() + "tmp.mp3", tmpDir.path() + QDir::separator() + baseName + ".mp3");
QFile::rename(tmpDir.path() + QDir::separator() + "tmp.cdg", tmpDir.path() + QDir::separator() + baseName + ".cdg");
zipper.setReplaceFnsWithZipFn(false);
zipper.setCompressionLevel(zipLvl);
if (zipper.createZip(tmpDir.path() + QDir::separator() + info.fileName(), tmpDir.path() + QDir::separator() + baseName + ".cdg", tmpDir.path() + QDir::separator() + baseName + ".mp3", true))
{
qWarning () << "Processing - Replacing original file";
if (QFile(tmpDir.path() + QDir::separator() + info.fileName()).exists())
{
//qWarning() << "Doing QFile::rename(" << info.absoluteFilePath() << ", " << info.absoluteFilePath() + ".tmp" << ");";
if (QFile::rename(info.absoluteFilePath(), info.absoluteFilePath() + ".tmp"))
{
if (QFile::copy(tmpDir.path() + QDir::separator() + info.fileName(), info.absoluteFilePath()))
{
qWarning() << "New file copied into place, deleting old one";
if (!QFile::remove(info.absoluteFilePath() + ".tmp"))
{
qWarning() << "Error deleting old file";
}
}
else
{
qWarning() << "Unable to move new file into place, restoring old one";
QFile::rename(info.absoluteFilePath() + ".tmp", info.absoluteFilePath());
}
}
else
{
qWarning() << "Unable to move existing file to tmp file";
return;
}
}
else
{
qWarning() << "Unexpected error, new processed zip file missing!";
}
qWarning() << "Processing - Complete for file: " << fileName;
}
else
{
qWarning() << "Failed to create new archive, leaving original file in place";
}
}