forked from Serial-ATA/lofty-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_tag.rs
132 lines (117 loc) · 3.19 KB
/
create_tag.rs
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
use lofty::ape::ApeTag;
use lofty::id3::v1::Id3v1Tag;
use lofty::id3::v2::Id3v2Tag;
use lofty::iff::aiff::AIFFTextChunks;
use lofty::iff::wav::RIFFInfoList;
use lofty::mp4::Ilst;
use lofty::ogg::VorbisComments;
use lofty::{Accessor, MimeType, Picture, PictureType, TagExt};
use criterion::{criterion_group, criterion_main, Criterion};
const ENCODER: &str = "Lavf57.56.101";
macro_rules! bench_tag_write {
($c:ident, [$(($NAME:literal, $tag:ty, |$tag_:ident| $extra_block:block)),+ $(,)?]) => {
let mut g = $c.benchmark_group("Tag writing");
$(
g.bench_function(
$NAME,
|b| b.iter(|| {
let mut v = Vec::new();
let mut $tag_ = <$tag>::default();
$tag_.set_artist(String::from("Dave Eddy"));
$tag_.set_title(String::from("TempleOS Hymn Risen (Remix)"));
$tag_.set_album(String::from("Summer"));
$tag_.set_year(2017);
$tag_.set_track(1);
$tag_.set_genre(String::from("Electronic"));
$extra_block
$tag_.dump_to(&mut v).unwrap();
})
);
)+
}
}
fn bench_write(c: &mut Criterion) {
bench_tag_write!(
c,
[
("AIFF Text Chunks", AIFFTextChunks, |tag| {}),
("APEv2", ApeTag, |tag| {
use lofty::ape::ApeItem;
use lofty::ItemValue;
let picture = Picture::new_unchecked(
PictureType::CoverFront,
MimeType::Jpeg,
None,
include_bytes!("../benches_assets/cover.jpg").to_vec(),
);
tag.insert(
ApeItem::new(
String::from("Cover (Front)"),
ItemValue::Binary(picture.as_ape_bytes()),
)
.unwrap(),
);
tag.insert(
ApeItem::new(
String::from("Encoder"),
ItemValue::Text(String::from(ENCODER)),
)
.unwrap(),
);
}),
("ID3v2", Id3v2Tag, |tag| {
use lofty::id3::v2::{Frame, FrameFlags, TextInformationFrame};
use lofty::TextEncoding;
let picture = Picture::new_unchecked(
PictureType::CoverFront,
MimeType::Jpeg,
None,
include_bytes!("../benches_assets/cover.jpg").to_vec(),
);
tag.insert_picture(picture);
tag.insert(
Frame::new(
"TSSE",
TextInformationFrame {
encoding: TextEncoding::Latin1,
value: String::from(ENCODER),
},
FrameFlags::default(),
)
.unwrap(),
);
}),
("ID3v1", Id3v1Tag, |tag| {}),
("MP4 Ilst", Ilst, |tag| {
use lofty::mp4::{Atom, AtomData, AtomIdent};
let picture = Picture::new_unchecked(
PictureType::CoverFront,
MimeType::Jpeg,
None,
include_bytes!("../benches_assets/cover.jpg").to_vec(),
);
tag.insert_picture(picture);
tag.insert(Atom::new(
AtomIdent::Fourcc(*b"\xa9too"),
AtomData::UTF8(String::from(ENCODER)),
));
}),
("RIFF INFO", RIFFInfoList, |tag| {
tag.insert(String::from("ISFT"), String::from(ENCODER));
}),
("Vorbis Comments", VorbisComments, |tag| {
use lofty::ogg::OggPictureStorage;
let picture = Picture::new_unchecked(
PictureType::CoverFront,
MimeType::Jpeg,
None,
include_bytes!("../benches_assets/cover.jpg").to_vec(),
);
let _ = tag.insert_picture(picture, None).unwrap();
tag.push(String::from("ENCODER"), String::from(ENCODER));
}),
]
);
}
criterion_group!(benches, bench_write);
criterion_main!(benches);