Updated Ukrainian translation.
[LameXP.git] / src / Tool_WaveProperties.cpp
blobd73354857ecc39510d091da3503f23098c4d0571
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Tool_WaveProperties.h"
24 #include "Global.h"
25 #include "Model_AudioFile.h"
27 #include <QProcess>
29 WaveProperties::WaveProperties(void)
31 m_binary(lamexp_lookup_tool("sox.exe"))
33 if(m_binary.isEmpty())
35 throw "Error initializing MP3 encoder. Tool 'lame.exe' is not registred!";
39 WaveProperties::~WaveProperties(void)
43 bool WaveProperties::detect(const QString &sourceFile, AudioFileModel *info, volatile bool *abortFlag)
45 QProcess process;
46 QStringList args;
48 args << "--i" << sourceFile;
50 if(!startProcess(process, m_binary, args))
52 return false;
55 bool bTimeout = false;
56 bool bAborted = false;
58 int progress = 0;
60 QRegExp regExp_precision("Precision\\s*:\\s*(\\d+)-bit", Qt::CaseInsensitive);
61 QRegExp regExp_samplerate("Sample Rate\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
62 QRegExp regExp_duration("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d)", Qt::CaseInsensitive);
63 QRegExp regExp_channels("Channels\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
64 QRegExp regExp_encoding("Sample Encoding\\s*:\\s*(\\d+)-bit\\s*Float", Qt::CaseInsensitive); //SoX returns a precision of 24-Bit for 32-Bit Float data, so we detect it this way!
66 while(process.state() != QProcess::NotRunning)
68 if(*abortFlag)
70 process.kill();
71 bAborted = true;
72 emit messageLogged("\nABORTED BY USER !!!");
73 break;
75 process.waitForReadyRead(m_processTimeoutInterval);
76 if(!process.bytesAvailable() && process.state() == QProcess::Running)
78 process.kill();
79 qWarning("SoX process timed out <-- killing!");
80 emit messageLogged("\nPROCESS TIMEOUT !!!");
81 bTimeout = true;
82 break;
84 while(process.bytesAvailable() > 0)
86 QByteArray line = process.readLine();
87 QString text = QString::fromUtf8(line.constData()).simplified();
88 if(regExp_precision.lastIndexIn(text) >= 0)
90 bool ok = false;
91 unsigned int tmp = regExp_precision.cap(1).toUInt(&ok);
92 if(ok) info->setFormatAudioBitdepth(tmp);
93 emit statusUpdated(qMin(progress += 25, 100));
95 if(regExp_encoding.lastIndexIn(text) >= 0)
97 bool ok = false;
98 unsigned int tmp = regExp_encoding.cap(1).toUInt(&ok);
99 if(ok) info->setFormatAudioBitdepth((tmp == 32) ? AudioFileModel::BITDEPTH_IEEE_FLOAT32 : tmp);
100 emit statusUpdated(qMin(progress += 25, 100));
102 if(regExp_samplerate.lastIndexIn(text) >= 0)
104 bool ok = false;
105 unsigned int tmp = regExp_samplerate.cap(1).toUInt(&ok);
106 if(ok) info->setFormatAudioSamplerate(tmp);
107 emit statusUpdated(qMin(progress += 25, 100));
109 if(regExp_duration.lastIndexIn(text) >= 0)
111 bool ok[4] = {false, false, false, false};
112 unsigned int tmp1 = regExp_duration.cap(1).toUInt(&ok[0]);
113 unsigned int tmp2 = regExp_duration.cap(2).toUInt(&ok[1]);
114 unsigned int tmp3 = regExp_duration.cap(3).toUInt(&ok[2]);
115 unsigned int tmp4 = regExp_duration.cap(4).toUInt(&ok[3]);
116 if(ok[0] && ok[1] && ok[2] && ok[3])
118 info->setFileDuration((tmp1 * 3600) + (tmp2 * 60) + tmp3 + qRound(static_cast<double>(tmp4) / 100.0));
120 emit statusUpdated(qMin(progress += 25, 100));
122 if(regExp_channels.lastIndexIn(text) >= 0)
124 bool ok = false;
125 unsigned int tmp = regExp_channels.cap(1).toUInt(&ok);
126 if(ok) info->setFormatAudioChannels(tmp);
127 emit statusUpdated(qMin(progress += 25, 100));
129 if(!text.isEmpty())
131 emit messageLogged(text);
136 process.waitForFinished();
137 if(process.state() != QProcess::NotRunning)
139 process.kill();
140 process.waitForFinished(-1);
143 emit statusUpdated(100);
144 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
146 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
148 return false;
151 return true;