OSDN Git Service

2b983d514a3dc0ba2ffe959f61046f54376d2179
[lamexp/LameXP.git] / src / Encoder_MP3.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_MP3.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29
30 #define IS_UNICODE(STR) (qstricmp(STR.toUtf8().constData(), QString::fromLocal8Bit(STR.toLocal8Bit()).toUtf8().constData()))
31
32 MP3Encoder::MP3Encoder(void)
33 :
34         m_binary(lamexp_lookup_tool("lame.exe"))
35 {
36         if(m_binary.isEmpty())
37         {
38                 throw "Error initializing MP3 encoder. Tool 'lame.exe' is not registred!";
39         }
40 }
41
42 MP3Encoder::~MP3Encoder(void)
43 {
44 }
45
46 bool MP3Encoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
47 {
48         QProcess process;
49         QStringList args;
50
51         args << "--nohist";
52         args << "-h";
53                 
54         switch(m_configRCMode)
55         {
56         case SettingsModel::VBRMode:
57                 args << "-V" << QString::number(9 - min(9, m_configBitrate));
58                 break;
59         case SettingsModel::ABRMode:
60                 args << "--abr" << QString::number(SettingsModel::mp3Bitrates[min(13, m_configBitrate)]);
61                 break;
62         case SettingsModel::CBRMode:
63                 args << "--cbr";
64                 args << "-b" << QString::number(SettingsModel::mp3Bitrates[min(13, m_configBitrate)]);
65                 break;
66         default:
67                 throw "Bad rate-control mode!";
68                 break;
69         }
70
71         if(!metaInfo.fileName().isEmpty()) args << (IS_UNICODE(metaInfo.fileName()) ? "--uTitle" : "--lTitle") << metaInfo.fileName();
72         if(!metaInfo.fileArtist().isEmpty()) args << (IS_UNICODE(metaInfo.fileArtist()) ? "--uArtist" : "--lArtist") << metaInfo.fileArtist();
73         if(!metaInfo.fileAlbum().isEmpty()) args << (IS_UNICODE(metaInfo.fileAlbum()) ? "--uAlbum" : "--lAlbum") << metaInfo.fileAlbum();
74         if(!metaInfo.fileGenre().isEmpty()) args << (IS_UNICODE(metaInfo.fileGenre()) ? "--uGenre" : "--lGenre") << metaInfo.fileGenre();
75         if(!metaInfo.fileComment().isEmpty()) args << (IS_UNICODE(metaInfo.fileComment()) ? "--uComment" : "--lComment") << metaInfo.fileComment();
76         if(metaInfo.fileYear()) args << "--ty" << QString::number(metaInfo.fileYear());
77         if(metaInfo.filePosition()) args << "--tn" << QString::number(metaInfo.filePosition());
78         
79         //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
80
81         args << QDir::toNativeSeparators(sourceFile);
82         args << QDir::toNativeSeparators(outputFile);
83
84         if(!startProcess(process, m_binary, args))
85         {
86                 return false;
87         }
88
89         bool bTimeout = false;
90         bool bAborted = false;
91
92         QRegExp regExp("\\(.*(\\d+)%\\)\\|");
93
94         while(process.state() != QProcess::NotRunning)
95         {
96                 if(*abortFlag)
97                 {
98                         process.kill();
99                         bAborted = true;
100                         emit messageLogged("\nABORTED BY USER !!!");
101                         break;
102                 }
103                 process.waitForReadyRead();
104                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
105                 {
106                         process.kill();
107                         qWarning("LAME process timed out <-- killing!");
108                         bTimeout = true;
109                         break;
110                 }
111                 while(process.bytesAvailable() > 0)
112                 {
113                         QByteArray line = process.readLine();
114                         QString text = QString::fromUtf8(line.constData()).simplified();
115                         if(regExp.lastIndexIn(text) >= 0)
116                         {
117                                 bool ok = false;
118                                 int progress = regExp.cap(1).toInt(&ok);
119                                 if(ok) emit statusUpdated(progress);
120                         }
121                         else if(!text.isEmpty())
122                         {
123                                 emit messageLogged(text);
124                         }
125                 }
126         }
127
128         process.waitForFinished();
129         if(process.state() != QProcess::NotRunning)
130         {
131                 process.kill();
132                 process.waitForFinished(-1);
133         }
134         
135         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
136
137         if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
138         {
139                 return false;
140         }
141         
142         return true;
143 }
144
145 QString MP3Encoder::extension(void)
146 {
147         return "mp3";
148 }
149
150 bool MP3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
151 {
152         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
153         {
154                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
155                 {
156                         return true;
157                 }
158         }
159         else if(containerType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
160         {
161                 if(formatType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
162                 {
163                         if(formatProfile.compare("Layer 3", Qt::CaseInsensitive) == 0 || formatProfile.compare("Layer 2", Qt::CaseInsensitive) == 0)
164                         {
165                                 if(formatVersion.compare("Version 1", Qt::CaseInsensitive) == 0 || formatVersion.compare("Version 2", Qt::CaseInsensitive) == 0)
166                                 {
167                                         return true;
168                                 }
169                         }
170                 }
171         }
172
173         return false;
174 }