File indexing completed on 2024-05-12 04:51:02

0001 /*
0002     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3baudionormalizejob.h"
0007 #include "k3bexternalbinmanager.h"
0008 #include "k3bprocess.h"
0009 #include "k3bcore.h"
0010 #include "k3b_i18n.h"
0011 
0012 #include <QDebug>
0013 
0014 
0015 K3b::AudioNormalizeJob::AudioNormalizeJob( K3b::JobHandler* hdl, QObject* parent )
0016     : K3b::Job( hdl, parent ),
0017       m_process(0)
0018 {
0019 }
0020 
0021 
0022 K3b::AudioNormalizeJob::~AudioNormalizeJob()
0023 {
0024     delete m_process;
0025 }
0026 
0027 
0028 void K3b::AudioNormalizeJob::start()
0029 {
0030     m_canceled = false;
0031     m_currentAction = COMPUTING_LEVELS;
0032     m_currentTrack = 1;
0033 
0034     jobStarted();
0035 
0036     if( m_process )
0037         delete m_process;
0038 
0039     m_process = new K3b::Process();
0040     connect( m_process, SIGNAL(stderrLine(QString)), this, SLOT(slotStdLine(QString)) );
0041     connect( m_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessExited(int,QProcess::ExitStatus)) );
0042 
0043     const K3b::ExternalBin* bin = k3bcore->externalBinManager()->binObject( "normalize" );
0044 
0045     if( !bin ) {
0046         emit infoMessage( i18n("Could not find normalize executable."), MessageError );
0047         jobFinished(false);
0048         return;
0049     }
0050 
0051     if( !bin->copyright().isEmpty() )
0052         emit infoMessage( i18n("Using %1 %2 – Copyright © %3",bin->name(),bin->version(),bin->copyright()), MessageInfo );
0053 
0054     // create the commandline
0055     *m_process << bin;
0056 
0057     // additional user parameters from config
0058     const QStringList& params = bin->userParameters();
0059     for( QStringList::const_iterator it = params.begin(); it != params.end(); ++it )
0060         *m_process << *it;
0061 
0062     // end the options
0063     *m_process << "--";
0064 
0065     // add the files
0066     for( int i = 0; i < m_files.count(); ++i )
0067         *m_process << m_files[i];
0068 
0069     // now start the process
0070     if( !m_process->start( KProcess::OnlyStderrChannel ) ) {
0071         // something went wrong when starting the program
0072         // it "should" be the executable
0073         qDebug() << "(K3b::AudioNormalizeJob) could not start normalize";
0074         emit infoMessage( i18n("Could not start normalize."), K3b::Job::MessageError );
0075         jobFinished(false);
0076     }
0077 }
0078 
0079 
0080 void K3b::AudioNormalizeJob::cancel()
0081 {
0082     m_canceled = true;
0083 
0084     if( m_process )
0085         if( m_process->isRunning() ) {
0086             m_process->kill();
0087         }
0088 }
0089 
0090 
0091 void K3b::AudioNormalizeJob::slotStdLine( const QString& line )
0092 {
0093     // percent, subPercent, newTask (compute level and adjust)
0094 
0095     //  emit newSubTask( i18n("Normalizing track %1 of %2 (%3)",t,tt,m_files.at(t-1)) );
0096 
0097     emit debuggingOutput( "normalize", line );
0098 
0099     // wenn "% done" drin:
0100     //    wenn ein --% drin ist, so beginnt ein neuer track
0101     //    sonst prozent parsen "batch xxx" ist der fortschritt der action
0102     //                         also ev. den batch fortschritt * 1/2
0103 
0104     if( line.startsWith( "Applying adjustment" ) ) {
0105         if( m_currentAction == COMPUTING_LEVELS ) {
0106             // starting the adjustment with track 1
0107             m_currentTrack = 1;
0108             m_currentAction = ADJUSTING_LEVELS;
0109         }
0110     }
0111 
0112     else if( line.contains( "already normalized" ) ) {
0113         // no normalization necessary for the current track
0114         emit infoMessage( i18n("Track %1 is already normalized.",m_currentTrack), MessageInfo );
0115         m_currentTrack++;
0116     }
0117 
0118     else if( line.contains( "--% done") ) {
0119         if( m_currentAction == ADJUSTING_LEVELS ) {
0120             emit newTask( i18n("Adjusting volume level for track %1 of %2",m_currentTrack,m_files.count()) );
0121             qDebug() << "(K3b::AudioNormalizeJob) adjusting level for track "
0122                      << m_currentTrack
0123                      << " "
0124                      << m_files.at(m_currentTrack-1)
0125                      << Qt::endl;
0126         }
0127         else {
0128             emit newTask( i18n("Computing level for track %1 of %2",m_currentTrack,m_files.count()) );
0129             qDebug() << "(K3b::AudioNormalizeJob) computing level for track "
0130                      << m_currentTrack
0131                      << " "
0132                      << m_files.at(m_currentTrack-1)
0133                      << Qt::endl;
0134         }
0135 
0136         m_currentTrack++;
0137     }
0138 
0139     else if( int pos = line.indexOf( "% done" ) > 0 ) {
0140         // parse progress: "XXX% done" and "batch XXX% done"
0141         pos -= 3;
0142         bool ok;
0143         // TODO: do not use fixed values
0144         // track progress starts at position 19 in version 0.7.6
0145         int p = line.mid( 19, 3 ).toInt(&ok);
0146         if( ok )
0147             emit subPercent( p );
0148         else
0149             qDebug() << "(K3b::AudioNormalizeJob) subPercent parsing error at pos "
0150                      << 19 << " in line '" << line.mid( 19, 3 ) << "'" << Qt::endl;
0151 
0152         // batch progress starts at position 50 in version 0.7.6
0153         p = line.mid( 50, 3 ).toInt(&ok);
0154         if( ok && m_currentAction == COMPUTING_LEVELS )
0155             emit percent( (int)((double)p/2.0) );
0156         else if( ok && m_currentAction == ADJUSTING_LEVELS )
0157             emit percent( 50 + (int)((double)p/2.0) );
0158         else
0159             qDebug() << "(K3b::AudioNormalizeJob) percent parsing error at pos "
0160                      << 50 << " in line '" << line.mid( 50, 3 ) << "'" << Qt::endl;
0161 
0162     }
0163 }
0164 
0165 
0166 void K3b::AudioNormalizeJob::slotProcessExited( int exitCode, QProcess::ExitStatus exitStatus )
0167 {
0168     if( exitStatus == QProcess::NormalExit ) {
0169         switch( exitCode ) {
0170         case 0:
0171             emit infoMessage( i18n("Successfully normalized all tracks."), MessageSuccess );
0172             jobFinished(true);
0173             break;
0174         default:
0175             if( !m_canceled ) {
0176                 emit infoMessage( i18n("%1 returned an unknown error (code %2).",QString("normalize"), exitCode),
0177                                   K3b::Job::MessageError );
0178                 emit infoMessage( i18n("Please send me an email with the last output."), K3b::Job::MessageError );
0179                 emit infoMessage( i18n("Error while normalizing tracks."), MessageError );
0180             }
0181             else
0182                 emit canceled();
0183             jobFinished(false);
0184             break;
0185         }
0186     }
0187     else {
0188         emit infoMessage( i18n("%1 did not exit cleanly.",QString("Normalize")), K3b::Job::MessageError );
0189         jobFinished( false );
0190     }
0191 }
0192 
0193 #include "moc_k3baudionormalizejob.cpp"