CLAM-Development  1.4.0
TonalAnalysis.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2002 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "TonalAnalysis.hxx"
23 #include "ChordExtractor.hxx"
24 #include "ProcessingFactory.hxx"
25 
26 
27 namespace CLAM
28 {
29 
30 namespace Hidden
31 {
32  static const char * metadata[] = {
33  "key", "TonalAnalysis",
34  "category", "Analysis",
35  "description", "TonalAnalysis",
36  0
37  };
38  static FactoryRegistrator<ProcessingFactory, TonalAnalysis> reg = metadata;
39 }
40 
41 
42 // TonalAnalysisConfig method definition
44 {
45  AddAll();
46 
47  UpdateData();
48 
49  SetFilterInertia(0.7);
50  SetTunningEnabled(true);
51  SetPeakWindowingEnabled(true);
52  SetHopRatio(8.0);
53  SetSegmentationMethod(1);
54 }
55 
56 
58  : _input("Audio Input",this)
59  , _pcp("Pitch Profile",this)
60  , _chordCorrelation("Chord Correlation",this)
61  , _segmentation("Chord Segmentation", this)
62  , _chromaPeaks("Chroma Peaks",this)
63  , _tunning("Tunning",this)
64  , _implementation( 0 )
65  , _currentTime( 0 )
66 {
67  Configure( cfg );
68 }
69 
71 {
72  if (_implementation) delete _implementation;
73 }
74 
76 {
77  CopyAsConcreteConfig(_config, c);
78 
79  if (_implementation) delete _implementation;
80  _implementation = new Simac::ChordExtractor;
81 
82  _implementation->filterInertia( _config.GetFilterInertia() );
83  _implementation->enableTunning( _config.GetTunningEnabled() );
84  _implementation->enablePeakWindowing( _config.GetPeakWindowingEnabled() );
85  _implementation->hopRatio( _config.GetHopRatio() );
86  unsigned segmentationMethod = _config.HasSegmentationMethod() ? _config.GetSegmentationMethod() : 0;
87  _implementation->segmentationMethod( segmentationMethod );
88 
89  _input.SetSize( _implementation->frameSize() );
90  _input.SetHop( _implementation->hop() );
91  _floatBuffer.resize(_implementation->frameSize());
92  return true;
93 }
94 
96 {
97  _currentTime = 0.0;
98  return true;
99 }
100 
102 {
103  _implementation->clear();
104  return true;
105 }
106 
108 {
109  if( !AbleToExecute() ) return true;
110  CLAM::TData * input = &(_input.GetAudio().GetBuffer()[0]);
111  for (unsigned i = 0; i < _implementation->frameSize(); i++)
112  _floatBuffer[i] = input[i];
113  _implementation->doIt(&_floatBuffer[0], _currentTime);
114 
115  std::vector<TData> & pcp = _pcp.GetData();
116  pcp.resize(_implementation->pcp().size());
117  for (unsigned i = 0; i < _implementation->pcp().size(); i++)
118  pcp[i] = _implementation->pcp()[i];
119  _pcp.Produce();
120 
121  std::vector<TData> & chordCorrelation = _chordCorrelation.GetData();
122  chordCorrelation.resize(_implementation->chordCorrelation().size());
123  for (unsigned i = 0; i < _implementation->chordCorrelation().size(); i++)
124  chordCorrelation[i] = _implementation->chordCorrelation()[i];
125  _chordCorrelation.Produce();
126 
127  DiscontinuousSegmentation & segmentation = _segmentation.GetData();
128  segmentation = _implementation->segmentation();
129  for (unsigned i=0; i<segmentation.onsets().size(); i++)
130  {
131  unsigned chordIndex = _implementation->chordIndexes()[i];
132  std::string chordName = _implementation->root(chordIndex) + " " + _implementation->mode(chordIndex);
133  segmentation.setLabel(i,chordName);
134  }
135  segmentation.dragOffset(segmentation.onsets().size()-1, _currentTime );
136 
137  _segmentation.Produce();
138 
139 
140  std::vector<std::pair<TData,TData> > & chromaPeaks = _chromaPeaks.GetData();
141  chromaPeaks.resize(_implementation->peaks().size()); //TODO processing time resize!!!!
142  for (unsigned i = 0; i < _implementation->peaks().size(); i++)
143  chromaPeaks[i] = _implementation->peaks()[i];
144  _chromaPeaks.Produce();
145 
146  std::pair<TData,TData> & tunning = _tunning.GetData();
147  tunning = _implementation->instantTunning();
148 // tunning.first=_implementation->tunning();
149 // tunning.second=_implementation->tunningStrength();
150  _tunning.Produce();
151 
152  _input.Consume();
153 
154  _currentTime += _implementation->hop()/44100.0;
155 
156  return true;
157 }
158 
159 
160 
161 } // namespace CLAM
162 
163 
164