comparison data/fileio/CSVFileReader.cpp @ 1518:9c09a3f05139 import-audio-data

Pull allocation/deallocation out of the inner loop
author Chris Cannam
date Sat, 08 Sep 2018 20:53:48 +0100
parents 925d205c39b4
children fbe8afdfa8a6
comparison
equal deleted inserted replaced
1517:925d205c39b4 1518:9c09a3f05139
217 bool haveEndTime = false; 217 bool haveEndTime = false;
218 bool pitchLooksLikeMIDI = true; 218 bool pitchLooksLikeMIDI = true;
219 219
220 sv_frame_t startFrame = 0; // for calculation of dense model resolution 220 sv_frame_t startFrame = 0; // for calculation of dense model resolution
221 bool firstEverValue = true; 221 bool firstEverValue = true;
222
223 map<QString, int> labelCountMap;
224 222
225 int valueColumns = 0; 223 int valueColumns = 0;
226 for (int i = 0; i < m_format.getColumnCount(); ++i) { 224 for (int i = 0; i < m_format.getColumnCount(); ++i) {
227 if (m_format.getColumnPurpose(i) == CSVFormat::ColumnValue) { 225 if (m_format.getColumnPurpose(i) == CSVFormat::ColumnValue) {
228 ++valueColumns; 226 ++valueColumns;
229 } 227 }
230 } 228 }
229
230 int audioChannels = 0;
231 float **audioSamples = 0;
232 float sampleShift = 0.f;
233 float sampleScale = 1.f;
234
235 if (modelType == CSVFormat::WaveFileModel) {
236
237 audioChannels = valueColumns;
238
239 audioSamples =
240 breakfastquay::allocate_and_zero_channels<float>
241 (audioChannels, 1);
242
243 switch (m_format.getAudioSampleRange()) {
244 case CSVFormat::SampleRangeSigned1:
245 case CSVFormat::SampleRangeOther:
246 sampleShift = 0.f;
247 sampleScale = 1.f;
248 break;
249 case CSVFormat::SampleRangeUnsigned255:
250 sampleShift = -128.f;
251 sampleScale = 1.f / 128.f;
252 break;
253 case CSVFormat::SampleRangeSigned32767:
254 sampleShift = 0.f;
255 sampleScale = 1.f / 32768.f;
256 break;
257 }
258 }
259
260 map<QString, int> labelCountMap;
231 261
232 bool abandoned = false; 262 bool abandoned = false;
233 263
234 while (!in.atEnd() && !abandoned) { 264 while (!in.atEnd() && !abandoned) {
235 265
342 float pitch = 0.f; 372 float pitch = 0.f;
343 QString label = ""; 373 QString label = "";
344 374
345 duration = 0.f; 375 duration = 0.f;
346 haveEndTime = false; 376 haveEndTime = false;
347 377
348 for (int i = 0; i < list.size(); ++i) { 378 for (int i = 0; i < list.size(); ++i) {
349 379
350 QString s = list[i]; 380 QString s = list[i];
351 381
352 CSVFormat::ColumnPurpose purpose = m_format.getColumnPurpose(i); 382 CSVFormat::ColumnPurpose purpose = m_format.getColumnPurpose(i);
464 494
465 model3->setColumn(lineno, values); 495 model3->setColumn(lineno, values);
466 496
467 } else if (modelType == CSVFormat::WaveFileModel) { 497 } else if (modelType == CSVFormat::WaveFileModel) {
468 498
469 int channels = modelW->getChannelCount();
470
471 float **samples =
472 breakfastquay::allocate_and_zero_channels<float>
473 (channels, 1);
474
475 int channel = 0; 499 int channel = 0;
476 float shift = 0.f; 500
477 float scale = 1.f; 501 for (int i = 0;
478 502 i < list.size() && channel < audioChannels;
479 switch (m_format.getAudioSampleRange()) { 503 ++i) {
480 case CSVFormat::SampleRangeSigned1:
481 case CSVFormat::SampleRangeOther:
482 shift = 0.f;
483 scale = 1.f;
484 break;
485 case CSVFormat::SampleRangeUnsigned255:
486 shift = -128.f;
487 scale = 1.f / 128.f;
488 break;
489 case CSVFormat::SampleRangeSigned32767:
490 shift = 0.f;
491 scale = 1.f / 32768.f;
492 break;
493 }
494
495 for (int i = 0; i < list.size() && channel < channels; ++i) {
496 504
497 if (m_format.getColumnPurpose(i) != 505 if (m_format.getColumnPurpose(i) !=
498 CSVFormat::ColumnValue) { 506 CSVFormat::ColumnValue) {
499 continue; 507 continue;
500 } 508 }
501 509
502 bool ok = false; 510 bool ok = false;
503 float value = list[i].toFloat(&ok); 511 float value = list[i].toFloat(&ok);
504 512 if (!ok) {
505 value += shift; 513 value = 0.f;
506 value *= scale; 514 }
515
516 value += sampleShift;
517 value *= sampleScale;
507 518
508 samples[channel][0] = value; 519 audioSamples[channel][0] = value;
509 520
510 ++channel; 521 ++channel;
511 } 522 }
512 523
513 bool ok = modelW->addSamples(samples, 1); 524 while (channel < audioChannels) {
514 525 audioSamples[channel][0] = 0.f;
515 breakfastquay::deallocate_channels(samples, channels); 526 ++channel;
527 }
528
529 bool ok = modelW->addSamples(audioSamples, 1);
516 530
517 if (!ok) { 531 if (!ok) {
518 if (warnings < warnLimit) { 532 if (warnings < warnLimit) {
519 SVCERR << "WARNING: CSVFileReader::load: " 533 SVCERR << "WARNING: CSVFileReader::load: "
520 << "Unable to add sample to wave-file model" 534 << "Unable to add sample to wave-file model"
599 model3->setMinimumLevel(min); 613 model3->setMinimumLevel(min);
600 model3->setMaximumLevel(max); 614 model3->setMaximumLevel(max);
601 } 615 }
602 616
603 if (modelW) { 617 if (modelW) {
618 breakfastquay::deallocate_channels(audioSamples, audioChannels);
604 modelW->updateModel(); 619 modelW->updateModel();
605 modelW->writeComplete(); 620 modelW->writeComplete();
606 } 621 }
607 622
608 return model; 623 return model;