3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 - 2009 Christian Schoenebeck * |
* Copyright (C) 2003 - 2009 Christian Schoenebeck * |
6 |
* Copyright (C) 2009 - 2011 Grigor Iliev * |
* Copyright (C) 2009 - 2013 Grigor Iliev * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
27 |
|
|
28 |
#include <cstring> |
#include <cstring> |
29 |
|
|
30 |
|
#define CONVERT_BUFFER_SIZE 4096 |
31 |
|
|
32 |
namespace LinuxSampler { |
namespace LinuxSampler { |
33 |
#if CONFIG_DEVMODE |
#if CONFIG_DEVMODE |
34 |
int SampleFile_OpenFilesCount = 0; |
int SampleFile_OpenFilesCount = 0; |
37 |
SampleFile::SampleFile(String File, bool DontClose) { |
SampleFile::SampleFile(String File, bool DontClose) { |
38 |
this->File = File; |
this->File = File; |
39 |
this->pSndFile = NULL; |
this->pSndFile = NULL; |
40 |
|
pConvertBuffer = NULL; |
41 |
|
|
42 |
SF_INFO sfInfo; |
SF_INFO sfInfo; |
43 |
sfInfo.format = 0; |
sfInfo.format = 0; |
85 |
#endif |
#endif |
86 |
} |
} |
87 |
if(!DontClose) Close(); |
if(!DontClose) Close(); |
88 |
|
|
89 |
|
#if HAVE_DECL_SF_FORMAT_FLAC |
90 |
|
if (FrameSize == 3 * ChannelCount && |
91 |
|
(Format & SF_FORMAT_TYPEMASK) == SF_FORMAT_FLAC) { |
92 |
|
pConvertBuffer = new int[CONVERT_BUFFER_SIZE]; |
93 |
|
} |
94 |
|
#endif |
95 |
} |
} |
96 |
|
|
97 |
SampleFile::~SampleFile() { |
SampleFile::~SampleFile() { |
98 |
Close(); |
Close(); |
99 |
ReleaseSampleData(); |
ReleaseSampleData(); |
100 |
|
delete[] pConvertBuffer; |
101 |
} |
} |
102 |
|
|
103 |
void SampleFile::Open() { |
void SampleFile::Open() { |
181 |
|
|
182 |
if (GetPos() + FrameCount > GetTotalFrameCount()) FrameCount = GetTotalFrameCount() - GetPos(); // For the cases where a different sample end is specified (not the end of the file) |
if (GetPos() + FrameCount > GetTotalFrameCount()) FrameCount = GetTotalFrameCount() - GetPos(); // For the cases where a different sample end is specified (not the end of the file) |
183 |
|
|
184 |
// ogg files must be read with sf_readf, not sf_read_raw. On |
// ogg and flac files must be read with sf_readf, not |
185 |
// big endian machines, sf_readf_short is also used for 16 bit |
// sf_read_raw. On big endian machines, sf_readf_short is also |
186 |
// wav files, to get automatic endian conversion (for 24 bit |
// used for 16 bit wav files, to get automatic endian |
187 |
// samples this is handled in Synthesize::GetSample instead). |
// conversion (for 24 bit samples this is handled in |
188 |
|
// Synthesize::GetSample instead). |
189 |
|
|
190 |
#if WORDS_BIGENDIAN || HAVE_DECL_SF_FORMAT_VORBIS |
#if WORDS_BIGENDIAN || HAVE_DECL_SF_FORMAT_VORBIS || HAVE_DECL_SF_FORMAT_FLAC |
191 |
if ( |
if ( |
192 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
193 |
FrameSize == 2 * ChannelCount |
FrameSize == 2 * ChannelCount |
194 |
#else |
#else |
195 |
(Format & SF_FORMAT_SUBMASK) == SF_FORMAT_VORBIS |
#if HAVE_DECL_SF_FORMAT_VORBIS |
196 |
|
((Format & SF_FORMAT_SUBMASK) == SF_FORMAT_VORBIS) |
197 |
|
#if HAVE_DECL_SF_FORMAT_FLAC |
198 |
|
|| |
199 |
|
#endif |
200 |
|
#endif |
201 |
|
#if HAVE_DECL_SF_FORMAT_FLAC |
202 |
|
(FrameSize == 2 * ChannelCount && |
203 |
|
(Format & SF_FORMAT_TYPEMASK) == SF_FORMAT_FLAC) |
204 |
|
#endif |
205 |
#endif |
#endif |
206 |
) { |
) { |
207 |
return sf_readf_short(pSndFile, static_cast<short*>(pBuffer), FrameCount); |
return sf_readf_short(pSndFile, static_cast<short*>(pBuffer), FrameCount); |
208 |
|
#if HAVE_DECL_SF_FORMAT_FLAC |
209 |
|
} else if (FrameSize == 3 * ChannelCount && |
210 |
|
(Format & SF_FORMAT_TYPEMASK) == SF_FORMAT_FLAC) { |
211 |
|
// 24 bit flac needs to be converted from the 32 bit |
212 |
|
// integers returned by libsndfile |
213 |
|
int j = 0; |
214 |
|
sf_count_t count = FrameCount; |
215 |
|
const sf_count_t bufsize = CONVERT_BUFFER_SIZE / ChannelCount; |
216 |
|
unsigned char* const dst = static_cast<unsigned char*>(pBuffer); |
217 |
|
while (count > 0) { |
218 |
|
int n = sf_readf_int(pSndFile, pConvertBuffer, std::min(count, bufsize)); |
219 |
|
if (n <= 0) break; |
220 |
|
for (int i = 0 ; i < n * ChannelCount ; i++) { |
221 |
|
dst[j++] = pConvertBuffer[i] >> 8; |
222 |
|
dst[j++] = pConvertBuffer[i] >> 16; |
223 |
|
dst[j++] = pConvertBuffer[i] >> 24; |
224 |
|
} |
225 |
|
count -= n; |
226 |
|
} |
227 |
|
return FrameCount - count; |
228 |
|
#endif |
229 |
} else |
} else |
230 |
#endif |
#endif |
231 |
{ |
{ |