~ubuntu-branches/ubuntu/dapper/kdemultimedia/dapper

« back to all changes in this revision

Viewing changes to kioslave/audiocd/plugins/lame/encoderlame.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-01-23 14:26:07 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060123142607-4cj5fiu4gef3wypf
Tags: 4:3.5.1-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <kprocess.h>
27
27
#include <kdebug.h>
28
28
 
29
 
#include <kglobal.h>  
 
29
#include <kglobal.h>
30
30
#include <klocale.h>
31
31
#include <kapplication.h>
32
32
#include <qfileinfo.h>
33
33
#include <ktempfile.h>
34
34
#include <kstandarddirs.h>
 
35
#include "collectingprocess.h"
35
36
 
36
37
extern "C"
37
38
{
47
48
public:
48
49
        int bitrate;
49
50
        bool waitingForWrite;
 
51
        bool processHasExited;
 
52
        QString lastErrorMessage;
 
53
        QStringList genreList;
50
54
        uint lastSize;
51
55
        KProcess *currentEncodeProcess;
52
56
        KTempFile *tempFile;
55
59
EncoderLame::EncoderLame(KIO::SlaveBase *slave) : QObject(), AudioCDEncoder(slave) {
56
60
        d = new Private();
57
61
        d->waitingForWrite = false;
 
62
        d->processHasExited = false;
58
63
        d->lastSize = 0;
59
64
        loadSettings();
60
65
}
64
69
}
65
70
 
66
71
QWidget* EncoderLame::getConfigureWidget(KConfigSkeleton** manager) const {
67
 
  (*manager) = Settings::self();
68
 
  KGlobal::locale()->insertCatalogue("audiocd_encoder_lame");
69
 
  EncoderLameConfig *config = new EncoderLameConfig();
70
 
  config->cbr_settings->hide();
71
 
  return config;
 
72
        (*manager) = Settings::self();
 
73
        KGlobal::locale()->insertCatalogue("audiocd_encoder_lame");
 
74
        EncoderLameConfig *config = new EncoderLameConfig();
 
75
        config->cbr_settings->hide();
 
76
        return config;
72
77
}
73
78
 
74
79
bool EncoderLame::init(){
75
80
        // Determine if lame is installed on the system or not.
76
 
        KProcess proc;
77
 
        proc << "which" << "lame";
78
 
        proc.start(KProcess::Block);
79
 
        if(proc.exitStatus() == 0)
80
 
                return false;
 
81
        if ( KStandardDirs::findExe( "lame" ).isEmpty() )
 
82
                return false;
 
83
 
 
84
        // Ask lame for the list of genres it knows; otherwise it barfs when doing
 
85
        // e.g. lame --tg 'Vocal Jazz'
 
86
    CollectingProcess proc;
 
87
        proc << "lame" << "--genre-list";
 
88
        proc.start(KProcess::Block, KProcess::Stdout);
 
89
 
 
90
        if(proc.exitStatus() != 0)
 
91
                return false;
 
92
 
 
93
        const QByteArray data = proc.collectedStdout();
 
94
        QString str;
 
95
        if ( !data.isEmpty() )
 
96
                str = QString::fromLocal8Bit( data, data.size() );
 
97
 
 
98
        d->genreList = QStringList::split( '\n', str );
 
99
        // Remove the numbers in front of every genre
 
100
        for( QStringList::Iterator it = d->genreList.begin(); it != d->genreList.end(); ++it ) {
 
101
                QString& genre = *it;
 
102
                uint i = 0;
 
103
                while ( i < genre.length() && ( genre[i].isSpace() || genre[i].isDigit() ) )
 
104
                        ++i;
 
105
                genre = genre.mid( i );
 
106
 
 
107
        }
 
108
        //kdDebug(7117) << "Available genres:" << d->genreList << endl;
 
109
 
81
110
        return true;
82
111
}
83
112
 
84
113
void EncoderLame::loadSettings(){
85
114
        // Generate the command line arguments for the current settings
86
115
        args.clear();
87
 
 
 
116
 
88
117
        Settings *settings = Settings::self();
89
118
 
90
119
        int quality = settings->quality();
92
121
        if (quality > 9) quality = 9;
93
122
 
94
123
        int method = settings->bitrate_constant() ? 0 : 1 ;
95
 
  
 
124
 
96
125
        if (method == 0) {
97
126
                // Constant Bitrate Encoding
98
127
                args.append("-b");
181
210
 
182
211
long EncoderLame::readInit(long /*size*/){
183
212
        // Create KProcess
184
 
        d->currentEncodeProcess = new KProcess(0,"encodeprocess");
 
213
        d->currentEncodeProcess = new KProcess(0);
185
214
        QString prefix = locateLocal("tmp", "");
186
215
        d->tempFile = new KTempFile(prefix, ".mp3");
187
216
        d->tempFile->setAutoDelete(true);
 
217
        d->lastErrorMessage = QString::null;
 
218
        d->processHasExited = false;
188
219
 
189
220
        // -x bitswap
190
221
        // -r raw/pcm
197
228
 
198
229
        // Read in stdin, output to the temp file
199
230
        *d->currentEncodeProcess << "-" << d->tempFile->name().latin1();
200
 
        
201
 
        
 
231
 
 
232
        //kdDebug(7117) << d->currentEncodeProcess->args() << endl;
 
233
 
 
234
 
202
235
        connect(d->currentEncodeProcess, SIGNAL(receivedStdout(KProcess *, char *, int)),
203
 
                         this, SLOT(receivedStdout(KProcess *, char *, int)));
 
236
                         this, SLOT(receivedStdout(KProcess *, char *, int)));
204
237
        connect(d->currentEncodeProcess, SIGNAL(receivedStderr(KProcess *, char *, int)),
205
 
                         this, SLOT(receivedStderr(KProcess *, char *, int)));
 
238
                         this, SLOT(receivedStderr(KProcess *, char *, int)));
206
239
        connect(d->currentEncodeProcess, SIGNAL(wroteStdin(KProcess *)),
207
 
                         this, SLOT(wroteStdin(KProcess *)));
 
240
                         this, SLOT(wroteStdin(KProcess *)));
208
241
 
209
242
        connect(d->currentEncodeProcess, SIGNAL(processExited(KProcess *)),
210
 
                         this, SLOT(processExited(KProcess *)));
 
243
                         this, SLOT(processExited(KProcess *)));
211
244
 
212
245
        // Launch!
213
246
        d->currentEncodeProcess->start(KProcess::NotifyOnExit, KShellProcess::All);
216
249
 
217
250
void EncoderLame::processExited ( KProcess *process ){
218
251
        kdDebug(7117) << "Lame Encoding process exited with: " << process->exitStatus() << endl;
 
252
        d->processHasExited = true;
219
253
}
220
254
 
221
255
void EncoderLame::receivedStderr( KProcess * /*process*/, char *buffer, int /*buflen*/ ){
222
256
        kdDebug(7117) << "Lame stderr: " << buffer << endl;
 
257
        if ( !d->lastErrorMessage.isEmpty() )
 
258
                d->lastErrorMessage += '\t';
 
259
        d->lastErrorMessage += QString::fromLocal8Bit( buffer );
223
260
}
224
261
 
225
262
void EncoderLame::receivedStdout( KProcess * /*process*/, char *buffer, int /*length*/ ){
231
268
}
232
269
 
233
270
long EncoderLame::read(int16_t *buf, int frames){
234
 
  if(!d->currentEncodeProcess)
 
271
        if(!d->currentEncodeProcess)
235
272
                return 0;
 
273
        if (d->processHasExited)
 
274
                return -1;
236
275
 
237
276
        // Pipe the raw data to lame
238
277
        char * cbuf = reinterpret_cast<char *>(buf);
281
320
        delete d->currentEncodeProcess;
282
321
        delete d->tempFile;
283
322
        d->lastSize = 0;
284
 
        
 
323
 
285
324
        return 0;
286
325
}
287
326
 
305
344
        trackInfo.append("--tn");
306
345
        trackInfo.append(QString("%1").arg(track+1));
307
346
 
308
 
        trackInfo.append("--tg");
309
 
        trackInfo.append(info.get("genre").toString());
310
 
}
311
 
 
 
347
        const QString genre = info.get( "genre" ).toString();
 
348
        if ( d->genreList.find( genre ) != d->genreList.end() )
 
349
        {
 
350
                trackInfo.append("--tg");
 
351
                trackInfo.append(genre);
 
352
        }
 
353
}
 
354
 
 
355
 
 
356
QString EncoderLame::lastErrorMessage() const
 
357
{
 
358
        return d->lastErrorMessage;
 
359
}
312
360
 
313
361
#include "encoderlame.moc"