2011-12-22 00:06:34 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2021-02-17 00:52:18 +01:00
// Copyright (C) 2004-2021 LoRd_MuldeR <MuldeR2@GMX.de>
2011-12-22 00:06:34 +01:00
//
// This program is free software; you can redistribute it and/or modify
2020-03-28 15:31:01 +01:00
// it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
2011-12-22 00:06:34 +01:00
// the Free Software Foundation; either version 2 of the License, or
2020-03-28 15:31:01 +01:00
// (at your option) any later version; always including the non-optional
// LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
2011-12-22 00:06:34 +01:00
//
// 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; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// http://www.gnu.org/licenses/gpl-2.0.txt
///////////////////////////////////////////////////////////////////////////////
# include "Tool_WaveProperties.h"
2014-11-30 21:32:23 +01:00
//Internal
2011-12-22 00:06:34 +01:00
# include "Global.h"
# include "Model_AudioFile.h"
2014-11-30 21:32:23 +01:00
//MUtils
# include <MUtils/Exception.h>
//Qt
2014-05-28 16:45:44 +02:00
# include <QDir>
2011-12-22 00:06:34 +01:00
# include <QProcess>
WaveProperties : : WaveProperties ( void )
:
2014-12-20 23:44:43 +01:00
m_binary ( lamexp_tools_lookup ( " sox.exe " ) )
2011-12-22 00:06:34 +01:00
{
if ( m_binary . isEmpty ( ) )
{
2014-11-30 21:32:23 +01:00
MUTILS_THROW ( " Error initializing MP3 encoder. Tool 'lame.exe' is not registred! " ) ;
2011-12-22 00:06:34 +01:00
}
}
WaveProperties : : ~ WaveProperties ( void )
{
}
2017-04-18 21:05:28 +02:00
bool WaveProperties : : detect ( const QString & sourceFile , AudioFileModel_TechInfo * info , QAtomicInt & abortFlag )
2011-12-22 00:06:34 +01:00
{
QProcess process ;
QStringList args ;
2014-05-28 16:45:44 +02:00
args < < " --i " < < QDir : : toNativeSeparators ( sourceFile ) ;
2011-12-22 00:06:34 +01:00
if ( ! startProcess ( process , m_binary , args ) )
{
return false ;
}
int progress = 0 ;
2017-12-11 20:00:50 +01:00
QRegExp regExp_prc ( " Precision \\ s*: \\ s*( \\ d+) - bit " , Qt::CaseInsensitive) ;
QRegExp regExp_srt ( " Sample Rate \\ s*: \\ s*( \\ d+) " , Qt::CaseInsensitive) ;
QRegExp regExp_drt ( " Duration \\ s*: \\ s*( \\ d \\ d) : ( \ \ d \ \ d ) : ( \ \ d \ \ d ) \ \ . ( \ \ d \ \ d ) " , Qt::CaseInsensitive) ;
QRegExp regExp_chl ( " Channels \\ s*: \\ s*( \\ d+) " , Qt::CaseInsensitive) ;
QRegExp regExp_enc ( " 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!
2011-12-22 00:06:34 +01:00
2017-12-11 20:00:50 +01:00
const result_t result = awaitProcess ( process , abortFlag , [ this , & info , & progress , & regExp_prc , & regExp_srt , & regExp_drt , & regExp_chl , & regExp_enc ] ( const QString & text )
2011-12-22 00:06:34 +01:00
{
2017-12-11 20:00:50 +01:00
if ( regExp_prc . lastIndexIn ( text ) > = 0 )
2011-12-22 00:06:34 +01:00
{
2017-12-11 20:00:50 +01:00
quint32 tmp ;
if ( MUtils : : regexp_parse_uint32 ( regExp_prc , tmp ) )
2011-12-22 00:06:34 +01:00
{
2017-12-11 20:00:50 +01:00
info - > setAudioBitdepth ( tmp ) ;
2011-12-22 00:06:34 +01:00
emit statusUpdated ( qMin ( progress + = 25 , 100 ) ) ;
}
2017-12-11 20:00:50 +01:00
return true ;
}
if ( regExp_enc . lastIndexIn ( text ) > = 0 )
{
quint32 tmp ;
if ( MUtils : : regexp_parse_uint32 ( regExp_enc , tmp ) )
2012-03-08 23:04:00 +01:00
{
2017-12-11 20:00:50 +01:00
info - > setAudioBitdepth ( ( tmp = = 32 ) ? AudioFileModel : : BITDEPTH_IEEE_FLOAT32 : tmp ) ;
2012-03-08 23:04:00 +01:00
emit statusUpdated ( qMin ( progress + = 25 , 100 ) ) ;
}
2017-12-11 20:00:50 +01:00
return true ;
}
if ( regExp_srt . lastIndexIn ( text ) > = 0 )
{
quint32 tmp ;
if ( MUtils : : regexp_parse_uint32 ( regExp_srt , tmp ) )
2011-12-22 00:06:34 +01:00
{
2017-12-11 20:00:50 +01:00
info - > setAudioSamplerate ( tmp ) ;
2011-12-22 00:06:34 +01:00
emit statusUpdated ( qMin ( progress + = 25 , 100 ) ) ;
}
2017-12-11 20:00:50 +01:00
return true ;
}
if ( regExp_drt . lastIndexIn ( text ) > = 0 )
{
quint32 tmp [ 4 ] ;
if ( MUtils : : regexp_parse_uint32 ( regExp_drt , tmp , 4 ) )
2011-12-22 00:06:34 +01:00
{
2017-12-11 20:00:50 +01:00
info - > setDuration ( ( tmp [ 0 ] * 3600 ) + ( tmp [ 1 ] * 60 ) + tmp [ 2 ] + qRound ( static_cast < double > ( tmp [ 3 ] ) / 100.0 ) ) ;
2011-12-22 00:06:34 +01:00
emit statusUpdated ( qMin ( progress + = 25 , 100 ) ) ;
}
2017-12-11 20:00:50 +01:00
return true ;
}
if ( regExp_chl . lastIndexIn ( text ) > = 0 )
{
quint32 tmp ;
if ( MUtils : : regexp_parse_uint32 ( regExp_chl , tmp ) )
2011-12-22 00:06:34 +01:00
{
2017-12-11 20:00:50 +01:00
info - > setAudioChannels ( tmp ) ;
2011-12-22 00:06:34 +01:00
emit statusUpdated ( qMin ( progress + = 25 , 100 ) ) ;
}
2017-12-11 20:00:50 +01:00
return true ;
2011-12-22 00:06:34 +01:00
}
return false ;
2017-12-11 20:00:50 +01:00
} ) ;
2011-12-22 00:06:34 +01:00
2017-12-11 20:00:50 +01:00
return ( result = = RESULT_SUCCESS ) ;
2011-12-22 00:06:34 +01:00
}