mirror of
https://github.com/tildearrow/furnace.git
synced 2024-11-15 17:25:06 +00:00
finally support loading floating-point samples
This commit is contained in:
parent
ed6a110d44
commit
327952f3f4
1 changed files with 47 additions and 11 deletions
|
@ -2201,8 +2201,22 @@ int DivEngine::addSampleFromFile(const char* path) {
|
||||||
BUSY_END;
|
BUSY_END;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
short* buf=new short[si.channels*si.frames];
|
void* buf=NULL;
|
||||||
if (sf_readf_short(f,buf,si.frames)!=si.frames) {
|
sf_count_t sampleLen=sizeof(short);
|
||||||
|
if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) {
|
||||||
|
logD("sample is 8-bit unsigned");
|
||||||
|
buf=new unsigned char[si.channels*si.frames];
|
||||||
|
sampleLen=sizeof(unsigned char);
|
||||||
|
} else if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_FLOAT) {
|
||||||
|
logD("sample is 32-bit float");
|
||||||
|
buf=new float[si.channels*si.frames];
|
||||||
|
sampleLen=sizeof(float);
|
||||||
|
} else {
|
||||||
|
logD("sample is 16-bit signed");
|
||||||
|
buf=new short[si.channels*si.frames];
|
||||||
|
sampleLen=sizeof(short);
|
||||||
|
}
|
||||||
|
if (sf_read_raw(f,buf,si.frames*si.channels*sampleLen)!=(si.frames*si.channels*sampleLen)) {
|
||||||
logW("sample read size mismatch!");
|
logW("sample read size mismatch!");
|
||||||
}
|
}
|
||||||
DivSample* sample=new DivSample;
|
DivSample* sample=new DivSample;
|
||||||
|
@ -2216,19 +2230,41 @@ int DivEngine::addSampleFromFile(const char* path) {
|
||||||
sample->depth=16;
|
sample->depth=16;
|
||||||
}
|
}
|
||||||
sample->init(si.frames);
|
sample->init(si.frames);
|
||||||
for (int i=0; i<si.frames*si.channels; i+=si.channels) {
|
if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) {
|
||||||
int averaged=0;
|
for (int i=0; i<si.frames*si.channels; i+=si.channels) {
|
||||||
for (int j=0; j<si.channels; j++) {
|
int averaged=0;
|
||||||
averaged+=buf[i+j];
|
for (int j=0; j<si.channels; j++) {
|
||||||
|
averaged+=((unsigned char*)buf)[i+j];
|
||||||
|
}
|
||||||
|
averaged/=si.channels;
|
||||||
|
sample->data8[index++]=averaged;
|
||||||
}
|
}
|
||||||
averaged/=si.channels;
|
delete[] (unsigned char*)buf;
|
||||||
if (((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8)) {
|
} else if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_FLOAT) {
|
||||||
sample->data8[index++]=averaged>>8;
|
for (int i=0; i<si.frames*si.channels; i+=si.channels) {
|
||||||
} else {
|
float averaged=0.0f;
|
||||||
|
for (int j=0; j<si.channels; j++) {
|
||||||
|
averaged+=((float*)buf)[i+j];
|
||||||
|
}
|
||||||
|
averaged/=si.channels;
|
||||||
|
averaged*=32767.0;
|
||||||
|
if (averaged<-32768.0) averaged=-32768.0;
|
||||||
|
if (averaged>32767.0) averaged=32767.0;
|
||||||
sample->data16[index++]=averaged;
|
sample->data16[index++]=averaged;
|
||||||
}
|
}
|
||||||
|
delete[] (float*)buf;
|
||||||
|
} else {
|
||||||
|
for (int i=0; i<si.frames*si.channels; i+=si.channels) {
|
||||||
|
int averaged=0;
|
||||||
|
for (int j=0; j<si.channels; j++) {
|
||||||
|
averaged+=((short*)buf)[i+j];
|
||||||
|
}
|
||||||
|
averaged/=si.channels;
|
||||||
|
sample->data16[index++]=averaged;
|
||||||
|
}
|
||||||
|
delete[] (short*)buf;
|
||||||
}
|
}
|
||||||
delete[] buf;
|
|
||||||
sample->rate=si.samplerate;
|
sample->rate=si.samplerate;
|
||||||
if (sample->rate<4000) sample->rate=4000;
|
if (sample->rate<4000) sample->rate=4000;
|
||||||
if (sample->rate>96000) sample->rate=96000;
|
if (sample->rate>96000) sample->rate=96000;
|
||||||
|
|
Loading…
Reference in a new issue