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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
/*
*
* $Id: k3blameencoder.cpp 653779 2007-04-14 07:58:51Z trueg $
* Copyright (C) 2003 Sebastian Trueg <[email protected]>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <[email protected]>
*
* 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 <config.h>
#include "k3blameencoder.h"
#include <k3bcore.h>
#include <k3bpluginfactory.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kinstance.h>
#include <knuminput.h>
#include <kcombobox.h>
#include <kdialogbase.h>
#include <tqlayout.h>
#include <tqcstring.h>
#include <tqradiobutton.h>
#include <tqcheckbox.h>
#include <tqspinbox.h>
#include <tqgroupbox.h>
#include <tqbuttongroup.h>
#include <tqtextcodec.h>
#include <tqfile.h>
#include <tqslider.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <stdio.h>
#include <lame/lame.h>
K_EXPORT_COMPONENT_FACTORY( libk3blameencoder, K3bPluginFactory<K3bLameEncoder>( "libk3blameencoder" ) )
static const int s_lame_bitrates[] = {
32,
40,
48,
56,
64,
80,
96,
112,
128,
160,
192,
224,
256,
320,
0 // just used for the loops below
};
static const int s_lame_presets[] = {
56, // ABR for Voice, Radio, Mono, etc.
90, //
V6, // ~115 kbps
V5, // ~130 kbps | Portable - small size
V4, // ~160 kbps
V3, // ~175 kbps
V2, // ~190 kbps | HiFi - for home or quite listening
V1, // ~210 kbps |
V0, // ~230 kbps
320 // ABR 320 neary lossless for archiving (not recommended, use flac instead)
};
static const int s_lame_preset_approx_bitrates[] = {
56,
90,
115,
130,
160,
175,
190,
210,
230,
320
};
static const char* s_lame_preset_strings[] = {
I18N_NOOP("Low quality (56 kbps)"),
I18N_NOOP("Low quality (90 kbps)"),
I18N_NOOP("Portable (average 115 kbps)"),
I18N_NOOP("Portable (average 130 kbps)"),
I18N_NOOP("Portable (average 160 kbps)"),
I18N_NOOP("HiFi (average 175 kbps)"),
I18N_NOOP("HiFi (average 190 kbps)"),
I18N_NOOP("HiFi (average 210 kbps)"),
I18N_NOOP("HiFi (average 230 kbps)"),
I18N_NOOP("Archiving (320 kbps)"),
};
static const char* s_lame_mode_strings[] = {
I18N_NOOP("Stereo"),
I18N_NOOP("Joint Stereo"),
I18N_NOOP("Mono")
};
class K3bLameEncoder::Private
{
public:
Private()
: flags(0),
fid(0) {
}
lame_global_flags* flags;
char buffer[8000];
TQString filename;
FILE* fid;
};
K3bLameEncoder::K3bLameEncoder( TQObject* parent, const char* name )
: K3bAudioEncoder( parent, name )
{
d = new Private();
}
K3bLameEncoder::~K3bLameEncoder()
{
closeFile();
delete d;
}
bool K3bLameEncoder::openFile( const TQString& extension, const TQString& filename, const K3b::Msf& length )
{
closeFile();
d->filename = filename;
d->fid = ::fopen( TQFile::encodeName( filename ), "w+" );
if( d->fid )
return initEncoder( extension, length );
else
return false;
}
bool K3bLameEncoder::isOpen() const
{
return ( d->fid != 0 );
}
void K3bLameEncoder::closeFile()
{
if( isOpen() ) {
finishEncoder();
::fclose( d->fid );
d->fid = 0;
d->filename.truncate(0);
}
}
const TQString& K3bLameEncoder::filename() const
{
return d->filename;
}
bool K3bLameEncoder::initEncoderInternal( const TQString&, const K3b::Msf& length )
{
TDEConfig* c = k3bcore->config();
c->setGroup( "K3bLameEncoderPlugin" );
d->flags = lame_init();
if( d->flags == 0 ) {
kdDebug() << "(K3bLameEncoder) lame_init failed." << endl;
return false;
}
//
// set the format of the input data
//
lame_set_num_samples( d->flags, length.lba()*588 );
lame_set_in_samplerate( d->flags, 44100 );
lame_set_num_channels( d->flags, 2 );
//
// Lame by default determines the samplerate based on the bitrate
// since we have no option for the user to influence this yet
// we just keep to the good old 44.1 khz
//
lame_set_out_samplerate( d->flags, 44100 );
//
// Choose the quality level
//
if( c->readBoolEntry( "Manual Bitrate Settings", false ) ) {
//
// Mode
//
TQString mode = c->readEntry( "Mode", "stereo" );
if( mode == "stereo" )
lame_set_mode( d->flags, STEREO );
else if( mode == "joint" )
lame_set_mode( d->flags, JOINT_STEREO );
else // mono
lame_set_mode( d->flags, MONO );
//
// Variable Bitrate
//
if( c->readBoolEntry( "VBR", false ) ) {
// we use the default algorithm here
lame_set_VBR( d->flags, vbr_default );
if( c->readBoolEntry( "Use Maximum Bitrate", false ) ) {
lame_set_VBR_max_bitrate_kbps( d->flags, c->readNumEntry( "Maximum Bitrate", 224 ) );
}
if( c->readBoolEntry( "Use Minimum Bitrate", false ) ) {
lame_set_VBR_min_bitrate_kbps( d->flags, c->readNumEntry( "Minimum Bitrate", 32 ) );
// TODO: lame_set_hard_min
}
if( c->readBoolEntry( "Use Average Bitrate", true ) ) {
lame_set_VBR( d->flags, vbr_abr );
lame_set_VBR_mean_bitrate_kbps( d->flags, c->readNumEntry( "Average Bitrate", 128 ) );
}
}
//
// Constant Bitrate
//
else {
lame_set_VBR( d->flags, vbr_off );
lame_set_brate( d->flags, c->readNumEntry( "Constant Bitrate", 128 ) );
}
}
else {
//
// In lame 0 is the highest quality. Since that is just confusing for the user
// if we call the setting "Quality" we simply invert the value.
//
int q = c->readNumEntry( "Quality Level", 5 );
if( q < 0 ) q = 0;
if( q > 9 ) q = 9;
kdDebug() << "(K3bLameEncoder) setting preset encoding value to " << q << endl;
if ( q < 2 || q > 8 ) {
lame_set_VBR( d->flags, vbr_abr );
}
else {
lame_set_VBR( d->flags, vbr_default );
}
lame_set_preset( d->flags, s_lame_presets[q] );
if( q < 2 )
lame_set_mode( d->flags, MONO );
}
//
// file options
//
lame_set_copyright( d->flags, c->readBoolEntry( "Copyright", false ) );
lame_set_original( d->flags, c->readBoolEntry( "Original", true ) );
lame_set_strict_ISO( d->flags, c->readBoolEntry( "ISO compliance", false ) );
lame_set_error_protection( d->flags, c->readBoolEntry( "Error Protection", false ) );
//
// Used Algorithm
//
// default to 2 which is the same as the -h lame option
// THIS HAS NO INFLUENCE ON THE SIZE OF THE FILE!
//
//
// In lame 0 is the highest quality. Since that is just confusing for the user
// if we call the setting "Quality" we simply invert the value.
//
int q = c->readNumEntry( "Encoder Quality", 7 );
if( q < 0 ) q = 0;
if( q > 9 ) q = 9;
lame_set_quality( d->flags, 9-q );
//
// ID3 settings
//
// for now we default to both v1 and v2 tags
id3tag_add_v2( d->flags );
id3tag_pad_v2( d->flags );
return( lame_init_params( d->flags ) != -1 );
}
long K3bLameEncoder::encodeInternal( const char* data, TQ_ULONG len )
{
// FIXME: we may have to swap data here
int size = lame_encode_buffer_interleaved( d->flags,
(short int*)data,
len/4,
(unsigned char*)d->buffer,
8000 );
if( size < 0 ) {
kdDebug() << "(K3bLameEncoder) lame_encode_buffer_interleaved failed." << endl;
return -1;
}
return ::fwrite( d->buffer, 1, size, d->fid );
}
void K3bLameEncoder::finishEncoderInternal()
{
int size = lame_encode_flush( d->flags,
(unsigned char*)d->buffer,
8000 );
if( size > 0 )
::fwrite( d->buffer, 1, size, d->fid );
lame_mp3_tags_fid( d->flags, d->fid );
lame_close( d->flags );
d->flags = 0;
}
void K3bLameEncoder::setMetaDataInternal( K3bAudioEncoder::MetaDataField f, const TQString& value )
{
// let's not use UTF-8 here since I don't know how to tell lame...
// FIXME: when we use the codec we only get garbage. Why?
TQTextCodec* codec = 0;//TQTextCodec::codecForName( "ISO8859-1" );
// if( !codec )
// kdDebug() << "(K3bLameEncoder) could not find codec ISO8859-1." << endl;
switch( f ) {
case META_TRACK_TITLE:
id3tag_set_title( d->flags, codec ? codec->fromUnicode(value).data() : value.latin1() );
break;
case META_TRACK_ARTIST:
id3tag_set_artist( d->flags, codec ? codec->fromUnicode(value).data() : value.latin1() );
break;
case META_ALBUM_TITLE:
id3tag_set_album( d->flags, codec ? codec->fromUnicode(value).data() : value.latin1() );
break;
case META_ALBUM_COMMENT:
id3tag_set_comment( d->flags, codec ? codec->fromUnicode(value).data() : value.latin1() );
break;
case META_YEAR:
id3tag_set_year( d->flags, codec ? codec->fromUnicode(value).data() : value.latin1() );
break;
case META_TRACK_NUMBER:
id3tag_set_track( d->flags, codec ? codec->fromUnicode(value).data() : value.latin1() );
break;
case META_GENRE:
if( id3tag_set_genre( d->flags, codec ? codec->fromUnicode(value).data() : value.latin1() ) )
kdDebug() << "(K3bLameEncoder) unable to set genre." << endl;
break;
default:
return;
}
if( lame_init_params( d->flags ) < 0 )
kdDebug() << "(K3bLameEncoder) lame_init_params failed." << endl;
}
K3bLameEncoderSettingsWidget::K3bLameEncoderSettingsWidget( TQWidget* parent, const char* name )
: K3bPluginConfigWidget( parent, name )
{
m_w = new base_K3bLameEncoderSettingsWidget( this );
m_w->m_sliderQuality->setRange( 0, 9 );
m_w->m_spinEncoderQuality->setRange( 0, 9, true );
m_manualSettingsDlg = new KDialogBase( this, 0, true,
i18n("(Lame) Manual Quality Settings") );
m_brW = new base_K3bManualBitrateSettingsWidget( m_manualSettingsDlg );
m_manualSettingsDlg->setMainWidget( m_brW );
for( int i = 0; s_lame_bitrates[i]; ++i )
m_brW->m_comboMaximumBitrate->insertItem( i18n("%1 kbps" ).arg(s_lame_bitrates[i]) );
for( int i = 0; s_lame_bitrates[i]; ++i )
m_brW->m_comboMinimumBitrate->insertItem( i18n("%1 kbps" ).arg(s_lame_bitrates[i]) );
for( int i = 0; s_lame_bitrates[i]; ++i )
m_brW->m_comboConstantBitrate->insertItem( i18n("%1 kbps" ).arg(s_lame_bitrates[i]) );
TQHBoxLayout* lay = new TQHBoxLayout( this );
lay->setMargin( 0 );
lay->addWidget( m_w );
// TODO: add whatsthis help for the quality level.
// TQString qualityLevelWhatsThis = i18n("<p>");
connect( m_w->m_buttonManualSettings, TQ_SIGNAL(clicked()),
this, TQ_SLOT(slotShowManualSettings()) );
connect( m_w->m_sliderQuality, TQ_SIGNAL(valueChanged(int)),
this, TQ_SLOT(slotQualityLevelChanged(int)) );
updateManualSettingsLabel();
slotQualityLevelChanged( 5 );
}
K3bLameEncoderSettingsWidget::~K3bLameEncoderSettingsWidget()
{
}
void K3bLameEncoderSettingsWidget::slotShowManualSettings()
{
// save current settings for proper cancellation
bool constant = m_brW->m_radioConstantBitrate->isChecked();
int constBitrate = m_brW->m_comboConstantBitrate->currentItem();
int max = m_brW->m_comboMaximumBitrate->currentItem();
int min = m_brW->m_comboMinimumBitrate->currentItem();
int av = m_brW->m_spinAverageBitrate->value();
int mode = m_brW->m_comboMode->currentItem();
if( m_manualSettingsDlg->exec() == TQDialog::Rejected ) {
m_brW->m_radioConstantBitrate->setChecked( constant );
m_brW->m_comboConstantBitrate->setCurrentItem( constBitrate );
m_brW->m_comboMaximumBitrate->setCurrentItem( max );
m_brW->m_comboMinimumBitrate->setCurrentItem( min );
m_brW->m_spinAverageBitrate->setValue( av );
m_brW->m_comboMode->setCurrentItem( mode );
}
else
updateManualSettingsLabel();
}
void K3bLameEncoderSettingsWidget::updateManualSettingsLabel()
{
if( m_brW->m_radioConstantBitrate->isChecked() )
m_w->m_labelManualSettings->setText( i18n("Constant Bitrate: %1 kbps (%2)")
.arg(s_lame_bitrates[m_brW->m_comboConstantBitrate->currentItem()])
.arg(i18n(s_lame_mode_strings[m_brW->m_comboMode->currentItem()])) );
else
m_w->m_labelManualSettings->setText( i18n("Variable Bitrate (%1)")
.arg(i18n(s_lame_mode_strings[m_brW->m_comboMode->currentItem()])) );
}
void K3bLameEncoderSettingsWidget::slotQualityLevelChanged( int val )
{
m_w->m_labelQualityLevel->setText( i18n(s_lame_preset_strings[val]) );
}
void K3bLameEncoderSettingsWidget::loadConfig()
{
TDEConfig* c = k3bcore->config();
c->setGroup( "K3bLameEncoderPlugin" );
TQString mode = c->readEntry( "Mode", "stereo" );
if( mode == "stereo" )
m_brW->m_comboMode->setCurrentItem( 0 );
else if( mode == "joint" )
m_brW->m_comboMode->setCurrentItem( 1 );
else // mono
m_brW->m_comboMode->setCurrentItem( 2 );
bool manual = c->readBoolEntry( "Manual Bitrate Settings", false );
if( manual )
m_w->m_radioManual->setChecked(true);
else
m_w->m_radioQualityLevel->setChecked(true);
if( c->readBoolEntry( "VBR", false ) )
m_brW->m_radioVariableBitrate->setChecked( true );
else
m_brW->m_radioConstantBitrate->setChecked( true );
m_brW->m_comboConstantBitrate->setCurrentItem( i18n("%1 kbps").arg(c->readNumEntry( "Constant Bitrate", 128 )) );
m_brW->m_comboMaximumBitrate->setCurrentItem( i18n("%1 kbps").arg(c->readNumEntry( "Maximum Bitrate", 224 )) );
m_brW->m_comboMinimumBitrate->setCurrentItem( i18n("%1 kbps").arg(c->readNumEntry( "Minimum Bitrate", 32 )) );
m_brW->m_spinAverageBitrate->setValue( c->readNumEntry( "Average Bitrate", 128) );
m_brW->m_checkBitrateMaximum->setChecked( c->readBoolEntry( "Use Maximum Bitrate", false ) );
m_brW->m_checkBitrateMinimum->setChecked( c->readBoolEntry( "Use Minimum Bitrate", false ) );
m_brW->m_checkBitrateAverage->setChecked( c->readBoolEntry( "Use Average Bitrate", true ) );
m_w->m_sliderQuality->setValue( c->readNumEntry( "Quality Level", 5 ) );
m_w->m_checkCopyright->setChecked( c->readBoolEntry( "Copyright", false ) );
m_w->m_checkOriginal->setChecked( c->readBoolEntry( "Original", true ) );
m_w->m_checkISO->setChecked( c->readBoolEntry( "ISO compliance", false ) );
m_w->m_checkError->setChecked( c->readBoolEntry( "Error Protection", false ) );
// default to 2 which is the same as the -h lame option
m_w->m_spinEncoderQuality->setValue( c->readNumEntry( "Encoder Quality", 7 ) );
updateManualSettingsLabel();
}
void K3bLameEncoderSettingsWidget::saveConfig()
{
TDEConfig* c = k3bcore->config();
c->setGroup( "K3bLameEncoderPlugin" );
TQString mode;
switch( m_brW->m_comboMode->currentItem() ) {
case 0:
mode = "stereo";
break;
case 1:
mode = "joint";
break;
case 2:
mode = "mono";
break;
}
c->writeEntry( "Mode", mode );
c->writeEntry( "Manual Bitrate Settings", m_w->m_radioManual->isChecked() );
c->writeEntry( "VBR", !m_brW->m_radioConstantBitrate->isChecked() );
c->writeEntry( "Constant Bitrate", m_brW->m_comboConstantBitrate->currentText().left(3).toInt() );
c->writeEntry( "Maximum Bitrate", m_brW->m_comboMaximumBitrate->currentText().left(3).toInt() );
c->writeEntry( "Minimum Bitrate", m_brW->m_comboMinimumBitrate->currentText().left(3).toInt() );
c->writeEntry( "Average Bitrate", m_brW->m_spinAverageBitrate->value() );
c->writeEntry( "Use Maximum Bitrate", m_brW->m_checkBitrateMaximum->isChecked() );
c->writeEntry( "Use Minimum Bitrate", m_brW->m_checkBitrateMinimum->isChecked() );
c->writeEntry( "Use Average Bitrate", m_brW->m_checkBitrateAverage->isChecked() );
c->writeEntry( "Quality Level", m_w->m_sliderQuality->value() );
c->writeEntry( "Copyright", m_w->m_checkCopyright->isChecked() );
c->writeEntry( "Original", m_w->m_checkOriginal->isChecked() );
c->writeEntry( "ISO compliance", m_w->m_checkISO->isChecked() );
c->writeEntry( "Error Protection", m_w->m_checkError->isChecked() );
// default to 2 which is the same as the -h lame option
c->writeEntry( "Encoder Quality", m_w->m_spinEncoderQuality->value() );
}
TQStringList K3bLameEncoder::extensions() const
{
return TQStringList( "mp3" );
}
TQString K3bLameEncoder::fileTypeComment( const TQString& ) const
{
return "MPEG1 Layer III (mp3)";
}
long long K3bLameEncoder::fileSize( const TQString&, const K3b::Msf& msf ) const
{
TDEConfig* c = k3bcore->config();
c->setGroup( "K3bLameEncoderPlugin" );
int bitrate = 0;
if( c->readBoolEntry( "Manual Bitrate Settings", false ) ) {
if( c->readBoolEntry( "VBR", false ) ) {
if( c->readBoolEntry( "Use Maximum Bitrate", false ) )
bitrate = c->readNumEntry( "Maximum Bitrate", 224 );
if( c->readBoolEntry( "Use Minimum Bitrate", false ) )
bitrate = ( bitrate > 0
? (bitrate - c->readNumEntry( "Minimum Bitrate", 32 )) / 2
: c->readNumEntry( "Minimum Bitrate", 32 ) );
if( c->readBoolEntry( "Use Average Bitrate", true ) )
bitrate = c->readNumEntry( "Average Bitrate", 128 );
}
else {
bitrate = c->readNumEntry( "Constant Bitrate", 128 );
}
}
else {
int q = c->readNumEntry( "Quality Level", 5 );
if( q < 0 ) q = 0;
if( q > 9 ) q = 9;
bitrate = s_lame_preset_approx_bitrates[q];
}
return (msf.totalFrames()/75 * bitrate * 1000)/8;
}
K3bPluginConfigWidget* K3bLameEncoder::createConfigWidget( TQWidget* parent,
const char* name ) const
{
return new K3bLameEncoderSettingsWidget( parent, name );
}
#include "k3blameencoder.moc"
|