mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 10:29:19 +02:00
read sounds samples with float, workaround for inconsistences in libsndfile
This commit is contained in:
37
audio.cpp
37
audio.cpp
@@ -34,28 +34,32 @@ openal_buffer::openal_buffer( std::string const &Filename ) :
|
|||||||
if (sf == nullptr)
|
if (sf == nullptr)
|
||||||
throw std::runtime_error("sound: sf_open failed");
|
throw std::runtime_error("sound: sf_open failed");
|
||||||
|
|
||||||
int16_t *fbuf = new int16_t[si.frames * si.channels];
|
sf_command(sf, SFC_SET_NORM_FLOAT, NULL, SF_TRUE);
|
||||||
if (sf_readf_short(sf, fbuf, si.frames) != si.frames)
|
|
||||||
|
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");
|
throw std::runtime_error("sound: incomplete file");
|
||||||
|
|
||||||
sf_close(sf);
|
sf_close(sf);
|
||||||
|
|
||||||
rate = si.samplerate;
|
rate = si.samplerate;
|
||||||
|
|
||||||
int16_t *buf = nullptr;
|
if (si.channels != 1)
|
||||||
if (si.channels == 1)
|
|
||||||
buf = fbuf;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WriteLog("sound: warning: mixing multichannel file to mono");
|
WriteLog("sound: warning: mixing multichannel file to mono");
|
||||||
buf = new int16_t[si.frames];
|
|
||||||
for (size_t i = 0; i < si.frames; i++)
|
int16_t *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++)
|
float accum = 0;
|
||||||
accum += fbuf[i * si.channels + j];
|
for (size_t j = 0; j < si.channels; j++)
|
||||||
buf[i] = accum / si.channels;
|
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;
|
id = 0;
|
||||||
@@ -65,8 +69,7 @@ openal_buffer::openal_buffer( std::string const &Filename ) :
|
|||||||
|
|
||||||
alBufferData(id, AL_FORMAT_MONO16, buf, si.frames * 2, rate);
|
alBufferData(id, AL_FORMAT_MONO16, buf, si.frames * 2, rate);
|
||||||
|
|
||||||
if (si.channels != 1)
|
delete[] buf;
|
||||||
delete[] buf;
|
|
||||||
delete[] fbuf;
|
delete[] fbuf;
|
||||||
|
|
||||||
fetch_caption();
|
fetch_caption();
|
||||||
|
|||||||
Reference in New Issue
Block a user