annotate MonoNote.cpp @ 164:a7d9c6142f8f tip

Added tag v1.2 for changeset 4a97f7638ffd
author Chris Cannam
date Thu, 06 Feb 2020 15:02:47 +0000
parents 729cc1da9b8d
children
rev   line source
Chris@9 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@9 2
Chris@9 3 /*
Chris@9 4 pYIN - A fundamental frequency estimator for monophonic audio
Chris@9 5 Centre for Digital Music, Queen Mary, University of London.
Chris@9 6
Chris@9 7 This program is free software; you can redistribute it and/or
Chris@9 8 modify it under the terms of the GNU General Public License as
Chris@9 9 published by the Free Software Foundation; either version 2 of the
Chris@9 10 License, or (at your option) any later version. See the file
Chris@9 11 COPYING included with this distribution for more information.
Chris@9 12 */
Chris@9 13
matthiasm@0 14 #include "MonoNote.h"
matthiasm@0 15 #include <vector>
matthiasm@0 16
matthiasm@0 17 #include <cstdio>
matthiasm@0 18 #include <cmath>
matthiasm@0 19 #include <complex>
matthiasm@0 20
matthiasm@0 21 using std::vector;
matthiasm@0 22 using std::pair;
matthiasm@0 23
Chris@150 24 MonoNote::MonoNote(bool fixedLag) :
Chris@150 25 m_fixedLag(fixedLag),
Chris@150 26 hmm(m_fixedLag ? 1000 : 0)
matthiasm@0 27 {
matthiasm@0 28 }
matthiasm@0 29
matthiasm@0 30 MonoNote::~MonoNote()
matthiasm@0 31 {
matthiasm@0 32 }
matthiasm@0 33
matthiasm@0 34 const vector<MonoNote::FrameOutput>
matthiasm@0 35 MonoNote::process(const vector<vector<pair<double, double> > > pitchProb)
matthiasm@0 36 {
Chris@146 37 // Previously, this built up a single matrix of probabilities, by
Chris@146 38 // calling calculateObsProb to get a column for each frame in
Chris@146 39 // pitchProb.
Chris@146 40 //
Chris@146 41 // The number of distinct states depends on MonoNoteParameters,
Chris@146 42 // but the defaults have 3 states per pitch, 3 pitches per MIDI
Chris@146 43 // note, and 69 MIDI notes, giving 681 states per frame. With a
Chris@146 44 // frame step size of 256 at 44100Hz sample rate, a 3-minute song
Chris@146 45 // has about 30K frames leading to a 20 million element
Chris@146 46 // probability matrix.
Chris@146 47 //
Chris@146 48 // Since the matrix is very sparse, we can avoid some of this by
Chris@146 49 // feeding the (sparse implementation of) HMM one column at a
Chris@146 50 // time.
Chris@146 51
Chris@146 52 vector<int> path;
Chris@150 53 path.reserve(pitchProb.size());
Chris@146 54
Chris@146 55 if (!pitchProb.empty()) {
Chris@146 56
Chris@146 57 hmm.initialise(hmm.calculateObsProb(pitchProb[0]));
Chris@146 58
Chris@146 59 for (size_t iFrame = 1; iFrame < pitchProb.size(); ++iFrame)
Chris@146 60 {
Chris@150 61 if (m_fixedLag && (int(iFrame) >= hmm.m_fixedLag))
Chris@150 62 {
Chris@150 63 vector<int> rawPath = hmm.track();
Chris@150 64 path.push_back(rawPath[0]);
Chris@150 65 }
Chris@150 66
Chris@146 67 hmm.process(hmm.calculateObsProb(pitchProb[iFrame]));
Chris@146 68 }
Chris@146 69
Chris@150 70 vector<int> rawPath = hmm.track();
Chris@150 71 path.insert(path.end(), rawPath.begin(), rawPath.end());
matthiasm@0 72 }
matthiasm@0 73
Chris@150 74 vector<MonoNote::FrameOutput> out;
Chris@150 75 out.reserve(path.size());
Chris@146 76
matthiasm@0 77 for (size_t iFrame = 0; iFrame < path.size(); ++iFrame)
matthiasm@0 78 {
matthiasm@0 79 double currPitch = -1;
matthiasm@0 80 int stateKind = 0;
matthiasm@0 81
matthiasm@0 82 currPitch = hmm.par.minPitch + (path[iFrame]/hmm.par.nSPP) * 1.0/hmm.par.nPPS;
matthiasm@0 83 stateKind = (path[iFrame]) % hmm.par.nSPP + 1;
matthiasm@0 84
matthiasm@0 85 out.push_back(FrameOutput(iFrame, currPitch, stateKind));
matthiasm@0 86 }
Chris@146 87
matthiasm@0 88 return(out);
Chris@9 89 }