forked from elha/CDex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyDlg.cpp
More file actions
1776 lines (1351 loc) · 46.8 KB
/
CopyDlg.cpp
File metadata and controls
1776 lines (1351 loc) · 46.8 KB
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** CDex - Open Source Digital Audio CD Extractor
**
** Copyright (C) 2006 - 2007 Georgy Berdyshev
** Copyright (C) 1999 - 2007 Albert L. Faber
**
** http://cdexos.sourceforge.net/
** http://sourceforge.net/projects/cdexos
**
** 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include <stdio.h>
#include <direct.h>
#include <math.h>
#include "cdex.h"
#include "cdexDoc.h"
#include "cdexView.h"
#include "CopyDlg.h"
#include "config.h"
#include "Audiofile.h"
#include "ID3Tag.h"
#include "Filename.h"
#include "Encode.h"
#include "AsyncEncoder.h"
#include "ISndStream.h"
#include "SndStreamFactory.h"
#include "StatusReporter.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
INITTRACE( _T( "CopyDlg" ) );
typedef BOOL (PASCAL *GFDPEX )(LPCSTR,PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
static void GetCDRipInfo(int& nSampleFreq,int& nChannels,int& nBitsPerChannel)
{
// Initialize to default values
nSampleFreq = 44100;
nChannels = 2;
nBitsPerChannel = 16;
}
#define TIMERID 2
#define TIMERSLOT 800
#define IS_IDLE 0
#define IS_READING 1
#define IS_CONVERTING 2
#define IS_READYTOCONVERT 3
#define IS_NORMALIZING 4
#define IS_READYTONORMALIZE 5
#define IS_WRITETAG 6
#define IS_NONE 7
#define IS_DETPEAKVALUE 8
CTasks CCopyDialog::m_Tasks;
CTaskInfo CCopyDialog::m_CurrentTask;
CCopyDialog::CCopyDialog(CCDexView* pView):
CDialog(CCopyDialog::IDD, pView)
{
// Set view pointer
m_pView = pView;
// initialize variables
m_wCurrentTrack = 0;
m_iStatus = IS_IDLE;
m_pRipInfoDB = NULL;
m_bResetTimeTrack = FALSE;
m_bYesToAll = TRUE;
m_bRetainWavFile = FALSE;
m_nPercent = 0;
m_nJitterErrors = 0;
m_nJitterPos = 50;
m_bCancelled = FALSE;
m_Tasks.ClearAll();
EXIT_TRACE( _T( "CCopyDialog::CCopyDialog" ) );
}
CCopyDialog::~CCopyDialog()
{
ENTRY_TRACE( _T( "CCopyDialog::~CCopyDialog" ) );
delete m_pRipInfoDB;
m_pRipInfoDB = NULL;
EXIT_TRACE( _T( "CCopyDialog::~CCopyDialog" ) );
}
void CCopyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCopyDialog)
DDX_Control(pDX, IDC_RECORDCTRL, m_TrackCtrl);
DDX_Control(pDX, IDC_JITTERCTRL, m_JitterCtrl);
DDX_Text(pDX, IDC_INFO1, m_strInfo1);
DDX_Text(pDX, IDC_INFO2, m_strInfo2);
DDX_Text(pDX, IDC_PEAKVALUE, m_strPeakValue);
DDX_Text(pDX, IDC_INFO3, m_strInfo3);
//}}AFX_DATA_MAP
DDX_Control(pDX, IDC_INFO4, m_strInfo4);
DDX_Control(pDX, IDC_INFO5, m_strInfo5);
}
BEGIN_MESSAGE_MAP(CCopyDialog, CDialog)
//{{AFX_MSG_MAP(CCopyDialog)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CDEX_ERR CCopyDialog:: CalculateNormalizationFactor( CCopyDialog* pDlg )
{
CDEX_ERR bReturn = CDEX_OK;
double dNormPercentage = CTaskInfo::NORM_DEFAULT_VALUE * 100.0;
ASSERT( pDlg );
ENTRY_TRACE( _T( "CCopyDialog::CalculateNormalizationFactor( %p )" ), pDlg );
// Determine normalization factor
dNormPercentage = ( pDlg->GetCurrentTask().GetPeakValue() - CTaskInfo::NORM_TUNE_FACTOR ) / 32768.0 * 100;
// Is normaliztion required ?
if ( dNormPercentage < (double)g_config.GetLowNormLevel() )
{
// Normalize to the desired level
pDlg->GetCurrentTask().SetNormalizationFactor(
(double) g_config.GetLNormFactor() /
dNormPercentage *
CTaskInfo::NORM_TUNE_FACTOR );
}
else if ( dNormPercentage > (double)g_config.GetHNormFactor() )
{
// Normalize to the desired level
pDlg->GetCurrentTask().SetNormalizationFactor(
(double) g_config.GetHNormFactor() /
dNormPercentage *
CTaskInfo::NORM_TUNE_FACTOR );
}
else
{
pDlg->GetCurrentTask().SetNormalizationFactor( CTaskInfo::NORM_DEFAULT_VALUE );
}
LTRACE( _T( "CCopyDialog::CalculateNormalizationFactor(), peak value = %d, norm levels(%d,%d), norm factors(%d,%d), dNormPercentage = %7.4f => normalization factor set to %7.4f" ),
pDlg->GetCurrentTask().GetPeakValue( ),
g_config.GetLowNormLevel(),
g_config.GetHighNormLevel(),
g_config.GetLNormFactor(),
g_config.GetHNormFactor(),
dNormPercentage,
pDlg->GetCurrentTask().GetNormalizationFactor() );
EXIT_TRACE( _T( "CCopyDialog::CalculateNormalizationFactor( %p ), return value: %d" ), pDlg, bReturn );
return bReturn;
}
CDEX_ERR CCopyDialog::RipToEncoder( CCopyDialog* pDlg,
ENCODER_TYPES nEncoderType,
BOOL bIsTempFile,
BOOL& bNoToAll )
{
CDEX_ERR bReturn = CDEX_OK;
CUString strLang;
int nSampleFreq = 44100;
int nChannels = 2;
int nBitsPerChannel = 16;
// LARGE_INTEGER liRipStart;
// LARGE_INTEGER liRipStop;
LARGE_INTEGER liTicksPerSecond;
DWORD dwNumberOfSamples = 0;
ENTRY_TRACE( _T( "CCopyDialog::RipToEncoder, nEncoderType = %d"), nEncoderType );
// Initialize paramters
pDlg->m_nPercent = 0;
pDlg->m_nJitterErrors = 0;
pDlg->GetCurrentTask().SetPeakValue( 0 );
// get high performance counter frequency
QueryPerformanceFrequency( &liTicksPerSecond );
DOUBLE dTicksPerSecond = (DOUBLE)liTicksPerSecond.QuadPart;
// initialize encoder object we have to rip to
auto_ptr<CEncoder> pEncoder( EncoderObjectFactory( nEncoderType ) );
CUString strRipFileNoExt( GetCurrentTask().GetOutFileNameNoExt() );
CUString strFullRipFile( GetCurrentTask().GetOutFullFileName() );
CUString strFullRipFileNoExt( GetCurrentTask().GetOutFullFileNameNoExt() );
// Initialize the encoder
if ( CDEX_OK != pEncoder->InitEncoder( &GetCurrentTask() ) )
{
return CDEX_FILEOPEN_ERROR;
}
// special case, we first have to rip to a WAV file (temporarily)
if ( ENCODER_FIXED_WAV == nEncoderType )
{
// Get the information regarding the CD-Ripping settings
GetCDRipInfo( nSampleFreq, nChannels, nBitsPerChannel );
}
if ( bIsTempFile )
{
// rip to temp dir
pDlg->GetCurrentTask().SetInDir( g_config.GetTempDir() );
// set input file name
GetCurrentTask().SetFullFileName( g_config.GetTempDir() + strRipFileNoExt + _W( ".wav" ) );
strFullRipFile = GetCurrentTask().GetFullFileName( );
strFullRipFileNoExt = GetCurrentTask().GetFullFileNameNoExt();
}
// Check if file name does already exist, but exclude temp files
if ( !bIsTempFile &&
CheckNoFileOverwrite( pDlg, strFullRipFile, TRUE, pDlg->m_bYesToAll, bNoToAll ) )
{
return CDEX_FILEOPEN_ERROR;
}
// Reset estimate timer
pDlg->m_bResetTimeTrack = TRUE;
// Reserve space for ID3 V2 tag
if ( ( g_config.GetID3Version() >= ID3_VERSION_2 ) && pEncoder->GetCanWriteTagV2() )
{
// Reserve space for ID3V2 tag
pEncoder->SetId3V2PadSize( 4096 );
}
// Open conversion stream of encoder
if ( CDEX_OK != pEncoder->OpenStream( strFullRipFileNoExt,
nSampleFreq,
nChannels ) )
{
return CDEX_FILEOPEN_ERROR;
}
// Step 2: Get the requested buffer size of the output stream
DWORD dwSampleBufferSize = pEncoder->GetSampleBufferSize();
// Step 3: Setup the ripper
LONG nBufferSize;
CUString strRipInfoDB;
// Create RipInfoDB file names based on CDDB ID
CTagData& tagData( GetCurrentTask().GetTagData() );
strRipInfoDB.Format( _W( "%08X"), tagData.GetCDBID() );
// Create a RipInfoDB object
pDlg->m_pRipInfoDB = new CRipInfoDB;
// Set Rip information file name
pDlg->m_pRipInfoDB->SetFileName( strRipInfoDB );
// Delete old stuff
pDlg->m_pRipInfoDB->DeleteTrack( tagData.GetTrackNumber() );
// Set current track number
pDlg->m_pRipInfoDB->SetCurrentTrack( tagData.GetTrackNumber() );
// Create start info
CTime myTime( CTime::GetCurrentTime() );
strLang = g_language.GetString( IDS_RIP_TRACK_TO_MPEG );
strRipInfoDB.Format( strLang, (LPCWSTR)CUString( myTime.Format( _T( "%A, %B %d, %Y %H:%M:%S" ) )), (LPCWSTR)GetCurrentTask().GetFullFileName() );
// Add start info to RipFileInfo
pDlg->m_pRipInfoDB->SetRipInfo( strRipInfoDB );
LTRACE( _T( "CCopyDialog::RipToEncoder, ripping from sector :%d up an till sector %d = %d sectors" ),
GetCurrentTask().GetStartSector(),
GetCurrentTask().GetEndSector(),
GetCurrentTask().GetEndSector() - GetCurrentTask().GetStartSector() + 1 );
if (CR_OpenRipper( &nBufferSize,
GetCurrentTask().GetStartSector(),
GetCurrentTask().GetEndSector(),
TRUE
)==CDEX_OK)
{
CAsyncEncoder feeder( pEncoder.get(), (BOOL&)pDlg->m_bAbortThread, dwSampleBufferSize, 256 );
LONG nNumBytesRead = 0;
LONG nOffset = 0;
// create the stream buffer, allocate on enocder frame additional memory
// allocate extra memory for offset correction
auto_ptr<BYTE> pbtBufferStream( new BYTE[ nBufferSize + dwSampleBufferSize * sizeof( SHORT ) + 16383 ] );
// Get a pointer to the buffer
BYTE* pbtStream = pbtBufferStream.get();
CDEX_ERR ripErr;
// QueryPerformanceCounter( &liRipStart );
// Read all chunks
while ( ( CDEX_RIPPING_DONE != ( ripErr = CR_RipChunk( pbtStream + nOffset, &nNumBytesRead, (BOOL&)pDlg->m_bAbortThread ) ) )
&& !pDlg->m_bAbortThread )
{
SHORT* psEncodeStream=(SHORT*)pbtStream;
DWORD dwSamplesToConvert= ( nNumBytesRead + nOffset ) / sizeof( SHORT );
/*
QueryPerformanceCounter( &liRipStop );
DOUBLE dRipTicks = ( (DOUBLE)liRipStop.QuadPart - (DOUBLE)liRipStart.QuadPart );
if ( dTicksPerSecond )
{
DOUBLE dRipTimeInSecs = dRipTicks / ( dTicksPerSecond );
CUString strOut;
strOut.Format( "Rip time %f\n", dRipTimeInSecs );
OutputDebugString( strOut );
}
*/
// Check for jitter errors
if ( CDEX_JITTER_ERROR == ripErr )
{
DWORD dwStartSector,dwEndSector;
// Get info where jitter error did occur
CR_GetLastJitterErrorPosition( dwStartSector, dwEndSector );
// Add the jitter error to the logging file
pDlg->m_pRipInfoDB->SetJitterError( dwStartSector, dwEndSector, GetCurrentTask().GetStartSector() );
}
// Check if an error did occur
if ( CDEX_ERROR == ripErr )
{
LTRACE( _T( "RipToEncoder::CDEX_ERROR" ) );
break;
}
// Get progress indication
pDlg->m_nPercent = CR_GetPercentCompleted();
// Get relative jitter position
pDlg->m_nJitterPos = CR_GetJitterPosition();
// Get the number of jitter errors
pDlg->m_nJitterErrors = CR_GetNumberOfJitterErrors();
// Get the Peak Value
pDlg->GetCurrentTask().SetPeakValue( CR_GetPeakValue() );
// Convert the samples with the encoder
while ( dwSamplesToConvert >= dwSampleBufferSize )
{
if ( (BOOL&)pDlg->m_bAbortThread )
{
return CDEX_ERROR;
}
dwNumberOfSamples += dwSampleBufferSize;
// add samples to feeder
if( CDEX_OK != feeder.Add( psEncodeStream, dwSampleBufferSize ) )
{
return CDEX_ERROR;
}
// Decrease the number of samples to convert
dwSamplesToConvert -= dwSampleBufferSize;
// Increase the sample buffer pointer
psEncodeStream += dwSampleBufferSize;
}
// Copy the remaing bytes up front, if necessary
if ( dwSamplesToConvert > 0 )
{
// Calculate the offset in bytes
nOffset = dwSamplesToConvert * sizeof( SHORT );
// Copy up front
memcpy( pbtStream, psEncodeStream, nOffset );
}
else
{
nOffset = 0;
}
// QueryPerformanceCounter( &liRipStart );
}
if ( nOffset && !pDlg->m_bAbortThread )
{
dwNumberOfSamples += nOffset / sizeof( SHORT );
if(feeder.Add((SHORT*)pbtStream, nOffset / sizeof( SHORT ) )!= CDEX_OK )
{
return CDEX_ERROR;
}
}
LTRACE( _T( "CCopyDialog::RipToEncoder, Wait for encoder to Finish" ) );
feeder.WaitForFinished();
LTRACE( _T( "CCopyDialog::RipToEncoder, Encoder finished" ) );
// Close the Ripper session
CR_CloseRipper( &pDlg->m_lCRC );
}
// set length in msec
GetCurrentTask().SetLengthInMs( (DWORD)( (double)dwNumberOfSamples / (double)nSampleFreq / (double)nChannels * 1000.0 ) );
LTRACE( _T( "CCopyDialog::RipToEncoder, LengthInMs = %d" ), GetCurrentTask().GetLengthInMs() );
// Create prolog info
CTimeSpan myEndTime = CTime::GetCurrentTime()-myTime;
LTRACE( _T( "CCopyDialog::RipToEncoder, bAbort = %d" ), pDlg->m_bAbortThread );
if ( TRUE == pDlg->m_bAbortThread )
{
myTime = CTime::GetCurrentTime();
strLang = g_language.GetString( IDS_RIP_ENCODE_ABORT );
strRipInfoDB.Format( strLang, (LPCWSTR)CUString( myTime.Format( _T( "%A, %B %d, %Y %H:%M:%S" ) ) ) );
// Add prolog info to RipFileInfo
pDlg->m_pRipInfoDB->SetAbortError( strRipInfoDB );
}
else
{
strLang = g_language.GetString( IDS_RIP_FINISHED_OK );
strRipInfoDB.Format( strLang, (LPCWSTR)CUString( myEndTime.Format( _T( "%H:%M:%S" ) ) ) );
// Add prolog info to RipFileInfo
pDlg->m_pRipInfoDB->SetRipInfoFinshedOK( strRipInfoDB, pDlg->m_lCRC );
}
// Close the output stream
pEncoder->CloseStream();
// De-initialize the encoder
pEncoder->DeInitEncoder();
// When aborted, return an error
if ( pDlg->m_bAbortThread )
{
LTRACE( _T( "RipToEncoder:: bAbort = TRUE" ) );
bReturn = CDEX_ERROR;
}
EXIT_TRACE( _T( "CCopyDialog::RipToEncoder, return value %d" ), bReturn );
// Everything went well, indicate so
return bReturn;
}
CDEX_ERR CCopyDialog::WavToMpeg( CCopyDialog* pDlg,
INT& nSampleRate,
INT& nChannels,
BOOL bWriteId3V2Tag,
BOOL& bNoToAll )
{
CUString strLang;
CDEX_ERR bReturn = CDEX_OK;
BOOL bEncoderStreamOpen = FALSE;
BOOL bInStreamOpen = FALSE;
ENTRY_TRACE( _T( "CCopyDialog::WavToMpeg, sample rate %d channels %d bWriteId3V2Tag %d" ),
nSampleRate,
nChannels,
bWriteId3V2Tag );
// Step 0: Open the encoder
auto_ptr<CEncoder> pEncoder( EncoderObjectFactory( GetCurrentTask().GetEncoderType() ) );
CTime myTime( CTime::GetCurrentTime() );
// Set the normalization value
pEncoder->SetNormalizationFactor( pDlg ->GetCurrentTask().GetNormalizationFactor() );
// Determine if encoder supports Chunk encoding
if ( pEncoder->GetChunkSupport() )
{
DWORD dwOutBufferSize = 0;
DWORD dwInBufferSize = 0;
DWORD dwStreamIndex = 0;
// create the input stream
auto_ptr<ISndStream> pInStream( ICreateStream( GetCurrentTask().GetFullFileName() ) );
// Initialize the encoder
bReturn = pEncoder->InitEncoder( &GetCurrentTask() );
// Step 1: Check that input name not equal to output name
if ( CDEX_OK == bReturn )
{
if ( 0 == GetCurrentTask().GetFullFileName().CompareNoCase( GetCurrentTask().GetOutFullFileName() ) )
{
CUString strLang;
CUString strMsg;
strLang = g_language.GetString( IDS_INPUTISOUTPUTFILENAME );
strMsg.Format( strLang, (LPCWSTR)GetCurrentTask().GetFullFileName(), (LPCWSTR)GetCurrentTask().GetOutFullFileName() );
CDexMessageBox( strMsg );
bReturn = CDEX_ERROR;
}
}
// Step 2: Setup and open the input stream
if ( CDEX_OK == bReturn )
{
// Make exception for MPEG to MPEG transcoding
if ( SndStream::SNDSTREAM_MPEG == pInStream->GetStreamType() &&
pEncoder->GetCanWriteTagV2() )
{
// bWriteId3V2Tag = TRUE;
}
// Open input stream
if ( ( NULL != pInStream.get() ) && ( pInStream->OpenStream( GetCurrentTask().GetFullFileName() ) ) )
{
bInStreamOpen = TRUE;
nSampleRate= pInStream->GetSampleRate();
// Check if file name does already exist
if ( TRUE == CheckNoFileOverwrite( pDlg, GetCurrentTask().GetOutFullFileName(), TRUE, pDlg->m_bYesToAll, bNoToAll ) )
{
bReturn = CDEX_FILEOPEN_ERROR;
}
}
else
{
strLang = g_language.GetString( IDS_ERROR_OPENING_INPUT_FILE );
CUString strTmp;
strTmp.Format( _W( "%s\r\n%s" ), (LPCWSTR)strLang, (LPCWSTR)GetCurrentTask().GetFullFileName() );
CDexMessageBox( strTmp );
bReturn = CDEX_ERROR;
}
pDlg->m_bResetTimeTrack = TRUE;
}
// Step 3: Setup and open the output stream
if ( CDEX_OK == bReturn )
{
dwInBufferSize = pInStream->GetBufferSize();
// Reserve space for ID3 V2 tag
if ( ( g_config.GetID3Version() >= ID3_VERSION_2 ) && bWriteId3V2Tag )
{
// Reserve space for ID3V2 tag
pEncoder->SetId3V2PadSize( 4096 );
}
// Open encoding stream
bReturn = pEncoder->OpenStream( GetCurrentTask().GetOutFullFileNameNoExt(),
pInStream->GetSampleRate(),
pInStream->GetChannels() );
bEncoderStreamOpen = (CDEX_OK == bReturn);
dwOutBufferSize = pEncoder->GetSampleBufferSize() * 2;
}
// Step 4: Create input stream buffer
auto_ptr<BYTE> pStream( new BYTE[ dwInBufferSize + dwOutBufferSize ] );
if ( NULL == pStream.get() )
{
bReturn = CDEX_ERROR;
}
// Step 5: Convert the input stream
if ( CDEX_OK == bReturn )
{
BOOL bFinished = FALSE;
while ( ( FALSE == pDlg->m_bAbortThread ) && ( CDEX_OK == bReturn ) && !bFinished )
{
int nInputBytes = pInStream->Read( (BYTE*)( pStream.get() ) + dwStreamIndex, dwInBufferSize );
if ( nInputBytes > 0 )
{
// increase current stream index position
dwStreamIndex+= nInputBytes;
// play the stuff when there is STREAMBUFFERSIZE samples are present
while ( ( dwStreamIndex >= dwOutBufferSize ) &&
( FALSE == pDlg->m_bAbortThread ) )
{
// Encode this chunk
bReturn = pEncoder->EncodeChunk( (SHORT*)pStream.get() , dwOutBufferSize / 2 );
if ( bReturn == CDEX_OK )
{
dwStreamIndex-= dwOutBufferSize;
if ( dwStreamIndex )
{
memmove( pStream.get(), (BYTE*)( pStream.get() ) + dwOutBufferSize, dwStreamIndex );
}
// Update percentage
pDlg->m_nPercent = pInStream->GetPercent();
}
}
}
else
{
// play the stuff when there is STREAMBUFFERSIZE samples are present
while ( dwStreamIndex && ( CDEX_OK == bReturn ) )
{
dwOutBufferSize = min( dwOutBufferSize, dwStreamIndex );
// Encode this chunk
bReturn = pEncoder->EncodeChunk( (SHORT*)pStream.get() , dwOutBufferSize / 2 );
if ( bReturn == CDEX_OK )
{
dwStreamIndex-= dwOutBufferSize;
if ( dwStreamIndex )
{
memmove( pStream.get(), (BYTE*)( pStream.get() ) + dwOutBufferSize, dwStreamIndex );
}
// Update percentage
pDlg->m_nPercent = pInStream->GetPercent();
}
}
bFinished = TRUE;
}
}
}
// set length in msec
if( bInStreamOpen )
{
GetCurrentTask().SetLengthInMs( pInStream->GetTotalTime() );
LTRACE( _T( "CCopyDialog::WavToMpeg, LengthInMs = %d" ), GetCurrentTask().GetLengthInMs() );
// Close the input stream
pInStream->CloseStream();
}
if( bEncoderStreamOpen )
{
// Close the output stream
pEncoder->CloseStream();
}
if ( CDEX_OK == bReturn )
{
CID3Tag copyTag( GetCurrentTask().GetFullFileName() );
copyTag.LoadTagProperties();
if ( /* SndStream::SNDSTREAM_MPEG == pInStream->GetStreamType() && */
( pEncoder->GetCanWriteTagV1() || pEncoder->GetCanWriteTagV2() ) )
{
// copy ID3 Tag
copyTag.CopyTags( GetCurrentTask().GetFullFileName(),
GetCurrentTask().GetOutFullFileName(),
pEncoder->GetCanWriteTagV1() && copyTag.IsV1Tag(),
pEncoder->GetCanWriteTagV2() && copyTag.IsV2Tag() ) ;
}
}
// De-initialize the encoder
pEncoder->DeInitEncoder();
if ( pDlg->m_bAbortThread )
{
CUString strFileToDelete = GetCurrentTask().GetOutFullFileName();
CUStringConvert strCnv;
DeleteFile( strCnv.ToT( strFileToDelete ) );
LTRACE( _T( "Delete file \"%s\" due to abort" ), strCnv.ToT( strFileToDelete ) );
}
if ( NULL != pDlg->m_pRipInfoDB )
{
CUString strRipInfoDB;
CTimeSpan myEndTime = CTime::GetCurrentTime()-myTime;
if ( TRUE == pDlg->m_bAbortThread )
{
strLang = g_language.GetString( IDS_ENCODE_ABORT );
strRipInfoDB.Format( strLang, (LPCWSTR)CUString( myTime.Format( _T( "%A, %B %d, %Y %H:%M:%S" ) ) ) );
// Add prolog info to RipFileInfo
pDlg->m_pRipInfoDB->SetAbortError( strRipInfoDB );
}
else
{
strLang = g_language.GetString( IDS_ENCODE_FINISHED_OK );
strRipInfoDB.Format( strLang, (LPCWSTR)CUString( myEndTime.Format( _T( "%H:%M:%S" ) ) ) );
// Add prolog info to RipFileInfo
pDlg->m_pRipInfoDB->SetRipInfoFinshedOK( strRipInfoDB, pDlg->m_lCRC );
}
}
}
else
{
LTRACE( _T( "CCopyDialog::WavToMpeg, Calling DOS Encoder" ) );
pEncoder->InitEncoder( &GetCurrentTask() );
// Do the encoding
CUString strInDir( GetCurrentTask().GetInDir() );
CUString strOutDir( GetCurrentTask().GetOutDir() );
CUString strInFileName( GetCurrentTask().GetFullFileNameNoExt() );
CUString strOutFileName( GetCurrentTask().GetOutFullFileNameNoExt() );
CUString strNrmInFileName( strInFileName );
// Does this file needs normalization ?
if ( CTaskInfo::NORM_DEFAULT_VALUE != pDlg->GetCurrentTask().GetNormalizationFactor() )
{
LTRACE( _T( "Normalizing for external codec" ) );
pDlg->SetupControls( IS_NORMALIZING );
// Normalize the file, keep the nrm file
NormWav( pDlg, strNrmInFileName , FALSE );
strNrmInFileName += _W( ".nrm" );
pDlg->SetupControls( IS_CONVERTING );
}
bReturn = pEncoder->DosEncode( strNrmInFileName,
GetCurrentTask().GetFileExt(),
strOutFileName,
strOutDir,
(int&)pDlg->m_nPercent,
(BOOL&)pDlg->m_bAbortThread );
// check if we have normalized the file, if so, delete the intermediate file
if ( CTaskInfo::NORM_DEFAULT_VALUE != pDlg->GetCurrentTask().GetNormalizationFactor() )
{
CUString strFileToDelete;
// delete the normalization file
strFileToDelete = strNrmInFileName + _W( "." ) + GetCurrentTask().GetFileExt();
LTRACE( _T( "Deleting normalized file \"%s\"" ), strFileToDelete );
CUStringConvert strCnv;
DeleteFile( strCnv.ToT( strFileToDelete ) ) ;
}
if ( pDlg->m_bAbortThread )
{
CUString strFileToDelete = GetCurrentTask().GetOutFullFileName();
CUStringConvert strCnv;
CDexDeleteFile( strFileToDelete ) ;
LTRACE( _T( "Delete file \"%s\" due to abort" ), strCnv.ToT( strFileToDelete ) );
}
}
EXIT_TRACE( _T( "CCopyDialog::WavToMpeg, return value: %d" ), bReturn );
return bReturn;
}
CDEX_ERR CCopyDialog::GetMaxWaveValue( CCopyDialog* pDlg )
{
CDEX_ERR bReturn = CDEX_OK;
ENTRY_TRACE( _T( "CCopyDialog::GetMaxWaveValue" ) );
// init incoming parameters
pDlg->GetCurrentTask().SetPeakValue( 0 );
pDlg->m_nPercent = 0;
// create WAV object
CWAV inWav;
// Step 1: Open the input WAV file
if ( inWav.OpenForRead( GetCurrentTask().GetFullFileName() ) != CDEX_OK )
{
ASSERT(FALSE);
bReturn = CDEX_ERROR;
}
else
{
int nPeak = pDlg->GetCurrentTask().GetPeakValue();
while( FALSE == inWav.GetMaxWaveValue(
(int&)pDlg->m_nPercent,
nPeak )
)
{
::Sleep( 0 );
}
pDlg->GetCurrentTask().SetPeakValue( nPeak );
// Close audio file
inWav.CloseStream();
}
EXIT_TRACE( _T( "GetMaxWaveValue, peak value is %d, return value d" ), pDlg->GetCurrentTask().GetPeakValue(), bReturn );
return bReturn;
}
CDEX_ERR CCopyDialog::NormWav( CCopyDialog* pDlg,
CUString strWavFileName,
BOOL bReplace )
{
CDEX_ERR bReturn = CDEX_OK;
ENTRY_TRACE( _T( "CCopyDialog::NormWav( %s, %f" ), strWavFileName );
// Local parameters
CWAV inWav;
pDlg->m_nPercent = 0;
// Step 1: Open the input WAV file
if ( 0 == inWav.StartNormalizeAudioFile( strWavFileName ) )
{
bReturn = CDEX_ERROR;
}
else
{
// Step 2: Loop through data and normalize chunk
while ( ( inWav.NormalizeAudioFileChunk( pDlg->GetCurrentTask().GetNormalizationFactor(), (int&)pDlg->m_nPercent ) == FALSE ) &&
!pDlg->m_bAbortThread )
{
::Sleep(0);
}
// Step 3: Close the WAV file
inWav.CloseNormalizeAudioFile( strWavFileName,
pDlg->m_bAbortThread,
bReplace );
}
EXIT_TRACE( _T( "CCopyDialog::NormWav, return value: %d" ), bReturn );
return bReturn;
}
CDEX_ERR CCopyDialog::AddToPlayList( CCopyDialog* pDlg )
{
CUString strLang;
ENTRY_TRACE( _T( "CCopyDialog::AddToPlayList" ) );
CUString strName( _W( "" ) );
CUString strDir( _W( "" ) );
CUString strBuild[NUMFILENAME_IDS];
CTagData& tagData( GetCurrentTask().GetTagData() );
// Build playlist filename
strBuild[0] = tagData.GetArtist();
strBuild[1] = tagData.GetAlbum();
strBuild[2].Format( _W( "%d" ), (LONG)tagData.GetTrackNumber() + tagData.GetTrackOffset() );
strBuild[3] = tagData.GetTitle();
strBuild[4].Format( _W( "%08x" ), tagData.GetCDBID() );
strBuild[5].Format( _W( "%08x" ), tagData.GetVOLID() );
strBuild[6].Format( _W( "%02d" ), (LONG)tagData.GetTrackNumber() + tagData.GetTrackOffset() );
strBuild[7].Format( _W( "%02d" ), tagData.GetTotalTracks() );
strBuild[8] = tagData.GetYear();
strBuild[9] = tagData.GetGenre();
strBuild[10] = GetCurrentTask().GetNonSplitArtist();
// Build the new file name
::BuildFileName(g_config.GetPlsFileFormat(),strBuild,strName,strDir);
CUString strPlsDir;
if ( strDir.Find( _W( ":\\" ) ) > 0 )
{
strPlsDir = strDir;
}
else
{
strPlsDir=g_config.GetMP3OutputDir()+strDir;
}
if ( g_config.GetM3UPlayList() || g_config.GetPLSPlayList() )
{
// Create playlist output directory if necessary
if ( CDEX_OK != DoesDirExist( strPlsDir, FALSE ) )
{
LTRACE( _T( "Write Id3 Tag And Play List::Error creating output dir %s" ), strPlsDir );
strLang = g_language.GetString( IDS_CANNOT_CREATE_OUTDIR );
CDexMessageBox( strLang + strPlsDir );
}
else
{
// Create Playlist object with proper filename
PlayList myList( strPlsDir+strName );
// Add this entry to the playlist
myList.AddEntry(GetCurrentTask().GetOutFullFileName() );
}
}
// some debugging information
EXIT_TRACE( _T( "CCopyDialog::AddToPlayList" ) );
return CDEX_OK;
}
CDEX_ERR CCopyDialog::WriteId3Tag( CCopyDialog* pDlg,
BOOL bCanWriteTagV1,
BOOL bCanWriteTagV2 )
{
ENTRY_TRACE( _T( "CCopyDialog::WriteId3Tag file to tag \"%s\"" ), GetCurrentTask().GetOutFullFileName() );
// Add ID3 Tag if necessary
CID3Tag myTag( GetCurrentTask().GetOutFullFileName() );
myTag.SetTagData( GetCurrentTask().GetTagData() );
// AF CHECK, can be removed, have to check all conditions
if ( GetCurrentTask().GetLengthInMs() )
{
CUString strTmp;
strTmp.Format( _W( "%d" ), GetCurrentTask().GetLengthInMs() );
myTag.GetTagDataReference().SetLengthInMs( strTmp );
}
// AF END
int nVersion = g_config.GetID3Version();
if ( ( nVersion > ID3_VERSION_NONE ) && (bCanWriteTagV1 || bCanWriteTagV2) )
{
// Get the desired ID3 tag version