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
|
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
*/
#include "extractjob.h"
#include "cdparanoia.h"
#include "device.h"
#include "gui/covers.h"
#include "gui/settings.h"
#include "mpd-interface/mpdconnection.h"
#include "support/utils.h"
#include "tags/tags.h"
#include <QFile>
#include <QProcess>
#include <QStringList>
const int ExtractJob::constWavHeaderSize = 44;// ffmpeg uses 46 byte header?
static void insertSize(unsigned char* data, qint32 size)
{
data[0] = (size & 0x000000ff);
data[1] = (size & 0x0000ff00) >> 8;
data[2] = (size & 0x00ff0000) >> 16;
data[3] = (size & 0xff000000) >> 24;
}
void ExtractJob::writeWavHeader(QIODevice& dev, qint32 size)
{
unsigned char riffHeader[] = {
0x52, 0x49, 0x46, 0x46,// 0 "RIFF"
0x00, 0x00, 0x00, 0x00,// 4 wavSize
0x57, 0x41, 0x56, 0x45,// 8 "WAVE"
0x66, 0x6d, 0x74, 0x20,// 12 "fmt "
0x10, 0x00, 0x00, 0x00,// 16 Size of WAVE section chunk (ffmpeg has 12 here???)
0x01, 0x00, 0x02, 0x00,// 20 WAVE type format / Number of channels
0x44, 0xac, 0x00, 0x00,// 24 Samples per second
0x10, 0xb1, 0x02, 0x00,// 28 Bytes per second
0x04, 0x00, 0x10, 0x00,// 32 Block alignment / Bits per sample
0x64, 0x61, 0x74, 0x61,// 36 "data" (ffmpeg preceeds this with two 0 bytes)
0x00, 0x00, 0x00, 0x00 // 40 byteCount
};
if (0 != size) {
insertSize(&riffHeader[4], (size + constWavHeaderSize) - 8);
insertSize(&riffHeader[40], size);
}
dev.write((char*)riffHeader, constWavHeaderSize);
}
ExtractJob::ExtractJob(const Encoders::Encoder& enc, int val, const QString& src, const QString& dest, const Song& s, const QString& cover)
: encoder(enc), value(val), srcFile(src), destFile(dest), song(s), coverFile(cover), copiedCover(false)
{
}
ExtractJob::~ExtractJob()
{
}
void ExtractJob::run()
{
if (stopRequested) {
emit result(Device::Cancelled);
}
else {
QStringList encParams = encoder.params(value, encoder.transcoder ? "pipe:" : "-", destFile);
CdParanoia cdparanoia(srcFile, Settings::self()->paranoiaFull(), Settings::self()->paranoiaNeverSkip(), false, Settings::self()->paranoiaOffset());
if (!cdparanoia) {
emit result(Device::FailedToLockDevice);
return;
}
QProcess process;
QString cmd = encParams.takeFirst();
process.start(cmd, encParams, QIODevice::WriteOnly);
process.waitForStarted();
if (stopRequested) {
emit result(Device::Cancelled);
process.close();
return;
}
int firstSector = cdparanoia.firstSectorOfTrack(song.id);
int lastSector = cdparanoia.lastSectorOfTrack(song.id);
int total = lastSector - firstSector;
int count = 0;
cdparanoia.seek(firstSector, SEEK_SET);
writeWavHeader(process);
while ((firstSector + count) <= lastSector) {
qint16* buf = cdparanoia.read();
if (!buf) {
emit result(Device::Failed);
process.close();
return;
}
if (stopRequested) {
emit result(Device::Cancelled);
process.close();
return;
}
char* buffer = (char*)buf;
qint64 writePos = 0;
do {
qint64 bytesWritten = process.write(&buffer[writePos], CD_FRAMESIZE_RAW - writePos);
if (stopRequested) {
emit result(Device::Cancelled);
process.close();
QFile::remove(destFile);
return;
}
if (-1 == bytesWritten) {
emit result(Device::WriteFailed);
process.close();
QFile::remove(destFile);
return;
}
writePos += bytesWritten;
} while (writePos < CD_FRAMESIZE_RAW);
count++;
setPercent((count * 100 / total) + 0.5);
}
process.closeWriteChannel();
process.waitForFinished();
Utils::setFilePerms(destFile);
Tags::update(destFile, Song(), song, 3);
if (!stopRequested && !coverFile.isEmpty()) {
copiedCover = Covers::copyImage(Utils::getDir(coverFile), Utils::getDir(destFile), Utils::getFile(coverFile), Covers::albumFileName(song) + Utils::getExtension(coverFile), 0);
}
emit result(Device::Ok);
}
}
#include "moc_extractjob.cpp"
|