forked from pure-data/pure-data
/
ceammc_loader_sndfile.cpp
394 lines (329 loc) · 12.9 KB
/
ceammc_loader_sndfile.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
/*****************************************************************************
* Copyright 2017 Serge Poltavsky. All rights reserved.
*
* This file may be distributed under the terms of GNU Public License version
* 3 (GPL v3) as defined by the Free Software Foundation (FSF). A copy of the
* license should have been included with this file, or the project in which
* this file belongs to. You may also find the details of GPL v3 at:
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* If you have any questions regarding the use of this file, feel free to
* contact the author of this file, or the owner of the project in which
* this file belongs to.
*****************************************************************************/
#include "ceammc_loader_sndfile.h"
#include "ceammc_config.h"
#include "fmt/core.h"
#include "soxr.h"
#include <cmath>
#include <iostream>
namespace ceammc {
namespace sound {
LibSndFile::LibSndFile()
{
}
bool LibSndFile::probe(const char* fname) const
{
SndfileHandle sf(fname, SFM_READ);
return sf.rawHandle();
}
bool LibSndFile::open(const char* fname, OpenMode mode, const SoundFileOpenParams& params)
{
auto fmt = makeLibFormat(params.file_format, params.sample_format);
switch (mode) {
case WRITE: {
if (!handle_.formatCheck(fmt, params.num_channels, params.samplerate)) {
error(fmt::format("[sndfile] invalid options for format {} (0x{:0x}): "
"num_channels={} samplerate={}",
to_string(params.file_format), fmt, params.num_channels, params.samplerate));
return false;
}
handle_ = SndfileHandle(fname, SFM_WRITE, fmt, params.num_channels, params.samplerate);
if (handle_.rawHandle() == 0) {
error(fmt::format("[sndfile] can't open file '{}' for writing. {}", fname, handle_.strError()));
return false;
}
} break;
case READ:
// auto detect soundfile format
handle_ = SndfileHandle(fname, SFM_READ, fmt, params.num_channels, params.samplerate);
if (handle_.rawHandle() == 0) {
error(fmt::format("[sndfile] can't open file '{}' for reading. {}", fname, handle_.strError()));
return false;
}
break;
case NONE:
default:
return false;
}
setOpenMode(mode);
fname_ = fname;
return true;
}
size_t LibSndFile::frameCount() const
{
return handle_.frames();
}
size_t LibSndFile::sampleRate() const
{
return handle_.samplerate();
}
size_t LibSndFile::channels() const
{
return handle_.channels();
}
bool LibSndFile::isOpened() const
{
return handle_.rawHandle() != NULL;
}
bool LibSndFile::close()
{
fname_.clear();
handle_ = SndfileHandle();
return true;
}
std::int64_t LibSndFile::read(t_word* dest, size_t sz, size_t ch, std::int64_t offset)
{
if (!isOpened()) {
error(fmt::format("[sndfile] not opened"));
return -1;
}
if (ch >= channels()) {
error(fmt::format("[sndfile] invalid channel number: {}", ch));
return -1;
}
if (resampleRatio() != 1)
return readResampled(dest, sz, ch, offset, sz);
t_word* x = dest;
constexpr sf_count_t FRAME_COUNT = 256;
const int n = channels();
const sf_count_t IN_BUF_SIZE = FRAME_COUNT * n;
float frame_buf[IN_BUF_SIZE];
// move to beginning
if (handle_.seek(offset, SEEK_SET) == -1) {
error(fmt::format("[sndfile] can't seek to sample: {}", offset));
return -1;
}
// read frames
sf_count_t frames_read_total = 0;
const sf_count_t steps = sf_count_t(sz) / FRAME_COUNT;
// read full buffers
for (sf_count_t i = 0; i < steps; i++) {
const auto frames_read = handle_.readf(frame_buf, FRAME_COUNT);
if (frames_read == 0)
break;
// write channel data to destination
for (sf_count_t j = 0; (j < frames_read) && (frames_read_total < sz); j++) {
x->w_float = frame_buf[j * n + ch] * gain();
x++;
frames_read_total++;
}
// seek to next
if (handle_.seek(offset + frames_read_total, SEEK_SET) == -1)
break;
}
// read remaining
if (sf_count_t(sz) % FRAME_COUNT != 0) {
const auto frames_read = handle_.readf(frame_buf, sf_count_t(sz) % FRAME_COUNT);
// write channel data to destination
for (sf_count_t j = 0; (j < frames_read) && (frames_read_total < sz); j++) {
x->w_float = frame_buf[j * n + ch] * gain();
x++;
frames_read_total++;
}
}
return frames_read_total;
}
std::int64_t LibSndFile::write(const t_word* const* src, size_t num_frames, std::int64_t offset)
{
if (!isOpened()) {
error(fmt::format("[sndfile] not opened"));
return -1;
}
constexpr sf_count_t FRAME_COUNT = 256;
const auto NCH = channels();
const sf_count_t OUT_BUF_SIZE = FRAME_COUNT * NCH;
float frame_buf[OUT_BUF_SIZE];
size_t buf_frames = 0;
std::int64_t samples_written = 0;
for (size_t i = 0; i < num_frames; i++, buf_frames++) {
// fill frame
for (size_t j = 0; j < NCH; j++) {
auto samp = src[j][i].w_float * gain();
frame_buf[(i % FRAME_COUNT) * NCH + j] = samp;
}
// frame buffer is full
if (buf_frames == FRAME_COUNT) {
samples_written += handle_.write(frame_buf, buf_frames * NCH);
buf_frames = 0;
}
}
// write remaining frames
if (buf_frames > 0)
samples_written += handle_.write(frame_buf, buf_frames * NCH);
handle_.writeSync();
handle_ = {};
return samples_written;
}
std::int64_t LibSndFile::readFrames(float* dest, size_t sz, std::int64_t offset)
{
if (!(isOpened() && openMode() == READ)) {
error(fmt::format("[sndfile] not opened for reading"));
return -1;
}
// move to beginning
if (handle_.seek(offset, SEEK_SET) == -1) {
error(fmt::format("[sndfile] can't seek to frame: {}", offset));
return -1;
}
return handle_.readf(dest, sz);
}
std::int64_t LibSndFile::readResampled(t_word* dest, size_t sz, size_t ch, long offset, size_t max_samples)
{
if (resampleRatio() < 0.001) {
error(fmt::format("[sndfile] invalid resample ratio: {}", resampleRatio()));
return -1;
}
t_word* x = dest;
const sf_count_t FRAME_COUNT = 256;
const int n = channels();
const sf_count_t IN_BUF_SIZE = FRAME_COUNT * n;
float frame_buf[IN_BUF_SIZE];
// move to beginning
if (handle_.seek(offset, SEEK_SET) == -1) {
error(fmt::format("[sndfile] can't seek to sample: {}", offset));
return -1;
}
// SoxR
soxr_error_t err;
soxr_t soxr = soxr_create(
1, resampleRatio(), n, /* Input rate, output rate, # of channels. */
&err, /* To report any error during creation. */
nullptr, nullptr, nullptr); /* Use configuration defaults.*/
if (err) {
error(fmt::format("[soxr] init error: {}", soxr_strerror(err)));
soxr_delete(soxr);
return -1;
}
const size_t OUT_FRAME_COUNT = std::round(FRAME_COUNT * resampleRatio());
const size_t OUT_BUF_SIZE = std::round(IN_BUF_SIZE * resampleRatio());
float resampled_buf[OUT_BUF_SIZE];
// read frames
sf_count_t frames_read_total = 0;
std::int64_t frames_resampled_total = 0;
const sf_count_t steps = sf_count_t(sz) / FRAME_COUNT;
// read full buffers
for (sf_count_t i = 0; i < steps; i++) {
const auto frames_read = handle_.readf(frame_buf, FRAME_COUNT);
if (frames_read == 0)
break;
/*
* Copy data from the input buffer into the resampler, and resample
* to produce as much output as is possible to the given output buffer:
*/
size_t odone;
err = soxr_process(soxr, frame_buf, frames_read, nullptr, resampled_buf, OUT_FRAME_COUNT, &odone);
if (err) {
error(fmt::format("[soxr] error: {}", err));
break;
}
frames_read_total += frames_read;
// write channel data to destination
for (sf_count_t j = 0; j < odone && frames_resampled_total < max_samples; j++) {
x->w_float = resampled_buf[j * n + ch] * gain();
x++;
frames_resampled_total++;
}
// seek to next
if (handle_.seek(offset + frames_read_total, SEEK_SET) == -1)
break;
}
// read remaining file samples
if (sf_count_t(sz) % FRAME_COUNT != 0) {
const sf_count_t frames_read = handle_.readf(frame_buf, sf_count_t(sz) % FRAME_COUNT);
size_t odone;
err = soxr_process(soxr, frame_buf, frames_read, nullptr, resampled_buf, OUT_FRAME_COUNT, &odone);
if (!err) {
// write channel data to destination
for (sf_count_t j = 0; j < odone && frames_resampled_total < max_samples; j++) {
x->w_float = resampled_buf[j * n + ch] * gain();
x++;
}
} else
error(fmt::format("[soxr] error: {}", err));
}
// process remaining resample data
while (true) {
size_t odone = 0;
// indicate end of input with nullptr
err = soxr_process(soxr, nullptr, 0, nullptr, resampled_buf, OUT_FRAME_COUNT, &odone);
if (err) {
error(fmt::format("[soxr] error: {}", err));
break;
}
if (odone == 0)
break;
// write channel data to destination
for (sf_count_t j = 0; j < odone && frames_resampled_total < max_samples; j++) {
x->w_float = resampled_buf[j * n + ch] * gain();
x++;
frames_resampled_total++;
}
}
soxr_delete(soxr);
return frames_resampled_total;
}
int LibSndFile::makeLibFormat(SoundFileFormat fileFormat, SampleFormat sampFormat) const
{
int fmt = 0;
// clang-format off
switch (fileFormat) {
case FORMAT_WAV: fmt = SF_FORMAT_WAV; break;
case FORMAT_AIFF: fmt = SF_FORMAT_AIFF; break;
case FORMAT_OGG: fmt = SF_FORMAT_OGG | SF_FORMAT_VORBIS; break;
#ifdef CEAMMC_HAVE_LIBSNDFILE_OPUS
case FORMAT_OPUS: fmt = SF_FORMAT_OGG | SF_FORMAT_OPUS; break;
#endif
case FORMAT_RAW: fmt = SF_FORMAT_RAW; break;
case FORMAT_FLAC: fmt = SF_FORMAT_FLAC; break;
default:
return fmt;
}
switch(sampFormat) {
case SAMPLE_PCM_8: fmt |= SF_FORMAT_PCM_S8; break;
case SAMPLE_PCM_16: fmt |= SF_FORMAT_PCM_16; break;
case SAMPLE_PCM_24: fmt |= SF_FORMAT_PCM_24; break;
case SAMPLE_PCM_32: fmt |= SF_FORMAT_PCM_32; break;
case SAMPLE_PCM_FLOAT: fmt |= SF_FORMAT_FLOAT; break;
default:
switch(fileFormat) {
case FORMAT_WAV: fmt |= SF_FORMAT_PCM_24; break;
case FORMAT_AIFF: fmt |= SF_FORMAT_PCM_24; break;
case FORMAT_RAW: fmt |= SF_FORMAT_PCM_24; break;
case FORMAT_FLAC: fmt |= SF_FORMAT_PCM_24; break;
default: break;
}
break;
}
return fmt;
// clang-format on
}
FormatList LibSndFile::supportedReadFormats()
{
FormatList res;
SF_FORMAT_INFO format_info;
int count;
sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(int));
for (int k = 0; k < count; k++) {
format_info.format = k;
sf_command(0, SFC_GET_FORMAT_MAJOR, &format_info, sizeof(format_info));
std::string finfo(format_info.name);
std::string::size_type space_pos = finfo.find(' ', 0);
std::string name = finfo.substr(0, space_pos);
std::string desc = finfo.substr(space_pos + 0, std::string::npos);
res.push_back(std::make_pair(name, desc));
};
return res;
}
}
}