| 1 |
/* |
| 2 |
Copyright (C) 2005 Benjamin Meyer <ben at meyerhome dot net> |
| 3 |
Copyright (C) 2018 Yuri Chornoivan <[email protected]> |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program; if not, write to the Free Software |
| 17 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 18 |
USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <config-audiocd.h> |
| 22 |
|
| 23 |
#include "audiocd_kio_debug.h" |
| 24 |
#include "audiocd_opus_encoder.h" |
| 25 |
#include "encoderopus.h" |
| 26 |
|
| 27 |
#include <QDir> |
| 28 |
#include <QFileInfo> |
| 29 |
#include <QStandardPaths> |
| 30 |
#include <QTemporaryFile> |
| 31 |
|
| 32 |
Q_LOGGING_CATEGORY(AUDIOCD_KIO_LOG, "kf.kio.workers.audiocd") |
| 33 |
|
| 34 |
extern "C" { |
| 35 |
AUDIOCDPLUGINS_EXPORT void create_audiocd_encoders(KIO::WorkerBase *worker, QList<AudioCDEncoder *> &encoders) |
| 36 |
{ |
| 37 |
encoders.append(new EncoderOpus(worker)); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
static const int bitrates[] = {6, 12, 24, 48, 64, 80, 96, 128, 160, 192, 256}; |
| 42 |
|
| 43 |
class EncoderOpus::Private |
| 44 |
{ |
| 45 |
public: |
| 46 |
int bitrate; |
| 47 |
bool write_opus_comments; |
| 48 |
bool waitingForWrite; |
| 49 |
bool processHasExited; |
| 50 |
QString lastErrorMessage; |
| 51 |
uint lastSize; |
| 52 |
KProcess *currentEncodeProcess = nullptr; |
| 53 |
QTemporaryFile *tempFile = nullptr; |
| 54 |
}; |
| 55 |
|
| 56 |
EncoderOpus::EncoderOpus(KIO::WorkerBase *worker) |
| 57 |
: QObject() |
| 58 |
, AudioCDEncoder(worker) |
| 59 |
{ |
| 60 |
d = new Private(); |
| 61 |
d->waitingForWrite = false; |
| 62 |
d->processHasExited = false; |
| 63 |
d->lastSize = 0; |
| 64 |
loadSettings(); |
| 65 |
} |
| 66 |
|
| 67 |
EncoderOpus::~EncoderOpus() |
| 68 |
{ |
| 69 |
delete d; |
| 70 |
} |
| 71 |
|
| 72 |
QWidget *EncoderOpus::getConfigureWidget(KConfigSkeleton **manager) const |
| 73 |
{ |
| 74 |
(*manager) = Settings::self(); |
| 75 |
auto config = new EncoderOpusConfig(); |
| 76 |
config->kcfg_opus_complexity->setRange(0, 10); |
| 77 |
config->kcfg_opus_complexity->setSingleStep(1); |
| 78 |
config->opus_bitrate_settings->hide(); |
| 79 |
return config; |
| 80 |
} |
| 81 |
|
| 82 |
bool EncoderOpus::init() |
| 83 |
{ |
| 84 |
// Determine if opusenc is installed on the system or not. |
| 85 |
if (QStandardPaths::findExecutable(QStringLiteral("opusenc")).isEmpty()) |
| 86 |
return false; |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
void EncoderOpus::loadSettings() |
| 92 |
{ |
| 93 |
// Generate the command line arguments for the current settings |
| 94 |
args.clear(); |
| 95 |
|
| 96 |
Settings *settings = Settings::self(); |
| 97 |
|
| 98 |
if (settings->opus_enc_complexity()) { |
| 99 |
args.append(QStringLiteral("--comp")); |
| 100 |
args.append(QStringLiteral("%1").arg(settings->opus_complexity())); |
| 101 |
} else { |
| 102 |
// Constant Bitrate Encoding |
| 103 |
if (settings->set_opus_cbr()) { |
| 104 |
args.append(QStringLiteral("--bitrate")); |
| 105 |
args.append(QStringLiteral("%1").arg(bitrates[settings->opus_cbr()])); |
| 106 |
d->bitrate = settings->opus_cbr(); |
| 107 |
args.append(QStringLiteral("--hard-cbr")); |
| 108 |
}; |
| 109 |
// Constrained Variable Bitrate Encoding |
| 110 |
if (settings->set_opus_cvbr()) { |
| 111 |
args.append(QStringLiteral("--bitrate")); |
| 112 |
args.append(QStringLiteral("%1").arg(bitrates[settings->opus_cvbr()])); |
| 113 |
d->bitrate = bitrates[settings->opus_cvbr()]; |
| 114 |
args.append(QStringLiteral("--cvbr")); |
| 115 |
}; |
| 116 |
// Average Variable Bitrate Encoding |
| 117 |
if (settings->set_opus_vbr()) { |
| 118 |
args.append(QStringLiteral("--bitrate")); |
| 119 |
args.append(QStringLiteral("%1").arg(bitrates[settings->opus_vbr()])); |
| 120 |
d->bitrate = bitrates[settings->opus_vbr()]; |
| 121 |
args.append(QStringLiteral("--vbr")); |
| 122 |
}; |
| 123 |
}; |
| 124 |
|
| 125 |
d->write_opus_comments = settings->opus_comments(); |
| 126 |
} |
| 127 |
|
| 128 |
unsigned long EncoderOpus::size(long time_secs) const |
| 129 |
{ |
| 130 |
return (time_secs * d->bitrate * 1000) / 8; |
| 131 |
} |
| 132 |
|
| 133 |
long EncoderOpus::readInit(long /*size*/) |
| 134 |
{ |
| 135 |
// Create KProcess |
| 136 |
d->currentEncodeProcess = new KProcess(); |
| 137 |
d->tempFile = new QTemporaryFile(QDir::tempPath() + QLatin1String("/kaudiocd_XXXXXX") + QLatin1String(".opus")); |
| 138 |
d->tempFile->open(); |
| 139 |
d->lastErrorMessage.clear(); |
| 140 |
d->processHasExited = false; |
| 141 |
|
| 142 |
// --raw raw/pcm |
| 143 |
// --raw-rate 44100 (because it is raw you have to specify this) |
| 144 |
*(d->currentEncodeProcess) << QStringLiteral("opusenc") << QStringLiteral("--raw") << QStringLiteral("--raw-rate") << QStringLiteral("44100"); |
| 145 |
*(d->currentEncodeProcess) << args; |
| 146 |
*d->currentEncodeProcess << trackInfo; |
| 147 |
|
| 148 |
// Read in stdin, output to the temp file |
| 149 |
*d->currentEncodeProcess << QStringLiteral("-") << d->tempFile->fileName(); |
| 150 |
|
| 151 |
// qCDebug(AUDIOCD_KIO_LOG) << args; |
| 152 |
|
| 153 |
connect(d->currentEncodeProcess, &KProcess::readyReadStandardOutput, this, &EncoderOpus::receivedStdout); |
| 154 |
connect(d->currentEncodeProcess, &KProcess::readyReadStandardError, this, &EncoderOpus::receivedStderr); |
| 155 |
connect(d->currentEncodeProcess, QOverload<int, QProcess::ExitStatus>::of(&KProcess::finished), this, &EncoderOpus::processExited); |
| 156 |
|
| 157 |
// Launch! |
| 158 |
d->currentEncodeProcess->setOutputChannelMode(KProcess::SeparateChannels); |
| 159 |
d->currentEncodeProcess->start(); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
void EncoderOpus::processExited(int exitCode, QProcess::ExitStatus /*status*/) |
| 164 |
{ |
| 165 |
qCDebug(AUDIOCD_KIO_LOG) << "Opusenc Encoding process exited with: " << exitCode; |
| 166 |
d->processHasExited = true; |
| 167 |
} |
| 168 |
|
| 169 |
void EncoderOpus::receivedStderr() |
| 170 |
{ |
| 171 |
QByteArray error = d->currentEncodeProcess->readAllStandardError(); |
| 172 |
qCDebug(AUDIOCD_KIO_LOG) << "Opusenc stderr: " << error; |
| 173 |
if (!d->lastErrorMessage.isEmpty()) |
| 174 |
d->lastErrorMessage += QLatin1Char('\t'); |
| 175 |
d->lastErrorMessage += QString::fromLocal8Bit(error); |
| 176 |
} |
| 177 |
|
| 178 |
void EncoderOpus::receivedStdout() |
| 179 |
{ |
| 180 |
QString output = QString::fromLocal8Bit(d->currentEncodeProcess->readAllStandardOutput()); |
| 181 |
qCDebug(AUDIOCD_KIO_LOG) << "Opusenc stdout: " << output; |
| 182 |
} |
| 183 |
|
| 184 |
long EncoderOpus::read(qint16 *buf, int frames) |
| 185 |
{ |
| 186 |
if (!d->currentEncodeProcess) |
| 187 |
return 0; |
| 188 |
if (d->processHasExited) |
| 189 |
return -1; |
| 190 |
|
| 191 |
// Pipe the raw data to opusenc |
| 192 |
char *cbuf = reinterpret_cast<char *>(buf); |
| 193 |
d->currentEncodeProcess->write(cbuf, frames * 4); |
| 194 |
// We can't return until the buffer has been written |
| 195 |
d->currentEncodeProcess->waitForBytesWritten(-1); |
| 196 |
|
| 197 |
// Determine the file size increase |
| 198 |
QFileInfo file(d->tempFile->fileName()); |
| 199 |
uint change = file.size() - d->lastSize; |
| 200 |
d->lastSize = file.size(); |
| 201 |
return change; |
| 202 |
} |
| 203 |
|
| 204 |
long EncoderOpus::readCleanup() |
| 205 |
{ |
| 206 |
if (!d->currentEncodeProcess) |
| 207 |
return 0; |
| 208 |
|
| 209 |
// Let opusenc tag the first frame of the opus |
| 210 |
d->currentEncodeProcess->closeWriteChannel(); |
| 211 |
d->currentEncodeProcess->waitForFinished(-1); |
| 212 |
|
| 213 |
// Now copy the file out of the temp into kio |
| 214 |
QFile file(d->tempFile->fileName()); |
| 215 |
if (file.open(QIODevice::ReadOnly)) { |
| 216 |
char data[1024]; |
| 217 |
while (!file.atEnd()) { |
| 218 |
uint read = file.read(data, 1024); |
| 219 |
QByteArray output(data, read); |
| 220 |
ioWorker->data(output); |
| 221 |
} |
| 222 |
file.close(); |
| 223 |
} |
| 224 |
|
| 225 |
// cleanup the process and temp |
| 226 |
delete d->currentEncodeProcess; |
| 227 |
delete d->tempFile; |
| 228 |
d->lastSize = 0; |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
void EncoderOpus::fillSongInfo(KCDDB::CDInfo info, int track, const QString &comment) |
| 234 |
{ |
| 235 |
trackInfo.clear(); |
| 236 |
|
| 237 |
if (!d->write_opus_comments) |
| 238 |
return; |
| 239 |
|
| 240 |
trackInfo.append(QStringLiteral("--album")); |
| 241 |
trackInfo.append(info.get(Title).toString()); |
| 242 |
|
| 243 |
trackInfo.append(QStringLiteral("--artist")); |
| 244 |
trackInfo.append(info.track(track - 1).get(Artist).toString()); |
| 245 |
|
| 246 |
trackInfo.append(QStringLiteral("--title")); |
| 247 |
trackInfo.append(info.track(track - 1).get(Title).toString()); |
| 248 |
|
| 249 |
trackInfo.append(QStringLiteral("--date")); |
| 250 |
trackInfo.append(QDate(info.get(Year).toInt(), 1, 1).toString(Qt::ISODate)); |
| 251 |
|
| 252 |
trackInfo.append(QStringLiteral("--comment")); |
| 253 |
trackInfo.append(QStringLiteral("DESCRIPTION=") + comment); |
| 254 |
|
| 255 |
trackInfo.append(QStringLiteral("--comment")); |
| 256 |
trackInfo.append(QStringLiteral("TRACKNUMBER=") + QString::number(track)); |
| 257 |
|
| 258 |
trackInfo.append(QStringLiteral("--genre")); |
| 259 |
trackInfo.append(QStringLiteral("%1").arg(info.get(Genre).toString())); |
| 260 |
} |
| 261 |
|
| 262 |
QString EncoderOpus::lastErrorMessage() const |
| 263 |
{ |
| 264 |
return d->lastErrorMessage; |
| 265 |
} |
| 266 |
|
| 267 |
#include "moc_encoderopus.cpp" |