File: k3bsoxencoder.cpp

package info (click to toggle)
k3b 2.0.3a-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 50,416 kB
  • ctags: 12,996
  • sloc: cpp: 100,125; sh: 67; xml: 10; makefile: 8
file content (345 lines) | stat: -rw-r--r-- 9,762 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 *
 * Copyright (C) 2003-2009 Sebastian Trueg <trueg@k3b.org>
 * Copyright (C) 2010 Michal Malek <michalm@jabster.pl>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2009 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bsoxencoder.h"
#include "k3bsoxencoderdefaults.h"

#include <config-k3b.h>

#include "k3bprocess.h"
#include "k3bcore.h"
#include "k3bexternalbinmanager.h"

#include <KConfig>
#include <KDebug>
#include <KLocale>

#include <QFile>
#include <QFileInfo>

#include <sys/types.h>


namespace {

// the sox external program
class SoxProgram : public K3b::ExternalProgram
{
public:
    SoxProgram()
        : K3b::ExternalProgram( "sox" ) {
    }

    bool scan( const QString& p ) {
        if( p.isEmpty() )
            return false;

        QString path = p;
        QFileInfo fi( path );
        if( fi.isDir() ) {
            path = buildProgramPath( path, "sox" );
        }

        if( !QFile::exists( path ) )
            return false;

        K3b::ExternalBin* bin = 0;

        // probe version
        KProcess vp;
        vp.setOutputChannelMode( KProcess::MergedChannels );

        vp << path << "--version";
        vp.start();
        if( vp.waitForFinished( -1 ) ) {
            QByteArray out = vp.readAll();
            int pos = out.indexOf( "sox: SoX Version" );
            if ( pos >= 0 ) {
                pos += 17;
            }
            else if ( ( pos = out.indexOf( "sox:      SoX v" ) ) >= 0 ) {
                pos += 15;
            }
            else if ( ( pos = out.indexOf( "sox: SoX v" ) ) >= 0 ) {
                pos += 10;
            }
            else if ( ( pos = out.indexOf( "sox: Version" ) ) >= 0 ) {
                pos += 13;
            }
            int endPos = out.indexOf( '\n', pos );
            if( pos > 0 && endPos > 0 ) {
                bin = new K3b::ExternalBin( this );
                bin->path = path;
                bin->version = out.mid( pos, endPos-pos );

                addBin( bin );

                return true;
            }
        }

        return false;
    }
};

} // namespace

class K3bSoxEncoder::Private
{
public:
    K3b::Process* process;
    QString fileName;
};


K3bSoxEncoder::K3bSoxEncoder( QObject* parent, const QVariantList& )
    : K3b::AudioEncoder( parent )
{
    if( k3bcore->externalBinManager()->program( "sox" ) == 0 )
        k3bcore->externalBinManager()->addProgram( new SoxProgram() );

    d = new Private();
    d->process = 0;
}


K3bSoxEncoder::~K3bSoxEncoder()
{
    delete d->process;
    delete d;
}


void K3bSoxEncoder::finishEncoderInternal()
{
    if( d->process && d->process->isRunning() ) {
        d->process->closeWriteChannel();

        // this is kind of evil...
        // but we need to be sure the process exited when this method returnes
        d->process->waitForFinished(-1);
    }
}


void K3bSoxEncoder::slotSoxFinished( int exitCode, QProcess::ExitStatus exitStatus )
{
    if( (exitStatus != QProcess::NormalExit) || (exitCode != 0) )
        kDebug() << "(K3bSoxEncoder) sox exited with error.";
}


bool K3bSoxEncoder::openFile( const QString& extension, const QString& filename, const K3b::Msf& length, const MetaData& metaData )
{
    d->fileName = filename;
    return initEncoderInternal( extension, length, metaData );
}


bool K3bSoxEncoder::isOpen() const
{
    return d->process && d->process->isRunning();
}


void K3bSoxEncoder::closeFile()
{
    finishEncoderInternal();
}


bool K3bSoxEncoder::initEncoderInternal( const QString& extension, const K3b::Msf& /*length*/, const MetaData& /*metaData*/ )
{
    const K3b::ExternalBin* soxBin = k3bcore->externalBinManager()->binObject( "sox" );
    if( soxBin ) {
        // we want to be thread-safe
        delete d->process;
        d->process = new K3b::Process();
        d->process->setSplitStdout(true);

        connect( d->process, SIGNAL(finished(int, QProcess::ExitStatus)),
                 this, SLOT(slotSoxFinished(int, QProcess::ExitStatus)) );
        connect( d->process, SIGNAL(stdoutLine(QString)),
                 this, SLOT(slotSoxOutputLine(QString)) );

        // input settings
        *d->process << soxBin->path
                    << "-t" << "raw"    // raw samples
                    << "-r" << "44100"  // samplerate
                    << "-s";            // signed linear
        if ( soxBin->version >= K3b::Version( 13, 0, 0 ) )
            *d->process << "-2";
        else
            *d->process << "-w";        // 16-bit words
        *d->process << "-c" << "2"      // stereo
                    << "-";             // read from stdin

        // output settings
        *d->process << "-t" << extension;

        KSharedConfig::Ptr c = KGlobal::config();
        KConfigGroup grp(c,"K3bSoxEncoderPlugin" );
        if( grp.readEntry( "manual settings", DEFAULT_MANUAL_SETTINGS ) ) {
            *d->process << "-r" << QString::number( grp.readEntry( "samplerate", DEFAULT_SAMPLE_RATE ) )
                        << "-c" << QString::number( grp.readEntry( "channels", DEFAULT_CHANNELS ) );

            int size = grp.readEntry( "data size", DEFAULT_DATA_SIZE );
            *d->process << ( size == 8 ? QString("-b") : ( size == 32 ? QString("-l") : QString("-w") ) );

            QString encoding = grp.readEntry( "data encoding", DEFAULT_DATA_ENCODING );
            if( encoding == "unsigned" )
                *d->process << "-u";
            else if( encoding == "u-law" )
                *d->process << "-U";
            else if( encoding == "A-law" )
                *d->process << "-A";
            else if( encoding == "ADPCM" )
                *d->process << "-a";
            else if( encoding == "IMA_ADPCM" )
                *d->process << "-i";
            else if( encoding == "GSM" )
                *d->process << "-g";
            else if( encoding == "Floating-point" )
                *d->process << "-f";
            else
                *d->process << "-s";
        }

        *d->process << d->fileName;

        kDebug() << "***** sox parameters:";
        QString s = d->process->joinedArgs();
        kDebug() << s << flush;

        return d->process->start( KProcess::MergedChannels );
    }
    else {
        kDebug() << "(K3bSoxEncoder) could not find sox bin.";
        return false;
    }
}


long K3bSoxEncoder::encodeInternal( const char* data, Q_ULONG len )
{
    if( d->process && d->process->isRunning() )
        return d->process->write( data, len );
    else
        return -1;
}


void K3bSoxEncoder::slotSoxOutputLine( const QString& line )
{
    kDebug() << "(sox) " << line;
}


QStringList K3bSoxEncoder::extensions() const
{
    static QStringList s_extensions;
    if( s_extensions.isEmpty() ) {
        s_extensions << "au"
                     << "8svx"
                     << "aiff"
                     << "avr"
                     << "cdr"
                     << "cvs"
                     << "dat"
                     << "gsm"
                     << "hcom"
                     << "maud"
                     << "sf"
                     << "sph"
                     << "smp"
                     << "txw"
                     << "vms"
                     << "voc"
                     << "wav"
                     << "wve"
                     << "raw";
    }

    if( k3bcore->externalBinManager()->foundBin( "sox" ) )
        return s_extensions;
    else
        return QStringList(); // no sox -> no encoding
}


QString K3bSoxEncoder::fileTypeComment( const QString& ext ) const
{
    if( ext == "au" )
        return i18n("Sun AU");
    else if( ext == "8svx" )
        return i18n("Amiga 8SVX");
    else if( ext == "aiff" )
        return i18n("AIFF");
    else if( ext == "avr" )
        return i18n("Audio Visual Research");
    else if( ext == "cdr" )
        return i18n("CD-R");
    else if( ext == "cvs" )
        return i18n("CVS");
    else if( ext == "dat" )
        return i18n("Text Data");
    else if( ext == "gsm" )
        return i18n("GSM Speech");
    else if( ext == "hcom" )
        return i18n("Macintosh HCOM");
    else if( ext == "maud" )
        return i18n("Maud (Amiga)");
    else if( ext == "sf" )
        return i18n("IRCAM");
    else if( ext == "sph" )
        return i18n("SPHERE");
    else if( ext == "smp" )
        return i18n("Turtle Beach SampleVision");
    else if( ext == "txw" )
        return i18n("Yamaha TX-16W");
    else if( ext == "vms" )
        return i18n("VMS");
    else if( ext == "voc" )
        return i18n("Sound Blaster VOC");
    else if( ext == "wav" )
        return i18n("Wave (Sox)");
    else if( ext == "wve" )
        return i18n("Psion 8-bit A-law");
    else if( ext == "raw" )
        return i18n("Raw");
    else
        return i18n("Error");
}


long long K3bSoxEncoder::fileSize( const QString&, const K3b::Msf& msf ) const
{
    // for now we make a rough assumption based on the settings
    KSharedConfig::Ptr c = KGlobal::config();
    KConfigGroup grp(c, "K3bSoxEncoderPlugin" );
    if( grp.readEntry( "manual settings", DEFAULT_MANUAL_SETTINGS ) ) {
        int sr =  grp.readEntry( "samplerate", DEFAULT_SAMPLE_RATE );
        int ch = grp.readEntry( "channels", DEFAULT_CHANNELS );
        int wsize = grp.readEntry( "data size", DEFAULT_DATA_SIZE );

        return msf.totalFrames()*sr*ch*wsize/75;
    }
    else {
        // fallback to raw
        return msf.audioBytes();
    }
}

#include "k3bsoxencoder.moc"