read sounds samples with float, workaround for inconsistences in libsndfile

This commit is contained in:
milek7
2018-04-01 21:50:54 +02:00
parent 8441fa5d85
commit 3d0024ab15
2 changed files with 21 additions and 18 deletions

View File

@@ -34,28 +34,32 @@ openal_buffer::openal_buffer( std::string const &Filename ) :
if (sf == nullptr)
throw std::runtime_error("sound: sf_open failed");
int16_t *fbuf = new int16_t[si.frames * si.channels];
if (sf_readf_short(sf, fbuf, si.frames) != si.frames)
sf_command(sf, SFC_SET_NORM_FLOAT, NULL, SF_TRUE);
float *fbuf = new float[si.frames * si.channels];
if (sf_readf_float(sf, fbuf, si.frames) != si.frames)
throw std::runtime_error("sound: incomplete file");
sf_close(sf);
rate = si.samplerate;
int16_t *buf = nullptr;
if (si.channels == 1)
buf = fbuf;
else
{
if (si.channels != 1)
WriteLog("sound: warning: mixing multichannel file to mono");
buf = new int16_t[si.frames];
for (size_t i = 0; i < si.frames; i++)
{
int32_t accum = 0;
for (size_t j = 0; j < si.channels; j++)
accum += fbuf[i * si.channels + j];
buf[i] = accum / si.channels;
}
int16_t *buf = new int16_t[si.frames];
for (size_t i = 0; i < si.frames; i++)
{
float accum = 0;
for (size_t j = 0; j < si.channels; j++)
accum += fbuf[i * si.channels + j];
long val = lrintf(accum / si.channels * 32767.0f);
if (val > 32767)
val = 32767;
if (val < -32767)
val = -32767;
buf[i] = val;
}
id = 0;
@@ -65,8 +69,7 @@ openal_buffer::openal_buffer( std::string const &Filename ) :
alBufferData(id, AL_FORMAT_MONO16, buf, si.frames * 2, rate);
if (si.channels != 1)
delete[] buf;
delete[] buf;
delete[] fbuf;
fetch_caption();

View File

@@ -1 +1 @@
#define VERSION_INFO "M7 1.04.2018, based on tmj 07f1438"
#define VERSION_INFO "M7 1.04.2018/v2, based on tmj 07f1438"