Skip to content
Snippets Groups Projects
Commit 07667e10 authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3757] Allow for longer (60 sec) wave file to be loaded

parent 4c6184a3
No related branches found
No related tags found
No related merge requests found
......@@ -248,7 +248,9 @@ bool WaveFile::isFileOpened()
bool WaveFile::openExistingWaveFile (const std::string& fileName, int audioSamplingRate)
{
SamplerateConverter _converter (88200, 1000);
// Sample rate converter initialized with 88200 sample long
int converterSamples = 88200;
SamplerateConverter _converter (converterSamples, 1000);
_debug ("WaveFile: Opening %s", fileName.c_str());
......@@ -327,19 +329,19 @@ bool WaveFile::openExistingWaveFile (const std::string& fileName, int audioSampl
if (format_tag == 1) {
if (dt == 8)
_data_type = 1; // STK_SINT8;
_data_type = 1; // SINT8;
else if (dt == 16)
_data_type = 2; // STK_SINT16;
_data_type = 2; // SINT16;
else if (dt == 32)
_data_type = 3; // STK_SINT32;
_data_type = 3; // SINT32;
}
/*
else if ( format_tag == 3 )
{
if (temp == 32)
dataType_ = STK_FLOAT32;
dataType_ = FLOAT32;
else if (temp == 64)
dataType_ = STK_FLOAT64;
dataType_ = FLOAT64;
}
*/
else {
......@@ -367,36 +369,49 @@ bool WaveFile::openExistingWaveFile (const std::string& fileName, int audioSampl
_debug ("WaveFile: data size in frame %ld", _file_size);
// Should not be longer than a minute
if (_file_size > (unsigned int) (60*srate))
_file_size = 60*srate;
SFLDataFormat *tempBuffer = new SFLDataFormat[_file_size];
SFLDataFormat *tempBufferRsmpl = NULL;
// Should not be longer than 2 secs
if (_file_size > 88200)
_file_size = 88200;
_file_stream.read ( (char *) tempBuffer, _file_size*sizeof (SFLDataFormat));
_file_stream.read ( (char *) tempBuffer, _file_size);
// compute size of final buffer
int nbSample;
int nbSample = _file_size;
if (srate != audioSamplingRate) {
nbSample = (int) ( (float) _file_size * ( (float) audioSamplingRate / (float) srate));
nbSample++;
} else
nbSample = _file_size;
// _debug ("--------------------- srate %d", srate);
// _debug ("--------------------- audioSamplingRate %d", audioSamplingRate);
// _debug ("--------------------- _file_size %d", _file_size);
// require resampling
if (srate != audioSamplingRate) {
if (srate < audioSamplingRate) {
int nb_sample_down = _file_size; //(_file_size * srate) / audioSamplingRate;
// _debug ("--------------------- nb_sample_down %d", nb_sample_down);
// initialize remaining samples to process
int remainingSamples = _file_size;
tempBufferRsmpl = new SFLDataFormat[_file_size];
nbSample = _converter.upsampleData (tempBuffer, tempBufferRsmpl, srate, audioSamplingRate, nb_sample_down);
// _debug ("--------------------- nbSample %d", nbSample);
} else if (srate > audioSamplingRate) {
int nb_sample_up = _file_size; // (_file_size * srate) / audioSamplingRate;
// _debug ("--------------------- nb_sample_up %d", nb_sample_up);
tempBufferRsmpl = new SFLDataFormat[nbSample];
SFLDataFormat *in = tempBuffer;
SFLDataFormat *out = tempBufferRsmpl;
tempBufferRsmpl = new SFLDataFormat[_file_size];
nbSample = _converter.downsampleData (tempBuffer, tempBufferRsmpl, audioSamplingRate, srate, nb_sample_up);
// _debug ("--------------------- nbSample %d", nbSample);
while (remainingSamples > 0) {
int toProcess = remainingSamples > converterSamples ? converterSamples : remainingSamples;
int nbSamplesConverted = 0;
if (srate < audioSamplingRate) {
nbSamplesConverted = _converter.upsampleData (in, out, srate, audioSamplingRate, toProcess);
} else if (srate > audioSamplingRate) {
nbSamplesConverted = _converter.downsampleData (in, out, audioSamplingRate, srate, toProcess);
}
in += toProcess;
out += nbSamplesConverted;
remainingSamples -= toProcess;
}
}
// Init audio loop buffer info
......@@ -406,9 +421,9 @@ bool WaveFile::openExistingWaveFile (const std::string& fileName, int audioSampl
// Copy audio into audioloop
if (srate != audioSamplingRate)
memcpy ( (void *) _buffer, (void *) tempBufferRsmpl, nbSample);
memcpy ( (void *) _buffer, (void *) tempBufferRsmpl, nbSample*sizeof (SFLDataFormat));
else
memcpy ( (void *) _buffer, (void *) tempBuffer, nbSample);
memcpy ( (void *) _buffer, (void *) tempBuffer, nbSample*sizeof (SFLDataFormat));
_debug ("WaveFile: file successfully opened");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment