annotate chromamethods.cpp @ 112:846b552ea3b0 monophonicness

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