forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDDAExtract.cpp
815 lines (725 loc) · 26.9 KB
/
CDDAExtract.cpp
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
#include "CDDAExtract.h"
#include "resource.h"
#include "Utility.h"
#include "ebur128.h"
#include <iomanip>
#include <sstream>
// The maximum number of times to read a CDDA sector.
static const long s_MaxReadPasses = 9;
// Timer ID.
static const long s_TimerID = 1212;
// Timer interval in milliseconds.
static const long s_TimerInterval = 250;
// ID to indicate that the read thread is fixing sectors.
static const long s_ReadFixingSectors = 110;
// ID to indicate that the read thread has finished.
static const long s_ReadFinished = 111;
// Message ID sent when the extraction has finished successfully.
// 'wParam' - unused.
// 'lParam' - unused.
static const UINT MSG_EXTRACTFINISHED = WM_APP + 90;
// Message ID sent when there has been an error during extraction.
// 'wParam' - error message resource ID.
// 'lParam' - unused.
static const UINT MSG_EXTRACTERROR = WM_APP + 91;
INT_PTR CALLBACK CDDAExtract::DialogProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch ( message ) {
case WM_INITDIALOG : {
CDDAExtract* dialog = reinterpret_cast<CDDAExtract*>( lParam );
if ( nullptr != dialog ) {
SetWindowLongPtr( hwnd, DWLP_USER, lParam );
dialog->OnInitDialog( hwnd );
return TRUE;
}
break;
}
case WM_DESTROY : {
SetWindowLongPtr( hwnd, DWLP_USER, 0 );
break;
}
case WM_COMMAND : {
switch ( LOWORD( wParam ) ) {
case IDCANCEL : {
CDDAExtract* dialog = reinterpret_cast<CDDAExtract*>( GetWindowLongPtr( hwnd, DWLP_USER ) );
if ( nullptr != dialog ) {
dialog->Close();
}
return TRUE;
}
default : {
break;
}
}
break;
}
case WM_TIMER : {
CDDAExtract* dialog = reinterpret_cast<CDDAExtract*>( GetWindowLongPtr( hwnd, DWLP_USER ) );
if ( ( nullptr != dialog ) && ( s_TimerID == wParam ) ) {
dialog->UpdateStatus();
}
break;
}
case MSG_EXTRACTFINISHED : {
CDDAExtract* dialog = reinterpret_cast<CDDAExtract*>( GetWindowLongPtr( hwnd, DWLP_USER ) );
if ( nullptr != dialog ) {
dialog->Close();
}
break;
}
case MSG_EXTRACTERROR : {
CDDAExtract* dialog = reinterpret_cast<CDDAExtract*>( GetWindowLongPtr( hwnd, DWLP_USER ) );
if ( nullptr != dialog ) {
dialog->Error( static_cast<WORD>( wParam ) );
}
break;
}
default : {
break;
}
}
return FALSE;
}
DWORD WINAPI CDDAExtract::ReadThreadProc( LPVOID lpParam )
{
CDDAExtract* extract = reinterpret_cast<CDDAExtract*>( lpParam );
if ( nullptr != extract ) {
extract->ReadHandler();
}
return 0;
}
DWORD WINAPI CDDAExtract::EncodeThreadProc( LPVOID lpParam )
{
CDDAExtract* extract = reinterpret_cast<CDDAExtract*>( lpParam );
if ( nullptr != extract ) {
CoInitializeEx( NULL /*reserved*/, COINIT_APARTMENTTHREADED );
extract->EncodeHandler();
CoUninitialize();
}
return 0;
}
CDDAExtract::CDDAExtract( const HINSTANCE instance, const HWND parent, Library& library, Settings& settings, Handlers& handlers, DiscManager& discManager, const Playlist::ItemList& tracks, const Handler::Ptr encoderHandler, const std::wstring& joinFilename ) :
m_hInst( instance ),
m_hWnd( nullptr ),
m_Library( library ),
m_Settings( settings ),
m_Handlers( handlers ),
m_DiscManager( discManager ),
m_Tracks( tracks ),
m_CancelEvent( CreateEvent( NULL /*attributes*/, TRUE /*manualReset*/, FALSE /*initialState*/, L"" /*name*/ ) ),
m_PendingEncodeEvent( CreateEvent( NULL /*attributes*/, TRUE /*manualReset*/, FALSE /*initialState*/, L"" /*name*/ ) ),
m_ReadThread( nullptr ),
m_EncodeThread( nullptr ),
m_PendingEncode(),
m_PendingEncodeMutex(),
m_StatusTrack( 0 ),
m_StatusPass( 0 ),
m_ProgressRead( 0 ),
m_ProgressEncode( 0 ),
m_ProgressRange( 100 ),
m_DisplayedTrack( 0 ),
m_DisplayedPass( 0 ),
m_EncoderHandler( encoderHandler ),
m_Encoder( encoderHandler ? encoderHandler->OpenEncoder() : nullptr ),
m_EncoderSettings( m_Encoder ? m_Settings.GetEncoderSettings( encoderHandler->GetDescription() ) : std::string() ),
m_JoinFilename( joinFilename )
{
DialogBoxParam( instance, MAKEINTRESOURCE( IDD_CONVERT_PROGRESS ), parent, DialogProc, reinterpret_cast<LPARAM>( this ) );
}
CDDAExtract::~CDDAExtract()
{
}
void CDDAExtract::OnInitDialog( const HWND hwnd )
{
m_hWnd = hwnd;
CentreDialog( m_hWnd );
const HWND progressRead = GetDlgItem( m_hWnd, IDC_EXTRACT_PROGRESS_TRACK );
if ( nullptr != progressRead ) {
RECT rect = {};
GetClientRect( progressRead, &rect );
if ( ( rect.right - rect.left ) > 0 ) {
m_ProgressRange = rect.right - rect.left;
SendMessage( progressRead, PBM_SETRANGE, 0, MAKELONG( 0, m_ProgressRange ) );
}
}
const HWND progressEncode = GetDlgItem( m_hWnd, IDC_EXTRACT_PROGRESS_TOTAL );
if ( nullptr != progressEncode ) {
SendMessage( progressEncode, PBM_SETRANGE, 0, MAKELONG( 0, m_ProgressRange ) );
}
UpdateStatus();
m_EncodeThread = CreateThread( NULL /*attributes*/, 0 /*stackSize*/, EncodeThreadProc, this /*param*/, 0 /*flags*/, NULL /*threadId*/ );
m_ReadThread = CreateThread( NULL /*attributes*/, 0 /*stackSize*/, ReadThreadProc, this /*param*/, 0 /*flags*/, NULL /*threadId*/ );
SetTimer( m_hWnd, s_TimerID, s_TimerInterval, NULL /*timerProc*/ );
}
void CDDAExtract::Close()
{
KillTimer( m_hWnd, s_TimerID );
SetEvent( m_CancelEvent );
if ( nullptr != m_EncodeThread ) {
WaitForSingleObject( m_EncodeThread, INFINITE );
CloseHandle( m_EncodeThread );
m_EncodeThread = nullptr;
}
if ( nullptr != m_ReadThread ) {
WaitForSingleObject( m_ReadThread, INFINITE );
CloseHandle( m_ReadThread );
m_ReadThread = nullptr;
}
CloseHandle( m_CancelEvent );
m_CancelEvent = nullptr;
CloseHandle( m_PendingEncodeEvent );
m_PendingEncodeEvent = nullptr;
EndDialog( m_hWnd, 0 );
}
bool CDDAExtract::Cancelled() const
{
const bool cancelled = ( WAIT_OBJECT_0 == WaitForSingleObject( m_CancelEvent, 0 ) );
return cancelled;
}
void CDDAExtract::Error( const WORD errorID )
{
SetEvent( m_CancelEvent );
KillTimer( m_hWnd, s_TimerID );
const int bufferSize = 256;
WCHAR buffer[ bufferSize ] = {};
if ( FALSE != LoadString( m_hInst, errorID, buffer, bufferSize ) ) {
SetDlgItemText( m_hWnd, IDC_EXTRACT_STATE_READ, buffer );
SetDlgItemText( m_hWnd, IDC_EXTRACT_STATE_ENCODER, buffer );
}
const HWND progressRead = GetDlgItem( m_hWnd, IDC_EXTRACT_PROGRESS_TRACK );
if ( nullptr != progressRead ) {
SendMessage( progressRead, PBM_SETPOS, m_ProgressRange, 0 );
}
const HWND progressEncode = GetDlgItem( m_hWnd, IDC_EXTRACT_PROGRESS_TOTAL );
if ( nullptr != progressEncode ) {
SendMessage( progressEncode, PBM_SETPOS, m_ProgressRange, 0 );
}
LoadString( m_hInst, IDS_CLOSE, buffer, bufferSize );
SetDlgItemText( m_hWnd, IDCANCEL, buffer );
}
void CDDAExtract::ReadHandler()
{
// A set of CDDA sector data.
typedef std::set<CDDAMedia::Data> SectorSet;
// A map of CDDA sector sets.
typedef std::map<long,SectorSet> SectorMap;
SectorMap sectorMap;
// A cache of CDDA sectors.
CDDAMedia::DataMap sectorCache;
const long maxCachedSectors = 32;
bool readError = false;
const DiscManager::CDDAMediaMap drives = m_DiscManager.GetCDDADrives();
auto trackIter = m_Tracks.begin();
while ( !Cancelled() && ( m_Tracks.end() != trackIter ) ) {
// Get the CDDA media object for the current track.
const CDDAMedia* media = nullptr;
wchar_t drive = 0;
long track = 0;
if ( CDDAMedia::FromMediaFilepath( trackIter->Info.GetFilename(), drive, track ) ) {
const auto driveIter = drives.find( drive );
if ( drives.end() != driveIter ) {
media = &driveIter->second;
}
}
// Keep reading the current track until we get two consistent reads for each sector.
if ( nullptr != media ) {
const long sectorCount = media->GetSectorCount( track );
const long sectorStart = media->GetStartSector( track );
const long sectorEnd = sectorStart + sectorCount;
if ( sectorCount > 0 ) {
const HANDLE mediaHandle = media->Open();
if ( nullptr != mediaHandle ) {
std::set<long> sectorsRemaining;
for ( long sectorIndex = sectorStart; sectorIndex < sectorEnd; sectorIndex++ ) {
sectorsRemaining.insert( sectorIndex );
}
sectorMap.clear();
sectorCache.clear();
long pass = 1;
m_StatusTrack.store( track );
m_StatusPass.store( pass );
m_ProgressRead.store( 0 );
while ( !Cancelled() && ( pass <= s_MaxReadPasses ) && !sectorsRemaining.empty() ) {
// Re-read all sectors on each pass, not just the remaining sectors, in an attempt to flush any cache on the device.
long sectorIndex = sectorStart;
while ( !Cancelled() && ( sectorIndex < sectorEnd ) && !sectorsRemaining.empty() ) {
auto cacheIter = sectorCache.find( sectorIndex );
if ( sectorCache.end() == cacheIter ) {
sectorCache.clear();
if ( media->Read( mediaHandle, sectorIndex, std::min<long>( maxCachedSectors, sectorEnd - sectorIndex ), sectorCache ) ) {
cacheIter = sectorCache.find( sectorIndex );
}
}
if ( sectorCache.end() != cacheIter ) {
const CDDAMedia::Data& data = cacheIter->second;
const auto sectorIter = sectorsRemaining.find( sectorIndex );
if ( sectorsRemaining.end() != sectorIter ) {
SectorSet& sectorSet = sectorMap[ sectorIndex ];
if ( sectorSet.end() == sectorSet.find( data ) ) {
sectorSet.insert( data );
} else {
sectorsRemaining.erase( sectorIter );
if ( sectorSet.size() > 1 ) {
// Keep a single copy of the valid data for this sector.
sectorSet.clear();
sectorSet.insert( data );
}
}
}
}
++sectorIndex;
m_ProgressRead.store( static_cast<float>( sectorIndex - sectorStart ) / sectorCount );
}
++pass;
if ( pass <= s_MaxReadPasses ) {
m_StatusPass.store( pass );
}
}
media->Close( mediaHandle );
if ( !Cancelled() ) {
if ( sectorMap.size() == static_cast<size_t>( sectorCount ) ) {
if ( !sectorsRemaining.empty() ) {
// For each inconsistent sector, take the modal value for each sample.
m_StatusPass.store( s_ReadFixingSectors );
typedef std::map<short,int> SampleMap;
SampleMap sampleMap;
auto sectorIter = sectorsRemaining.begin();
const size_t sectorsRemainingCount = sectorsRemaining.size();
size_t currentSector = 0;
while ( !Cancelled() && ( sectorsRemaining.end() != sectorIter ) ) {
m_ProgressRead.store( static_cast<float>( currentSector ) / sectorsRemainingCount );
SectorSet& sectorSet = sectorMap[ *sectorIter ];
if ( sectorSet.size() > 1 ) {
const size_t sampleCount = sectorSet.begin()->size();
CDDAMedia::Data fixedSector( sampleCount );
for ( size_t sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++ ) {
sampleMap.clear();
for ( const auto& sector : sectorSet ) {
auto sampleMapIter = sampleMap.insert( SampleMap::value_type( sector[ sampleIndex ], 0 ) ).first;
if ( sampleMap.end() != sampleMapIter ) {
sampleMapIter->second++;
}
}
if ( 1 == sampleMap.size() ) {
fixedSector[ sampleIndex ] = sampleMap.begin()->first;
} else {
int sampleSize = INT_MIN;
short modalValue = 0;
for ( const auto& sampleIter : sampleMap ) {
if ( sampleIter.second > sampleSize ) {
sampleSize = sampleIter.second;
modalValue = sampleIter.first;
}
}
fixedSector[ sampleIndex ] = modalValue;
}
}
sectorSet.clear();
sectorSet.insert( fixedSector );
}
++sectorIter;
++currentSector;
}
}
if ( !Cancelled() ) {
// Concatenate sector data and pass off to the encoder.
const size_t totalSize = sectorMap.begin()->second.begin()->size() * sectorCount;
DataPtr trackData( new CDDAMedia::Data() );
trackData->reserve( totalSize );
for ( const auto& sector : sectorMap ) {
const CDDAMedia::Data& sectorData = *sector.second.begin();
trackData->insert( trackData->end(), sectorData.begin(), sectorData.end() );
}
std::lock_guard<std::mutex> lock( m_PendingEncodeMutex );
m_PendingEncode.insert( MediaData::value_type( trackIter->Info, trackData ) );
SetEvent( m_PendingEncodeEvent );
}
} else {
readError = true;
PostMessage( m_hWnd, MSG_EXTRACTERROR, IDS_EXTRACT_ERROR_READ, 0 );
break;
}
}
}
}
}
++trackIter;
}
if ( !readError ) {
m_StatusTrack.store( s_ReadFinished );
}
}
void CDDAExtract::EncodeHandler()
{
if ( m_Encoder && !m_Tracks.empty() ) {
long long totalSamples = 0;
for ( const auto& media : m_Tracks ) {
totalSamples += ( media.Info.GetFilesize() / 4 );
}
long long totalSamplesEncoded = 0;
const size_t trackCount = m_Tracks.size();
size_t tracksEncoded = 0;
MediaInfo::List encodedMediaList;
std::wstring extractFolder;
std::wstring extractFilename;
bool extractToLibrary = false;
bool extractJoin = false;
m_Settings.GetExtractSettings( extractFolder, extractFilename, extractToLibrary, extractJoin );
std::vector<ebur128_state*> r128States;
if ( !extractJoin ) {
r128States.reserve( trackCount );
}
ebur128_state* r128State = nullptr;
bool encoderOK = true;
if ( extractJoin ) {
const long sampleRate = m_Tracks.front().Info.GetSampleRate();
const long channels = m_Tracks.front().Info.GetChannels();
const auto bps = m_Tracks.front().Info.GetBitsPerSample();
encoderOK = m_Encoder->Open( m_JoinFilename, sampleRate, channels, bps, totalSamples, m_EncoderSettings, {} /*tags*/ );
r128State = ebur128_init( static_cast<unsigned int>( channels ), static_cast<unsigned int>( sampleRate ), EBUR128_MODE_I );
if ( nullptr != r128State ) {
r128States.push_back( r128State );
}
}
if ( encoderOK ) {
const HANDLE eventHandles[ 2 ] = { m_CancelEvent, m_PendingEncodeEvent };
while ( WaitForMultipleObjects( 2, eventHandles, FALSE /*waitAll*/, INFINITE ) != WAIT_OBJECT_0 ) {
MediaInfo mediaInfo;
DataPtr data;
m_PendingEncodeMutex.lock();
auto dataIter = m_PendingEncode.begin();
if ( m_PendingEncode.end() != dataIter ) {
mediaInfo = dataIter->first;
data = dataIter->second;
m_PendingEncode.erase( dataIter );
if ( m_PendingEncode.empty() ) {
ResetEvent( m_PendingEncodeEvent );
}
}
m_PendingEncodeMutex.unlock();
if ( data ) {
const long sampleRate = mediaInfo.GetSampleRate();
const long channels = mediaInfo.GetChannels();
const auto bps = mediaInfo.GetBitsPerSample();
const long long trackSamplesTotal = static_cast<long long>( mediaInfo.GetDuration() * sampleRate );
long long samplesEncoded = 0;
if ( !extractJoin ) {
r128State = ebur128_init( static_cast<unsigned int>( channels ), static_cast<unsigned int>( sampleRate ), EBUR128_MODE_I );
if ( nullptr != r128State ) {
r128States.push_back( r128State );
}
}
std::wstring filename = extractJoin ? m_JoinFilename : GetOutputFilename( mediaInfo );
if ( !filename.empty() ) {
if ( extractJoin || m_Encoder->Open( filename, sampleRate, channels, bps, trackSamplesTotal, m_EncoderSettings, m_Library.GetTags( mediaInfo ) ) ) {
const long sampleBufferSize = 65536;
std::vector<float> sampleBuffer( sampleBufferSize * channels );
int r128Error = EBUR128_SUCCESS;
auto sourceIter = data->begin();
while ( !Cancelled() && ( data->end() != sourceIter ) ) {
auto destIter = sampleBuffer.begin();
while ( ( data->end() != sourceIter ) && ( sampleBuffer.end() != destIter ) ) {
*destIter++ = *sourceIter++ / 32768.0f;
}
const long sampleCount = static_cast<long>( destIter - sampleBuffer.begin() ) / channels;
if ( m_Encoder->Write( &sampleBuffer[ 0 ], sampleCount ) ) {
if ( ( nullptr != r128State ) && ( EBUR128_SUCCESS == r128Error ) ) {
r128Error = ebur128_add_frames_float( r128State, &sampleBuffer[ 0 ], static_cast<size_t>( sampleCount ) );
}
samplesEncoded += sampleCount;
totalSamplesEncoded += sampleCount;
m_ProgressEncode.store( static_cast<float>( totalSamplesEncoded ) / totalSamples );
} else {
break;
}
}
if ( !extractJoin ) {
m_Encoder->Close();
}
}
}
if ( !Cancelled() ) {
const bool encodeSuccess = ( ( mediaInfo.GetFilesize() / 4 ) == samplesEncoded );
if ( encodeSuccess ) {
if ( extractJoin ) {
if ( ++tracksEncoded == trackCount ) {
break;
}
} else {
if ( nullptr != r128State ) {
double loudness = 0;
if ( EBUR128_SUCCESS == ebur128_loudness_global( r128State, &loudness ) ) {
const float trackGain = LOUDNESS_REFERENCE - static_cast<float>( loudness );
mediaInfo.SetGainTrack( trackGain );
}
}
WriteTrackTags( filename, mediaInfo );
encodedMediaList.push_back( MediaInfo( filename ) );
if ( ++tracksEncoded == trackCount ) {
std::optional<float> albumGain;
if ( !r128States.empty() ) {
double loudness = 0;
if ( EBUR128_SUCCESS == ebur128_loudness_global_multiple( &r128States[ 0 ], r128States.size(), &loudness ) ) {
albumGain = LOUDNESS_REFERENCE - static_cast<float>( loudness );
}
}
mediaInfo.SetGainAlbum( albumGain );
for ( auto& encodedMedia : encodedMediaList ) {
WriteAlbumTags( encodedMedia.GetFilename(), mediaInfo );
if ( extractToLibrary || m_Library.GetMediaInfo( encodedMedia, false /*checkFileAttributes*/, false /*scanMedia*/ ) ) {
m_Library.GetMediaInfo( encodedMedia );
}
}
PostMessage( m_hWnd, MSG_EXTRACTFINISHED, 0, 0 );
break;
}
}
} else {
encoderOK = false;
break;
}
}
}
}
if ( extractJoin ) {
m_Encoder->Close();
MediaInfo joinedMediaInfo;
MediaInfo::List mediaList;
for ( const auto& item : m_Tracks ) {
mediaList.push_back( item.Info );
}
MediaInfo::GetCommonInfo( mediaList, joinedMediaInfo );
joinedMediaInfo.SetFilename( m_JoinFilename );
if ( nullptr != r128State ) {
double loudness = 0;
if ( EBUR128_SUCCESS == ebur128_loudness_global( r128State, &loudness ) ) {
const float trackGain = LOUDNESS_REFERENCE - static_cast<float>( loudness );
joinedMediaInfo.SetGainTrack( trackGain );
}
}
WriteTrackTags( joinedMediaInfo.GetFilename(), joinedMediaInfo );
if ( extractToLibrary || m_Library.GetMediaInfo( joinedMediaInfo, false /*checkFileAttributes*/, false /*scanMedia*/ ) ) {
m_Library.GetMediaInfo( joinedMediaInfo );
}
if ( encoderOK && !Cancelled() ) {
PostMessage( m_hWnd, MSG_EXTRACTFINISHED, 0, 0 );
}
}
}
if ( !encoderOK && !Cancelled() ) {
PostMessage( m_hWnd, MSG_EXTRACTERROR, IDS_EXTRACT_ERROR_ENCODER, 0 );
}
for ( const auto& iter : r128States ) {
ebur128_state* state = iter;
ebur128_destroy( &state );
}
}
}
std::wstring CDDAExtract::GetOutputFilename( const MediaInfo& mediaInfo ) const
{
std::wstring outputFilename;
std::wstring extractFolder;
std::wstring extractFilename;
bool extractToLibrary = false;
bool extractJoin = false;
m_Settings.GetExtractSettings( extractFolder, extractFilename, extractToLibrary, extractJoin );
WCHAR buffer[ 64 ] = {};
const std::wstring emptyArtist = LoadString( m_hInst, IDS_EMPTYARTIST, buffer, 64 ) ? buffer : std::wstring();
const std::wstring emptyAlbum = LoadString( m_hInst, IDS_EMPTYALBUM, buffer, 64 ) ? buffer : std::wstring();
std::wstring title = mediaInfo.GetTitle( true /*filenameAsTitle*/ );
WideStringReplaceInvalidFilenameCharacters( title, L"_", true /*replaceFolderDelimiters*/ );
std::wstring artist = mediaInfo.GetArtist().empty() ? emptyArtist : mediaInfo.GetArtist();
WideStringReplaceInvalidFilenameCharacters( artist, L"_", true /*replaceFolderDelimiters*/ );
std::wstring album = mediaInfo.GetAlbum().empty() ? emptyAlbum : mediaInfo.GetAlbum();
WideStringReplaceInvalidFilenameCharacters( album, L"_", true /*replaceFolderDelimiters*/ );
const std::wstring sourceFilename = std::filesystem::path( mediaInfo.GetFilename() ).stem();
std::wstringstream ss;
ss << std::setfill( static_cast<wchar_t>( '0' ) ) << std::setw( 2 ) << mediaInfo.GetTrack();
const std::wstring track = ss.str();
auto currentChar = extractFilename.begin();
while ( extractFilename.end() != currentChar ) {
switch ( *currentChar ) {
case '%' : {
const auto nextChar = 1 + currentChar;
if ( extractFilename.end() == nextChar ) {
outputFilename += *currentChar;
} else {
switch ( *nextChar ) {
case 'A' :
case 'a' : {
outputFilename += artist;
++currentChar;
break;
}
case 'D' :
case 'd' : {
outputFilename += album;
++currentChar;
break;
}
case 'N' :
case 'n' : {
outputFilename += track;
++currentChar;
break;
}
case 'T' :
case 't' : {
outputFilename += title;
++currentChar;
break;
}
case 'F' :
case 'f' : {
outputFilename += sourceFilename;
++currentChar;
break;
}
default : {
outputFilename += *currentChar;
break;
}
}
}
break;
}
default : {
outputFilename += *currentChar;
break;
}
}
++currentChar;
}
// Sanitise folder and file names.
if ( !extractFolder.empty() && ( extractFolder.back() != '\\' ) ) {
extractFolder += '\\';
}
WideStringReplaceInvalidFilenameCharacters( outputFilename, L"_", false /*replaceFolderDelimiters*/ );
WideStringReplace( outputFilename, L"/", L"\\" );
const size_t firstPos = outputFilename.find_first_not_of( L"\\ " );
const size_t lastPos = outputFilename.find_last_not_of( L"\\ " );
if ( ( std::wstring::npos != firstPos ) && ( std::wstring::npos != lastPos ) ) {
outputFilename = outputFilename.substr( firstPos, 1 + lastPos );
} else {
outputFilename.clear();
}
if ( outputFilename.empty() ) {
outputFilename = std::to_wstring( mediaInfo.GetTrack() );
}
outputFilename = extractFolder + outputFilename;
// Create the output folder, if necessary.
size_t pos = outputFilename.find( '\\', extractFolder.size() );
while ( std::wstring::npos != pos ) {
const std::wstring folder = outputFilename.substr( 0, pos );
CreateDirectory( folder.c_str(), NULL /*attributes*/ );
pos = outputFilename.find( '\\', 1 + folder.size() );
};
return outputFilename;
}
void CDDAExtract::UpdateStatus()
{
const long currentTrack = m_StatusTrack.load();
const long currentPass = m_StatusPass.load();
if ( ( currentTrack != m_DisplayedTrack ) || ( currentPass != m_DisplayedPass ) ) {
m_DisplayedTrack = currentTrack;
m_DisplayedPass = currentPass;
const int bufSize = 128;
WCHAR buffer[ bufSize ];
std::wstring readStatus;
if ( s_ReadFinished == m_DisplayedTrack ) {
LoadString( m_hInst, IDS_EXTRACT_STATUS_READCOMPLETE, buffer, bufSize );
readStatus = buffer;
const HWND progressRead = GetDlgItem( m_hWnd, IDC_EXTRACT_PROGRESS_TRACK );
if ( nullptr != progressRead ) {
SendMessage( progressRead, PBM_SETPOS, m_ProgressRange, 0 );
}
} else {
if ( s_ReadFixingSectors == m_DisplayedPass ) {
LoadString( m_hInst, IDS_EXTRACT_STATUS_FIXING, buffer, bufSize );
readStatus = buffer;
WideStringReplace( readStatus, L"%", std::to_wstring( m_DisplayedTrack ) );
readStatus += L":";
} else {
LoadString( m_hInst, IDS_EXTRACT_STATUS_TRACK, buffer, bufSize );
std::wstring trackStr( buffer );
WideStringReplace( trackStr, L"%", std::to_wstring( m_DisplayedTrack ) );
LoadString( m_hInst, IDS_EXTRACT_STATUS_PASS, buffer, bufSize );
std::wstring passStr( buffer );
WideStringReplace( passStr, L"%", std::to_wstring( m_DisplayedPass ) );
readStatus = trackStr + L" (" + passStr + L"):";
}
}
SetDlgItemText( m_hWnd, IDC_EXTRACT_STATE_READ, readStatus.c_str() );
}
const HWND progressRead = GetDlgItem( m_hWnd, IDC_EXTRACT_PROGRESS_TRACK );
if ( nullptr != progressRead ) {
const long displayPosition = static_cast<long>( SendMessage( progressRead, PBM_GETPOS, 0, 0 ) );
const long currentPosition = static_cast<long>( m_ProgressRead * m_ProgressRange );
if ( currentPosition != displayPosition ) {
SendMessage( progressRead, PBM_SETPOS, currentPosition, 0 );
}
}
const HWND progressEncode = GetDlgItem( m_hWnd, IDC_EXTRACT_PROGRESS_TOTAL );
if ( nullptr != progressEncode ) {
const long displayPosition = static_cast<long>( SendMessage( progressEncode, PBM_GETPOS, 0, 0 ) );
const long currentPosition = static_cast<long>( m_ProgressEncode * m_ProgressRange );
if ( currentPosition != displayPosition ) {
SendMessage( progressEncode, PBM_SETPOS, currentPosition, 0 );
}
}
}
void CDDAExtract::WriteTrackTags( const std::wstring& filename, const MediaInfo& mediaInfo )
{
Tags tags;
const std::wstring& title = mediaInfo.GetTitle();
if ( !title.empty() ) {
tags.insert( Tags::value_type( Tag::Title, WideStringToUTF8( title ) ) );
}
const std::wstring& artist = mediaInfo.GetArtist();
if ( !artist.empty() ) {
tags.insert( Tags::value_type( Tag::Artist, WideStringToUTF8( artist ) ) );
}
const std::wstring& album = mediaInfo.GetAlbum();
if ( !album.empty() ) {
tags.insert( Tags::value_type( Tag::Album, WideStringToUTF8( album ) ) );
}
const std::wstring& genre = mediaInfo.GetGenre();
if ( !genre.empty() ) {
tags.insert( Tags::value_type( Tag::Genre, WideStringToUTF8( genre ) ) );
}
const std::wstring& comment = mediaInfo.GetComment();
if ( !comment.empty() ) {
tags.insert( Tags::value_type( Tag::Comment, WideStringToUTF8( comment ) ) );
}
const std::string track = ( mediaInfo.GetTrack() > 0 ) ? std::to_string( mediaInfo.GetTrack() ) : std::string();
if ( !track.empty() ) {
tags.insert( Tags::value_type( Tag::Track, track ) );
}
const std::string year = ( mediaInfo.GetYear() > 0 ) ? std::to_string( mediaInfo.GetYear() ) : std::string();
if ( !year.empty() ) {
tags.insert( Tags::value_type( Tag::Year, year ) );
}
const std::string gainTrack = GainToString( mediaInfo.GetGainTrack() );
if ( !gainTrack.empty() ) {
tags.insert( Tags::value_type( Tag::GainTrack, gainTrack ) );
}
const std::vector<BYTE> imageBytes = m_Library.GetMediaArtwork( mediaInfo );
if ( !imageBytes.empty() ) {
const std::string encodedArtwork = Base64Encode( &imageBytes[ 0 ], static_cast<int>( imageBytes.size() ) );
tags.insert( Tags::value_type( Tag::Artwork, encodedArtwork ) );
}
if ( !tags.empty() ) {
m_EncoderHandler->SetTags( filename, tags );
}
}
void CDDAExtract::WriteAlbumTags( const std::wstring& filename, const MediaInfo& mediaInfo )
{
Tags tags;
const std::string gainAlbum = GainToString( mediaInfo.GetGainAlbum() );
if ( !gainAlbum.empty() ) {
tags.insert( Tags::value_type( Tag::GainAlbum, gainAlbum ) );
}
if ( !tags.empty() ) {
m_EncoderHandler->SetTags( filename, tags );
}
}