TonalAnalysis.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "TonalAnalysis.hxx"
00023 #include "ChordExtractor.hxx"
00024 #include "ProcessingFactory.hxx"
00025
00026
00027 namespace CLAM
00028 {
00029
00030 namespace Hidden
00031 {
00032 static const char * metadata[] = {
00033 "key", "TonalAnalysis",
00034 "category", "Analysis",
00035 "description", "TonalAnalysis",
00036 0
00037 };
00038 static FactoryRegistrator<ProcessingFactory, TonalAnalysis> reg = metadata;
00039 }
00040
00041
00042
00043 void TonalAnalysisConfig::DefaultInit(void)
00044 {
00045 AddAll();
00046
00047 UpdateData();
00048
00049 SetFilterInertia(0.7);
00050 SetTunningEnabled(true);
00051 SetPeakWindowingEnabled(true);
00052 SetHopRatio(8.0);
00053 SetSegmentationMethod(1);
00054 }
00055
00056
00057 TonalAnalysis::TonalAnalysis( const TonalAnalysisConfig& cfg )
00058 : _input("Audio Input",this)
00059 , _pcp("Pitch Profile",this)
00060 , _chordCorrelation("Chord Correlation",this)
00061 , _segmentation("Chord Segmentation", this)
00062 , _chromaPeaks("Chroma Peaks",this)
00063 , _tunning("Tunning",this)
00064 , _implementation( 0 )
00065 , _currentTime( 0 )
00066 {
00067 Configure( cfg );
00068 }
00069
00070 TonalAnalysis::~TonalAnalysis()
00071 {
00072 if (_implementation) delete _implementation;
00073 }
00074
00075 bool TonalAnalysis::ConcreteConfigure( const ProcessingConfig& c )
00076 {
00077 CopyAsConcreteConfig(_config, c);
00078
00079 if (_implementation) delete _implementation;
00080 _implementation = new Simac::ChordExtractor;
00081
00082 _implementation->filterInertia( _config.GetFilterInertia() );
00083 _implementation->enableTunning( _config.GetTunningEnabled() );
00084 _implementation->enablePeakWindowing( _config.GetPeakWindowingEnabled() );
00085 _implementation->hopRatio( _config.GetHopRatio() );
00086 unsigned segmentationMethod = _config.HasSegmentationMethod() ? _config.GetSegmentationMethod() : 0;
00087 _implementation->segmentationMethod( segmentationMethod );
00088
00089 _input.SetSize( _implementation->frameSize() );
00090 _input.SetHop( _implementation->hop() );
00091 _floatBuffer.resize(_implementation->frameSize());
00092 return true;
00093 }
00094
00095 bool TonalAnalysis::ConcreteStart()
00096 {
00097 _currentTime = 0.0;
00098 return true;
00099 }
00100
00101 bool TonalAnalysis::ConcreteStop()
00102 {
00103 _implementation->clear();
00104 return true;
00105 }
00106
00107 bool TonalAnalysis::Do()
00108 {
00109 if( !AbleToExecute() ) return true;
00110 CLAM::TData * input = &(_input.GetAudio().GetBuffer()[0]);
00111 for (unsigned i = 0; i < _implementation->frameSize(); i++)
00112 _floatBuffer[i] = input[i];
00113 _implementation->doIt(&_floatBuffer[0], _currentTime);
00114
00115 std::vector<TData> & pcp = _pcp.GetData();
00116 pcp.resize(_implementation->pcp().size());
00117 for (unsigned i = 0; i < _implementation->pcp().size(); i++)
00118 pcp[i] = _implementation->pcp()[i];
00119 _pcp.Produce();
00120
00121 std::vector<TData> & chordCorrelation = _chordCorrelation.GetData();
00122 chordCorrelation.resize(_implementation->chordCorrelation().size());
00123 for (unsigned i = 0; i < _implementation->chordCorrelation().size(); i++)
00124 chordCorrelation[i] = _implementation->chordCorrelation()[i];
00125 _chordCorrelation.Produce();
00126
00127 DiscontinuousSegmentation & segmentation = _segmentation.GetData();
00128 segmentation = _implementation->segmentation();
00129 for (unsigned i=0; i<segmentation.onsets().size(); i++)
00130 {
00131 unsigned chordIndex = _implementation->chordIndexes()[i];
00132 std::string chordName = _implementation->root(chordIndex) + " " + _implementation->mode(chordIndex);
00133 segmentation.setLabel(i,chordName);
00134 }
00135 segmentation.dragOffset(segmentation.onsets().size()-1, _currentTime );
00136
00137 _segmentation.Produce();
00138
00139
00140 std::vector<std::pair<TData,TData> > & chromaPeaks = _chromaPeaks.GetData();
00141 chromaPeaks.resize(_implementation->peaks().size());
00142 for (unsigned i = 0; i < _implementation->peaks().size(); i++)
00143 chromaPeaks[i] = _implementation->peaks()[i];
00144 _chromaPeaks.Produce();
00145
00146 std::pair<TData,TData> & tunning = _tunning.GetData();
00147 tunning = _implementation->instantTunning();
00148
00149
00150 _tunning.Produce();
00151
00152 _input.Consume();
00153
00154 _currentTime += _implementation->hop()/44100.0;
00155
00156 return true;
00157 }
00158
00159
00160
00161 }
00162
00163
00164