forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandlerBass.cpp
396 lines (365 loc) · 12.3 KB
/
HandlerBass.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
#include "HandlerBass.h"
#include "DecoderBass.h"
#include "Settings.h"
#include "Utility.h"
#include "vcedit.h"
#include <list>
#include <sstream>
std::set<std::wstring> HandlerBass::s_SupportedFileExtensions( { L"mod", L"s3m", L"xm", L"it", L"mtm", L"mo3", L"umx", L"mp2", L"mp3", L"ogg", L"wav", L"mid", L"midi", L"dsd", L"dsf", L"wma", L"wmv" } );
HandlerBass::HandlerBass() :
Handler(),
m_BassMidi( BASS_PluginLoad( L"bassmidi.dll", BASS_UNICODE ) ),
m_BassDSD( BASS_PluginLoad( L"bassdsd.dll", BASS_UNICODE ) ),
m_BassHLS( BASS_PluginLoad( L"basshls.dll", BASS_UNICODE ) ),
m_BassMidiSoundFont( 0 ),
m_SoundFontFilename()
{
}
HandlerBass::~HandlerBass()
{
if ( 0 != m_BassMidiSoundFont ) {
BASS_MIDI_FontFree( m_BassMidiSoundFont );
m_BassMidiSoundFont = 0;
}
if ( 0 != m_BassMidi ) {
BASS_PluginFree( m_BassMidi );
m_BassMidi = 0;
}
if ( 0 != m_BassDSD ) {
BASS_PluginFree( m_BassDSD );
m_BassDSD = 0;
}
if ( 0 != m_BassHLS ) {
BASS_PluginFree( m_BassHLS );
m_BassHLS = 0;
}
}
std::wstring HandlerBass::GetDescription() const
{
const DWORD version = BASS_GetVersion();
const DWORD d = version & 0xff;
const DWORD c = ( version >> 8 ) & 0xff;
const DWORD b = ( version >> 16 ) & 0xff;
const DWORD a = ( version >> 24 ) & 0xff;
std::wstringstream string;
string << L"BASS " << a << L"." << b << L"." << c << L"." << d;
return string.str();
}
std::set<std::wstring> HandlerBass::GetSupportedFileExtensions() const
{
return s_SupportedFileExtensions;
}
bool HandlerBass::GetTags( const std::wstring& filename, Tags& tags ) const
{
bool success = false;
tags.clear();
DWORD flags = BASS_UNICODE | BASS_MUSIC_NOSAMPLE;
const HMUSIC music = BASS_MusicLoad( FALSE /*mem*/, filename.c_str(), 0 /*offset*/, 0 /*length*/, flags, 0 /*freq*/ );
if ( music != 0 ) {
const char* author = BASS_ChannelGetTags( music, BASS_TAG_MUSIC_AUTH );
if ( author != nullptr ) {
tags.insert( Tags::value_type( Tag::Artist, author ) );
}
const char* title = BASS_ChannelGetTags( music, BASS_TAG_MUSIC_NAME );
if ( title != nullptr ) {
tags.insert( Tags::value_type( Tag::Title, title ) );
}
const char* comment = BASS_ChannelGetTags( music, BASS_TAG_MUSIC_MESSAGE );
if ( comment != nullptr ) {
tags.insert( Tags::value_type( Tag::Comment, comment ) );
}
BASS_MusicFree( music );
success = true;
} else {
flags = BASS_UNICODE;
const HSTREAM stream = BASS_StreamCreateFile( FALSE /*mem*/, filename.c_str(), 0 /*offset*/, 0 /*length*/, flags );
if ( stream != 0 ) {
BASS_CHANNELINFO info = {};
BASS_ChannelGetInfo( stream, &info );
if ( BASS_CTYPE_STREAM_OGG == info.ctype ) {
const char* oggTags = BASS_ChannelGetTags( stream, BASS_TAG_OGG );
if ( nullptr != oggTags ) {
ReadOggTags( oggTags, tags );
success = true;
}
} else if ( BASS_CTYPE_STREAM_MIDI == info.ctype ) {
const char* midiTags = BASS_ChannelGetTags( stream, BASS_TAG_MIDI_TRACK );
if ( ( nullptr != midiTags ) && ( strlen( midiTags ) > 0 ) ) {
tags.insert( Tags::value_type( Tag::Title, midiTags ) );
success = true;
}
} else if ( BASS_CTYPE_STREAM_DSD == info.ctype ) {
const char* dsdArtist = BASS_ChannelGetTags( stream, BASS_TAG_DSD_ARTIST );
if ( ( nullptr != dsdArtist ) && ( strlen( dsdArtist ) > 0 ) ) {
tags.insert( Tags::value_type( Tag::Artist, dsdArtist ) );
success = true;
}
const char* dsdTitle = BASS_ChannelGetTags( stream, BASS_TAG_DSD_TITLE );
if ( ( nullptr != dsdTitle ) && ( strlen( dsdTitle ) > 0 ) ) {
tags.insert( Tags::value_type( Tag::Title, dsdTitle ) );
success = true;
}
}
BASS_StreamFree( stream );
}
}
return success;
}
bool HandlerBass::SetTags( const std::wstring& filename, const Tags& tags ) const
{
bool success = false;
const DWORD flags = BASS_UNICODE | BASS_SAMPLE_FLOAT | BASS_STREAM_DECODE;
const HSTREAM handle = BASS_StreamCreateFile( FALSE /*mem*/, filename.c_str(), 0 /*offset*/, 0 /*length*/, flags );
if ( 0 != handle ) {
BASS_CHANNELINFO info = {};
BASS_ChannelGetInfo( handle, &info );
BASS_StreamFree( handle );
if ( BASS_CTYPE_STREAM_OGG == info.ctype ) {
success = WriteOggTags( filename, tags );
}
}
return success;
}
Decoder::Ptr HandlerBass::OpenDecoder( const std::wstring& filename ) const
{
DecoderBass* streamBass = nullptr;
try {
bool ignoreFile = false;
if ( s_SupportedFileExtensions.end() == s_SupportedFileExtensions.find( WideStringToLower( GetFileExtension( filename ) ) ) ) {
// Prevent executables from incorrectly being identified as streams.
FILE* f = nullptr;
if ( 0 == _wfopen_s( &f, filename.c_str(), L"rb" ) ) {
const int bufferSize = 2;
char buffer[ bufferSize ] = {};
fread_s( buffer, bufferSize, 1 /*size*/, 2 /*count*/, f );
fclose( f );
ignoreFile = ( "MZ" == std::string( buffer, 2 ) );
}
}
if ( !ignoreFile ) {
streamBass = new DecoderBass( filename );
}
} catch ( const std::runtime_error& ) {
}
const Decoder::Ptr stream( streamBass );
return stream;
}
Encoder::Ptr HandlerBass::OpenEncoder() const
{
return nullptr;
}
bool HandlerBass::IsDecoder() const
{
return true;
}
bool HandlerBass::IsEncoder() const
{
return false;
}
bool HandlerBass::CanConfigureEncoder() const
{
return false;
}
bool HandlerBass::ConfigureEncoder( const HINSTANCE /*instance*/, const HWND /*parent*/, std::string& /*settings*/ ) const
{
return false;
}
void HandlerBass::ReadOggTags( const char* oggTags, Tags& tags ) const
{
if ( nullptr != oggTags ) {
const char* currentTag = oggTags;
while ( 0 != *currentTag ) {
const size_t tagLength = strlen( currentTag );
const std::string tag = currentTag;
const size_t pos = tag.find( '=' );
if ( std::string::npos != pos ) {
const std::string field = tag.substr( 0 /*offset*/, pos );
const std::string value = tag.substr( 1 + pos /*offset*/ );
if ( !field.empty() && !value.empty() ) {
if ( 0 == _stricmp( field.c_str(), "ARTIST" ) ) {
tags.insert( Tags::value_type( Tag::Artist, value ) );
} else if ( 0 == _stricmp( field.c_str(), "TITLE" ) ) {
tags.insert( Tags::value_type( Tag::Title, value ) );
} else if ( 0 == _stricmp( field.c_str(), "ALBUM" ) ) {
tags.insert( Tags::value_type( Tag::Album, value ) );
} else if ( 0 == _stricmp( field.c_str(), "GENRE" ) ) {
tags.insert( Tags::value_type( Tag::Genre, value ) );
} else if ( ( 0 == _stricmp( field.c_str(), "YEAR" ) ) || ( 0 == _stricmp( field.c_str(), "DATE" ) ) ) {
tags.insert( Tags::value_type( Tag::Year, value ) );
} else if ( 0 == _stricmp( field.c_str(), "COMMENT" ) ) {
tags.insert( Tags::value_type( Tag::Comment, value ) );
} else if ( ( 0 == _stricmp( field.c_str(), "TRACK" ) ) || ( 0 == _stricmp( field.c_str(), "TRACKNUMBER" ) ) ) {
tags.insert( Tags::value_type( Tag::Track, value ) );
} else if ( 0 == _stricmp( field.c_str(), "REPLAYGAIN_TRACK_GAIN" ) ) {
tags.insert( Tags::value_type( Tag::GainTrack, value ) );
} else if ( 0 == _stricmp( field.c_str(), "REPLAYGAIN_ALBUM_GAIN" ) ) {
tags.insert( Tags::value_type( Tag::GainAlbum, value ) );
}
}
}
currentTag += 1 + tagLength;
}
}
}
bool HandlerBass::WriteOggTags( const std::wstring& filename, const Tags& tags ) const
{
bool handled = true;
// Check if there are any supported tags.
for ( auto iter = tags.begin(); handled && ( iter != tags.end() ); iter++ ) {
const Tag tag = iter->first;
switch ( tag ) {
case Tag::Album :
case Tag::Artist :
case Tag::Comment :
case Tag::GainAlbum :
case Tag::GainTrack :
case Tag::Genre :
case Tag::Title :
case Tag::Track :
case Tag::Year : {
handled = false;
break;
}
default : {
break;
}
}
}
if ( !handled ) {
const std::wstring tempFilename = GetTemporaryFilename();
FILE* inputStream = nullptr;
if ( 0 == _wfopen_s( &inputStream, filename.c_str(), L"rb" ) ) {
FILE* outputStream = nullptr;
if ( 0 == _wfopen_s( &outputStream, tempFilename.c_str(), L"wb" ) ) {
vcedit_state* state = vcedit_new_state();
if ( 0 == vcedit_open( state, inputStream ) ) {
vorbis_comment* originalComments = vcedit_comments( state );
if ( nullptr != originalComments ) {
vorbis_comment modifiedComments = {};
vorbis_comment_init( &modifiedComments );
const std::string vendor = originalComments->vendor;
std::set<int> originalCommentsToDrop;
for ( const auto& tagIter : tags ) {
const Tag tag = tagIter.first;
const std::string& tagValue = tagIter.second;
std::list<std::string> tagFields;
switch ( tag ) {
case Tag::Album : {
tagFields = { "ALBUM" };
break;
}
case Tag::Artist : {
tagFields = { "ARTIST" };
break;
}
case Tag::Comment : {
tagFields = { "COMMENT" };
break;
}
case Tag::Genre : {
tagFields = { "GENRE" };
break;
}
case Tag::Title : {
tagFields = { "TITLE" };
break;
}
case Tag::Track : {
tagFields = { "TRACKNUMBER", "TRACK" };
break;
}
case Tag::Year : {
tagFields = { "DATE", "YEAR" };
break;
}
case Tag::GainAlbum : {
tagFields = { "REPLAYGAIN_ALBUM_GAIN" };
break;
}
case Tag::GainTrack : {
tagFields = { "REPLAYGAIN_TRACK_GAIN" };
break;
}
default : {
break;
}
}
if ( !tagFields.empty() ) {
for ( int index = 0; index < originalComments->comments; ++index ) {
const std::string comment = originalComments->user_comments[ index ];
const size_t delimiter = comment.find( '=' );
if ( ( std::string::npos != delimiter ) && ( delimiter > 0 ) ) {
const std::string field = StringToUpper( comment.substr( 0 /*offset*/, delimiter /*count*/ ) );
if ( tagFields.end() != std::find( tagFields.begin(), tagFields.end(), field ) ) {
originalCommentsToDrop.insert( index );
}
}
}
const std::string comment = tagFields.front() + '=' + tagValue;
vorbis_comment_add( &modifiedComments, comment.c_str() );
}
}
for ( int index = 0; index < originalComments->comments; ++index ) {
if ( originalCommentsToDrop.end() == originalCommentsToDrop.find( index ) ) {
vorbis_comment_add( &modifiedComments, originalComments->user_comments[ index ] );
}
}
vorbis_comment_clear( originalComments );
vorbis_comment_init( originalComments );
for ( int index = 0; index < modifiedComments.comments; ++index ) {
vorbis_comment_add( originalComments, modifiedComments.user_comments[ index ] );
}
originalComments->vendor = const_cast<char*>( vendor.c_str() );
if ( 0 == vcedit_write( state, outputStream ) ) {
handled = true;
}
originalComments->vendor = nullptr;
vorbis_comment_clear( originalComments );
vorbis_comment_clear( &modifiedComments );
}
}
vcedit_clear(state);
fclose( outputStream );
}
fclose( inputStream );
}
if ( handled ) {
if ( 0 == MoveFileEx( tempFilename.c_str(), filename.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH ) ) {
DeleteFile( tempFilename.c_str() );
handled = false;
}
}
}
return handled;
}
std::wstring HandlerBass::GetTemporaryFilename() const
{
std::wstring filename;
WCHAR pathName[ MAX_PATH ];
if ( 0 != GetTempPath( MAX_PATH, pathName ) ) {
filename = pathName + UTF8ToWideString( GenerateGUIDString() );
}
return filename;
}
void HandlerBass::SettingsChanged( Settings& settings )
{
LoadSoundFont( settings );
}
void HandlerBass::LoadSoundFont( Settings& settings )
{
if ( 0 != m_BassMidi ) {
const std::wstring filename = settings.GetSoundFont();
if ( filename != m_SoundFontFilename ) {
m_SoundFontFilename = filename;
if ( 0 != m_BassMidiSoundFont ) {
BASS_MIDI_FontFree( m_BassMidiSoundFont );
m_BassMidiSoundFont = 0;
}
m_BassMidiSoundFont = BASS_MIDI_FontInit( filename.c_str(), BASS_UNICODE );
if ( 0 != m_BassMidiSoundFont ) {
const BASS_MIDI_FONT font = { m_BassMidiSoundFont, -1 /*preset*/, 0 /*bank*/ };
BASS_MIDI_StreamSetFonts( 0 /*default*/, &font, 1 /*count*/ );
}
}
}
}