Adapt for latest MUtils changes.
[LameXP.git] / src / Decoder_Opus.cpp
blob4226cfa9ce735bf7de2b5e3274e4b2b8deae02d6
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Decoder_Opus.h"
25 //Internal
26 #include "Global.h"
28 //MUtils
29 #include <MUtils/Exception.h>
31 //Qt
32 #include <QDir>
33 #include <QProcess>
34 #include <QRegExp>
35 #include <QUuid>
37 bool OpusDecoder::m_disableResampling = false;
39 OpusDecoder::OpusDecoder(void)
41 m_binary(lamexp_tools_lookup("opusdec.exe"))
43 if(m_binary.isEmpty())
45 MUTILS_THROW("Error initializing Opus decoder. Tool 'opusdec.exe' is not registred!");
49 OpusDecoder::~OpusDecoder(void)
53 bool OpusDecoder::decode(const QString &sourceFile, const QString &outputFile, volatile bool *abortFlag)
55 QProcess process;
56 QStringList args;
58 if(m_disableResampling)
60 args << "--no-resample";
63 args << QDir::toNativeSeparators(sourceFile);
64 args << QDir::toNativeSeparators(outputFile);
66 if(!startProcess(process, m_binary, args))
68 return false;
71 bool bTimeout = false;
72 bool bAborted = false;
73 int prevProgress = -1;
75 QRegExp regExp("\\((\\d+)%\\)");
77 while(process.state() != QProcess::NotRunning)
79 if(*abortFlag)
81 process.kill();
82 bAborted = true;
83 emit messageLogged("\nABORTED BY USER !!!");
84 break;
86 process.waitForReadyRead(m_processTimeoutInterval);
87 if(!process.bytesAvailable() && process.state() == QProcess::Running)
89 process.kill();
90 qWarning("opusdec process timed out <-- killing!");
91 emit messageLogged("\nPROCESS TIMEOUT !!!");
92 bTimeout = true;
93 break;
95 while(process.bytesAvailable() > 0)
97 QByteArray line = process.readLine();
98 QString text = QString::fromUtf8(line.constData()).simplified();
99 if(regExp.lastIndexIn(text) >= 0)
101 bool ok = false;
102 int progress = regExp.cap(1).toInt(&ok);
103 if(ok && (progress > prevProgress))
105 emit statusUpdated(progress);
106 prevProgress = qMin(progress + 2, 99);
109 else if(!text.isEmpty())
111 emit messageLogged(text);
116 process.waitForFinished();
117 if(process.state() != QProcess::NotRunning)
119 process.kill();
120 process.waitForFinished(-1);
123 emit statusUpdated(100);
124 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
126 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
128 return false;
131 return true;
134 bool OpusDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
136 if(containerType.compare("OGG", Qt::CaseInsensitive) == 0)
138 if(formatType.compare("Opus", Qt::CaseInsensitive) == 0)
141 return true;
146 return false;
149 const AbstractDecoder::supportedType_t *OpusDecoder::supportedTypes(void)
151 static const char *exts[] =
153 "opus", "ogg", NULL
156 static const supportedType_t s_supportedTypes[] =
158 { "Opus Audio Codec", exts },
159 { NULL, NULL }
162 return s_supportedTypes;