rendered paste body#ifndef NoteCounter_hxx#define NoteCounter_hxx#include <vector>#include <iostream>#include <algorithm> #include <iterator>#include <CLAM/InPort.hxx>#include <CLAM/OutPort.hxx>#include <CLAM/Processing.hxx>#include <CLAM/NullProcessingConfig.hxx>#include <numeric>// Those two configuration types could be the ones you choose.// Your own ones or any type already in CLAM.#include "CLAM/Fundamental.hxx"#include <CLAM/SpectralPeakArray.hxx>class NoteCounter : public CLAM::Processing{ //in ports CLAM::InPort<CLAM::SpectralPeakArray> mIn; CLAM::InPort<CLAM::Fundamental> mIn; typedef CLAM::NullProcessingConfig Config;//variablespublic: CLAM::TData _lastValue; bool _increasing; unsigned _nNotes; std::vector<float> notes; int output; int median;//clam configuration const char* GetClassName() const { return "NoteCounter"; } NoteCounter(const Config & config=Config()) : mIn("Input", this) : mIn("Fundamental", this) { Configure( config ); _lastValue=0; _increasing=true; _nNotes=0; } ~NoteCounter() {} // Configuration to the minimal expression bool ConcreteConfigure(const CLAM::ProcessingConfig&) { return true; // I got proper configuration } const CLAM::ProcessingConfig& GetConfig() const { static Config config; return config; } bool Do() { bool result = Do(mIn.GetData()); notes.reserve(450); // Tell the ports this is done mIn.Consume(); return result; } bool Do(const CLAM::SpectralPeakArray & in) { const CLAM::Array<CLAM::TData> & magnitudes = mIn.GetData().GetMagBuffer(); CLAM::TData sum=0; for (int i=0;i<magnitudes.Size();i++) { double tempsum; tempsum+=pow(10,magnitudes[i]/20); if (tempsum >= .04) { sum+=pow(10,magnitudes[i]/20);} } bool thisOneIncreasing = _lastValue<=sum; if (thisOneIncreasing && !_increasing) _nNotes++; _increasing=thisOneIncreasing; _lastValue=sum; return 1; } bool Do(const CLAM::Fundamental& in) { CLAM::TData newNote = in.GetCandidatesFreq()[0]; if (newNote<1.0) return true; std::vector<float>::iterator it = std::lower_bound(notes.begin(), notes.end(), newNote); notes.insert(it, newNote); return true; } bool ConcreteStop() {std::cout << _nNotes << std::endl;std::sort(notes.begin(), notes.end()); int median = notes[notes.size()/2];std::cout << median << std::endl; //std::copy(notes.begin(), notes.end(), std::ostream_iterator<float>(std::cout, "\n"));return 1; }};#endif // NoteCounter_hxx