# HG changeset patch # User Chris Cannam # Date 1536436428 -3600 # Node ID 9c09a3f0513921425e8b392c6f1d26e9062e840c # Parent 925d205c39b45f7e716e56850cbb10d5b45ca951 Pull allocation/deallocation out of the inner loop diff -r 925d205c39b4 -r 9c09a3f05139 data/fileio/CSVFileReader.cpp --- a/data/fileio/CSVFileReader.cpp Sat Sep 08 20:43:14 2018 +0100 +++ b/data/fileio/CSVFileReader.cpp Sat Sep 08 20:53:48 2018 +0100 @@ -219,8 +219,6 @@ sv_frame_t startFrame = 0; // for calculation of dense model resolution bool firstEverValue = true; - - map labelCountMap; int valueColumns = 0; for (int i = 0; i < m_format.getColumnCount(); ++i) { @@ -229,6 +227,38 @@ } } + int audioChannels = 0; + float **audioSamples = 0; + float sampleShift = 0.f; + float sampleScale = 1.f; + + if (modelType == CSVFormat::WaveFileModel) { + + audioChannels = valueColumns; + + audioSamples = + breakfastquay::allocate_and_zero_channels + (audioChannels, 1); + + switch (m_format.getAudioSampleRange()) { + case CSVFormat::SampleRangeSigned1: + case CSVFormat::SampleRangeOther: + sampleShift = 0.f; + sampleScale = 1.f; + break; + case CSVFormat::SampleRangeUnsigned255: + sampleShift = -128.f; + sampleScale = 1.f / 128.f; + break; + case CSVFormat::SampleRangeSigned32767: + sampleShift = 0.f; + sampleScale = 1.f / 32768.f; + break; + } + } + + map labelCountMap; + bool abandoned = false; while (!in.atEnd() && !abandoned) { @@ -344,7 +374,7 @@ duration = 0.f; haveEndTime = false; - + for (int i = 0; i < list.size(); ++i) { QString s = list[i]; @@ -466,33 +496,11 @@ } else if (modelType == CSVFormat::WaveFileModel) { - int channels = modelW->getChannelCount(); - - float **samples = - breakfastquay::allocate_and_zero_channels - (channels, 1); + int channel = 0; - int channel = 0; - float shift = 0.f; - float scale = 1.f; - - switch (m_format.getAudioSampleRange()) { - case CSVFormat::SampleRangeSigned1: - case CSVFormat::SampleRangeOther: - shift = 0.f; - scale = 1.f; - break; - case CSVFormat::SampleRangeUnsigned255: - shift = -128.f; - scale = 1.f / 128.f; - break; - case CSVFormat::SampleRangeSigned32767: - shift = 0.f; - scale = 1.f / 32768.f; - break; - } - - for (int i = 0; i < list.size() && channel < channels; ++i) { + for (int i = 0; + i < list.size() && channel < audioChannels; + ++i) { if (m_format.getColumnPurpose(i) != CSVFormat::ColumnValue) { @@ -501,18 +509,24 @@ bool ok = false; float value = list[i].toFloat(&ok); + if (!ok) { + value = 0.f; + } - value += shift; - value *= scale; + value += sampleShift; + value *= sampleScale; - samples[channel][0] = value; + audioSamples[channel][0] = value; ++channel; } - bool ok = modelW->addSamples(samples, 1); + while (channel < audioChannels) { + audioSamples[channel][0] = 0.f; + ++channel; + } - breakfastquay::deallocate_channels(samples, channels); + bool ok = modelW->addSamples(audioSamples, 1); if (!ok) { if (warnings < warnLimit) { @@ -601,6 +615,7 @@ } if (modelW) { + breakfastquay::deallocate_channels(audioSamples, audioChannels); modelW->updateModel(); modelW->writeComplete(); }