forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandlerWavpack.cpp
187 lines (165 loc) · 4.97 KB
/
HandlerWavpack.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
#include "HandlerWavpack.h"
#include "resource.h"
#include "DecoderWavpack.h"
#include "Utility.h"
#include <array>
#include <fstream>
// Supported tags and their WavPack equivalents.
static const std::map<Tag,std::string> s_SupportedTags = {
{ Tag::Album, "ALBUM" },
{ Tag::Artist, "ARTIST" },
{ Tag::Comment, "COMMENT" },
{ Tag::GainAlbum, "REPLAYGAIN_ALBUM_GAIN" },
{ Tag::GainTrack, "REPLAYGAIN_TRACK_GAIN" },
{ Tag::Genre, "GENRE" },
{ Tag::Title, "TITLE" },
{ Tag::Track, "TRACKNUMBER" },
{ Tag::Year, "DATE" }
};
HandlerWavpack::HandlerWavpack()
{
}
HandlerWavpack::~HandlerWavpack()
{
}
std::wstring HandlerWavpack::GetDescription() const
{
const std::wstring description = L"WavPack " + UTF8ToWideString( WavpackGetLibraryVersionString() );
return description;
}
std::set<std::wstring> HandlerWavpack::GetSupportedFileExtensions() const
{
const std::set<std::wstring> filetypes = { L"wv" };
return filetypes;
}
bool HandlerWavpack::GetTags( const std::wstring& filename, Tags& tags ) const
{
bool success = false;
bool foundHeader = false;
if ( std::ifstream testStream( filename, std::ios::binary | std::ios::in ); testStream.is_open() ) {
std::array<char, 4> header = {};
testStream.read( header.data(), 4 );
foundHeader = ( 'w' == header[ 0 ] ) && ( 'v' == header[ 1 ] ) && ( 'p' == header[ 2 ] ) && ( 'k' == header[ 3 ] );
}
if ( foundHeader ) {
char* error = nullptr;
const int flags = OPEN_TAGS | OPEN_WVC | OPEN_NORMALIZE | OPEN_DSD_AS_PCM | OPEN_FILE_UTF8;
const int offset = 0;
WavpackContext* context = WavpackOpenFileInput( WideStringToUTF8( filename ).c_str(), error, flags, offset );
if ( nullptr != context ) {
for ( const auto& tagIter : s_SupportedTags ) {
const std::string& tagField = tagIter.second;
const int tagLength = WavpackGetTagItem( context, tagField.c_str(), nullptr /*buffer*/, 0 /*bufferSize*/ );
if ( tagLength > 0 ) {
std::vector<char> buffer( tagLength + 1, 0 );
if ( tagLength == WavpackGetTagItem( context, tagField.c_str(), buffer.data(), tagLength + 1 ) ) {
tags.insert( Tags::value_type( tagIter.first, buffer.data() ) );
}
}
}
const unsigned char format = WavpackGetFileFormat( context );
UINT stringID = 0;
switch ( format ) {
case WP_FORMAT_WAV : {
const int mode = WavpackGetMode( context );
stringID = ( mode & MODE_LOSSLESS ) ? ( ( mode & MODE_HYBRID ) ? IDS_WAVPACK_HYBRID : IDS_WAVPACK_LOSSLESS ) : IDS_WAVPACK_LOSSY;
break;
}
case WP_FORMAT_W64 : {
stringID = IDS_WAVPACK_W64;
break;
}
case WP_FORMAT_CAF : {
stringID = IDS_WAVPACK_CAF;
break;
}
case WP_FORMAT_DFF : {
stringID = IDS_WAVPACK_DFF;
break;
}
case WP_FORMAT_DSF : {
stringID = IDS_WAVPACK_DSD;
break;
}
default : {
break;
}
}
if ( 0 != stringID ) {
const int bufferSize = 32;
char buffer[ bufferSize ] = {};
if ( 0 != LoadStringA( GetModuleHandle( NULL ), stringID, buffer, bufferSize ) ) {
tags.insert( Tags::value_type( Tag::Version, buffer ) );
}
}
WavpackCloseFile( context );
success = true;
}
}
return success;
}
bool HandlerWavpack::SetTags( const std::wstring& filename, const Tags& tags ) const
{
bool success = false;
char* error = nullptr;
const int flags = OPEN_TAGS | OPEN_EDIT_TAGS | OPEN_WVC | OPEN_NORMALIZE | OPEN_DSD_AS_PCM | OPEN_FILE_UTF8;
const int offset = 0;
WavpackContext* context = WavpackOpenFileInput( WideStringToUTF8( filename ).c_str(), error, flags, offset );
if ( nullptr != context ) {
success = true;
bool writeTags = false;
for ( const auto& tagIter : tags ) {
const Tag tagField = tagIter.first;
const auto mapIter = s_SupportedTags.find( tagField );
if ( s_SupportedTags.end() != mapIter ) {
const std::string& fieldName = mapIter->second;
const std::string& value = tagIter.second;
if ( value.empty() ) {
WavpackDeleteTagItem( context, fieldName.c_str() );
writeTags = true;
} else {
WavpackAppendTagItem( context, fieldName.c_str(), value.c_str(), static_cast<int>( value.size() ) );
writeTags = true;
}
}
}
if ( writeTags ) {
success = ( 0 != WavpackWriteTag( context ) );
}
WavpackCloseFile( context );
}
return success;
}
Decoder::Ptr HandlerWavpack::OpenDecoder( const std::wstring& filename ) const
{
DecoderWavpack* decoderWavpack = nullptr;
try {
decoderWavpack = new DecoderWavpack( filename );
} catch ( const std::runtime_error& ) {
}
const Decoder::Ptr stream( decoderWavpack );
return stream;
}
Encoder::Ptr HandlerWavpack::OpenEncoder() const
{
return nullptr;
}
bool HandlerWavpack::IsDecoder() const
{
return true;
}
bool HandlerWavpack::IsEncoder() const
{
return false;
}
bool HandlerWavpack::CanConfigureEncoder() const
{
return false;
}
bool HandlerWavpack::ConfigureEncoder( const HINSTANCE /*instance*/, const HWND /*parent*/, std::string& /*settings*/ ) const
{
return false;
}
void HandlerWavpack::SettingsChanged( Settings& /*settings*/ )
{
}