annotate chromamethods.cpp @ 115:526250b06fe0 monophonicness

some comments, some variable renamings
author Matthias Mauch <mail@matthiasmauch.net>
date Thu, 31 Mar 2011 14:26:53 +0100
parents 846b552ea3b0
children 7a8956e903e1
rev   line source
Chris@35 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@35 2
Chris@35 3 /*
Chris@35 4 NNLS-Chroma / Chordino
Chris@35 5
Chris@35 6 Audio feature extraction plugins for chromagram and chord
Chris@35 7 estimation.
Chris@35 8
Chris@35 9 Centre for Digital Music, Queen Mary University of London.
Chris@35 10 This file copyright 2008-2010 Matthias Mauch and QMUL.
Chris@35 11
Chris@35 12 This program is free software; you can redistribute it and/or
Chris@35 13 modify it under the terms of the GNU General Public License as
Chris@35 14 published by the Free Software Foundation; either version 2 of the
Chris@35 15 License, or (at your option) any later version. See the file
Chris@35 16 COPYING included with this distribution for more information.
Chris@35 17 */
Chris@35 18
Chris@27 19 #include "chromamethods.h"
Chris@27 20
Chris@27 21 #include <cmath>
Chris@27 22 #include <list>
Chris@27 23 #include <iostream>
Chris@27 24 #include <fstream>
Chris@27 25 #include <sstream>
Chris@27 26 #include <cassert>
Chris@27 27 #include <cstdlib>
Chris@27 28 #include <cstdio>
Chris@27 29 #include <boost/tokenizer.hpp>
Chris@27 30 #include <boost/iostreams/device/file.hpp>
Chris@27 31 #include <boost/iostreams/stream.hpp>
Chris@27 32 #include <boost/lexical_cast.hpp>
Chris@27 33
Chris@27 34 #include "chorddict.cpp"
Chris@27 35
Chris@27 36 using namespace std;
Chris@27 37 using namespace boost;
Chris@27 38
Chris@27 39
Chris@27 40 /** Special Convolution
mail@115 41 Special convolution is as long as the convolvee, i.e. the first argument.
mail@115 42 In the "valid" core part of the convolution it contains the usual convolution
mail@115 43 values, but the parts at the beginning (ending) that would normally be
mail@115 44 calculated using zero padding simply have the same values as the first
mail@115 45 (last) valid convolution bin.
Chris@27 46 **/
Chris@27 47
Chris@27 48 vector<float> SpecialConvolution(vector<float> convolvee, vector<float> kernel)
Chris@27 49 {
Chris@27 50 float s;
Chris@27 51 int m, n;
Chris@27 52 int lenConvolvee = convolvee.size();
Chris@27 53 int lenKernel = kernel.size();
Chris@27 54
mail@77 55 vector<float> Z(nNote,0);
Chris@27 56 assert(lenKernel % 2 != 0); // no exception handling !!!
Chris@27 57
Chris@27 58 for (n = lenKernel - 1; n < lenConvolvee; n++) {
Chris@27 59 s=0.0;
Chris@27 60 for (m = 0; m < lenKernel; m++) {
Chris@27 61 // cerr << "m = " << m << ", n = " << n << ", n-m = " << (n-m) << '\n';
Chris@27 62 s += convolvee[n-m] * kernel[m];
Chris@27 63 // if (debug_on) cerr << "--> s = " << s << '\n';
Chris@27 64 }
Chris@27 65 // cerr << n - lenKernel/2 << endl;
Chris@27 66 Z[n -lenKernel/2] = s;
Chris@27 67 }
Chris@27 68
Chris@27 69 // fill upper and lower pads
Chris@27 70 for (n = 0; n < lenKernel/2; n++) Z[n] = Z[lenKernel/2];
Chris@27 71 for (n = lenConvolvee; n < lenConvolvee +lenKernel/2; n++) Z[n - lenKernel/2] =
Chris@27 72 Z[lenConvolvee - lenKernel/2 - 1];
Chris@27 73 return Z;
Chris@27 74 }
Chris@27 75
Chris@27 76 float cospuls(float x, float centre, float width)
Chris@27 77 {
Chris@27 78 float recipwidth = 1.0/width;
Chris@27 79 if (abs(x - centre) <= 0.5 * width) {
Chris@27 80 return cos((x-centre)*2*M_PI*recipwidth)*.5+.5;
Chris@27 81 }
Chris@27 82 return 0.0;
Chris@27 83 }
Chris@27 84
Chris@27 85 float pitchCospuls(float x, float centre, int binsperoctave)
Chris@27 86 {
Chris@27 87 float warpedf = -binsperoctave * (log2(centre) - log2(x));
Chris@27 88 float out = cospuls(warpedf, 0.0, 2.0);
Chris@27 89 // now scale to correct for note density
Chris@27 90 float c = log(2.0)/binsperoctave;
Chris@27 91 if (x > 0) {
Chris@27 92 out = out / (c * x);
Chris@27 93 } else {
Chris@27 94 out = 0;
Chris@27 95 }
Chris@27 96 return out;
Chris@27 97 }
Chris@27 98
mail@115 99 /**
mail@115 100 * Calculates a matrix that can be used to linearly map from the magnitude spectrum to a pitch-scale spectrum.
mail@115 101 * @return this always returns true, which is a bit stupid, really. The main purpose of the function is to change the values in the "matrix" pointed to by *outmatrix
mail@115 102 */
Chris@27 103 bool logFreqMatrix(int fs, int blocksize, float *outmatrix) {
mail@115 104 // TODO: rewrite so that everyone understands what is done here.
mail@115 105 // TODO: make this more general, such that it works with all minoctave, maxoctave and whatever nBPS (or check if it already does)
Chris@27 106
mail@80 107 int binspersemitone = nBPS;
Chris@27 108 int minoctave = 0; // this must be 0
Chris@27 109 int maxoctave = 7; // this must be 7
Chris@27 110 int oversampling = 80;
Chris@27 111
Chris@27 112 // linear frequency vector
Chris@27 113 vector<float> fft_f;
Chris@27 114 for (int i = 0; i < blocksize/2; ++i) {
Chris@27 115 fft_f.push_back(i * (fs * 1.0 / blocksize));
Chris@27 116 }
Chris@27 117 float fft_width = fs * 2.0 / blocksize;
Chris@27 118
Chris@27 119 // linear oversampled frequency vector
Chris@27 120 vector<float> oversampled_f;
Chris@91 121 for (int i = 0; i < oversampling * blocksize/2; ++i) {
Chris@27 122 oversampled_f.push_back(i * ((fs * 1.0 / blocksize) / oversampling));
Chris@27 123 }
Chris@27 124
Chris@27 125 // pitch-spaced frequency vector
Chris@27 126 int minMIDI = 21 + minoctave * 12 - 1; // this includes one additional semitone!
Chris@27 127 int maxMIDI = 21 + maxoctave * 12; // this includes one additional semitone!
Chris@27 128 vector<float> cq_f;
Chris@27 129 float oob = 1.0/binspersemitone; // one over binspersemitone
mail@80 130 for (int i = minMIDI; i < maxMIDI; ++i) {
mail@80 131 for (int k = 0; k < binspersemitone; ++k) {
Chris@27 132 cq_f.push_back(440 * pow(2.0,0.083333333333 * (i+oob*k-69)));
Chris@27 133 }
Chris@27 134 }
mail@80 135 // cq_f.push_back(440 * pow(2.0,0.083333 * (minMIDI-oob-69)));
Chris@27 136 cq_f.push_back(440 * pow(2.0,0.083333 * (maxMIDI-69)));
Chris@27 137
Chris@27 138 int nFFT = fft_f.size();
Chris@27 139
Chris@27 140 vector<float> fft_activation;
Chris@27 141 for (int iOS = 0; iOS < 2 * oversampling; ++iOS) {
Chris@27 142 float cosp = cospuls(oversampled_f[iOS],fft_f[1],fft_width);
Chris@27 143 fft_activation.push_back(cosp);
Chris@27 144 // cerr << cosp << endl;
Chris@27 145 }
Chris@27 146
Chris@27 147 float cq_activation;
Chris@27 148 for (int iFFT = 1; iFFT < nFFT; ++iFFT) {
Chris@27 149 // find frequency stretch where the oversampled vector can be non-zero (i.e. in a window of width fft_width around the current frequency)
Chris@27 150 int curr_start = oversampling * iFFT - oversampling;
Chris@27 151 int curr_end = oversampling * iFFT + oversampling; // don't know if I should add "+1" here
Chris@27 152 // cerr << oversampled_f[curr_start] << " " << fft_f[iFFT] << " " << oversampled_f[curr_end] << endl;
Chris@27 153 for (unsigned iCQ = 0; iCQ < cq_f.size(); ++iCQ) {
Chris@27 154 outmatrix[iFFT + nFFT * iCQ] = 0;
Chris@27 155 if (cq_f[iCQ] * pow(2.0, 0.084) + fft_width > fft_f[iFFT] && cq_f[iCQ] * pow(2.0, -0.084 * 2) - fft_width < fft_f[iFFT]) { // within a generous neighbourhood
Chris@27 156 for (int iOS = curr_start; iOS < curr_end; ++iOS) {
Chris@27 157 cq_activation = pitchCospuls(oversampled_f[iOS],cq_f[iCQ],binspersemitone*12);
Chris@27 158 // cerr << oversampled_f[iOS] << " " << cq_f[iCQ] << " " << cq_activation << endl;
Chris@27 159 outmatrix[iFFT + nFFT * iCQ] += cq_activation * fft_activation[iOS-curr_start];
Chris@27 160 }
mail@115 161 }
Chris@27 162 }
Chris@27 163 }
Chris@27 164 return true;
Chris@27 165 }
Chris@27 166
mail@41 167 void dictionaryMatrix(float* dm, float s_param) {
mail@115 168 // TODO: make this more general, such that it works with all minoctave, maxoctave and even more than one note per semitone
mail@80 169 int binspersemitone = nBPS;
Chris@27 170 int minoctave = 0; // this must be 0
Chris@27 171 int maxoctave = 7; // this must be 7
Chris@27 172
Chris@27 173 // pitch-spaced frequency vector
Chris@27 174 int minMIDI = 21 + minoctave * 12 - 1; // this includes one additional semitone!
Chris@27 175 int maxMIDI = 21 + maxoctave * 12; // this includes one additional semitone!
Chris@27 176 vector<float> cq_f;
Chris@27 177 float oob = 1.0/binspersemitone; // one over binspersemitone
mail@80 178 for (int i = minMIDI; i < maxMIDI; ++i) {
mail@80 179 for (int k = 0; k < binspersemitone; ++k) {
Chris@27 180 cq_f.push_back(440 * pow(2.0,0.083333333333 * (i+oob*k-69)));
Chris@27 181 }
Chris@27 182 }
Chris@27 183 cq_f.push_back(440 * pow(2.0,0.083333 * (maxMIDI-69)));
Chris@27 184
Chris@27 185 float curr_f;
Chris@27 186 float floatbin;
Chris@27 187 float curr_amp;
Chris@27 188 // now for every combination calculate the matrix element
Chris@91 189 for (int iOut = 0; iOut < 12 * (maxoctave - minoctave); ++iOut) {
Chris@27 190 // cerr << iOut << endl;
Chris@91 191 for (int iHarm = 1; iHarm <= 20; ++iHarm) {
Chris@27 192 curr_f = 440 * pow(2,(minMIDI-69+iOut)*1.0/12) * iHarm;
Chris@27 193 // if (curr_f > cq_f[nNote-1]) break;
Chris@27 194 floatbin = ((iOut + 1) * binspersemitone + 1) + binspersemitone * 12 * log2(iHarm);
Chris@27 195 // cerr << floatbin << endl;
Chris@27 196 curr_amp = pow(s_param,float(iHarm-1));
Chris@27 197 // cerr << "curramp" << curr_amp << endl;
Chris@91 198 for (int iNote = 0; iNote < nNote; ++iNote) {
Chris@27 199 if (abs(iNote+1.0-floatbin)<2) {
mail@77 200 dm[iNote + nNote * iOut] += cospuls(iNote+1.0, floatbin, binspersemitone + 0.0) * curr_amp;
Chris@27 201 // dm[iNote + nNote * iOut] += 1 * curr_amp;
Chris@27 202 }
Chris@27 203 }
Chris@27 204 }
Chris@27 205 }
Chris@27 206 }
Chris@27 207
Chris@30 208 static
Chris@30 209 std::vector<std::string>
Chris@30 210 getPluginPath()
Chris@30 211 {
Chris@30 212 //!!! This is duplicated from PluginHostAdapter::getPluginPath,
Chris@30 213 //!!! which is not available to us in the plugin (only to the
Chris@30 214 //!!! host)
Chris@30 215
Chris@30 216 std::vector<std::string> path;
Chris@30 217 std::string envPath;
Chris@30 218
Chris@30 219 char *cpath = getenv("VAMP_PATH");
Chris@30 220 if (cpath) envPath = cpath;
Chris@30 221
Chris@30 222 #ifdef _WIN32
Chris@30 223 #define PATH_SEPARATOR ';'
Chris@30 224 #define DEFAULT_VAMP_PATH "%ProgramFiles%\\Vamp Plugins"
Chris@30 225 #else
Chris@30 226 #define PATH_SEPARATOR ':'
Chris@30 227 #ifdef __APPLE__
Chris@30 228 #define DEFAULT_VAMP_PATH "$HOME/Library/Audio/Plug-Ins/Vamp:/Library/Audio/Plug-Ins/Vamp"
Chris@30 229 #else
Chris@30 230 #define DEFAULT_VAMP_PATH "$HOME/vamp:$HOME/.vamp:/usr/local/lib/vamp:/usr/lib/vamp"
Chris@30 231 #endif
Chris@30 232 #endif
Chris@30 233
Chris@30 234 if (envPath == "") {
Chris@30 235 envPath = DEFAULT_VAMP_PATH;
Chris@30 236 char *chome = getenv("HOME");
Chris@30 237 if (chome) {
Chris@30 238 std::string home(chome);
Chris@30 239 std::string::size_type f;
Chris@30 240 while ((f = envPath.find("$HOME")) != std::string::npos &&
Chris@30 241 f < envPath.length()) {
Chris@30 242 envPath.replace(f, 5, home);
Chris@30 243 }
Chris@30 244 }
Chris@30 245 #ifdef _WIN32
Chris@30 246 char *cpfiles = getenv("ProgramFiles");
Chris@30 247 if (!cpfiles) cpfiles = (char *)"C:\\Program Files";
Chris@30 248 std::string pfiles(cpfiles);
Chris@30 249 std::string::size_type f;
Chris@30 250 while ((f = envPath.find("%ProgramFiles%")) != std::string::npos &&
Chris@30 251 f < envPath.length()) {
Chris@30 252 envPath.replace(f, 14, pfiles);
Chris@30 253 }
Chris@30 254 #endif
Chris@30 255 }
Chris@30 256
Chris@30 257 std::string::size_type index = 0, newindex = 0;
Chris@30 258
Chris@30 259 while ((newindex = envPath.find(PATH_SEPARATOR, index)) < envPath.size()) {
Chris@30 260 path.push_back(envPath.substr(index, newindex - index));
Chris@30 261 index = newindex + 1;
Chris@30 262 }
Chris@30 263
Chris@30 264 path.push_back(envPath.substr(index));
Chris@30 265
Chris@30 266 return path;
Chris@30 267 }
Chris@27 268
mail@90 269
mail@90 270
mail@90 271 static vector<string> staticChordnames() {
mail@90 272 vector<string> chordnames;
mail@90 273 chordnames.push_back("");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@90 274 chordnames.push_back("");// =0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@90 275 chordnames.push_back("m");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0
mail@90 276 chordnames.push_back("m");//=0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0
mail@112 277 chordnames.push_back("hdim7");//=0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,1,0,0,0,1,0
mail@112 278 chordnames.push_back("hdim7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1,0
mail@90 279 chordnames.push_back("6");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0
mail@90 280 chordnames.push_back("7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0
mail@90 281 chordnames.push_back("maj7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1
mail@90 282 chordnames.push_back("m7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0
mail@90 283 chordnames.push_back("m6");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,1,0,0
mail@90 284 chordnames.push_back("");//=0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@90 285 chordnames.push_back("");//=0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@90 286 chordnames.push_back("");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0
mail@90 287 chordnames.push_back("aug");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0
mail@90 288 chordnames.push_back("");//=0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@90 289 chordnames.push_back("");//=0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@90 290 chordnames.push_back("7");//=0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0
mail@112 291 // from here: Harte syntax
mail@112 292 chordnames.push_back("");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@112 293 chordnames.push_back("");// =0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@112 294 chordnames.push_back(":min");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0
mail@112 295 chordnames.push_back(":min");//=0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0
mail@112 296 chordnames.push_back(":hdim7");//=0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,1,0,0,0,1,0
mail@112 297 chordnames.push_back(":hdim7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1,0
mail@112 298 chordnames.push_back(":maj6");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0
mail@112 299 chordnames.push_back(":7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0
mail@112 300 chordnames.push_back(":maj7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1
mail@112 301 chordnames.push_back(":min7");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0
mail@112 302 chordnames.push_back(":min6");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,1,0,0
mail@112 303 chordnames.push_back("");//=0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@112 304 chordnames.push_back("");//=0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@112 305 chordnames.push_back("");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0
mail@112 306 chordnames.push_back(":aug");//=1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0
mail@112 307 chordnames.push_back("");//=0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@112 308 chordnames.push_back("");//=0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,0,0,0,0
mail@112 309 chordnames.push_back(":7");//=0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0
mail@112 310 // TODO: what about a dim chord?
mail@90 311 return chordnames;
mail@90 312 }
mail@90 313
mail@90 314 static vector<float> staticChordvalues() {
mail@90 315 vector<float> chordvalues;
mail@90 316 float chordvaluearray[] = {1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,
mail@90 317 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,
mail@90 318 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,
mail@90 319 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,
mail@90 320 0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,1,0,0,0,1,0,
mail@90 321 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1,0,
mail@90 322 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0,
mail@90 323 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0,
mail@90 324 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,
mail@90 325 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,
mail@90 326 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,1,0,0,
mail@90 327 0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,
mail@90 328 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,
mail@90 329 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
mail@90 330 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,
mail@90 331 0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,
mail@90 332 0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,0,0,0,0,
mail@90 333 0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0};
mail@90 334 for (int iChord = 0; iChord < 18; ++iChord) {
mail@90 335 for (int iNote = 0; iNote < 24; iNote++) {
mail@90 336 chordvalues.push_back(chordvaluearray[24*iChord+iNote]);
mail@90 337 }
mail@90 338 }
mail@90 339 return chordvalues;
mail@90 340 }
mail@90 341
mail@115 342 vector<string> chordDictionary(vector<float> *mchorddict, vector<vector<int> > *m_chordnotes, float boostN, float harte_syntax) {
matthiasm@86 343
Chris@27 344 typedef tokenizer<char_separator<char> > Tok;
Chris@27 345 char_separator<char> sep(",; ","=");
Chris@30 346
Chris@30 347 string chordDictBase("chord.dict");
Chris@30 348 string chordDictFilename;
Chris@30 349
Chris@30 350 vector<string> ppath = getPluginPath();
mail@90 351
mail@90 352 bool hasExternalDictinoary = true;
mail@90 353
Chris@91 354 for (size_t i = 0; i < ppath.size(); ++i) {
mail@61 355 chordDictFilename = ppath[i] + "/" + chordDictBase;
mail@61 356 cerr << "Looking for chord.dict in " << chordDictFilename << "..." ;
mail@61 357 fstream fin;
mail@61 358 fin.open(chordDictFilename.c_str(),ios::in);
mail@61 359 if( fin.is_open() )
mail@61 360 {
mail@61 361 fin.close();
mail@61 362 cerr << " success." << endl;
mail@61 363 break;
mail@61 364 } else {
Chris@91 365 if (i+1 < ppath.size()) cerr << " (not found yet) ..." << endl;
mail@90 366 else {
mail@97 367 cerr << "* WARNING: failed to find chord dictionary, using default chord dictionary." << endl;
mail@90 368 hasExternalDictinoary = false;
mail@90 369 }
mail@61 370 }
Chris@30 371 }
Chris@30 372
Chris@30 373 iostreams::stream<iostreams::file_source> chordDictFile(chordDictFilename);
Chris@27 374 string line;
mail@89 375 // int iElement = 0;
Chris@27 376 int nChord = 0;
Chris@27 377
mail@112 378 vector<float> tempChordDict = staticChordvalues();
Chris@91 379 vector<string> tempChordNames = staticChordnames();
mail@115 380 if (harte_syntax == 1.0) {
mail@112 381 tempChordNames.erase(tempChordNames.begin(),tempChordNames.begin()+tempChordNames.size()/2);
mail@112 382 } else {
mail@112 383 tempChordNames.erase(tempChordNames.begin()+tempChordNames.size()/2,tempChordNames.begin()+tempChordNames.size());
mail@112 384 }
mail@112 385
Chris@27 386 vector<string> loadedChordNames;
Chris@27 387 vector<float> loadedChordDict;
mail@90 388 if (hasExternalDictinoary && chordDictFile.is_open()) {
mail@90 389 cerr << "-----------------> " << tempChordNames.size() << endl;
mail@90 390 tempChordDict.clear();
mail@90 391 tempChordNames.clear();
matthiasm@86 392 while (std::getline(chordDictFile, line)) { // loop over lines in chord.dict file
Chris@27 393 // first, get the chord definition
Chris@27 394 string chordType;
Chris@27 395 vector<float> tempPCVector;
Chris@27 396 // cerr << line << endl;
Chris@27 397 if (!line.empty() && line.substr(0,1) != "#") {
Chris@27 398 Tok tok(line, sep);
Chris@27 399 for(Tok::iterator tok_iter = tok.begin(); tok_iter != tok.end(); ++tok_iter) { // loop over line elements
Chris@27 400 string tempString = *tok_iter;
Chris@27 401 // cerr << tempString << endl;
mail@90 402 if (tok_iter == tok.begin()) { // either the chord name or a colon
Chris@27 403 if (tempString == "=") {
Chris@27 404 chordType = "";
Chris@27 405 } else {
Chris@27 406 chordType = tempString;
mail@90 407 tok_iter++;
Chris@27 408 }
Chris@27 409 } else {
mail@90 410 tempChordDict.push_back(lexical_cast<float>(*tok_iter));
Chris@27 411 }
mail@90 412 }
mail@90 413 tempChordNames.push_back(chordType);
mail@90 414 }
mail@90 415 }
mail@90 416 }
mail@90 417
mail@90 418
Chris@91 419 for (int iType = 0; iType < (int)tempChordNames.size(); ++iType) {
mail@90 420 // now make all 12 chords of every type
Chris@91 421 for (int iSemitone = 0; iSemitone < 12; iSemitone++) {
mail@90 422 vector<int> tempchordnotes;
mail@90 423 // add bass slash notation
mail@90 424 string slashNotation = "";
Chris@91 425 for (int kSemitone = 1; kSemitone < 12; kSemitone++) {
mail@90 426 if (tempChordDict[24*iType+(kSemitone) % 12] > 0.99) {
mail@115 427 if (harte_syntax == 0.0) {
mail@112 428 slashNotation = bassnames[iSemitone][kSemitone];
mail@112 429 } else {
mail@112 430 slashNotation = bassnames[12][kSemitone];
mail@112 431 }
Chris@27 432 }
Chris@27 433 }
mail@90 434 if (slashNotation=="") tempchordnotes.push_back(MIDI_basenote + (iSemitone+12) % 12);
Chris@91 435 for (int kSemitone = 0; kSemitone < 12; kSemitone++) { // bass pitch classes
mail@90 436 // cerr << ((kSemitone - iSemitone + 12) % 12) << endl;
mail@90 437 float bassValue = 0;
mail@90 438 if (tempChordDict[24*iType+(kSemitone - iSemitone + 12) % 12]==1) {
mail@90 439 bassValue = 1;
mail@90 440 tempchordnotes.push_back(MIDI_basenote + (kSemitone+12) % 12);
mail@90 441 } else {
mail@90 442 if (tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12] == 1) bassValue = 0.5;
mail@90 443 }
mail@90 444 loadedChordDict.push_back(bassValue);
mail@90 445 }
Chris@91 446 for (int kSemitone = 0; kSemitone < 12; kSemitone++) { // chord pitch classes
mail@90 447 loadedChordDict.push_back(tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12]);
mail@90 448 if (tempChordDict[24*iType+((kSemitone - iSemitone + 12) % 12) + 12] > 0) tempchordnotes.push_back(MIDI_basenote + (kSemitone+12+6) % 12 - 6 + 24);
mail@90 449 }
mail@90 450 ostringstream os;
mail@90 451 if (slashNotation.empty()) {
mail@90 452 os << notenames[12+iSemitone] << tempChordNames[iType];
mail@90 453 } else {
mail@90 454 os << notenames[12+iSemitone] << tempChordNames[iType] << "/" << slashNotation;
mail@90 455 }
mail@90 456 // cerr << os.str() << endl;
mail@90 457 loadedChordNames.push_back(os.str());
mail@90 458
mail@90 459 m_chordnotes->push_back(tempchordnotes);
mail@90 460 // for (int iNote = 0; iNote < tempchordnotes.size(); ++iNote) {
mail@90 461 // cerr << tempchordnotes[iNote] << " ";
mail@90 462 // }
mail@90 463 // cerr << endl;
Chris@27 464 }
mail@90 465 }
mail@90 466
mail@90 467
mail@90 468 // N type
mail@90 469 loadedChordNames.push_back("N");
mail@90 470 for (unsigned kSemitone = 0; kSemitone < 12; kSemitone++) loadedChordDict.push_back(0.5);
mail@90 471 for (unsigned kSemitone = 0; kSemitone < 12; kSemitone++) loadedChordDict.push_back(1.0);
mail@90 472 vector<int> tempchordvector;
mail@90 473 m_chordnotes->push_back(tempchordvector);
mail@90 474 float exponent = 2.0;
mail@90 475 // float m_boostN = 1.1;
mail@90 476 // cerr << " N BOOST : " << boostN << endl << endl;
Chris@91 477 for (int iChord = 0; iChord < (int)loadedChordDict.size()/24; iChord++) {
mail@90 478 float sum = 0;
mail@90 479 float stand = 0;
mail@90 480 for (int iST = 0; iST < 24; ++iST) {
mail@90 481 sum += loadedChordDict[24 * iChord + iST];
mail@90 482 }
mail@90 483 for (int iST = 0; iST < 24; ++iST) {
mail@90 484 // loadedChordDict[24 * iChord + iST] -= sum/24;
mail@90 485 stand += pow(abs(loadedChordDict[24 * iChord + iST]),exponent)/24;
mail@90 486 }
Chris@91 487 if (iChord < (int)loadedChordDict.size()/24 - 1) {
Chris@91 488 stand = powf(stand,1.0f/exponent);
mail@90 489 } else {
matthiasm@95 490 stand = powf(stand,1.0f/exponent) / (1+boostN);
mail@90 491 }
mail@90 492 for (int iST = 0; iST < 24; ++iST) {
mail@90 493 loadedChordDict[24 * iChord + iST] /= stand;
Chris@27 494 }
matthiasm@44 495
mail@90 496 }
mail@90 497
mail@90 498
mail@90 499
mail@90 500 nChord = 0;
Chris@91 501 for (int i = 0; i < (int)loadedChordNames.size(); i++) {
mail@90 502 nChord++;
mail@90 503 }
mail@90 504 chordDictFile.close();
mail@90 505
mail@90 506
mail@90 507 // mchorddict = new float[nChord*24];
mail@90 508 for (int i = 0; i < nChord*24; i++) {
mail@90 509 mchorddict->push_back(loadedChordDict[i]);
mail@90 510 }
Chris@27 511
Chris@27 512 // cerr << "before leaving" << chordnames[1] << endl;
Chris@27 513 return loadedChordNames;
Chris@27 514 }
mail@90 515
Chris@91 516