-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaygain_writer.cpp
283 lines (221 loc) · 6.86 KB
/
replaygain_writer.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
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <boost/algorithm/string/predicate.hpp>
#include <FLAC++/metadata.h>
#include "errors.hpp"
#include "replaygain_writer.hpp"
namespace flacsplit {
class Metadata_editor;
}
namespace {
inline flacsplit::Metadata_editor *
as_editor(FLAC__IOHandle handle) {
return reinterpret_cast<flacsplit::Metadata_editor *>(handle);
}
} // end anon
static int metadata_editor_close(FLAC__IOHandle);
static int metadata_editor_eof(FLAC__IOHandle);
static size_t metadata_editor_read(void *, size_t, size_t,
FLAC__IOHandle);
static int metadata_editor_seek(FLAC__IOHandle, FLAC__int64, int);
static FLAC__int64 metadata_editor_tell(FLAC__IOHandle);
static size_t metadata_editor_write(const void *, size_t, size_t,
FLAC__IOHandle);
namespace flacsplit {
class Metadata_editor {
public:
Metadata_editor(FILE *fp) :
_callbacks(make_callbacks()),
_fp(fp)
{
_chain.read(this, _callbacks);
}
virtual ~Metadata_editor() {}
std::unique_ptr<FLAC::Metadata::Iterator> iterator() {
auto iter = std::make_unique<FLAC::Metadata::Iterator>();
iter->init(_chain);
return iter;
}
bool check_if_tempfile_needed(bool use_padding) const {
return const_cast<FLAC::Metadata::Chain &>(
_chain).check_if_tempfile_needed(use_padding);
}
FLAC::Metadata::Chain::Status status() {
return _chain.status();
}
bool valid() const {
return _chain.is_valid();
}
bool write(bool use_padding) {
return _chain.write(use_padding, this, _callbacks);
}
virtual size_t read_callback(uint8_t *buf, size_t size, size_t nmemb) {
return fread(buf, size, nmemb, _fp);
}
virtual size_t write_callback(const uint8_t *buf, size_t size,
size_t nmemb) {
return fwrite(buf, size, nmemb, _fp);
}
virtual int seek_callback(off_t offset, int whence) {
long loffset = offset;
if (loffset != offset)
throw_traced(std::runtime_error("bad narrow cast"));
return fseek(_fp, offset, whence);
}
virtual off_t tell_callback() const {
return ftell(const_cast<FILE *>(_fp));
}
virtual int eof_callback() const {
return feof(const_cast<FILE *>(_fp));
}
virtual int close_callback() {
return 0;
}
private:
static ::FLAC__IOCallbacks make_callbacks() {
::FLAC__IOCallbacks callbacks;
callbacks.close = metadata_editor_close;
callbacks.eof = metadata_editor_eof;
callbacks.read = metadata_editor_read;
callbacks.seek = metadata_editor_seek;
callbacks.tell = metadata_editor_tell;
callbacks.write = metadata_editor_write;
return callbacks;
}
::FLAC__IOCallbacks _callbacks;
FLAC::Metadata::Chain _chain;
FILE *_fp;
};
class Replaygain_writer_impl : public Metadata_editor {
public:
Replaygain_writer_impl(FILE *fp) : Metadata_editor(fp) {}
void add_replaygain(const flacsplit::Replaygain_stats &);
void save();
private:
FLAC::Metadata::VorbisComment *find_comment();
};
} // end flacsplit
static int
metadata_editor_close(FLAC__IOHandle handle) {
return as_editor(handle)->close_callback();
}
static int
metadata_editor_eof(FLAC__IOHandle handle) {
return as_editor(handle)->eof_callback();
}
static size_t
metadata_editor_read(void *ptr, size_t size, size_t nmemb,
FLAC__IOHandle handle) {
uint8_t *buf = reinterpret_cast<uint8_t *>(ptr);
return as_editor(handle)->read_callback(buf, size, nmemb);
}
static int
metadata_editor_seek(FLAC__IOHandle handle, FLAC__int64 offset, int whence) {
return as_editor(handle)->seek_callback(offset, whence);
}
static FLAC__int64
metadata_editor_tell(FLAC__IOHandle handle) {
return as_editor(handle)->tell_callback();
}
static size_t
metadata_editor_write(const void *ptr, size_t size, size_t nmemb,
FLAC__IOHandle handle) {
const uint8_t *buf = reinterpret_cast<const uint8_t *>(ptr);
return as_editor(handle)->write_callback(buf, size, nmemb);
}
flacsplit::Replaygain_writer::Replaygain_writer(FILE *fp) :
_impl(new Replaygain_writer_impl(fp))
{}
flacsplit::Replaygain_writer::~Replaygain_writer() {}
void
flacsplit::Replaygain_writer::add_replaygain(
const flacsplit::Replaygain_stats &gain_stats) {
_impl->add_replaygain(gain_stats);
}
bool
flacsplit::Replaygain_writer::check_if_tempfile_needed() const {
return _impl->check_if_tempfile_needed(true);
}
void
flacsplit::Replaygain_writer::save() {
_impl->save();
}
FLAC::Metadata::VorbisComment *
flacsplit::Replaygain_writer_impl::find_comment() {
auto iter = iterator();
do {
FLAC::Metadata::VorbisComment *comment =
dynamic_cast<FLAC::Metadata::VorbisComment *>(
iter->get_block());
if (comment)
return comment;
} while (iter->next());
return nullptr;
}
inline void
flacsplit::Replaygain_writer_impl::add_replaygain(
const flacsplit::Replaygain_stats &gain_stats) {
FLAC::Metadata::VorbisComment *comment = find_comment();
if (!comment) {
throw_traced(std::runtime_error(
"Vorbis comment should have been present"
));
}
append_replaygain_tags(*comment, gain_stats);
}
inline void
flacsplit::Replaygain_writer_impl::save() {
write(true);
}
void
flacsplit::append_replaygain_tags(FLAC::Metadata::VorbisComment &comment,
const flacsplit::Replaygain_stats &gain_stats) {
using FLAC::Metadata::VorbisComment;
std::ostringstream formatter;
formatter << std::fixed;
formatter << std::setprecision(2) << std::showpos
<< gain_stats.album_gain() << " dB";
std::string album_gain = formatter.str();
formatter.str("");
formatter << std::setprecision(8) << std::noshowpos
<< gain_stats.album_peak();
std::string album_peak = formatter.str();
formatter.str("");
formatter << std::setprecision(1) << std::noshowpos
<< gain_stats.reference_loudness() << " dB";
std::string ref_loudness = formatter.str();
formatter.str("");
formatter << std::setprecision(2) << std::showpos
<< gain_stats.track_gain() << " dB";
std::string track_gain = formatter.str();
formatter.str("");
formatter << std::setprecision(8) << std::noshowpos
<< gain_stats.track_peak();
std::string track_peak = formatter.str();
comment.append_comment(VorbisComment::Entry(
"REPLAYGAIN_ALBUM_GAIN", album_gain.c_str()));
comment.append_comment(VorbisComment::Entry(
"REPLAYGAIN_ALBUM_PEAK", album_peak.c_str()));
comment.append_comment(VorbisComment::Entry(
"REPLAYGAIN_REFERENCE_LOUDNESS", ref_loudness.c_str()));
comment.append_comment(VorbisComment::Entry(
"REPLAYGAIN_TRACK_GAIN", track_gain.c_str()));
comment.append_comment(VorbisComment::Entry(
"REPLAYGAIN_TRACK_PEAK", track_peak.c_str()));
}
void
flacsplit::delete_replaygain_tags(FLAC::Metadata::VorbisComment &comment) {
using FLAC::Metadata::VorbisComment;
// move backwards so the entries don't get shifted on us
for (unsigned i = comment.get_num_comments(); i != 0;) {
i--;
VorbisComment::Entry entry = comment.get_comment(i);
if (boost::starts_with(entry.get_field_name(), "REPLAYGAIN_"))
comment.delete_comment(i);
}
}