OSDN Git Service

FLAC encoding support.
[lamexp/LameXP.git] / src / Encoder_FLAC.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 LoRd_MuldeR <MuldeR2@GMX.de>
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 along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Encoder_FLAC.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29
30 #define max(a,b) (((a) > (b)) ? (a) : (b))
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
32 #define IS_UNICODE(STR) (qstricmp(STR.toUtf8().constData(), QString::fromLocal8Bit(STR.toLocal8Bit()).toUtf8().constData()))
33
34 FLACEncoder::FLACEncoder(void)
35 :
36         m_binary(lamexp_lookup_tool("flac.exe"))
37 {
38         if(m_binary.isEmpty())
39         {
40                 throw "Error initializing FLAC encoder. Tool 'flac.exe' is not registred!";
41         }
42 }
43
44 FLACEncoder::~FLACEncoder(void)
45 {
46 }
47
48 bool FLACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
49 {
50         QProcess process;
51         QStringList args;
52
53         args << QString("-%1").arg(QString::number(max(0, min(8, m_configBitrate))));
54         qWarning("Year: %u", metaInfo.fileYear());
55
56         if(!metaInfo.fileName().isEmpty()) args << "-T" << QString("title=%1").arg(metaInfo.fileName());
57         if(!metaInfo.fileArtist().isEmpty()) args << "-T" << QString("artist=%1").arg(metaInfo.fileArtist());
58         if(!metaInfo.fileAlbum().isEmpty()) args << "-T" << QString("album=%1").arg(metaInfo.fileAlbum());
59         if(!metaInfo.fileGenre().isEmpty()) args << "-T" << QString("genre=%1").arg(metaInfo.fileGenre());
60         if(!metaInfo.fileComment().isEmpty()) args << "-T" << QString("comment=%1").arg(metaInfo.fileComment());
61         if(metaInfo.fileYear()) args << "-T" << QString("date=%1").arg(QString::number(metaInfo.fileYear()));
62         if(metaInfo.filePosition()) args << "-T" << QString("track=%1").arg(QString::number(metaInfo.filePosition()));
63         
64         //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
65
66         args << "-f" << "-o" << QDir::toNativeSeparators(outputFile);
67         args << QDir::toNativeSeparators(sourceFile);
68
69         if(!startProcess(process, m_binary, args))
70         {
71                 return false;
72         }
73
74         bool bTimeout = false;
75         bool bAborted = false;
76
77         QRegExp regExp("\\s(\\d+)% complete");
78
79         while(process.state() != QProcess::NotRunning)
80         {
81                 if(*abortFlag)
82                 {
83                         process.kill();
84                         bAborted = true;
85                         emit messageLogged("\nABORTED BY USER !!!");
86                         break;
87                 }
88                 process.waitForReadyRead();
89                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
90                 {
91                         process.kill();
92                         qWarning("FLAC process timed out <-- killing!");
93                         bTimeout = true;
94                         break;
95                 }
96                 while(process.bytesAvailable() > 0)
97                 {
98                         QByteArray line = process.readLine();
99                         QString text = QString::fromUtf8(line.constData()).simplified();
100                         if(regExp.lastIndexIn(text) >= 0)
101                         {
102                                 bool ok = false;
103                                 int progress = regExp.cap(1).toInt(&ok);
104                                 if(ok) emit statusUpdated(progress);
105                         }
106                         else if(!text.isEmpty())
107                         {
108                                 emit messageLogged(text);
109                         }
110                 }
111         }
112
113         process.waitForFinished();
114         if(process.state() != QProcess::NotRunning)
115         {
116                 process.kill();
117                 process.waitForFinished(-1);
118         }
119         
120         emit statusUpdated(100);
121         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
122
123         if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
124         {
125                 return false;
126         }
127         
128         return true;
129 }
130
131 QString FLACEncoder::extension(void)
132 {
133         return "flac";
134 }
135
136 bool FLACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
137 {
138         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
139         {
140                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
141                 {
142                         return true;
143                 }
144         }
145
146         return false;
147 }