annotate MonoNote.cpp @ 146:8404827a4b02 memory

Avoid calculating a temporary obsprob matrix for note tracking; + some tidying
author Chris Cannam
date Wed, 17 May 2017 14:50:10 +0100
parents 83978b93aac1
children 729cc1da9b8d
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
matthiasm@0 24 MonoNote::MonoNote() :
mail@132 25 hmm(0)
matthiasm@0 26 {
matthiasm@0 27 }
matthiasm@0 28
matthiasm@0 29 MonoNote::~MonoNote()
matthiasm@0 30 {
matthiasm@0 31 }
matthiasm@0 32
matthiasm@0 33 const vector<MonoNote::FrameOutput>
matthiasm@0 34 MonoNote::process(const vector<vector<pair<double, double> > > pitchProb)
matthiasm@0 35 {
Chris@146 36 // Previously, this built up a single matrix of probabilities, by
Chris@146 37 // calling calculateObsProb to get a column for each frame in
Chris@146 38 // pitchProb.
Chris@146 39 //
Chris@146 40 // The number of distinct states depends on MonoNoteParameters,
Chris@146 41 // but the defaults have 3 states per pitch, 3 pitches per MIDI
Chris@146 42 // note, and 69 MIDI notes, giving 681 states per frame. With a
Chris@146 43 // frame step size of 256 at 44100Hz sample rate, a 3-minute song
Chris@146 44 // has about 30K frames leading to a 20 million element
Chris@146 45 // probability matrix.
Chris@146 46 //
Chris@146 47 // Since the matrix is very sparse, we can avoid some of this by
Chris@146 48 // feeding the (sparse implementation of) HMM one column at a
Chris@146 49 // time.
Chris@146 50
Chris@146 51 vector<int> path;
Chris@146 52
Chris@146 53 if (!pitchProb.empty()) {
Chris@146 54
Chris@146 55 hmm.initialise(hmm.calculateObsProb(pitchProb[0]));
Chris@146 56
Chris@146 57 for (size_t iFrame = 1; iFrame < pitchProb.size(); ++iFrame)
Chris@146 58 {
Chris@146 59 hmm.process(hmm.calculateObsProb(pitchProb[iFrame]));
Chris@146 60 }
Chris@146 61
Chris@146 62 path = hmm.track();
matthiasm@0 63 }
matthiasm@0 64
matthiasm@0 65 vector<MonoNote::FrameOutput> out;
Chris@146 66
matthiasm@0 67 for (size_t iFrame = 0; iFrame < path.size(); ++iFrame)
matthiasm@0 68 {
matthiasm@0 69 double currPitch = -1;
matthiasm@0 70 int stateKind = 0;
matthiasm@0 71
matthiasm@0 72 currPitch = hmm.par.minPitch + (path[iFrame]/hmm.par.nSPP) * 1.0/hmm.par.nPPS;
matthiasm@0 73 stateKind = (path[iFrame]) % hmm.par.nSPP + 1;
matthiasm@0 74
matthiasm@0 75 out.push_back(FrameOutput(iFrame, currPitch, stateKind));
matthiasm@0 76 }
Chris@146 77
matthiasm@0 78 return(out);
Chris@9 79 }