2017-04-17 10:27:19 +00:00
|
|
|
/*
|
|
|
|
** sndfile_decoder.cpp
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2008-2010 Chris Robinson
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
2015-04-25 10:25:10 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-20 06:03:13 +00:00
|
|
|
#include "sndfile_decoder.h"
|
2015-04-25 15:50:57 +00:00
|
|
|
#include "templates.h"
|
2014-06-25 11:25:36 +00:00
|
|
|
#include "files.h"
|
2015-04-25 15:50:57 +00:00
|
|
|
#include "xs_Float.h"
|
2017-04-17 15:05:09 +00:00
|
|
|
#include "i_module.h"
|
|
|
|
#include "cmdlib.h"
|
2014-06-19 11:13:42 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SNDFILE
|
2014-09-13 18:51:53 +00:00
|
|
|
|
2017-04-17 15:05:09 +00:00
|
|
|
FModule SndFileModule{"SndFile"};
|
|
|
|
|
|
|
|
#include "sndload.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define SNDFILELIB "libsndfile-1.dll"
|
|
|
|
#elif defined(__APPLE__)
|
2017-04-17 16:21:57 +00:00
|
|
|
#define SNDFILELIB "libsndfile.1.dylib"
|
2017-04-17 15:05:09 +00:00
|
|
|
#else
|
|
|
|
#define SNDFILELIB "libsndfile.so.1"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool IsSndFilePresent()
|
|
|
|
{
|
|
|
|
#if !defined DYN_SNDFILE
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
static bool cached_result = false;
|
|
|
|
static bool done = false;
|
|
|
|
|
|
|
|
if (!done)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
cached_result = SndFileModule.Load({NicePath("$PROGDIR/" SNDFILELIB), SNDFILELIB});
|
|
|
|
}
|
|
|
|
return cached_result;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 11:13:42 +00:00
|
|
|
sf_count_t SndFileDecoder::file_get_filelen(void *user_data)
|
|
|
|
{
|
2014-06-25 11:25:36 +00:00
|
|
|
FileReader *reader = reinterpret_cast<SndFileDecoder*>(user_data)->Reader;
|
|
|
|
return reader->GetLength();
|
2014-06-19 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sf_count_t SndFileDecoder::file_seek(sf_count_t offset, int whence, void *user_data)
|
|
|
|
{
|
2014-06-25 11:25:36 +00:00
|
|
|
FileReader *reader = reinterpret_cast<SndFileDecoder*>(user_data)->Reader;
|
2014-06-19 11:13:42 +00:00
|
|
|
|
2015-04-24 15:42:56 +00:00
|
|
|
if(reader->Seek((long)offset, whence) != 0)
|
2014-06-19 11:13:42 +00:00
|
|
|
return -1;
|
2014-06-25 11:25:36 +00:00
|
|
|
return reader->Tell();
|
2014-06-19 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sf_count_t SndFileDecoder::file_read(void *ptr, sf_count_t count, void *user_data)
|
|
|
|
{
|
2014-06-25 11:25:36 +00:00
|
|
|
FileReader *reader = reinterpret_cast<SndFileDecoder*>(user_data)->Reader;
|
2015-04-24 15:42:56 +00:00
|
|
|
return reader->Read(ptr, (long)count);
|
2014-06-19 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sf_count_t SndFileDecoder::file_write(const void *ptr, sf_count_t count, void *user_data)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sf_count_t SndFileDecoder::file_tell(void *user_data)
|
|
|
|
{
|
2014-06-25 11:25:36 +00:00
|
|
|
FileReader *reader = reinterpret_cast<SndFileDecoder*>(user_data)->Reader;
|
|
|
|
return reader->Tell();
|
2014-06-19 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SndFileDecoder::~SndFileDecoder()
|
|
|
|
{
|
|
|
|
if(SndFile)
|
|
|
|
sf_close(SndFile);
|
|
|
|
SndFile = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-25 11:25:36 +00:00
|
|
|
bool SndFileDecoder::open(FileReader *reader)
|
2014-06-19 11:13:42 +00:00
|
|
|
{
|
2017-04-17 15:05:09 +00:00
|
|
|
if (!IsSndFilePresent()) return false;
|
|
|
|
|
|
|
|
SF_VIRTUAL_IO sfio = { file_get_filelen, file_seek, file_read, file_write, file_tell };
|
|
|
|
|
|
|
|
Reader = reader;
|
|
|
|
SndInfo.format = 0;
|
|
|
|
SndFile = sf_open_virtual(&sfio, SFM_READ, &SndInfo, this);
|
|
|
|
if (SndFile)
|
|
|
|
{
|
|
|
|
if (SndInfo.channels == 1 || SndInfo.channels == 2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
sf_close(SndFile);
|
|
|
|
SndFile = 0;
|
2015-04-25 15:50:57 +00:00
|
|
|
}
|
2014-06-20 00:50:03 +00:00
|
|
|
return false;
|
2014-06-19 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SndFileDecoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *type)
|
|
|
|
{
|
|
|
|
*samplerate = SndInfo.samplerate;
|
|
|
|
|
|
|
|
if(SndInfo.channels == 2)
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
else
|
|
|
|
*chans = ChannelConfig_Mono;
|
|
|
|
|
|
|
|
*type = SampleType_Int16;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SndFileDecoder::read(char *buffer, size_t bytes)
|
|
|
|
{
|
2014-06-27 08:29:34 +00:00
|
|
|
short *out = (short*)buffer;
|
|
|
|
size_t frames = bytes / SndInfo.channels / 2;
|
|
|
|
size_t total = 0;
|
|
|
|
|
|
|
|
// It seems libsndfile has a bug with converting float samples from Vorbis
|
|
|
|
// to the 16-bit shorts we use, which causes some PCM samples to overflow
|
|
|
|
// and wrap, creating static. So instead, read the samples as floats and
|
|
|
|
// convert to short ourselves.
|
|
|
|
// Use a loop to convert a handful of samples at a time, avoiding a heap
|
|
|
|
// allocation for temporary storage. 64 at a time works, though maybe it
|
|
|
|
// could be more.
|
|
|
|
while(total < frames)
|
|
|
|
{
|
2015-04-25 15:50:57 +00:00
|
|
|
size_t todo = MIN<size_t>(frames-total, 64/SndInfo.channels);
|
2014-06-27 08:29:34 +00:00
|
|
|
float tmp[64];
|
|
|
|
|
2015-04-24 15:42:56 +00:00
|
|
|
size_t got = (size_t)sf_readf_float(SndFile, tmp, todo);
|
2014-06-27 08:29:34 +00:00
|
|
|
if(got < todo) frames = total + got;
|
|
|
|
|
|
|
|
for(size_t i = 0;i < got*SndInfo.channels;i++)
|
2015-04-25 15:50:57 +00:00
|
|
|
*out++ = (short)xs_CRoundToInt(clamp(tmp[i] * 32767.f, -32768.f, 32767.f));
|
2014-06-27 08:29:34 +00:00
|
|
|
total += got;
|
|
|
|
}
|
|
|
|
return total * SndInfo.channels * 2;
|
2014-06-19 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 18:51:06 +00:00
|
|
|
TArray<uint8_t> SndFileDecoder::readAll()
|
2014-06-19 11:13:42 +00:00
|
|
|
{
|
|
|
|
if(SndInfo.frames <= 0)
|
|
|
|
return SoundDecoder::readAll();
|
|
|
|
|
|
|
|
int framesize = 2 * SndInfo.channels;
|
2017-04-26 18:51:06 +00:00
|
|
|
TArray<uint8_t> output;
|
2014-06-19 11:13:42 +00:00
|
|
|
|
2015-04-24 15:42:56 +00:00
|
|
|
output.Resize((unsigned)(SndInfo.frames * framesize));
|
2017-04-26 18:51:06 +00:00
|
|
|
size_t got = read((char*)&output[0], output.Size());
|
2016-02-13 12:37:28 +00:00
|
|
|
output.Resize((unsigned)got);
|
2014-06-19 11:13:42 +00:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-04-25 19:30:11 +00:00
|
|
|
bool SndFileDecoder::seek(size_t ms_offset, bool ms, bool /*mayrestart*/)
|
2014-06-19 11:13:42 +00:00
|
|
|
{
|
2017-04-01 17:46:38 +00:00
|
|
|
size_t smp_offset = ms? (size_t)((double)ms_offset / 1000. * SndInfo.samplerate) : ms_offset;
|
2014-06-19 11:13:42 +00:00
|
|
|
if(sf_seek(SndFile, smp_offset, SEEK_SET) < 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SndFileDecoder::getSampleOffset()
|
|
|
|
{
|
2015-04-24 15:42:56 +00:00
|
|
|
return (size_t)sf_seek(SndFile, 0, SEEK_CUR);
|
2014-06-19 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 04:14:35 +00:00
|
|
|
size_t SndFileDecoder::getSampleLength()
|
|
|
|
{
|
2015-04-24 15:42:56 +00:00
|
|
|
return (size_t)((SndInfo.frames > 0) ? SndInfo.frames : 0);
|
2014-06-26 04:14:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 11:13:42 +00:00
|
|
|
#endif
|