summaryrefslogtreecommitdiffstats
path: root/tdeioslave/audiocd/plugins/lame/encoderlame.cpp
blob: 76a19122b7871d73f230c302c4c659924cbab343 (plain)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
  Copyright (C) 2005 Benjamin Meyer <ben at meyerhome dot net>

  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.

  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.
*/

#include <config.h>

#include "encoderlame.h"
#include "encoderlameconfig.h"
#include "audiocd_lame_encoder.h"

#include <kdebug.h>
#include <tqgroupbox.h>
#include <kprocess.h>
#include <kdebug.h>

#include <tdeglobal.h>
#include <tdelocale.h>
#include <tdeapplication.h>
#include <tqfileinfo.h>
#include <tdetempfile.h>
#include <kstandarddirs.h>
#include "collectingprocess.h"

extern "C"
{
	TDE_EXPORT void create_audiocd_encoders(TDEIO::SlaveBase *slave, TQPtrList<AudioCDEncoder> &encoders) {
		encoders.append(new EncoderLame(slave));
	}
}

static int bitrates[] = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 };

class EncoderLame::Private
{
public:
	int bitrate;
	bool waitingForWrite;
	bool processHasExited;
	TQString lastErrorMessage;
	TQStringList genreList;
	uint lastSize;
	TDEProcess *currentEncodeProcess;
	KTempFile *tempFile;
};

EncoderLame::EncoderLame(TDEIO::SlaveBase *slave) : TQObject(), AudioCDEncoder(slave) {
	d = new Private();
	d->waitingForWrite = false;
	d->processHasExited = false;
	d->lastSize = 0;
	loadSettings();
}

EncoderLame::~EncoderLame(){
	delete d;
}

TQWidget* EncoderLame::getConfigureWidget(TDEConfigSkeleton** manager) const {
	(*manager) = Settings::self();
	TDEGlobal::locale()->insertCatalogue("audiocd_encoder_lame");
	EncoderLameConfig *config = new EncoderLameConfig();
	config->cbr_settings->hide();
	return config;
}

bool EncoderLame::init(){
	// Determine if lame is installed on the system or not.
	if ( TDEStandardDirs::findExe( "lame" ).isEmpty() )
		return false;

	// Ask lame for the list of genres it knows; otherwise it barfs when doing
	// e.g. lame --tg 'Vocal Jazz'
    CollectingProcess proc;
	proc << "lame" << "--genre-list";
	proc.start(TDEProcess::Block, TDEProcess::Stdout);

	if(proc.exitStatus() != 0)
		return false;

	const TQByteArray data = proc.collectedStdout();
	TQString str;
	if ( !data.isEmpty() )
		str = TQString::fromLocal8Bit( data, data.size() );

	d->genreList = TQStringList::split( '\n', str );
	// Remove the numbers in front of every genre
	for( TQStringList::Iterator it = d->genreList.begin(); it != d->genreList.end(); ++it ) {
		TQString& genre = *it;
		uint i = 0;
		while ( i < genre.length() && ( genre[i].isSpace() || genre[i].isDigit() ) )
			++i;
		genre = genre.mid( i );

	}
	//kdDebug(7117) << "Available genres:" << d->genreList << endl;

	return true;
}

void EncoderLame::loadSettings(){
	// Generate the command line arguments for the current settings
	args.clear();

	Settings *settings = Settings::self();

	int quality = settings->quality();
	if (quality < 0 ) quality = quality *-1;
	if (quality > 9) quality = 9;

	int method = settings->bitrate_constant() ? 0 : 1 ;

	if (method == 0) {
		// Constant Bitrate Encoding
		args.append("-b");
		args.append(TQString("%1").arg(bitrates[settings->cbr_bitrate()]));
		d->bitrate = bitrates[settings->cbr_bitrate()];
		args.append("-q");
		args.append(TQString("%1").arg(quality));
	}
	else {
		// Variable Bitrate Encoding
		if (settings->vbr_average_br()) {
			args.append("--abr");
			args.append(TQString("%1").arg(bitrates[settings->vbr_mean_brate()]));
			d->bitrate = bitrates[settings->vbr_mean_brate()];
			if (settings->vbr_min_br()){
				args.append("-b");
				args.append(TQString("%1").arg(bitrates[settings->vbr_min_brate()]));
			}
			if (settings->vbr_min_hard())
				args.append("-F");
			if (settings->vbr_max_br()){
				args.append("-B");
				args.append(TQString("%1").arg(bitrates[settings->vbr_max_brate()]));
			}
		} else {
			d->bitrate = 128;
			args.append("-V");
			args.append(TQString("%1").arg(quality));
		}
		if ( !settings->vbr_xing_tag() )
			args.append("-t");
	}

	args.append("-m");
	switch ( settings->stereo() ) {
		case 0:
			args.append("s");
			break;
		case 1:
			args.append("j");
			break;
		case 2:
			args.append("d");
			break;
		case 3:
			args.append("m");
			break;
		default:
			args.append("s");
			break;
	}

	if(settings->copyright())
		args.append("-c");
	if(!settings->original())
		args.append("-o");
	if(settings->iso())
		args.append("--strictly-enforce-ISO");
	if(settings->crc())
		args.append("-p");

	if ( settings->enable_lowpass() ) {
		args.append("--lowpass");
		args.append(TQString("%1").arg(settings->lowfilterfreq()));

		if (settings->set_lpf_width()){
			args.append("--lowpass-width");
			args.append(TQString("%1").arg(settings->lowfilterwidth()));
		}
	}

	if ( settings->enable_highpass()) {
		args.append("--hipass");
		args.append(TQString("%1").arg(settings->highfilterfreq()));

		if (settings->set_hpf_width()){
			args.append("--hipass-width");
			args.append(TQString("%1").arg(settings->highfilterwidth()));
		}
	}
}

unsigned long EncoderLame::size(long time_secs) const {
	return (time_secs * d->bitrate * 1000)/8;
}

long EncoderLame::readInit(long /*size*/){
	// Create TDEProcess
	d->currentEncodeProcess	= new TDEProcess(0);
	TQString prefix = locateLocal("tmp", "");
	d->tempFile = new KTempFile(prefix, ".mp3");
	d->tempFile->setAutoDelete(true);
	d->lastErrorMessage = TQString();
	d->processHasExited = false;

	// -x bitswap
	// -r raw/pcm
	// -s 44.1 (because it is raw you have to specify this)
	*(d->currentEncodeProcess)      << "lame" << "--verbose" << "-r" << "-s" << "44.1";

	*(d->currentEncodeProcess) << args;
	if(Settings::self()->id3_tag())
		*d->currentEncodeProcess << trackInfo;

	// Read in stdin, output to the temp file
	*d->currentEncodeProcess << "-" << d->tempFile->name().latin1();

	//kdDebug(7117) << d->currentEncodeProcess->args() << endl;


	connect(d->currentEncodeProcess, TQ_SIGNAL(receivedStdout(TDEProcess *, char *, int)),
	                 this, TQ_SLOT(receivedStdout(TDEProcess *, char *, int)));
	connect(d->currentEncodeProcess, TQ_SIGNAL(receivedStderr(TDEProcess *, char *, int)),
	                 this, TQ_SLOT(receivedStderr(TDEProcess *, char *, int)));
	connect(d->currentEncodeProcess, TQ_SIGNAL(wroteStdin(TDEProcess *)),
	                 this, TQ_SLOT(wroteStdin(TDEProcess *)));

	connect(d->currentEncodeProcess, TQ_SIGNAL(processExited(TDEProcess *)),
	                 this, TQ_SLOT(processExited(TDEProcess *)));

	// Launch!
	d->currentEncodeProcess->start(TDEProcess::NotifyOnExit, KShellProcess::All);
	return 0;
}

void EncoderLame::processExited ( TDEProcess *process ){
	kdDebug(7117) << "Lame Encoding process exited with: " << process->exitStatus() << endl;
	d->processHasExited = true;
}

void EncoderLame::receivedStderr( TDEProcess * /*process*/, char *buffer, int /*buflen*/ ){
	kdDebug(7117) << "Lame stderr: " << buffer << endl;
	if ( !d->lastErrorMessage.isEmpty() )
		d->lastErrorMessage += '\t';
	d->lastErrorMessage += TQString::fromLocal8Bit( buffer );
}

void EncoderLame::receivedStdout( TDEProcess * /*process*/, char *buffer, int /*length*/ ){
	kdDebug(7117) << "Lame stdout: " << buffer << endl;
}

void EncoderLame::wroteStdin( TDEProcess * /*procces*/ ){
	d->waitingForWrite = false;
}

long EncoderLame::read(int16_t *buf, int frames){
	if(!d->currentEncodeProcess)
		return 0;
        if (d->processHasExited)
		return -1;

	// Pipe the raw data to lame
	char * cbuf = reinterpret_cast<char *>(buf);
	d->currentEncodeProcess->writeStdin( cbuf, frames*4);

	// We can't return until the buffer has been written
	d->waitingForWrite = true;
	while(d->waitingForWrite && d->currentEncodeProcess->isRunning()){
		kapp->processEvents();
		usleep(1);
	}

	// Determine the file size increase
	TQFileInfo file(d->tempFile->name());
	uint change = file.size() - d->lastSize;
	d->lastSize = file.size();
	return change;
}

long EncoderLame::readCleanup(){
	if(!d->currentEncodeProcess)
		return 0;

	// Let lame tag the first frame of the mp3
	d->currentEncodeProcess->closeStdin();
	while( d->currentEncodeProcess->isRunning()){
		kapp->processEvents();
		usleep(1);
	}

	// Now copy the file out of the temp into tdeio
	TQFile file( d->tempFile->name() );
	if ( file.open( IO_ReadOnly ) ) {
		TQByteArray output;
		char data[1024];
		while ( !file.atEnd() ) {
			uint read = file.readBlock(data, 1024);
			output.setRawData(data, read);
			ioslave->data(output);
			output.resetRawData(data, read);
		}
		file.close();
	}

	// cleanup the process and temp
	delete d->currentEncodeProcess;
	delete d->tempFile;
	d->lastSize = 0;

	return 0;
}

void EncoderLame::fillSongInfo( KCDDB::CDInfo info, int track, const TQString &comment ){
	trackInfo.clear();
	trackInfo.append("--tt");
	trackInfo.append(info.trackInfoList[track].get("title").toString());

	trackInfo.append("--ta");
	trackInfo.append(info.get("artist").toString());

	trackInfo.append("--tl");
	trackInfo.append(info.get("title").toString());

	trackInfo.append("--ty");
	trackInfo.append(TQString("%1").arg(info.get("year").toString()));

	trackInfo.append("--tc");
	trackInfo.append(comment);

	trackInfo.append("--tn");
	trackInfo.append(TQString("%1").arg(track+1));

	const TQString genre = info.get( "genre" ).toString();
	if ( d->genreList.find( genre ) != d->genreList.end() )
	{
		trackInfo.append("--tg");
		trackInfo.append(genre);
	}
}


TQString EncoderLame::lastErrorMessage() const
{
	return d->lastErrorMessage;
}

#include "encoderlame.moc"