forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandlerMAC.cpp
137 lines (120 loc) · 3.72 KB
/
HandlerMAC.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
#include "HandlerMAC.h"
#include "DecoderMAC.h"
// Supported APE tags.
static const std::map<std::wstring,Tag> s_SupportedAPETags = {
{ L"ALBUM", Tag::Album },
{ L"ARTIST", Tag::Artist },
{ L"COMMENT", Tag::Comment },
{ L"REPLAYGAIN_ALBUM_GAIN", Tag::GainAlbum },
{ L"REPLAYGAIN_TRACK_GAIN", Tag::GainTrack },
{ L"GENRE", Tag::Genre },
{ L"TITLE", Tag::Title },
{ L"TRACKNUMBER", Tag::Track },
{ L"DATE", Tag::Year }
};
HandlerMAC::HandlerMAC()
{
}
HandlerMAC::~HandlerMAC()
{
}
std::wstring HandlerMAC::GetDescription() const
{
const std::wstring description = L"Monkey's Audio";
return description;
}
std::set<std::wstring> HandlerMAC::GetSupportedFileExtensions() const
{
const std::set<std::wstring> filetypes = { L"ape", L"apl" };
return filetypes;
}
bool HandlerMAC::GetTags( const std::wstring& filename, Tags& tags ) const
{
bool success = false;
std::unique_ptr<APE::IAPEDecompress> decompress( CreateIAPEDecompress( filename.c_str() ) );
if ( decompress ) {
auto apeTag = reinterpret_cast<APE::CAPETag*>( decompress->GetInfo( APE::APE_INFO_TAG ) );
if ( ( nullptr != apeTag ) && apeTag->GetHasAPETag() ) {
int tagIndex = 0;
auto tagField = apeTag->GetTagField( tagIndex );
while ( nullptr != tagField ) {
const auto fieldName = tagField->GetFieldName();
const auto tagIter = ( nullptr != fieldName ) ? s_SupportedAPETags.find( fieldName ) : s_SupportedAPETags.end();
if ( s_SupportedAPETags.end() != tagIter ) {
const auto fieldValue = tagField->GetFieldValue();
const int fieldValueSize = tagField->GetFieldValueSize();
if ( ( nullptr != fieldValue ) && ( fieldValueSize > 0 ) && tagField->GetIsUTF8Text() ) {
const std::string value( fieldValue, fieldValueSize );
if ( !value.empty() ) {
tags.insert( Tags::value_type( tagIter->second, value ) );
}
}
}
tagField = apeTag->GetTagField( ++tagIndex );
}
}
success = true;
}
return success;
}
bool HandlerMAC::SetTags( const std::wstring& filename, const Tags& tags ) const
{
bool success = false;
std::unique_ptr<APE::IAPEDecompress> decompress( CreateIAPEDecompress( filename.c_str(), nullptr /*errorCode*/, false /*readOnly*/ ) );
if ( decompress ) {
auto apeTag = reinterpret_cast<APE::CAPETag*>( decompress->GetInfo( APE::APE_INFO_TAG ) );
if ( nullptr != apeTag ) {
for ( const auto& tagIter : tags ) {
const Tag tagType = tagIter.first;
const std::string& tagValue = tagIter.second;
const auto fieldName = std::find_if( s_SupportedAPETags.begin(), s_SupportedAPETags.end(),
[ tagType ] ( const std::map<std::wstring,Tag>::value_type& entry )
{
return ( tagType == entry.second );
} );
if ( s_SupportedAPETags.end() != fieldName ) {
if ( tagValue.empty() ) {
apeTag->RemoveField( fieldName->first.c_str() );
} else {
apeTag->SetFieldString( fieldName->first.c_str(), tagIter.second.c_str(), true /*UTF-8*/ );
}
}
}
success = ( ERROR_SUCCESS == apeTag->Save() );
}
}
return success;
}
Decoder::Ptr HandlerMAC::OpenDecoder( const std::wstring& filename ) const
{
DecoderMAC* decoderMAC = nullptr;
try {
decoderMAC = new DecoderMAC( filename );
} catch ( const std::runtime_error& ) {
}
const Decoder::Ptr stream( decoderMAC );
return stream;
}
Encoder::Ptr HandlerMAC::OpenEncoder() const
{
return nullptr;
}
bool HandlerMAC::IsDecoder() const
{
return true;
}
bool HandlerMAC::IsEncoder() const
{
return false;
}
bool HandlerMAC::CanConfigureEncoder() const
{
return false;
}
bool HandlerMAC::ConfigureEncoder( const HINSTANCE /*instance*/, const HWND /*parent*/, std::string& /*settings*/ ) const
{
return false;
}
void HandlerMAC::SettingsChanged( Settings& /*settings*/ )
{
}