-
Notifications
You must be signed in to change notification settings - Fork 3
/
LibSNDWrapper.cpp
202 lines (165 loc) · 5.94 KB
/
LibSNDWrapper.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
#include "LibSNDWrapper.h"
#include "Config.h"
#include "Common.h"
#include "CommonExceptions.h"
#include "AtomicWrite.h"
#include <cstring>
#include <thread> // std::this_thread::sleep_for
#include <chrono>
LibSNDWrapper::LibSNDWrapper(string filename) : StandardWrapper(filename)
{
this->initAttr();
}
LibSNDWrapper::LibSNDWrapper(string filename, Nullable<size_t> offset, Nullable<size_t> len) : StandardWrapper(filename, offset, len)
{
this->initAttr();
}
void LibSNDWrapper::initAttr()
{
this->Format.SampleFormat = SampleFormat_t::int32;
}
LibSNDWrapper::~LibSNDWrapper ()
{
this->releaseBuffer();
this->close();
}
void LibSNDWrapper::open ()
{
// avoid multiple calls to open()
if(this->sndfile!=nullptr)
{
return;
}
memset(&sfinfo, 0, sizeof (sfinfo));
if ( (this->sndfile = sf_open (this->Filename.c_str(), SFM_READ, &sfinfo))==nullptr )
{
THROW_RUNTIME_ERROR(sf_strerror (nullptr) << " (in File \"" << this->Filename << ")\"");
};
if (sfinfo.channels < 1)
{
THROW_RUNTIME_ERROR("channels == " << sfinfo.channels);
};
// dont know how many voices there can be, must be adjusted by user
this->Format.SetVoices(1);
this->Format.VoiceChannels[0] = sfinfo.channels;
this->Format.SampleRate = sfinfo.samplerate;
// set scale factor for file containing floats as recommended by:
// http://www.mega-nerd.com/libsndfile/api.html#note2
switch(this->sfinfo.format & SF_FORMAT_SUBMASK)
{
case SF_FORMAT_FLOAT:
[[fallthrough]];
case SF_FORMAT_DOUBLE:
sf_command(this->sndfile, SFC_SET_SCALE_FLOAT_INT_READ, nullptr, SF_TRUE);
break;
default:
break;
}
}
void LibSNDWrapper::close() noexcept
{
if(this->sndfile!=nullptr)
{
sf_close (this->sndfile);
this->sndfile=nullptr;
}
}
void LibSNDWrapper::fillBuffer()
{
if(this->data==nullptr)
{
if(this->fileOffset.hasValue)
{
sf_seek(this->sndfile, msToFrames(this->fileOffset.Value, this->Format.SampleRate), SEEK_SET);
}
}
StandardWrapper::fillBuffer(this);
}
void LibSNDWrapper::render(pcm_t* bufferToFill, frame_t framesToRender)
{
// unfortuately libsndfile does not provide fixed integer width API
// we can only ask to read ints from it which we then have to force to int32
// so here we have to handle the case for architectures where int is 64 bit wide
constexpr bool haveInt32 = sizeof(int)==4;
constexpr bool haveInt64 = sizeof(int)==8;
static_assert(haveInt32 || haveInt64, "sizeof(int) is neither 4 nor 8 bits on your platform");
if(haveInt32)
{
// no extra work necessary here, as usual write directly to pcm buffer
STANDARDWRAPPER_RENDER(int32_t, sf_read_int(this->sndfile, pcm, framesToDoNow*Channels))
}
else if(haveInt64)
{
// allocate an extra tempbuffer to store the int64s in
// then convert them to int32
int* int64_temp_buf = new int[gConfig.FramesToRender*this->Format.Channels()];
STANDARDWRAPPER_RENDER( int32_t,
sf_read_int(this->sndfile, int64_temp_buf, framesToDoNow*Channels);
for(unsigned int i=0; i<framesToDoNow*Channels; i++)
{
// see http://www.mega-nerd.com/libsndfile/api.html#note1:
// Whenever integer data is moved from one sized container to another sized container, the
// most significant bit in the source container will become the most significant bit in the destination container.
pcm[i] = static_cast<int32_t>(int64_temp_buf[i] >> 32);
}
)
delete [] int64_temp_buf;
}
}
vector<loop_t> LibSNDWrapper::getLoopArray () const noexcept
{
vector<loop_t> res;
if(res.empty())
{
SF_INSTRUMENT inst;
int ret = sf_command (this->sndfile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) ;
if(ret == SF_TRUE && inst.loop_count > 0)
{
for (int i=0; i<inst.loop_count; i++)
{
loop_t l;
l.start = inst.loops[i].start;
l.stop = inst.loops[i].end;
// WARNING: AGAINST RIFF SPEC ahead!!!
// quoting RIFFNEW.pdf: "dwEnd: Specifies the endpoint of the loop
// in samples (this sample will also be played)."
// however (nearly) every piece of software out there ignores that and
// specifies the sample excluded from the loop
// THUS: submit to peer pressure
l.stop -= 1;
l.count = inst.loops[i].count;
res.push_back(l);
}
}
}
return res;
}
frame_t LibSNDWrapper::getFrames () const
{
int framesAvail = this->sfinfo.frames;
if(this->fileOffset.hasValue)
{
framesAvail -= msToFrames(this->fileOffset.Value, this->Format.SampleRate);
}
if(framesAvail < 0)
{
framesAvail=0;
}
frame_t totalFrames = this->fileLen.hasValue ? msToFrames(this->fileLen.Value, this->Format.SampleRate) : framesAvail;
if(totalFrames > framesAvail)
{
totalFrames = framesAvail;
}
return totalFrames;
}
void LibSNDWrapper::buildMetadata() noexcept
{
#define READ_METADATA(name, id) if(sf_get_string(this->sndfile, id) != nullptr) (name = string(sf_get_string(this->sndfile, id)))
READ_METADATA (this->Metadata.Title, SF_STR_TITLE);
READ_METADATA (this->Metadata.Artist, SF_STR_ARTIST);
READ_METADATA (this->Metadata.Year, SF_STR_DATE);
READ_METADATA (this->Metadata.Album, SF_STR_ALBUM);
READ_METADATA (this->Metadata.Genre, SF_STR_GENRE);
READ_METADATA (this->Metadata.Track, SF_STR_TRACKNUMBER);
READ_METADATA (this->Metadata.Comment, SF_STR_COMMENT);
}