-
Notifications
You must be signed in to change notification settings - Fork 3
/
TagLib+CoverArt.cpp
102 lines (92 loc) · 3.32 KB
/
TagLib+CoverArt.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
//
// TagLib+AIFFCoverArt.cpp
// Loopseque Twins
//
// Created by Павел Литвиненко on 23.10.14.
// Copyright (c) 2014 Casual Underground. All rights reserved.
//
//_______________________________________________________________________________________________________________
#include "TagLib+CoverArt.h"
#include <id3v2tag.h>
#include <tag.h>
#include <aifffile.h>
#include <attachedpictureframe.h>
//_______________________________________________________________________________________________________________
static const char *kArtworkId = "APIC";
//_______________________________________________________________________________________________________________
Boolean get_attached_picture_frame(TagLib::ID3v2::Tag *tag, TagLib::ID3v2::AttachedPictureFrame **frame)
{
// Check if tag is exists
if (tag != NULL)
{
TagLib::ID3v2::FrameList list = tag->frameListMap()[kArtworkId];
TagLib::ID3v2::AttachedPictureFrame *_frame;
if (!list.isEmpty())
{
for(TagLib::ID3v2::FrameList::ConstIterator it = list.begin(); it != list.end(); ++it)
{
_frame = (TagLib::ID3v2::AttachedPictureFrame *)(*it);
if (_frame->picture().size() > 0)
{
*frame = _frame;
return TRUE;
}
}
}
}
return FALSE;
}
//_______________________________________________________________________________________________________________
Boolean CopyCoverArtDataFromFileAtPath(CFStringRef path, CFDataRef *data)
{
// Open file
const char *cString = CFStringGetCStringPtr(path, kCFStringEncodingUTF8);
TagLib::RIFF::AIFF::File audioFile(cString);
// Get file tag
TagLib::ID3v2::Tag *tag = audioFile.tag();
// Check if tag is exists
if (tag != NULL)
{
TagLib::ID3v2::AttachedPictureFrame *frame;
if (get_attached_picture_frame(tag, &frame))
{
// Init data
*data = CFDataCreate(kCFAllocatorDefault,
(const UInt8*)frame->picture().data(),
frame->picture().size());
}
return *data != NULL;
}
return FALSE;
}
Boolean SetCoverArtDataToFileAtPath(CFStringRef path, CFDataRef data)
{
// Open file
const char *cString = CFStringGetCStringPtr(path, kCFStringEncodingUTF8);
TagLib::RIFF::AIFF::File audioFile(cString);
// Get attached picture frame
TagLib::ID3v2::Tag *tag = audioFile.tag();
TagLib::ID3v2::AttachedPictureFrame *frame;
// Set data
if (tag != NULL && data != NULL)
{
const UInt8 *bytes = CFDataGetBytePtr(data);
TagLib::ByteVector imageData((const char *)bytes, (uint)CFDataGetLength(data));
// Create new frame
if (!get_attached_picture_frame(tag, &frame))
{
frame = new TagLib::ID3v2::AttachedPictureFrame;
frame->setMimeType("image/jpeg");
frame->setPicture(imageData);
tag->addFrame(frame);
}
// Update picture frame
else
{
frame->setMimeType("image/jpeg");
frame->setPicture(imageData);
}
}
return audioFile.save();
}
//_______________________________________________________________________________________________________________