forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportMP3.cpp
More file actions
1105 lines (909 loc) · 27.3 KB
/
Copy pathImportMP3.cpp
File metadata and controls
1105 lines (909 loc) · 27.3 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
/**********************************************************************
Audacity: A Digital Audio Editor
ImportMP3.cpp
Joshua Haberman
Leland Lucius
*//****************************************************************//**
\class MP3ImportFileHandle
\brief An ImportFileHandle for MP3 data
Audacity has finally moved to using a single mp3 library on all
platforms! It is the high performance, beautifully written libmad
(mpeg audio decoder). Finally there is harmony in the mp3 universe.
Much of this source code is based on 'minimad.c' as distributed
with libmad.
*//****************************************************************//**
\class MP3ImportPlugin
\brief An ImportPlugin for MP3 data
*//*******************************************************************/
#include "../Audacity.h" // for USE_* macros
#include <wx/defs.h>
#include "Import.h"
#include "ImportPlugin.h"
#include "../Project.h"
#define DESC XO("MP3 files")
static const auto exts =
{
wxT("mp3"),
wxT("mp2"),
wxT("mpa")
};
#ifndef USE_LIBMAD
static Importer::RegisteredUnusableImportPlugin registered
{
std::make_unique<UnusableImportPlugin>(DESC, FileExtensions(exts.begin(), exts.end()))
};
#else
#if defined(__WXMSW__)
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <wx/file.h>
#include <wx/string.h>
#include "../Prefs.h"
#include "../Tags.h"
#include "../WaveTrack.h"
#include "../widgets/AudacityMessageBox.h"
#include "../widgets/ProgressDialog.h"
// PRL: include these last,
// and correct some preprocessor namespace pollution from wxWidgets that
// caused a warning about duplicate definition
#undef SIZEOF_LONG
extern "C"
{
#include "mad.h"
#ifdef USE_LIBID3TAG
#include <id3tag.h>
#endif
}
// Specifies the number of bytes in the input buffer. This also controls
// how many bytes will be scanned when searching for the first MP3 frame.
#define INPUT_BUFFER_SIZE 65535
// This is the number of decoded samples libmad adds at the beginning
// (This is an "observed" value.)
#define MAD_DELAY 529
class MP3ImportPlugin final : public ImportPlugin
{
public:
MP3ImportPlugin();
~MP3ImportPlugin();
wxString GetPluginStringID() override;
TranslatableString GetPluginFormatDescription() override;
std::unique_ptr<ImportFileHandle> Open(const FilePath &Filename, AudacityProject*) override;
};
using NewChannelGroup = std::vector< std::shared_ptr<WaveTrack> >;
class MP3ImportFileHandle final : public ImportFileHandle
{
public:
MP3ImportFileHandle(const FilePath &filename);
~MP3ImportFileHandle();
TranslatableString GetFileDescription() override;
ByteCount GetFileUncompressedBytes() override;
ProgressResult Import(WaveTrackFactory *trackFactory, TrackHolders &outTracks, Tags *tags) override;
wxInt32 GetStreamCount() override;
const TranslatableStrings &GetStreamInfo() override;
void SetStreamUsage(wxInt32 StreamID, bool Use) override;
private:
bool Open();
void CheckTags();
void CheckAPETags(bool atEnd);
void CheckID3V1Tags();
void CheckID3V2Tags(bool atEnd);
void CheckLyrics();
bool CheckMP3();
bool FillBuffer();
void LoadID3(Tags *tags);
// The MAD callbacks
static mad_flow input_cb(void *that,
struct mad_stream *stream);
mad_flow InputCB(struct mad_stream *stream);
static mad_flow filter_cb(void *that,
struct mad_stream const *stream,
struct mad_frame *frame);
mad_flow FilterCB(struct mad_stream const *stream, struct mad_frame *frame);
static mad_flow output_cb(void *that,
struct mad_header const *header,
struct mad_pcm *pcm);
mad_flow OutputCB(struct mad_header const *header, struct mad_pcm *pcm);
static mad_flow error_cb(void *that,
struct mad_stream *stream,
struct mad_frame *frame);
mad_flow ErrorCB(struct mad_stream *stream, struct mad_frame *frame);
private:
mad_decoder mDecoder;
wxFile mFile;
wxFileOffset mFilePos;
wxFileOffset mFileLen;
unsigned char mInputBuffer[INPUT_BUFFER_SIZE + MAD_BUFFER_GUARD];
int mInputBufferLen;
WaveTrackFactory *mTrackFactory;
NewChannelGroup mChannels;
unsigned mNumChannels;
ProgressResult mUpdateResult;
int mDelay;
int mPadding;
bool mHaveID3;
friend MP3ImportPlugin;
};
// ============================================================================
// MP3ImportPlugin
// ============================================================================
MP3ImportPlugin::MP3ImportPlugin()
: ImportPlugin(FileExtensions(exts.begin(), exts.end()))
{
}
MP3ImportPlugin::~MP3ImportPlugin()
{
}
wxString MP3ImportPlugin::GetPluginStringID()
{
return wxT("libmad");
}
TranslatableString MP3ImportPlugin::GetPluginFormatDescription()
{
return DESC;
}
std::unique_ptr<ImportFileHandle> MP3ImportPlugin::Open(
const FilePath &Filename, AudacityProject *)
{
auto handle = std::make_unique<MP3ImportFileHandle>(Filename);
if (!handle->Open())
{
return nullptr;
}
return handle;
}
static Importer::RegisteredImportPlugin registered
{
"MP3",
std::make_unique<MP3ImportPlugin>()
};
// ============================================================================
// MP3ImportFileHandle
// ============================================================================
MP3ImportFileHandle::MP3ImportFileHandle(const FilePath &filename)
: ImportFileHandle(filename)
{
}
MP3ImportFileHandle::~MP3ImportFileHandle()
{
}
TranslatableString MP3ImportFileHandle::GetFileDescription()
{
return DESC;
}
auto MP3ImportFileHandle::GetFileUncompressedBytes() -> ByteCount
{
// TODO
return 0;
}
wxInt32 MP3ImportFileHandle::GetStreamCount()
{
return 1;
}
const TranslatableStrings &MP3ImportFileHandle::GetStreamInfo()
{
static TranslatableStrings empty;
return empty;
}
void MP3ImportFileHandle::SetStreamUsage(wxInt32 WXUNUSED(StreamID), bool WXUNUSED(Use))
{
}
ProgressResult MP3ImportFileHandle::Import(WaveTrackFactory *trackFactory,
TrackHolders &outTracks,
Tags *tags)
{
outTracks.clear();
CreateProgress();
mTrackFactory = trackFactory;
mUpdateResult = ProgressResult::Success;
mNumChannels = 0;
// Set delay and padding to best possible in case the LAME tag is not present
mDelay = MAD_DELAY;
mPadding = 0;
// Initialize decoder
mad_decoder_init(&mDecoder, this, input_cb, 0, filter_cb, output_cb, error_cb, 0);
// Send the decoder on its way!
auto res = mad_decoder_run(&mDecoder, MAD_DECODER_MODE_SYNC);
// Terminate decoder
mad_decoder_finish(&mDecoder);
// Decoding failed, so pass it on
if (res != 0)
{
return ProgressResult::Failed;
}
// The user canceled the decoding, so bail without saving tracks or tags
if (mUpdateResult == ProgressResult::Cancelled)
{
return mUpdateResult;
}
// Flush and trim the channels
for (const auto &channel : mChannels)
{
channel->Flush();
// Trim any padding
if (mPadding)
{
double et = channel->GetEndTime();
double t1 = et - channel->LongSamplesToTime(mPadding);
channel->Clear(t1, et);
}
// And delay
if (mDelay)
{
double st = channel->GetStartTime();
double t0 = st + channel->LongSamplesToTime(mDelay);
channel->Clear(st, t0);
}
}
// Copy the WaveTrack pointers into the Track pointer list that
// we are expected to fill
outTracks.push_back(std::move(mChannels));
// Load ID3 tags from the file
LoadID3(tags);
return mUpdateResult;
}
bool MP3ImportFileHandle::Open()
{
mInputBufferLen = 0;
mFilePos = 0;
mHaveID3 = false;
// Open the file
if (!mFile.Open(mFilename))
{
return false;
}
// Get the length of the file
mFileLen = mFile.Seek(0, wxFromEnd);
if (mFileLen == wxInvalidOffset || mFile.Error())
{
mFile.Close();
return false;
}
if (mFile.Seek(0, wxFromStart) == wxInvalidOffset || mFile.Error())
{
mFile.Close();
return false;
}
// Check for ID3 tags
CheckTags();
// Scan for the first MP3 frame
if (!CheckMP3())
{
mFile.Close();
return false;
}
return true;
}
void MP3ImportFileHandle::CheckTags()
{
// We do this twice to allow them to be in any order
for (int i = 0; i < 2; ++i)
{
CheckAPETags(false);
CheckID3V2Tags(false);
}
// We do this twice to allow them to be in any order. Even though ID3v1 is
// supposed to at the end, some apps put the v2 tags after the v1 tags.
for (int i = 0; i < 2; ++i)
{
CheckAPETags(true);
CheckID3V1Tags();
CheckLyrics();
CheckID3V2Tags(true);
}
return;
}
void MP3ImportFileHandle::CheckAPETags(bool atEnd)
{
int offset = atEnd ? mFileLen - 32 : mFilePos;
// Ensure file is positioned to start of (possible) tags
if (mFile.Seek(offset, wxFromStart) == wxInvalidOffset || mFile.Error())
{
return;
}
// An APE tag header is 32 bytes
if (mFile.Read(mInputBuffer, 32) != 32 || mFile.Error())
{
return;
}
// Do we have an APE preamble?
if (memcmp(mInputBuffer, "APETAGEX", 8) != 0)
{
return;
}
// Get the (little endian) length
wxFileOffset len = (mInputBuffer[12] & 0xff) |
((mInputBuffer[13] & 0xff) << 8) |
((mInputBuffer[14] & 0xff) << 16) |
((mInputBuffer[15] & 0xff) << 24);
// Get needed flags
bool hasHeader = mInputBuffer[23] & 0x80;
// Skip the tags
if (!atEnd)
{
mFilePos += (32 + len);
}
else
{
mFileLen -= ((hasHeader ? 32 : 0) + len);
}
}
void MP3ImportFileHandle::CheckID3V1Tags()
{
// Ensure file is positioned to start of (possible) tags
if (mFile.Seek(mFileLen - 128, wxFromStart) == wxInvalidOffset || mFile.Error())
{
return;
}
// An ID3v1 tag header is 3 bytes
if (mFile.Read(mInputBuffer, 3) != 3 || mFile.Error())
{
return;
}
// Do we have ID3v1 tags?
if (memcmp(mInputBuffer, "TAG", 3) != 0)
{
return;
}
// Adjust file length
mFileLen -= 128;
// Remember that we have tags
mHaveID3 = true;
}
void MP3ImportFileHandle::CheckLyrics()
{
int offset = mFileLen - 9;
// Ensure file is positioned to start of (possible) lyrics
if (mFile.Seek(offset, wxFromStart) == wxInvalidOffset || mFile.Error())
{
return;
}
// An Lyrics3 footeris 9 bytes
if (mFile.Read(mInputBuffer, 9) != 9 || mFile.Error())
{
return;
}
// Found a v1 Lyrics footer?
if (memcmp(mInputBuffer, "LYRICSEND", 9) == 0)
{
wxFileOffset pos = wxMax(offset - 5100, 0);
size_t len = offset - pos;
// Ensure file is positioned to start of (possible) lyrics
if (mFile.Seek(pos, wxFromStart) == wxInvalidOffset || mFile.Error())
{
return;
}
// Read the lyrics
if (mFile.Read(mInputBuffer, len) != len || mFile.Error())
{
return;
}
// Search forward to find the beginning of the lyrics
for (size_t i = 0; i < len; ++i)
{
if (memcmp(&mInputBuffer[i], "LYRICSBEGIN", 11) == 0)
{
// Adjust the file length to exclude the lyrics
mFileLen = pos + i;
break;
}
}
}
// Found a v2 Lyrics footer?
else if (memcmp(mInputBuffer, "LYRICS200", 9) == 0)
{
// Ensure file is positioned to start of (possible) lyrics
if (mFile.Seek(-15, wxFromCurrent) == wxInvalidOffset || mFile.Error())
{
return;
}
// An Lyrics3v2 length is 6 bytes
if (mFile.Read(mInputBuffer, 6) != 6 || mFile.Error())
{
return;
}
// Adjust the file length to exclude the lyrics
mInputBuffer[6] = 0;
mFileLen -= (wxAtoi((char *) mInputBuffer) + 15);
}
}
void MP3ImportFileHandle::CheckID3V2Tags(bool atEnd)
{
int offset = atEnd ? mFileLen - 10 : mFilePos;
// Ensure file is positioned to start of (possible) tags
if (mFile.Seek(offset, wxFromStart) == wxInvalidOffset || mFile.Error())
{
return;
}
// An ID3v2 tag header is 10 bytes
if (mFile.Read(mInputBuffer, 10) != 10 || mFile.Error())
{
return;
}
// Do we have an ID3v2 header or footer?
if (memcmp(mInputBuffer, atEnd ? "3DI" : "ID3", 3) != 0)
{
return;
}
// Get and decode the length
wxFileOffset len = (mInputBuffer[6] & 0x7f);
len = (len << 7) | (mInputBuffer[7] & 0x7f);
len = (len << 7) | (mInputBuffer[8] & 0x7f);
len = (len << 7) | (mInputBuffer[9] & 0x7f);
// Skip the tags
if (!atEnd)
{
mFilePos += (10 + len);
}
else
{
mFileLen -= (10 + len + 10);
}
// Remember that we have tags
mHaveID3 = true;
}
bool MP3ImportFileHandle::CheckMP3()
{
wxFileOffset savedPos = mFilePos;
// Ensure file is positioned to start of 1st mp3 frame
if (mFile.Seek(mFilePos, wxFromStart) == wxInvalidOffset || mFile.Error())
{
return false;
}
// Load as much as will fit into the buffer
if (!FillBuffer())
{
return false;
}
// Initialize mad stream
mad_stream stream;
mad_stream_init(&stream);
mad_stream_buffer(&stream, mInputBuffer, mInputBufferLen);
// And header
mad_header header;
mad_header_init(&header);
// Scan the input buffer for 2 consecutive MP3 frames. When the header
// decoder finds a frame, it decodes it and ensures it is followed by
// another frame or EOF...thus 2 (or 1) consecutive frame(s) are detected.
int consecutive = 1;
while (consecutive > 0)
{
// Decode the header at the current stream position.
if (mad_header_decode(&header, &stream))
{
// End of buffer.
if (stream.error != MAD_ERROR_NONE)
{
break;
}
}
consecutive -= 1;
}
// Remember how many bytes were processed
int used = stream.this_frame - stream.buffer;
// Cleanup
mad_header_finish(&header);
mad_stream_finish(&stream);
// Did we find all that we wanted?
if (consecutive)
{
return false;
}
// Reset file controls
mInputBufferLen = 0;
// Reposition file to start of mp3 frames to prepare for the Import.
mFilePos = savedPos + used;
if (mFile.Seek(mFilePos, wxFromStart) == wxInvalidOffset || mFile.Error())
{
return false;
}
// Looks like an MP3...
return true;
}
bool MP3ImportFileHandle::FillBuffer()
{
// We either want enough to fill the input buffer or what's left in the file
auto want = wxMin(INPUT_BUFFER_SIZE - mInputBufferLen, mFileLen - mFilePos);
if (want > 0)
{
// We should always get what we ask for
auto got = mFile.Read(&mInputBuffer[mInputBufferLen], want);
if (got != want || mFile.Error())
{
return false;
}
// Adjust input control
mInputBufferLen += got;
mFilePos += got;
}
// MAD requires that we add MAD_BUFFER_GUARD extra bytes when we've processed
// all of the MP3 frames. Otherwise, we will drop the last frame.
if (mFilePos == mFileLen)
{
memset(&mInputBuffer[mInputBufferLen], 0, MAD_BUFFER_GUARD);
mInputBufferLen += MAD_BUFFER_GUARD;
}
return true;
}
void MP3ImportFileHandle::LoadID3(Tags *tags)
{
#ifdef USE_LIBID3TAG
struct id3_file *id3file = NULL;
auto cleanup = finally([&]
{
if (id3file)
{
id3_file_close(id3file);
}
});
// Use id3_file_fdopen() instead of id3_file_open since wxWidgets can open a
// file with a Unicode name and id3_file_open() can't (under Windows).
id3file = id3_file_fdopen(mFile.fd(), ID3_FILE_MODE_READONLY);
if (!id3file)
{
return;
}
// The file descriptor is now owned by "id3file", so we must tell "mFile" to forget
// about it.
mFile.Detach();
// Load the tags
struct id3_tag *id3tags = id3_file_tag(id3file);
if (!id3tags || id3tags->nframes == 0)
{
return;
}
// Convert from libid3tag's ucs4 type to wxString.
//
// The ucs4 type is unsigned long which can be 8 bytes instead
// of the expected 4 bytes for a UTF-32 character, so we have
// to convert to unsigned int and then to wxString.
wxMBConvUTF32 converter;
auto toString = [=](const id3_ucs4_t *in)
{
// Count the number of characters
size_t len = 0;
for (const id3_ucs4_t *p = in; *p; p++)
{
len++;
}
// Would like to use std::dynarray or runtime-sized array,
// but VS doesn't support either.
wxUint32 *buf = (wxUint32 *) alloca((len + 1) * sizeof(wxUint32));
// Copy and convert to unsigned int
wxUint32 *out;
for (out = buf; *in; in++, out++)
{
*out = (wxUint32) (*in);
}
*out = 0;
// Finally convert to and return wxString
return wxString((char *) buf, converter);
};
tags->Clear();
// Extract tags from ID3 frames and add to our tags
bool have_year = false;
for (unsigned int i = 0; i < id3tags->nframes; ++i)
{
struct id3_frame *frame = id3tags->frames[i];
#if 0
wxLogDebug("ID: %08x '%4s'", (int) *(int *)frame->id, frame->id);
wxLogDebug("Desc: %s", frame->description);
wxLogDebug("Num fields: %d", frame->nfields);
for (unsigned int j = 0; j < frame->nfields; ++j)
{
wxLogDebug("field %d type %d", j, frame->fields[j].type);
if (frame->fields[j].type == ID3_FIELD_TYPE_STRINGLIST)
{
wxLogDebug("num strings %d", frame->fields[j].stringlist.nstrings);
}
}
#endif
wxString n;
wxString v;
// Determine the tag name
if (strcmp(frame->id, ID3_FRAME_TITLE) == 0)
{
n = TAG_TITLE;
}
else if (strcmp(frame->id, ID3_FRAME_ARTIST) == 0)
{
n = TAG_ARTIST;
}
else if (strcmp(frame->id, ID3_FRAME_ALBUM) == 0)
{
n = TAG_ALBUM;
}
else if (strcmp(frame->id, ID3_FRAME_TRACK) == 0)
{
n = TAG_TRACK;
}
else if (strcmp(frame->id, ID3_FRAME_YEAR) == 0)
{
// LLL: When libid3tag encounters the "TYER" tag, it converts it to a
// "ZOBS" (obsolete) tag and adds a "TDRC" tag at the end of the
// list of tags using the first 4 characters of the "TYER" tag.
// Since we write both the "TDRC" and "TYER" tags, the "TDRC" tag
// will always be encountered first in the list. We want to use
// it since the converted "TYER" tag may have been truncated.
if (have_year)
{
continue;
}
n = TAG_YEAR;
have_year = true;
}
else if (strcmp(frame->id, ID3_FRAME_COMMENT) == 0)
{
n = TAG_COMMENTS;
}
else if (strcmp(frame->id, ID3_FRAME_GENRE) == 0)
{
n = TAG_GENRE;
}
else
{
// Use frame description as default tag name. The descriptions
// may include several "meanings" separated by "/" characters, so
// we just use the first meaning
n = UTF8CTOWX(frame->description).BeforeFirst(wxT('/'));
}
// Now get the tag value
const id3_ucs4_t *ustr = NULL;
if (n == TAG_COMMENTS)
{
ustr = id3_field_getfullstring(&frame->fields[3]);
}
else if (frame->nfields == 3)
{
ustr = id3_field_getstring(&frame->fields[1]);
if (ustr)
{
n = toString(ustr);
}
ustr = id3_field_getstring(&frame->fields[2]);
}
else if (frame->nfields >= 2)
{
ustr = id3_field_getstrings(&frame->fields[1], 0);
}
// Convert the value
if (ustr)
{
v = toString(ustr);
}
// And add it to the list of tags
if (!n.empty() && !v.empty())
{
tags->SetTag(n, v);
}
}
// Convert v1 genre to name
if (tags->HasTag(TAG_GENRE))
{
long g = -1;
if (tags->GetTag(TAG_GENRE).ToLong(&g))
{
tags->SetTag(TAG_GENRE, tags->GetGenre(g));
}
}
#else
(void) tags;
#endif
}
//
// MAD Callbacks
//
// The input callback is called when the decoder wants more data
mad_flow MP3ImportFileHandle::input_cb(void *that,
struct mad_stream *stream)
{
auto cb = [&]()
{
return ((MP3ImportFileHandle *) that)->InputCB(stream);
};
return GuardedCall<mad_flow>(cb, MakeSimpleGuard(MAD_FLOW_BREAK));
}
mad_flow MP3ImportFileHandle::InputCB(struct mad_stream *stream)
{
// Update the progress
mUpdateResult = mProgress->Update((wxLongLong_t) mFilePos, (wxLongLong_t) mFileLen);
if (mUpdateResult != ProgressResult::Success)
{
return MAD_FLOW_STOP;
}
// Stop if we've consumed all of the MP3 data
if (mFilePos == mFileLen)
{
return MAD_FLOW_STOP;
}
// "Each time you refill your buffer, you need to preserve the data in
// your existing buffer from stream.next_frame to the end.
//
// This usually amounts to calling memmove() on this unconsumed portion
// of the buffer and appending NEW data after it, before calling
// mad_stream_buffer()
// -- Rob Leslie, on the mad-dev mailing list
if (stream->next_frame)
{
mInputBufferLen -= (stream->next_frame - mInputBuffer);
memmove(mInputBuffer, stream->next_frame, mInputBufferLen);
}
// Refill the buffer
if (!FillBuffer())
{
return MAD_FLOW_BREAK;
}
// And give it back to MAD
mad_stream_buffer(stream, mInputBuffer, mInputBufferLen);
return MAD_FLOW_CONTINUE;
}
// The filter callback lets us examine each frame and decide if it should be
// kept or tossed. We use this to detect the Xing or LAME tags.
mad_flow MP3ImportFileHandle::filter_cb(void *that,
struct mad_stream const *stream,
struct mad_frame *frame)
{
auto cb = [&]()
{
return ((MP3ImportFileHandle *) that)->FilterCB(stream, frame);
};
return GuardedCall<mad_flow>(cb, MakeSimpleGuard(MAD_FLOW_BREAK));
}
mad_flow MP3ImportFileHandle::FilterCB(struct mad_stream const *stream,
struct mad_frame *frame)
{
// We only want to jinspect the first frame, so disable future calls
mDecoder.filter_func = nullptr;
// Is it a VBRI info frame?
if (memcmp(&stream->this_frame[4 + 32], "VBRI", 4) == 0)
{
mDelay = (stream->this_frame[4 + 32 + 6] & 0xff) << 8 |
(stream->this_frame[4 + 32 + 7] & 0xff);
return MAD_FLOW_CONTINUE;
}
// Look for Xing/Info information
// Get the ancillary data ptr and length. If the frame has CRC protection, we make
// a small adjustment to get around an apparent bug in libmad.
auto ptr = stream->anc_ptr.byte - (frame->header.flags & MAD_FLAG_PROTECTION ? 2 : 0);
int len = stream->anc_bitlen / 8;
// Ensure it's something we can understand
if (len < 4 || (memcmp(ptr, "Xing", 4) != 0 && memcmp(ptr, "Info", 4) != 0))
{
return MAD_FLOW_CONTINUE;
}
// Skip the tag
ptr += 4;
len -= 4;
enum flagBits
{
hasFrames = 0x0001,
hasBytes = 0x0002,
hasToc = 0x0004,
hasScale = 0x0008
};
// Extract the flags
unsigned int flags = (((((ptr[0] << 8) + ptr[1]) << 8) + ptr[2]) << 8) + ptr[3];
ptr += 4;
len -= 4;
// Skip the number of frames
if (len >= 4 && flags & hasFrames)
{
ptr += 4;
len -= 4;
}
// Skip the number of bytes
if (len >= 4 && flags & hasBytes)
{
ptr += 4;
len -= 4;
}
// Skip the TOC
if (len >= 100 && flags & hasToc)
{
ptr += 100;
len -= 100;
}
// Skip the VBR Scale
if (len >= 4 && flags & hasScale)
{
ptr += 4;
len -= 4;
}
// Bail if LAME wasn't the encoder or we don't have enough ancillary data left
if (len < 24 || memcmp(ptr, "LAME", 4) != 0)
{
return MAD_FLOW_IGNORE;
}
// Skip down to the delay and padding
ptr += 21;
len -= 21;
// Extract the delay and padding and adjust for decoder delay
mDelay = (ptr[0] << 4) + (ptr[1] >> 4) + MAD_DELAY;