comparison transform/FeatureExtractionModelTransformer.cpp @ 1429:48e9f538e6e9

Untabify
author Chris Cannam
date Thu, 01 Mar 2018 18:02:22 +0000
parents 8db84bd61eaa
children 73b3dd65e0b3
comparison
equal deleted inserted replaced
1428:87ae75da6527 1429:48e9f538e6e9
98 FeatureExtractionPluginFactory::instance(); 98 FeatureExtractionPluginFactory::instance();
99 99
100 if (!factory) { 100 if (!factory) {
101 m_message = tr("No factory available for feature extraction plugin id \"%1\" (unknown plugin type, or internal error?)").arg(pluginId); 101 m_message = tr("No factory available for feature extraction plugin id \"%1\" (unknown plugin type, or internal error?)").arg(pluginId);
102 SVCERR << m_message << endl; 102 SVCERR << m_message << endl;
103 return false; 103 return false;
104 } 104 }
105 105
106 DenseTimeValueModel *input = getConformingInput(); 106 DenseTimeValueModel *input = getConformingInput();
107 if (!input) { 107 if (!input) {
108 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId); 108 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId);
115 115
116 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate()); 116 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate());
117 if (!m_plugin) { 117 if (!m_plugin) {
118 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId); 118 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId);
119 SVCERR << m_message << endl; 119 SVCERR << m_message << endl;
120 return false; 120 return false;
121 } 121 }
122 122
123 TransformFactory::getInstance()->makeContextConsistentWithPlugin 123 TransformFactory::getInstance()->makeContextConsistentWithPlugin
124 (primaryTransform, m_plugin); 124 (primaryTransform, m_plugin);
125 125
126 TransformFactory::getInstance()->setPluginParameters 126 TransformFactory::getInstance()->setPluginParameters
127 (primaryTransform, m_plugin); 127 (primaryTransform, m_plugin);
128 128
129 int channelCount = input->getChannelCount(); 129 int channelCount = input->getChannelCount();
130 if ((int)m_plugin->getMaxChannelCount() < channelCount) { 130 if ((int)m_plugin->getMaxChannelCount() < channelCount) {
131 channelCount = 1; 131 channelCount = 1;
132 } 132 }
133 if ((int)m_plugin->getMinChannelCount() > channelCount) { 133 if ((int)m_plugin->getMinChannelCount() > channelCount) {
134 m_message = tr("Cannot provide enough channels to feature extraction plugin \"%1\" (plugin min is %2, max %3; input model has %4)") 134 m_message = tr("Cannot provide enough channels to feature extraction plugin \"%1\" (plugin min is %2, max %3; input model has %4)")
135 .arg(pluginId) 135 .arg(pluginId)
136 .arg(m_plugin->getMinChannelCount()) 136 .arg(m_plugin->getMinChannelCount())
137 .arg(m_plugin->getMaxChannelCount()) 137 .arg(m_plugin->getMaxChannelCount())
138 .arg(input->getChannelCount()); 138 .arg(input->getChannelCount());
139 SVCERR << m_message << endl; 139 SVCERR << m_message << endl;
140 return false; 140 return false;
141 } 141 }
142 142
143 int step = primaryTransform.getStepSize(); 143 int step = primaryTransform.getStepSize();
144 int block = primaryTransform.getBlockSize(); 144 int block = primaryTransform.getBlockSize();
145 145
215 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors(); 215 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
216 216
217 if (outputs.empty()) { 217 if (outputs.empty()) {
218 m_message = tr("Plugin \"%1\" has no outputs").arg(pluginId); 218 m_message = tr("Plugin \"%1\" has no outputs").arg(pluginId);
219 SVCERR << m_message << endl; 219 SVCERR << m_message << endl;
220 return false; 220 return false;
221 } 221 }
222 222
223 for (int j = 0; j < (int)m_transforms.size(); ++j) { 223 for (int j = 0; j < (int)m_transforms.size(); ++j) {
224 224
225 for (int i = 0; i < (int)outputs.size(); ++i) { 225 for (int i = 0; i < (int)outputs.size(); ++i) {
288 float minValue = 0.0, maxValue = 0.0; 288 float minValue = 0.0, maxValue = 0.0;
289 bool haveExtents = false; 289 bool haveExtents = false;
290 bool haveBinCount = m_descriptors[n]->hasFixedBinCount; 290 bool haveBinCount = m_descriptors[n]->hasFixedBinCount;
291 291
292 if (haveBinCount) { 292 if (haveBinCount) {
293 binCount = (int)m_descriptors[n]->binCount; 293 binCount = (int)m_descriptors[n]->binCount;
294 } 294 }
295 295
296 m_needAdditionalModels[n] = false; 296 m_needAdditionalModels[n] = false;
297 297
298 // cerr << "FeatureExtractionModelTransformer: output bin count " 298 // cerr << "FeatureExtractionModelTransformer: output bin count "
299 // << binCount << endl; 299 // << binCount << endl;
300 300
301 if (binCount > 0 && m_descriptors[n]->hasKnownExtents) { 301 if (binCount > 0 && m_descriptors[n]->hasKnownExtents) {
302 minValue = m_descriptors[n]->minValue; 302 minValue = m_descriptors[n]->minValue;
303 maxValue = m_descriptors[n]->maxValue; 303 maxValue = m_descriptors[n]->maxValue;
304 haveExtents = true; 304 haveExtents = true;
305 } 305 }
306 306
307 sv_samplerate_t modelRate = input->getSampleRate(); 307 sv_samplerate_t modelRate = input->getSampleRate();
308 sv_samplerate_t outputRate = modelRate; 308 sv_samplerate_t outputRate = modelRate;
328 } 328 }
329 329
330 switch (m_descriptors[n]->sampleType) { 330 switch (m_descriptors[n]->sampleType) {
331 331
332 case Vamp::Plugin::OutputDescriptor::VariableSampleRate: 332 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
333 if (outputRate != 0.0) { 333 if (outputRate != 0.0) {
334 modelResolution = int(round(modelRate / outputRate)); 334 modelResolution = int(round(modelRate / outputRate));
335 } 335 }
336 break; 336 break;
337 337
338 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep: 338 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
339 modelResolution = m_transforms[n].getStepSize(); 339 modelResolution = m_transforms[n].getStepSize();
340 break; 340 break;
341 341
342 case Vamp::Plugin::OutputDescriptor::FixedSampleRate: 342 case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
343 if (outputRate <= 0.0) { 343 if (outputRate <= 0.0) {
344 SVDEBUG << "WARNING: Fixed sample-rate plugin reports invalid sample rate " << m_descriptors[n]->sampleRate << "; defaulting to input rate of " << input->getSampleRate() << endl; 344 SVDEBUG << "WARNING: Fixed sample-rate plugin reports invalid sample rate " << m_descriptors[n]->sampleRate << "; defaulting to input rate of " << input->getSampleRate() << endl;
345 modelResolution = 1; 345 modelResolution = 1;
346 } else { 346 } else {
347 modelResolution = int(round(modelRate / outputRate)); 347 modelResolution = int(round(modelRate / outputRate));
348 // cerr << "modelRate = " << modelRate << ", descriptor rate = " << outputRate << ", modelResolution = " << modelResolution << endl; 348 // cerr << "modelRate = " << modelRate << ", descriptor rate = " << outputRate << ", modelResolution = " << modelResolution << endl;
349 } 349 }
350 break; 350 break;
351 } 351 }
352 352
353 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2); 353 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2);
354 354
355 Model *out = 0; 355 Model *out = 0;
514 new EditableDenseThreeDimensionalModel 514 new EditableDenseThreeDimensionalModel
515 (modelRate, modelResolution, binCount, 515 (modelRate, modelResolution, binCount,
516 EditableDenseThreeDimensionalModel::BasicMultirateCompression, 516 EditableDenseThreeDimensionalModel::BasicMultirateCompression,
517 false); 517 false);
518 518
519 if (!m_descriptors[n]->binNames.empty()) { 519 if (!m_descriptors[n]->binNames.empty()) {
520 std::vector<QString> names; 520 std::vector<QString> names;
521 for (int i = 0; i < (int)m_descriptors[n]->binNames.size(); ++i) { 521 for (int i = 0; i < (int)m_descriptors[n]->binNames.size(); ++i) {
522 names.push_back(m_descriptors[n]->binNames[i].c_str()); 522 names.push_back(m_descriptors[n]->binNames[i].c_str());
523 } 523 }
524 model->setBinNames(names); 524 model->setBinNames(names);
525 } 525 }
526 526
527 out = model; 527 out = model;
528 528
529 QString outputSignalTypeURI = description.getOutputSignalTypeURI(outputId); 529 QString outputSignalTypeURI = description.getOutputSignalTypeURI(outputId);
530 out->setRDFTypeURI(outputSignalTypeURI); 530 out->setRDFTypeURI(outputSignalTypeURI);
619 FeatureExtractionModelTransformer::getConformingInput() 619 FeatureExtractionModelTransformer::getConformingInput()
620 { 620 {
621 // SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: input model is " << getInputModel() << endl; 621 // SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: input model is " << getInputModel() << endl;
622 622
623 DenseTimeValueModel *dtvm = 623 DenseTimeValueModel *dtvm =
624 dynamic_cast<DenseTimeValueModel *>(getInputModel()); 624 dynamic_cast<DenseTimeValueModel *>(getInputModel());
625 if (!dtvm) { 625 if (!dtvm) {
626 SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: WARNING: Input model is not conformable to DenseTimeValueModel" << endl; 626 SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: WARNING: Input model is not conformable to DenseTimeValueModel" << endl;
627 } 627 }
628 return dtvm; 628 return dtvm;
629 } 629 }
630 630
631 void 631 void
663 663
664 sv_samplerate_t sampleRate = input->getSampleRate(); 664 sv_samplerate_t sampleRate = input->getSampleRate();
665 665
666 int channelCount = input->getChannelCount(); 666 int channelCount = input->getChannelCount();
667 if ((int)m_plugin->getMaxChannelCount() < channelCount) { 667 if ((int)m_plugin->getMaxChannelCount() < channelCount) {
668 channelCount = 1; 668 channelCount = 1;
669 } 669 }
670 670
671 float **buffers = new float*[channelCount]; 671 float **buffers = new float*[channelCount];
672 for (int ch = 0; ch < channelCount; ++ch) { 672 for (int ch = 0; ch < channelCount; ++ch) {
673 buffers[ch] = new float[primaryTransform.getBlockSize() + 2]; 673 buffers[ch] = new float[primaryTransform.getBlockSize() + 2];
674 } 674 }
675 675
676 int stepSize = primaryTransform.getStepSize(); 676 int stepSize = primaryTransform.getStepSize();
677 int blockSize = primaryTransform.getBlockSize(); 677 int blockSize = primaryTransform.getBlockSize();
678 678
752 } else { 752 } else {
753 if (blockFrame >= 753 if (blockFrame >=
754 contextStart + contextDuration) break; 754 contextStart + contextDuration) break;
755 } 755 }
756 756
757 // SVDEBUG << "FeatureExtractionModelTransformer::run: blockFrame " 757 // SVDEBUG << "FeatureExtractionModelTransformer::run: blockFrame "
758 // << blockFrame << ", endFrame " << endFrame << ", blockSize " 758 // << blockFrame << ", endFrame " << endFrame << ", blockSize "
759 // << blockSize << endl; 759 // << blockSize << endl;
760 760
761 int completion = int 761 int completion = int
762 ((((blockFrame - contextStart) / stepSize) * 99) / 762 ((((blockFrame - contextStart) / stepSize) * 99) /
763 (contextDuration / stepSize + 1)); 763 (contextDuration / stepSize + 1));
925 // << endl; 925 // << endl;
926 926
927 sv_frame_t frame = blockFrame; 927 sv_frame_t frame = blockFrame;
928 928
929 if (m_descriptors[n]->sampleType == 929 if (m_descriptors[n]->sampleType ==
930 Vamp::Plugin::OutputDescriptor::VariableSampleRate) { 930 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
931 931
932 if (!feature.hasTimestamp) { 932 if (!feature.hasTimestamp) {
933 SVDEBUG 933 SVDEBUG
934 << "WARNING: FeatureExtractionModelTransformer::addFeature: " 934 << "WARNING: FeatureExtractionModelTransformer::addFeature: "
935 << "Feature has variable sample rate but no timestamp!" 935 << "Feature has variable sample rate but no timestamp!"
936 << endl; 936 << endl;
937 return; 937 return;
938 } else { 938 } else {
939 frame = RealTime::realTime2Frame(feature.timestamp, inputRate); 939 frame = RealTime::realTime2Frame(feature.timestamp, inputRate);
940 } 940 }
941 941
942 // cerr << "variable sample rate: timestamp = " << feature.timestamp 942 // cerr << "variable sample rate: timestamp = " << feature.timestamp
943 // << " at input rate " << inputRate << " -> " << frame << endl; 943 // << " at input rate " << inputRate << " -> " << frame << endl;
944 944
945 } else if (m_descriptors[n]->sampleType == 945 } else if (m_descriptors[n]->sampleType ==
946 Vamp::Plugin::OutputDescriptor::FixedSampleRate) { 946 Vamp::Plugin::OutputDescriptor::FixedSampleRate) {
947 947
948 sv_samplerate_t rate = m_descriptors[n]->sampleRate; 948 sv_samplerate_t rate = m_descriptors[n]->sampleRate;
949 if (rate <= 0.0) { 949 if (rate <= 0.0) {
950 rate = inputRate; 950 rate = inputRate;
951 } 951 }
982 982
983 if (isOutput<SparseOneDimensionalModel>(n)) { 983 if (isOutput<SparseOneDimensionalModel>(n)) {
984 984
985 SparseOneDimensionalModel *model = 985 SparseOneDimensionalModel *model =
986 getConformingOutput<SparseOneDimensionalModel>(n); 986 getConformingOutput<SparseOneDimensionalModel>(n);
987 if (!model) return; 987 if (!model) return;
988 988
989 model->addPoint(SparseOneDimensionalModel::Point 989 model->addPoint(SparseOneDimensionalModel::Point
990 (frame, feature.label.c_str())); 990 (frame, feature.label.c_str()));
991 991
992 } else if (isOutput<SparseTimeValueModel>(n)) { 992 } else if (isOutput<SparseTimeValueModel>(n)) {
993 993
994 SparseTimeValueModel *model = 994 SparseTimeValueModel *model =
995 getConformingOutput<SparseTimeValueModel>(n); 995 getConformingOutput<SparseTimeValueModel>(n);
996 if (!model) return; 996 if (!model) return;
997 997
998 for (int i = 0; i < (int)feature.values.size(); ++i) { 998 for (int i = 0; i < (int)feature.values.size(); ++i) {
999 999
1000 float value = feature.values[i]; 1000 float value = feature.values[i];
1001 1001
1049 model->addPoint(FlexiNoteModel::Point(frame, 1049 model->addPoint(FlexiNoteModel::Point(frame,
1050 value, // value is pitch 1050 value, // value is pitch
1051 duration, 1051 duration,
1052 velocity / 127.f, 1052 velocity / 127.f,
1053 feature.label.c_str())); 1053 feature.label.c_str()));
1054 // GF: end -- added for flexi note model 1054 // GF: end -- added for flexi note model
1055 } else if (isOutput<NoteModel>(n)) { 1055 } else if (isOutput<NoteModel>(n)) {
1056 1056
1057 float velocity = 100; 1057 float velocity = 100;
1058 if ((int)feature.values.size() > index) { 1058 if ((int)feature.values.size() > index) {
1059 velocity = feature.values[index++]; 1059 velocity = feature.values[index++];
1094 value, 1094 value,
1095 duration, 1095 duration,
1096 feature.label.c_str())); 1096 feature.label.c_str()));
1097 } 1097 }
1098 } 1098 }
1099 1099
1100 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) { 1100 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) {
1101 1101
1102 DenseThreeDimensionalModel::Column values = feature.values; 1102 DenseThreeDimensionalModel::Column values = feature.values;
1103 1103
1104 EditableDenseThreeDimensionalModel *model = 1104 EditableDenseThreeDimensionalModel *model =
1105 getConformingOutput<EditableDenseThreeDimensionalModel>(n); 1105 getConformingOutput<EditableDenseThreeDimensionalModel>(n);
1106 if (!model) return; 1106 if (!model) return;
1107 1107
1108 // cerr << "(note: model resolution = " << model->getResolution() << ")" 1108 // cerr << "(note: model resolution = " << model->getResolution() << ")"
1109 // << endl; 1109 // << endl;
1110 1110
1111 if (!feature.hasTimestamp && m_fixedRateFeatureNos[n] >= 0) { 1111 if (!feature.hasTimestamp && m_fixedRateFeatureNos[n] >= 0) {
1125 // SVDEBUG << "FeatureExtractionModelTransformer::setCompletion(" 1125 // SVDEBUG << "FeatureExtractionModelTransformer::setCompletion("
1126 // << completion << ")" << endl; 1126 // << completion << ")" << endl;
1127 1127
1128 if (isOutput<SparseOneDimensionalModel>(n)) { 1128 if (isOutput<SparseOneDimensionalModel>(n)) {
1129 1129
1130 SparseOneDimensionalModel *model = 1130 SparseOneDimensionalModel *model =
1131 getConformingOutput<SparseOneDimensionalModel>(n); 1131 getConformingOutput<SparseOneDimensionalModel>(n);
1132 if (!model) return; 1132 if (!model) return;
1133 if (model->isAbandoning()) abandon(); 1133 if (model->isAbandoning()) abandon();
1134 model->setCompletion(completion, true); 1134 model->setCompletion(completion, true);
1135 1135
1136 } else if (isOutput<SparseTimeValueModel>(n)) { 1136 } else if (isOutput<SparseTimeValueModel>(n)) {
1137 1137
1138 SparseTimeValueModel *model = 1138 SparseTimeValueModel *model =
1139 getConformingOutput<SparseTimeValueModel>(n); 1139 getConformingOutput<SparseTimeValueModel>(n);
1140 if (!model) return; 1140 if (!model) return;
1141 if (model->isAbandoning()) abandon(); 1141 if (model->isAbandoning()) abandon();
1142 model->setCompletion(completion, true); 1142 model->setCompletion(completion, true);
1143 1143
1144 } else if (isOutput<NoteModel>(n)) { 1144 } else if (isOutput<NoteModel>(n)) {
1145 1145
1146 NoteModel *model = getConformingOutput<NoteModel>(n); 1146 NoteModel *model = getConformingOutput<NoteModel>(n);
1147 if (!model) return; 1147 if (!model) return;
1148 if (model->isAbandoning()) abandon(); 1148 if (model->isAbandoning()) abandon();
1149 model->setCompletion(completion, true); 1149 model->setCompletion(completion, true);
1150 1150
1151 } else if (isOutput<FlexiNoteModel>(n)) { 1151 } else if (isOutput<FlexiNoteModel>(n)) {
1152 1152
1153 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>(n); 1153 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>(n);
1154 if (!model) return; 1154 if (!model) return;
1155 if (model->isAbandoning()) abandon(); 1155 if (model->isAbandoning()) abandon();
1156 model->setCompletion(completion, true); 1156 model->setCompletion(completion, true);
1157 1157
1158 } else if (isOutput<RegionModel>(n)) { 1158 } else if (isOutput<RegionModel>(n)) {
1159 1159
1160 RegionModel *model = getConformingOutput<RegionModel>(n); 1160 RegionModel *model = getConformingOutput<RegionModel>(n);
1161 if (!model) return; 1161 if (!model) return;
1162 if (model->isAbandoning()) abandon(); 1162 if (model->isAbandoning()) abandon();
1163 model->setCompletion(completion, true); 1163 model->setCompletion(completion, true);
1164 1164
1165 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) { 1165 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) {
1166 1166
1167 EditableDenseThreeDimensionalModel *model = 1167 EditableDenseThreeDimensionalModel *model =
1168 getConformingOutput<EditableDenseThreeDimensionalModel>(n); 1168 getConformingOutput<EditableDenseThreeDimensionalModel>(n);
1169 if (!model) return; 1169 if (!model) return;
1170 if (model->isAbandoning()) abandon(); 1170 if (model->isAbandoning()) abandon();
1171 model->setCompletion(completion, true); //!!!m_context.updates); 1171 model->setCompletion(completion, true); //!!!m_context.updates);
1172 } 1172 }
1173 } 1173 }
1174 1174