Mercurial > hg > svcore
comparison transform/FeatureExtractionModelTransformer.cpp @ 1039:b14064bd1f97 cxx11
This code now compiles. Main problem: sample rate types
author | Chris Cannam |
---|---|
date | Tue, 03 Mar 2015 17:09:19 +0000 |
parents | d9e0e59a1581 |
children | a1cd5abcb38b |
comparison
equal
deleted
inserted
replaced
1038:cc27f35aa75c | 1039:b14064bd1f97 |
---|---|
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, input->getSampleRate()); | 103 m_plugin = factory->instantiatePlugin(pluginId, float(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 |
233 float minValue = 0.0, maxValue = 0.0; | 233 float minValue = 0.0, maxValue = 0.0; |
234 bool haveExtents = false; | 234 bool haveExtents = false; |
235 bool haveBinCount = m_descriptors[n]->hasFixedBinCount; | 235 bool haveBinCount = m_descriptors[n]->hasFixedBinCount; |
236 | 236 |
237 if (haveBinCount) { | 237 if (haveBinCount) { |
238 binCount = m_descriptors[n]->binCount; | 238 binCount = (int)m_descriptors[n]->binCount; |
239 } | 239 } |
240 | 240 |
241 m_needAdditionalModels[n] = false; | 241 m_needAdditionalModels[n] = false; |
242 | 242 |
243 // cerr << "FeatureExtractionModelTransformer: output bin count " | 243 // cerr << "FeatureExtractionModelTransformer: output bin count " |
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(modelRate / m_descriptors[n]->sampleRate + 0.001); | 267 modelResolution = int(round(float(modelRate) / |
268 m_descriptors[n]->sampleRate)); | |
268 } | 269 } |
269 break; | 270 break; |
270 | 271 |
271 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep: | 272 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep: |
272 modelResolution = m_transforms[n].getStepSize(); | 273 modelResolution = m_transforms[n].getStepSize(); |
279 //!!! the resolution appropriately. We can't properly display | 280 //!!! the resolution appropriately. We can't properly display |
280 //!!! data with a higher resolution than the base model at all | 281 //!!! data with a higher resolution than the base model at all |
281 if (m_descriptors[n]->sampleRate > input->getSampleRate()) { | 282 if (m_descriptors[n]->sampleRate > input->getSampleRate()) { |
282 modelResolution = 1; | 283 modelResolution = 1; |
283 } else { | 284 } else { |
284 modelResolution = int(round(input->getSampleRate() / | 285 modelResolution = int(round(float(modelRate) / |
285 m_descriptors[n]->sampleRate)); | 286 m_descriptors[n]->sampleRate)); |
286 } | 287 } |
287 break; | 288 break; |
288 } | 289 } |
289 | 290 |
290 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2); | 291 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2); |
637 } | 638 } |
638 if (contextStart + contextDuration > endFrame) { | 639 if (contextStart + contextDuration > endFrame) { |
639 contextDuration = endFrame - contextStart; | 640 contextDuration = endFrame - contextStart; |
640 } | 641 } |
641 | 642 |
642 long blockFrame = contextStart; | 643 sv_frame_t blockFrame = contextStart; |
643 | 644 |
644 long prevCompletion = 0; | 645 long prevCompletion = 0; |
645 | 646 |
646 for (int j = 0; j < (int)m_outputNos.size(); ++j) { | 647 for (int j = 0; j < (int)m_outputNos.size(); ++j) { |
647 setCompletion(j, 0); | 648 setCompletion(j, 0); |
668 | 669 |
669 // SVDEBUG << "FeatureExtractionModelTransformer::run: blockFrame " | 670 // SVDEBUG << "FeatureExtractionModelTransformer::run: blockFrame " |
670 // << blockFrame << ", endFrame " << endFrame << ", blockSize " | 671 // << blockFrame << ", endFrame " << endFrame << ", blockSize " |
671 // << blockSize << endl; | 672 // << blockSize << endl; |
672 | 673 |
673 long completion = | 674 int completion = int |
674 (((blockFrame - contextStart) / stepSize) * 99) / | 675 ((((blockFrame - contextStart) / stepSize) * 99) / |
675 (contextDuration / stepSize + 1); | 676 (contextDuration / stepSize + 1)); |
676 | 677 |
677 // channelCount is either m_input.getModel()->channelCount or 1 | 678 // channelCount is either m_input.getModel()->channelCount or 1 |
678 | 679 |
679 if (frequencyDomain) { | 680 if (frequencyDomain) { |
680 for (int ch = 0; ch < channelCount; ++ch) { | 681 for (int ch = 0; ch < channelCount; ++ch) { |
681 int column = (blockFrame - startFrame) / stepSize; | 682 int column = int((blockFrame - startFrame) / stepSize); |
682 if (fftModels[ch]->getValuesAt(column, reals, imaginaries)) { | 683 if (fftModels[ch]->getValuesAt(column, reals, imaginaries)) { |
683 for (int i = 0; i <= blockSize/2; ++i) { | 684 for (int i = 0; i <= blockSize/2; ++i) { |
684 buffers[ch][i*2] = reals[i]; | 685 buffers[ch][i*2] = reals[i]; |
685 buffers[ch][i*2+1] = imaginaries[i]; | 686 buffers[ch][i*2+1] = imaginaries[i]; |
686 } | 687 } |
754 delete[] buffers; | 755 delete[] buffers; |
755 } | 756 } |
756 | 757 |
757 void | 758 void |
758 FeatureExtractionModelTransformer::getFrames(int channelCount, | 759 FeatureExtractionModelTransformer::getFrames(int channelCount, |
759 long startFrame, long size, | 760 sv_frame_t startFrame, |
761 sv_frame_t size, | |
760 float **buffers) | 762 float **buffers) |
761 { | 763 { |
762 long offset = 0; | 764 sv_frame_t offset = 0; |
763 | 765 |
764 if (startFrame < 0) { | 766 if (startFrame < 0) { |
765 for (int c = 0; c < channelCount; ++c) { | 767 for (int c = 0; c < channelCount; ++c) { |
766 for (int i = 0; i < size && startFrame + i < 0; ++i) { | 768 for (sv_frame_t i = 0; i < size && startFrame + i < 0; ++i) { |
767 buffers[c][i] = 0.0f; | 769 buffers[c][i] = 0.0f; |
768 } | 770 } |
769 } | 771 } |
770 offset = -startFrame; | 772 offset = -startFrame; |
771 size -= offset; | 773 size -= offset; |
774 } | 776 } |
775 | 777 |
776 DenseTimeValueModel *input = getConformingInput(); | 778 DenseTimeValueModel *input = getConformingInput(); |
777 if (!input) return; | 779 if (!input) return; |
778 | 780 |
779 long got = 0; | 781 sv_frame_t got = 0; |
780 | 782 |
781 if (channelCount == 1) { | 783 if (channelCount == 1) { |
782 | 784 |
783 got = input->getData(m_input.getChannel(), startFrame, size, | 785 got = input->getData(m_input.getChannel(), startFrame, size, |
784 buffers[0] + offset); | 786 buffers[0] + offset); |
785 | 787 |
786 if (m_input.getChannel() == -1 && input->getChannelCount() > 1) { | 788 if (m_input.getChannel() == -1 && input->getChannelCount() > 1) { |
787 // use mean instead of sum, as plugin input | 789 // use mean instead of sum, as plugin input |
788 float cc = float(input->getChannelCount()); | 790 float cc = float(input->getChannelCount()); |
789 for (long i = 0; i < size; ++i) { | 791 for (sv_frame_t i = 0; i < size; ++i) { |
790 buffers[0][i + offset] /= cc; | 792 buffers[0][i + offset] /= cc; |
791 } | 793 } |
792 } | 794 } |
793 | 795 |
794 } else { | 796 } else { |
814 } | 816 } |
815 } | 817 } |
816 | 818 |
817 void | 819 void |
818 FeatureExtractionModelTransformer::addFeature(int n, | 820 FeatureExtractionModelTransformer::addFeature(int n, |
819 int blockFrame, | 821 sv_frame_t blockFrame, |
820 const Vamp::Plugin::Feature &feature) | 822 const Vamp::Plugin::Feature &feature) |
821 { | 823 { |
822 int inputRate = m_input.getModel()->getSampleRate(); | 824 int inputRate = m_input.getModel()->getSampleRate(); |
823 | 825 |
824 // cerr << "FeatureExtractionModelTransformer::addFeature: blockFrame = " | 826 // cerr << "FeatureExtractionModelTransformer::addFeature: blockFrame = " |
825 // << blockFrame << ", hasTimestamp = " << feature.hasTimestamp | 827 // << blockFrame << ", hasTimestamp = " << feature.hasTimestamp |
826 // << ", timestamp = " << feature.timestamp << ", hasDuration = " | 828 // << ", timestamp = " << feature.timestamp << ", hasDuration = " |
827 // << feature.hasDuration << ", duration = " << feature.duration | 829 // << feature.hasDuration << ", duration = " << feature.duration |
828 // << endl; | 830 // << endl; |
829 | 831 |
830 int frame = blockFrame; | 832 sv_frame_t frame = blockFrame; |
831 | 833 |
832 if (m_descriptors[n]->sampleType == | 834 if (m_descriptors[n]->sampleType == |
833 Vamp::Plugin::OutputDescriptor::VariableSampleRate) { | 835 Vamp::Plugin::OutputDescriptor::VariableSampleRate) { |
834 | 836 |
835 if (!feature.hasTimestamp) { | 837 if (!feature.hasTimestamp) { |
847 | 849 |
848 if (!feature.hasTimestamp) { | 850 if (!feature.hasTimestamp) { |
849 ++m_fixedRateFeatureNos[n]; | 851 ++m_fixedRateFeatureNos[n]; |
850 } else { | 852 } else { |
851 RealTime ts(feature.timestamp.sec, feature.timestamp.nsec); | 853 RealTime ts(feature.timestamp.sec, feature.timestamp.nsec); |
852 m_fixedRateFeatureNos[n] = | 854 m_fixedRateFeatureNos[n] = (int) |
853 lrint(ts.toDouble() * m_descriptors[n]->sampleRate); | 855 lrint(ts.toDouble() * m_descriptors[n]->sampleRate); |
854 } | 856 } |
855 | 857 |
856 // cerr << "m_fixedRateFeatureNo = " << m_fixedRateFeatureNo | 858 // cerr << "m_fixedRateFeatureNo = " << m_fixedRateFeatureNo |
857 // << ", m_descriptor->sampleRate = " << m_descriptor->sampleRate | 859 // << ", m_descriptor->sampleRate = " << m_descriptor->sampleRate |
858 // << ", inputRate = " << inputRate | 860 // << ", inputRate = " << inputRate |
859 // << " giving frame = "; | 861 // << " giving frame = "; |
860 frame = lrintf((m_fixedRateFeatureNos[n] / m_descriptors[n]->sampleRate) | 862 frame = lrint((double(m_fixedRateFeatureNos[n]) |
861 * int(inputRate)); | 863 / m_descriptors[n]->sampleRate) |
864 * inputRate); | |
862 } | 865 } |
863 | 866 |
864 if (frame < 0) { | 867 if (frame < 0) { |
865 cerr | 868 cerr |
866 << "WARNING: FeatureExtractionModelTransformer::addFeature: " | 869 << "WARNING: FeatureExtractionModelTransformer::addFeature: " |
920 float value = 0.0; | 923 float value = 0.0; |
921 if ((int)feature.values.size() > index) { | 924 if ((int)feature.values.size() > index) { |
922 value = feature.values[index++]; | 925 value = feature.values[index++]; |
923 } | 926 } |
924 | 927 |
925 float duration = 1; | 928 sv_frame_t duration = 1; |
926 if (feature.hasDuration) { | 929 if (feature.hasDuration) { |
927 duration = Vamp::RealTime::realTime2Frame(feature.duration, inputRate); | 930 duration = Vamp::RealTime::realTime2Frame(feature.duration, inputRate); |
928 } else { | 931 } else { |
929 if ((int)feature.values.size() > index) { | 932 if (in_range_for(feature.values, index)) { |
930 duration = feature.values[index++]; | 933 duration = lrintf(feature.values[index++]); |
931 } | 934 } |
932 } | 935 } |
933 | 936 |
934 if (isOutput<FlexiNoteModel>(n)) { // GF: added for flexi note model | 937 if (isOutput<FlexiNoteModel>(n)) { // GF: added for flexi note model |
935 | 938 |
940 if (velocity < 0) velocity = 127; | 943 if (velocity < 0) velocity = 127; |
941 if (velocity > 127) velocity = 127; | 944 if (velocity > 127) velocity = 127; |
942 | 945 |
943 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>(n); | 946 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>(n); |
944 if (!model) return; | 947 if (!model) return; |
945 model->addPoint(FlexiNoteModel::Point(frame, value, // value is pitch | 948 model->addPoint(FlexiNoteModel::Point(frame, |
946 lrintf(duration), | 949 value, // value is pitch |
947 velocity / 127.f, | 950 duration, |
948 feature.label.c_str())); | 951 velocity / 127.f, |
952 feature.label.c_str())); | |
949 // GF: end -- added for flexi note model | 953 // GF: end -- added for flexi note model |
950 } else if (isOutput<NoteModel>(n)) { | 954 } else if (isOutput<NoteModel>(n)) { |
951 | 955 |
952 float velocity = 100; | 956 float velocity = 100; |
953 if ((int)feature.values.size() > index) { | 957 if ((int)feature.values.size() > index) { |
957 if (velocity > 127) velocity = 127; | 961 if (velocity > 127) velocity = 127; |
958 | 962 |
959 NoteModel *model = getConformingOutput<NoteModel>(n); | 963 NoteModel *model = getConformingOutput<NoteModel>(n); |
960 if (!model) return; | 964 if (!model) return; |
961 model->addPoint(NoteModel::Point(frame, value, // value is pitch | 965 model->addPoint(NoteModel::Point(frame, value, // value is pitch |
962 lrintf(duration), | 966 duration, |
963 velocity / 127.f, | 967 velocity / 127.f, |
964 feature.label.c_str())); | 968 feature.label.c_str())); |
965 } else { | 969 } else { |
966 | 970 |
967 RegionModel *model = getConformingOutput<RegionModel>(n); | 971 RegionModel *model = getConformingOutput<RegionModel>(n); |
976 QString label = feature.label.c_str(); | 980 QString label = feature.label.c_str(); |
977 if (feature.values.size() > 1) { | 981 if (feature.values.size() > 1) { |
978 label = QString("[%1] %2").arg(i+1).arg(label); | 982 label = QString("[%1] %2").arg(i+1).arg(label); |
979 } | 983 } |
980 | 984 |
981 model->addPoint(RegionModel::Point(frame, value, | 985 model->addPoint(RegionModel::Point(frame, |
982 lrintf(duration), | 986 value, |
987 duration, | |
983 label)); | 988 label)); |
984 } | 989 } |
985 } else { | 990 } else { |
986 | 991 |
987 model->addPoint(RegionModel::Point(frame, value, | 992 model->addPoint(RegionModel::Point(frame, |
988 lrintf(duration), | 993 value, |
994 duration, | |
989 feature.label.c_str())); | 995 feature.label.c_str())); |
990 } | 996 } |
991 } | 997 } |
992 | 998 |
993 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) { | 999 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) { |
1003 // << endl; | 1009 // << endl; |
1004 | 1010 |
1005 if (!feature.hasTimestamp && m_fixedRateFeatureNos[n] >= 0) { | 1011 if (!feature.hasTimestamp && m_fixedRateFeatureNos[n] >= 0) { |
1006 model->setColumn(m_fixedRateFeatureNos[n], values); | 1012 model->setColumn(m_fixedRateFeatureNos[n], values); |
1007 } else { | 1013 } else { |
1008 model->setColumn(frame / model->getResolution(), values); | 1014 model->setColumn(int(frame / model->getResolution()), values); |
1009 } | 1015 } |
1010 | 1016 |
1011 } else { | 1017 } else { |
1012 SVDEBUG << "FeatureExtractionModelTransformer::addFeature: Unknown output model type!" << endl; | 1018 SVDEBUG << "FeatureExtractionModelTransformer::addFeature: Unknown output model type!" << endl; |
1013 } | 1019 } |