comparison transform/FeatureExtractionModelTransformer.cpp @ 1040:a1cd5abcb38b cxx11

Introduce and use a samplerate type
author Chris Cannam
date Wed, 04 Mar 2015 12:01:04 +0000
parents b14064bd1f97
children f4ad0bfceeb7
comparison
equal deleted inserted replaced
1039:b14064bd1f97 1040:a1cd5abcb38b
98 if (!input) { 98 if (!input) {
99 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId); 99 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId);
100 return false; 100 return false;
101 } 101 }
102 102
103 m_plugin = factory->instantiatePlugin(pluginId, float(input->getSampleRate())); 103 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate());
104 if (!m_plugin) { 104 if (!m_plugin) {
105 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId); 105 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId);
106 return false; 106 return false;
107 } 107 }
108 108
247 minValue = m_descriptors[n]->minValue; 247 minValue = m_descriptors[n]->minValue;
248 maxValue = m_descriptors[n]->maxValue; 248 maxValue = m_descriptors[n]->maxValue;
249 haveExtents = true; 249 haveExtents = true;
250 } 250 }
251 251
252 int modelRate = input->getSampleRate(); 252 sv_samplerate_t modelRate = input->getSampleRate();
253 int modelResolution = 1; 253 int modelResolution = 1;
254 254
255 if (m_descriptors[n]->sampleType != 255 if (m_descriptors[n]->sampleType !=
256 Vamp::Plugin::OutputDescriptor::OneSamplePerStep) { 256 Vamp::Plugin::OutputDescriptor::OneSamplePerStep) {
257 if (m_descriptors[n]->sampleRate > input->getSampleRate()) { 257 if (m_descriptors[n]->sampleRate > input->getSampleRate()) {
262 262
263 switch (m_descriptors[n]->sampleType) { 263 switch (m_descriptors[n]->sampleType) {
264 264
265 case Vamp::Plugin::OutputDescriptor::VariableSampleRate: 265 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
266 if (m_descriptors[n]->sampleRate != 0.0) { 266 if (m_descriptors[n]->sampleRate != 0.0) {
267 modelResolution = int(round(float(modelRate) / 267 modelResolution = int(round(modelRate / m_descriptors[n]->sampleRate));
268 m_descriptors[n]->sampleRate));
269 } 268 }
270 break; 269 break;
271 270
272 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep: 271 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
273 modelResolution = m_transforms[n].getStepSize(); 272 modelResolution = m_transforms[n].getStepSize();
280 //!!! the resolution appropriately. We can't properly display 279 //!!! the resolution appropriately. We can't properly display
281 //!!! data with a higher resolution than the base model at all 280 //!!! data with a higher resolution than the base model at all
282 if (m_descriptors[n]->sampleRate > input->getSampleRate()) { 281 if (m_descriptors[n]->sampleRate > input->getSampleRate()) {
283 modelResolution = 1; 282 modelResolution = 1;
284 } else { 283 } else {
285 modelResolution = int(round(float(modelRate) / 284 modelResolution = int(round(modelRate / m_descriptors[n]->sampleRate));
286 m_descriptors[n]->sampleRate));
287 } 285 }
288 break; 286 break;
289 } 287 }
290 288
291 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2); 289 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2);
572 cerr << "FeatureExtractionModelTransformer::run: Waiting for input model to be ready..." << endl; 570 cerr << "FeatureExtractionModelTransformer::run: Waiting for input model to be ready..." << endl;
573 usleep(500000); 571 usleep(500000);
574 } 572 }
575 if (m_abandoned) return; 573 if (m_abandoned) return;
576 574
577 int sampleRate = input->getSampleRate(); 575 sv_samplerate_t sampleRate = input->getSampleRate();
578 576
579 int channelCount = input->getChannelCount(); 577 int channelCount = input->getChannelCount();
580 if ((int)m_plugin->getMaxChannelCount() < channelCount) { 578 if ((int)m_plugin->getMaxChannelCount() < channelCount) {
581 channelCount = 1; 579 channelCount = 1;
582 } 580 }
615 model->resume(); 613 model->resume();
616 fftModels.push_back(model); 614 fftModels.push_back(model);
617 } 615 }
618 } 616 }
619 617
620 long startFrame = m_input.getModel()->getStartFrame(); 618 sv_frame_t startFrame = m_input.getModel()->getStartFrame();
621 long endFrame = m_input.getModel()->getEndFrame(); 619 sv_frame_t endFrame = m_input.getModel()->getEndFrame();
622 620
623 RealTime contextStartRT = primaryTransform.getStartTime(); 621 RealTime contextStartRT = primaryTransform.getStartTime();
624 RealTime contextDurationRT = primaryTransform.getDuration(); 622 RealTime contextDurationRT = primaryTransform.getDuration();
625 623
626 long contextStart = 624 sv_frame_t contextStart =
627 RealTime::realTime2Frame(contextStartRT, sampleRate); 625 RealTime::realTime2Frame(contextStartRT, sampleRate);
628 626
629 long contextDuration = 627 sv_frame_t contextDuration =
630 RealTime::realTime2Frame(contextDurationRT, sampleRate); 628 RealTime::realTime2Frame(contextDurationRT, sampleRate);
631 629
632 if (contextStart == 0 || contextStart < startFrame) { 630 if (contextStart == 0 || contextStart < startFrame) {
633 contextStart = startFrame; 631 contextStart = startFrame;
634 } 632 }
703 } 701 }
704 702
705 if (m_abandoned) break; 703 if (m_abandoned) break;
706 704
707 Vamp::Plugin::FeatureSet features = m_plugin->process 705 Vamp::Plugin::FeatureSet features = m_plugin->process
708 (buffers, Vamp::RealTime::frame2RealTime(blockFrame, sampleRate)); 706 (buffers, RealTime::frame2RealTime(blockFrame, sampleRate).toVampRealTime());
709 707
710 if (m_abandoned) break; 708 if (m_abandoned) break;
711 709
712 for (int j = 0; j < (int)m_outputNos.size(); ++j) { 710 for (int j = 0; j < (int)m_outputNos.size(); ++j) {
713 for (int fi = 0; fi < (int)features[m_outputNos[j]].size(); ++fi) { 711 for (int fi = 0; fi < (int)features[m_outputNos[j]].size(); ++fi) {
819 void 817 void
820 FeatureExtractionModelTransformer::addFeature(int n, 818 FeatureExtractionModelTransformer::addFeature(int n,
821 sv_frame_t blockFrame, 819 sv_frame_t blockFrame,
822 const Vamp::Plugin::Feature &feature) 820 const Vamp::Plugin::Feature &feature)
823 { 821 {
824 int inputRate = m_input.getModel()->getSampleRate(); 822 sv_samplerate_t inputRate = m_input.getModel()->getSampleRate();
825 823
826 // cerr << "FeatureExtractionModelTransformer::addFeature: blockFrame = " 824 // cerr << "FeatureExtractionModelTransformer::addFeature: blockFrame = "
827 // << blockFrame << ", hasTimestamp = " << feature.hasTimestamp 825 // << blockFrame << ", hasTimestamp = " << feature.hasTimestamp
828 // << ", timestamp = " << feature.timestamp << ", hasDuration = " 826 // << ", timestamp = " << feature.timestamp << ", hasDuration = "
829 // << feature.hasDuration << ", duration = " << feature.duration 827 // << feature.hasDuration << ", duration = " << feature.duration
839 << "WARNING: FeatureExtractionModelTransformer::addFeature: " 837 << "WARNING: FeatureExtractionModelTransformer::addFeature: "
840 << "Feature has variable sample rate but no timestamp!" 838 << "Feature has variable sample rate but no timestamp!"
841 << endl; 839 << endl;
842 return; 840 return;
843 } else { 841 } else {
844 frame = Vamp::RealTime::realTime2Frame(feature.timestamp, inputRate); 842 frame = RealTime::realTime2Frame(feature.timestamp, inputRate);
845 } 843 }
846 844
847 } else if (m_descriptors[n]->sampleType == 845 } else if (m_descriptors[n]->sampleType ==
848 Vamp::Plugin::OutputDescriptor::FixedSampleRate) { 846 Vamp::Plugin::OutputDescriptor::FixedSampleRate) {
849 847
925 value = feature.values[index++]; 923 value = feature.values[index++];
926 } 924 }
927 925
928 sv_frame_t duration = 1; 926 sv_frame_t duration = 1;
929 if (feature.hasDuration) { 927 if (feature.hasDuration) {
930 duration = Vamp::RealTime::realTime2Frame(feature.duration, inputRate); 928 duration = RealTime::realTime2Frame(feature.duration, inputRate);
931 } else { 929 } else {
932 if (in_range_for(feature.values, index)) { 930 if (in_range_for(feature.values, index)) {
933 duration = lrintf(feature.values[index++]); 931 duration = lrintf(feature.values[index++]);
934 } 932 }
935 } 933 }