Chris@23: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ matthiasm@0: matthiasm@0: #include "NNLSChroma.h" Chris@27: Chris@27: #include "chromamethods.h" Chris@27: Chris@27: #include Chris@27: #include matthiasm@0: #include matthiasm@9: Chris@27: #include matthiasm@0: matthiasm@0: const bool debug_on = false; matthiasm@0: Chris@27: const vector hw(hammingwind, hammingwind+19); matthiasm@0: matthiasm@0: NNLSChroma::NNLSChroma(float inputSampleRate) : Chris@23: Plugin(inputSampleRate), Chris@23: m_fl(0), Chris@23: m_blockSize(0), Chris@23: m_stepSize(0), Chris@23: m_lengthOfNoteIndex(0), Chris@23: m_meanTuning0(0), Chris@23: m_meanTuning1(0), Chris@23: m_meanTuning2(0), Chris@23: m_localTuning0(0), Chris@23: m_localTuning1(0), Chris@23: m_localTuning2(0), Chris@23: m_paling(1.0), Chris@23: m_preset(0.0), Chris@23: m_localTuning(0), Chris@23: m_kernelValue(0), Chris@23: m_kernelFftIndex(0), Chris@23: m_kernelNoteIndex(0), Chris@23: m_dict(0), Chris@23: m_tuneLocal(false), Chris@23: m_dictID(0), Chris@23: m_chorddict(0), Chris@23: m_chordnames(0), Chris@23: m_doNormalizeChroma(0), Chris@23: m_rollon(0.01) matthiasm@0: { Chris@23: if (debug_on) cerr << "--> NNLSChroma" << endl; matthiasm@7: Chris@23: // make the *note* dictionary matrix Chris@23: m_dict = new float[nNote * 84]; Chris@23: for (unsigned i = 0; i < nNote * 84; ++i) m_dict[i] = 0.0; Chris@23: dictionaryMatrix(m_dict); matthiasm@7: Chris@23: // get the *chord* dictionary from file (if the file exists) Chris@23: m_chordnames = chordDictionary(&m_chorddict); matthiasm@0: } matthiasm@0: matthiasm@0: matthiasm@0: NNLSChroma::~NNLSChroma() matthiasm@0: { Chris@23: if (debug_on) cerr << "--> ~NNLSChroma" << endl; Chris@23: delete [] m_dict; Chris@23: // delete [] m_chorddict; Chris@23: // delete m_chordnames; matthiasm@0: } matthiasm@0: matthiasm@0: string matthiasm@0: NNLSChroma::getIdentifier() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getIdentifier" << endl; matthiasm@0: return "nnls_chroma"; matthiasm@0: } matthiasm@0: matthiasm@0: string matthiasm@0: NNLSChroma::getName() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getName" << endl; matthiasm@0: return "NNLS Chroma"; matthiasm@0: } matthiasm@0: matthiasm@0: string matthiasm@0: NNLSChroma::getDescription() const matthiasm@0: { matthiasm@0: // Return something helpful here! Chris@23: if (debug_on) cerr << "--> getDescription" << endl; matthiasm@13: return "This plugin provides a number of features derived from a log-frequency amplitude spectrum of the DFT: some variants of the log-frequency spectrum, including a semitone spectrum derived from approximate transcription using the NNLS algorithm; based on this semitone spectrum, chroma features and a simple chord estimate."; matthiasm@0: } matthiasm@0: matthiasm@0: string matthiasm@0: NNLSChroma::getMaker() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getMaker" << endl; matthiasm@0: // Your name here matthiasm@0: return "Matthias Mauch"; matthiasm@0: } matthiasm@0: matthiasm@0: int matthiasm@0: NNLSChroma::getPluginVersion() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getPluginVersion" << endl; matthiasm@0: // Increment this each time you release a version that behaves matthiasm@0: // differently from the previous one matthiasm@0: return 1; matthiasm@0: } matthiasm@0: matthiasm@0: string matthiasm@0: NNLSChroma::getCopyright() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getCopyright" << endl; matthiasm@0: // This function is not ideally named. It does not necessarily matthiasm@0: // need to say who made the plugin -- getMaker does that -- but it matthiasm@0: // should indicate the terms under which it is distributed. For matthiasm@0: // example, "Copyright (year). All Rights Reserved", or "GPL" matthiasm@0: return "Copyright (2010). All rights reserved."; matthiasm@0: } matthiasm@0: matthiasm@0: NNLSChroma::InputDomain matthiasm@0: NNLSChroma::getInputDomain() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getInputDomain" << endl; matthiasm@0: return FrequencyDomain; matthiasm@0: } matthiasm@0: matthiasm@0: size_t matthiasm@0: NNLSChroma::getPreferredBlockSize() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getPreferredBlockSize" << endl; matthiasm@0: return 16384; // 0 means "I can handle any block size" matthiasm@0: } matthiasm@0: matthiasm@0: size_t matthiasm@0: NNLSChroma::getPreferredStepSize() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getPreferredStepSize" << endl; matthiasm@0: return 2048; // 0 means "anything sensible"; in practice this Chris@23: // means the same as the block size for TimeDomain Chris@23: // plugins, or half of it for FrequencyDomain plugins matthiasm@0: } matthiasm@0: matthiasm@0: size_t matthiasm@0: NNLSChroma::getMinChannelCount() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getMinChannelCount" << endl; matthiasm@0: return 1; matthiasm@0: } matthiasm@0: matthiasm@0: size_t matthiasm@0: NNLSChroma::getMaxChannelCount() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getMaxChannelCount" << endl; matthiasm@0: return 1; matthiasm@0: } matthiasm@0: matthiasm@0: NNLSChroma::ParameterList matthiasm@0: NNLSChroma::getParameterDescriptors() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getParameterDescriptors" << endl; matthiasm@0: ParameterList list; matthiasm@0: matthiasm@3: ParameterDescriptor d3; matthiasm@3: d3.identifier = "preset"; matthiasm@3: d3.name = "preset"; matthiasm@3: d3.description = "Spectral paling: no paling - 0; whitening - 1."; matthiasm@3: d3.unit = ""; Chris@23: d3.isQuantized = true; Chris@23: d3.quantizeStep = 1; matthiasm@3: d3.minValue = 0.0; matthiasm@4: d3.maxValue = 3.0; matthiasm@3: d3.defaultValue = 0.0; matthiasm@3: d3.valueNames.push_back("polyphonic pop"); Chris@23: d3.valueNames.push_back("polyphonic pop (fast)"); matthiasm@3: d3.valueNames.push_back("solo keyboard"); Chris@23: d3.valueNames.push_back("manual"); matthiasm@3: list.push_back(d3); matthiasm@4: matthiasm@17: ParameterDescriptor d5; Chris@23: d5.identifier = "rollon"; Chris@23: d5.name = "spectral roll-on"; Chris@23: d5.description = "The bins below the spectral roll-on quantile will be set to 0."; Chris@23: d5.unit = ""; Chris@23: d5.minValue = 0; Chris@23: d5.maxValue = 1; Chris@23: d5.defaultValue = 0; Chris@23: d5.isQuantized = false; Chris@23: list.push_back(d5); matthiasm@17: matthiasm@4: // ParameterDescriptor d0; matthiasm@4: // d0.identifier = "notedict"; matthiasm@4: // d0.name = "note dictionary"; matthiasm@4: // d0.description = "Notes in different note dictionaries differ by their spectral shapes."; matthiasm@4: // d0.unit = ""; matthiasm@4: // d0.minValue = 0; matthiasm@4: // d0.maxValue = 1; matthiasm@4: // d0.defaultValue = 0; matthiasm@4: // d0.isQuantized = true; matthiasm@4: // d0.valueNames.push_back("s = 0.6"); matthiasm@4: // d0.valueNames.push_back("no NNLS"); matthiasm@4: // d0.quantizeStep = 1.0; matthiasm@4: // list.push_back(d0); matthiasm@4: matthiasm@4: ParameterDescriptor d1; matthiasm@4: d1.identifier = "tuningmode"; matthiasm@4: d1.name = "tuning mode"; matthiasm@4: d1.description = "Tuning can be performed locally or on the whole extraction segment. Local tuning is only advisable when the tuning is likely to change over the audio, for example in podcasts, or in a cappella singing."; matthiasm@4: d1.unit = ""; matthiasm@4: d1.minValue = 0; matthiasm@4: d1.maxValue = 1; matthiasm@4: d1.defaultValue = 0; matthiasm@4: d1.isQuantized = true; matthiasm@4: d1.valueNames.push_back("global tuning"); matthiasm@4: d1.valueNames.push_back("local tuning"); matthiasm@4: d1.quantizeStep = 1.0; matthiasm@4: list.push_back(d1); matthiasm@4: Chris@23: // ParameterDescriptor d2; Chris@23: // d2.identifier = "paling"; Chris@23: // d2.name = "spectral paling"; Chris@23: // d2.description = "Spectral paling: no paling - 0; whitening - 1."; Chris@23: // d2.unit = ""; Chris@23: // d2.isQuantized = true; Chris@23: // // d2.quantizeStep = 0.1; Chris@23: // d2.minValue = 0.0; Chris@23: // d2.maxValue = 1.0; Chris@23: // d2.defaultValue = 1.0; Chris@23: // d2.isQuantized = false; Chris@23: // list.push_back(d2); Chris@23: ParameterDescriptor d4; matthiasm@12: d4.identifier = "chromanormalize"; matthiasm@12: d4.name = "chroma normalization"; matthiasm@12: d4.description = "How shall the chroma vector be normalized?"; matthiasm@12: d4.unit = ""; matthiasm@12: d4.minValue = 0; matthiasm@13: d4.maxValue = 3; matthiasm@12: d4.defaultValue = 0; matthiasm@12: d4.isQuantized = true; matthiasm@13: d4.valueNames.push_back("none"); matthiasm@13: d4.valueNames.push_back("maximum norm"); Chris@23: d4.valueNames.push_back("L1 norm"); Chris@23: d4.valueNames.push_back("L2 norm"); matthiasm@12: d4.quantizeStep = 1.0; matthiasm@12: list.push_back(d4); matthiasm@4: matthiasm@0: return list; matthiasm@0: } matthiasm@0: matthiasm@0: float matthiasm@0: NNLSChroma::getParameter(string identifier) const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getParameter" << endl; matthiasm@0: if (identifier == "notedict") { matthiasm@0: return m_dictID; matthiasm@0: } matthiasm@0: matthiasm@0: if (identifier == "paling") { matthiasm@0: return m_paling; matthiasm@0: } matthiasm@17: Chris@23: if (identifier == "rollon") { matthiasm@17: return m_rollon; matthiasm@17: } matthiasm@0: matthiasm@0: if (identifier == "tuningmode") { matthiasm@0: if (m_tuneLocal) { matthiasm@0: return 1.0; matthiasm@0: } else { matthiasm@0: return 0.0; matthiasm@0: } matthiasm@0: } Chris@23: if (identifier == "preset") { Chris@23: return m_preset; matthiasm@3: } Chris@23: if (identifier == "chromanormalize") { Chris@23: return m_doNormalizeChroma; matthiasm@12: } matthiasm@0: return 0; matthiasm@0: matthiasm@0: } matthiasm@0: matthiasm@0: void matthiasm@0: NNLSChroma::setParameter(string identifier, float value) matthiasm@0: { Chris@23: if (debug_on) cerr << "--> setParameter" << endl; matthiasm@0: if (identifier == "notedict") { matthiasm@0: m_dictID = (int) value; matthiasm@0: } matthiasm@0: matthiasm@0: if (identifier == "paling") { matthiasm@0: m_paling = value; matthiasm@0: } matthiasm@0: matthiasm@0: if (identifier == "tuningmode") { matthiasm@0: m_tuneLocal = (value > 0) ? true : false; matthiasm@0: // cerr << "m_tuneLocal :" << m_tuneLocal << endl; matthiasm@0: } matthiasm@3: if (identifier == "preset") { matthiasm@3: m_preset = value; Chris@23: if (m_preset == 0.0) { Chris@23: m_tuneLocal = false; Chris@23: m_paling = 1.0; Chris@23: m_dictID = 0.0; Chris@23: } Chris@23: if (m_preset == 1.0) { Chris@23: m_tuneLocal = false; Chris@23: m_paling = 1.0; Chris@23: m_dictID = 1.0; Chris@23: } Chris@23: if (m_preset == 2.0) { Chris@23: m_tuneLocal = false; Chris@23: m_paling = 0.7; Chris@23: m_dictID = 0.0; Chris@23: } matthiasm@3: } Chris@23: if (identifier == "chromanormalize") { Chris@23: m_doNormalizeChroma = value; Chris@23: } matthiasm@17: Chris@23: if (identifier == "rollon") { Chris@23: m_rollon = value; Chris@23: } matthiasm@0: } matthiasm@0: matthiasm@0: NNLSChroma::ProgramList matthiasm@0: NNLSChroma::getPrograms() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getPrograms" << endl; matthiasm@0: ProgramList list; matthiasm@0: matthiasm@0: // If you have no programs, return an empty list (or simply don't matthiasm@0: // implement this function or getCurrentProgram/selectProgram) matthiasm@0: matthiasm@0: return list; matthiasm@0: } matthiasm@0: matthiasm@0: string matthiasm@0: NNLSChroma::getCurrentProgram() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getCurrentProgram" << endl; matthiasm@0: return ""; // no programs matthiasm@0: } matthiasm@0: matthiasm@0: void matthiasm@0: NNLSChroma::selectProgram(string name) matthiasm@0: { Chris@23: if (debug_on) cerr << "--> selectProgram" << endl; matthiasm@0: } matthiasm@0: matthiasm@0: matthiasm@0: NNLSChroma::OutputList matthiasm@0: NNLSChroma::getOutputDescriptors() const matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getOutputDescriptors" << endl; matthiasm@0: OutputList list; matthiasm@0: matthiasm@0: // Make chroma names for the binNames property matthiasm@0: vector chromanames; matthiasm@0: vector bothchromanames; matthiasm@0: for (int iNote = 0; iNote < 24; iNote++) { matthiasm@0: bothchromanames.push_back(notenames[iNote]); matthiasm@0: if (iNote < 12) { matthiasm@0: chromanames.push_back(notenames[iNote]); matthiasm@0: } matthiasm@0: } matthiasm@0: Chris@23: // int nNote = 84; matthiasm@0: matthiasm@0: // See OutputDescriptor documentation for the possibilities here. matthiasm@0: // Every plugin must have at least one output. matthiasm@0: matthiasm@0: OutputDescriptor d0; matthiasm@0: d0.identifier = "tuning"; matthiasm@0: d0.name = "Tuning"; matthiasm@0: d0.description = "The concert pitch."; matthiasm@0: d0.unit = "Hz"; matthiasm@0: d0.hasFixedBinCount = true; matthiasm@0: d0.binCount = 0; matthiasm@0: d0.hasKnownExtents = true; Chris@23: d0.minValue = 427.47; Chris@23: d0.maxValue = 452.89; matthiasm@0: d0.isQuantized = false; matthiasm@0: d0.sampleType = OutputDescriptor::VariableSampleRate; matthiasm@0: d0.hasDuration = false; matthiasm@0: list.push_back(d0); matthiasm@0: Chris@23: OutputDescriptor d1; matthiasm@0: d1.identifier = "logfreqspec"; matthiasm@0: d1.name = "Log-Frequency Spectrum"; matthiasm@0: d1.description = "A Log-Frequency Spectrum (constant Q) that is obtained by cosine filter mapping."; matthiasm@0: d1.unit = ""; matthiasm@0: d1.hasFixedBinCount = true; matthiasm@0: d1.binCount = nNote; matthiasm@0: d1.hasKnownExtents = false; matthiasm@0: d1.isQuantized = false; matthiasm@0: d1.sampleType = OutputDescriptor::FixedSampleRate; matthiasm@0: d1.hasDuration = false; matthiasm@0: d1.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@0: list.push_back(d1); matthiasm@0: Chris@23: OutputDescriptor d2; matthiasm@0: d2.identifier = "tunedlogfreqspec"; matthiasm@0: d2.name = "Tuned Log-Frequency Spectrum"; matthiasm@0: d2.description = "A Log-Frequency Spectrum (constant Q) that is obtained by cosine filter mapping, then its tuned using the estimated tuning frequency."; matthiasm@0: d2.unit = ""; matthiasm@0: d2.hasFixedBinCount = true; matthiasm@0: d2.binCount = 256; matthiasm@0: d2.hasKnownExtents = false; matthiasm@0: d2.isQuantized = false; matthiasm@0: d2.sampleType = OutputDescriptor::FixedSampleRate; matthiasm@0: d2.hasDuration = false; matthiasm@0: d2.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@0: list.push_back(d2); matthiasm@0: matthiasm@0: OutputDescriptor d3; matthiasm@0: d3.identifier = "semitonespectrum"; matthiasm@0: d3.name = "Semitone Spectrum"; matthiasm@0: d3.description = "A semitone-spaced log-frequency spectrum derived from the third-of-a-semitone-spaced tuned log-frequency spectrum."; matthiasm@0: d3.unit = ""; matthiasm@0: d3.hasFixedBinCount = true; matthiasm@0: d3.binCount = 84; matthiasm@0: d3.hasKnownExtents = false; matthiasm@0: d3.isQuantized = false; matthiasm@0: d3.sampleType = OutputDescriptor::FixedSampleRate; matthiasm@0: d3.hasDuration = false; matthiasm@0: d3.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@0: list.push_back(d3); matthiasm@0: matthiasm@0: OutputDescriptor d4; matthiasm@0: d4.identifier = "chroma"; matthiasm@0: d4.name = "Chromagram"; matthiasm@0: d4.description = "Tuning-adjusted chromagram from NNLS soft transcription, with an emphasis on the medium note range."; matthiasm@0: d4.unit = ""; matthiasm@0: d4.hasFixedBinCount = true; matthiasm@0: d4.binCount = 12; matthiasm@0: d4.binNames = chromanames; matthiasm@0: d4.hasKnownExtents = false; matthiasm@0: d4.isQuantized = false; matthiasm@0: d4.sampleType = OutputDescriptor::FixedSampleRate; matthiasm@0: d4.hasDuration = false; matthiasm@0: d4.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@0: list.push_back(d4); matthiasm@0: matthiasm@0: OutputDescriptor d5; matthiasm@0: d5.identifier = "basschroma"; matthiasm@0: d5.name = "Bass Chromagram"; matthiasm@0: d5.description = "Tuning-adjusted bass chromagram from NNLS soft transcription, with an emphasis on the bass note range."; matthiasm@0: d5.unit = ""; matthiasm@0: d5.hasFixedBinCount = true; matthiasm@0: d5.binCount = 12; matthiasm@0: d5.binNames = chromanames; matthiasm@0: d5.hasKnownExtents = false; matthiasm@0: d5.isQuantized = false; matthiasm@0: d5.sampleType = OutputDescriptor::FixedSampleRate; matthiasm@0: d5.hasDuration = false; matthiasm@0: d5.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@0: list.push_back(d5); matthiasm@0: matthiasm@0: OutputDescriptor d6; matthiasm@0: d6.identifier = "bothchroma"; matthiasm@0: d6.name = "Chromagram and Bass Chromagram"; matthiasm@0: d6.description = "Tuning-adjusted chromagram and bass chromagram (stacked on top of each other) from NNLS soft transcription."; matthiasm@0: d6.unit = ""; matthiasm@0: d6.hasFixedBinCount = true; matthiasm@0: d6.binCount = 24; matthiasm@0: d6.binNames = bothchromanames; matthiasm@0: d6.hasKnownExtents = false; matthiasm@0: d6.isQuantized = false; matthiasm@0: d6.sampleType = OutputDescriptor::FixedSampleRate; matthiasm@0: d6.hasDuration = false; matthiasm@0: d6.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@0: list.push_back(d6); matthiasm@0: matthiasm@0: OutputDescriptor d7; matthiasm@0: d7.identifier = "simplechord"; matthiasm@0: d7.name = "Simple Chord Estimate"; matthiasm@0: d7.description = "A simple chord estimate based on the inner product of chord templates with the smoothed chroma."; matthiasm@0: d7.unit = ""; matthiasm@0: d7.hasFixedBinCount = true; matthiasm@0: d7.binCount = 0; matthiasm@0: d7.hasKnownExtents = false; matthiasm@0: d7.isQuantized = false; matthiasm@0: d7.sampleType = OutputDescriptor::VariableSampleRate; matthiasm@0: d7.hasDuration = false; matthiasm@0: d7.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@0: list.push_back(d7); matthiasm@0: Chris@23: // Chris@23: // OutputDescriptor d9; Chris@23: // d9.identifier = "inconsistencysegment"; Chris@23: // d9.name = "Harmonic inconsistency segmenter"; Chris@23: // d9.description = "Segments the audio based on the harmonic inconsistency value into speech and music."; Chris@23: // d9.unit = ""; Chris@23: // d9.hasFixedBinCount = true; Chris@23: // d9.binCount = 0; Chris@23: // d9.hasKnownExtents = true; Chris@23: // d9.minValue = 0.1; Chris@23: // d9.maxValue = 0.9; Chris@23: // d9.isQuantized = false; Chris@23: // d9.sampleType = OutputDescriptor::VariableSampleRate; Chris@23: // d9.hasDuration = false; Chris@23: // d9.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; Chris@23: // list.push_back(d9); Chris@23: // Chris@23: OutputDescriptor d10; Chris@23: d10.identifier = "localtuning"; Chris@23: d10.name = "Local tuning"; Chris@23: d10.description = "Tuning based on the history up to this timestamp."; Chris@23: d10.unit = "Hz"; Chris@23: d10.hasFixedBinCount = true; Chris@23: d10.binCount = 1; Chris@23: d10.hasKnownExtents = true; Chris@23: d10.minValue = 427.47; Chris@23: d10.maxValue = 452.89; Chris@23: d10.isQuantized = false; Chris@23: d10.sampleType = OutputDescriptor::FixedSampleRate; Chris@23: d10.hasDuration = false; Chris@23: // d10.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; Chris@23: list.push_back(d10); matthiasm@17: Chris@23: OutputDescriptor d8; matthiasm@17: d8.identifier = "harmonicchange"; matthiasm@17: d8.name = "Harmonic change value"; matthiasm@17: d8.description = "Harmonic change."; matthiasm@17: d8.unit = ""; matthiasm@17: d8.hasFixedBinCount = true; matthiasm@17: d8.binCount = 1; matthiasm@17: d8.hasKnownExtents = true; Chris@23: d8.minValue = 0.0; Chris@23: d8.maxValue = 0.999; matthiasm@17: d8.isQuantized = false; matthiasm@17: d8.sampleType = OutputDescriptor::FixedSampleRate; matthiasm@17: d8.hasDuration = false; matthiasm@17: // d8.sampleRate = (m_stepSize == 0) ? m_inputSampleRate/2048 : m_inputSampleRate/m_stepSize; matthiasm@17: list.push_back(d8); matthiasm@1: matthiasm@0: return list; matthiasm@0: } matthiasm@0: matthiasm@0: matthiasm@0: bool matthiasm@0: NNLSChroma::initialise(size_t channels, size_t stepSize, size_t blockSize) matthiasm@0: { Chris@23: if (debug_on) { Chris@23: cerr << "--> initialise"; Chris@23: } matthiasm@1: matthiasm@0: if (channels < getMinChannelCount() || matthiasm@0: channels > getMaxChannelCount()) return false; matthiasm@0: m_blockSize = blockSize; matthiasm@0: m_stepSize = stepSize; matthiasm@0: frameCount = 0; Chris@23: int tempn = 256 * m_blockSize/2; Chris@23: // cerr << "length of tempkernel : " << tempn << endl; Chris@23: float *tempkernel; matthiasm@1: Chris@23: tempkernel = new float[tempn]; matthiasm@1: Chris@23: logFreqMatrix(m_inputSampleRate, m_blockSize, tempkernel); Chris@23: m_kernelValue.clear(); Chris@23: m_kernelFftIndex.clear(); Chris@23: m_kernelNoteIndex.clear(); Chris@23: int countNonzero = 0; Chris@23: for (unsigned iNote = 0; iNote < nNote; ++iNote) { // I don't know if this is wise: manually making a sparse matrix Chris@23: for (unsigned iFFT = 0; iFFT < blockSize/2; ++iFFT) { Chris@23: if (tempkernel[iFFT + blockSize/2 * iNote] > 0) { Chris@23: m_kernelValue.push_back(tempkernel[iFFT + blockSize/2 * iNote]); Chris@23: if (tempkernel[iFFT + blockSize/2 * iNote] > 0) { Chris@23: countNonzero++; Chris@23: } Chris@23: m_kernelFftIndex.push_back(iFFT); Chris@23: m_kernelNoteIndex.push_back(iNote); Chris@23: } Chris@23: } Chris@23: } Chris@23: // cerr << "nonzero count : " << countNonzero << endl; Chris@23: delete [] tempkernel; Chris@23: ofstream myfile; Chris@23: myfile.open ("matrix.txt"); matthiasm@3: // myfile << "Writing this to a file.\n"; Chris@23: for (int i = 0; i < nNote * 84; ++i) { Chris@23: myfile << m_dict[i] << endl; Chris@23: } matthiasm@3: myfile.close(); matthiasm@0: return true; matthiasm@0: } matthiasm@0: matthiasm@0: void matthiasm@0: NNLSChroma::reset() matthiasm@0: { Chris@23: if (debug_on) cerr << "--> reset"; matthiasm@4: matthiasm@0: // Clear buffers, reset stored values, etc Chris@23: frameCount = 0; Chris@23: m_dictID = 0; Chris@23: m_fl.clear(); Chris@23: m_meanTuning0 = 0; Chris@23: m_meanTuning1 = 0; Chris@23: m_meanTuning2 = 0; Chris@23: m_localTuning0 = 0; Chris@23: m_localTuning1 = 0; Chris@23: m_localTuning2 = 0; Chris@23: m_localTuning.clear(); matthiasm@0: } matthiasm@0: matthiasm@0: NNLSChroma::FeatureSet matthiasm@0: NNLSChroma::process(const float *const *inputBuffers, Vamp::RealTime timestamp) matthiasm@0: { Chris@23: if (debug_on) cerr << "--> process" << endl; Chris@23: frameCount++; Chris@23: float *magnitude = new float[m_blockSize/2]; matthiasm@0: Chris@23: Feature f10; // local tuning Chris@23: f10.hasTimestamp = true; Chris@23: f10.timestamp = timestamp; Chris@23: const float *fbuf = inputBuffers[0]; Chris@23: float energysum = 0; Chris@23: // make magnitude Chris@23: float maxmag = -10000; Chris@23: for (size_t iBin = 0; iBin < m_blockSize/2; iBin++) { Chris@23: magnitude[iBin] = sqrt(fbuf[2 * iBin] * fbuf[2 * iBin] + Chris@23: fbuf[2 * iBin + 1] * fbuf[2 * iBin + 1]); Chris@23: if (maxmag < magnitude[iBin]) maxmag = magnitude[iBin]; Chris@23: if (m_rollon > 0) { Chris@23: energysum += pow(magnitude[iBin],2); Chris@23: } Chris@23: } matthiasm@14: Chris@23: float cumenergy = 0; Chris@23: if (m_rollon > 0) { Chris@23: for (size_t iBin = 2; iBin < m_blockSize/2; iBin++) { Chris@23: cumenergy += pow(magnitude[iBin],2); Chris@23: if (cumenergy < energysum * m_rollon) magnitude[iBin-2] = 0; Chris@23: else break; Chris@23: } Chris@23: } matthiasm@17: Chris@23: if (maxmag < 2) { Chris@23: // cerr << "timestamp " << timestamp << ": very low magnitude, setting magnitude to all zeros" << endl; Chris@23: for (size_t iBin = 0; iBin < m_blockSize/2; iBin++) { Chris@23: magnitude[iBin] = 0; Chris@23: } Chris@23: } matthiasm@4: Chris@23: // note magnitude mapping using pre-calculated matrix Chris@23: float *nm = new float[nNote]; // note magnitude Chris@23: for (size_t iNote = 0; iNote < nNote; iNote++) { Chris@23: nm[iNote] = 0; // initialise as 0 Chris@23: } Chris@23: int binCount = 0; Chris@23: for (vector::iterator it = m_kernelValue.begin(); it != m_kernelValue.end(); ++it) { Chris@23: // cerr << "."; Chris@23: nm[m_kernelNoteIndex[binCount]] += magnitude[m_kernelFftIndex[binCount]] * m_kernelValue[binCount]; Chris@23: // cerr << m_kernelFftIndex[binCount] << " -- " << magnitude[m_kernelFftIndex[binCount]] << " -- "<< m_kernelValue[binCount] << endl; Chris@23: binCount++; Chris@23: } Chris@23: // cerr << nm[20]; Chris@23: // cerr << endl; matthiasm@0: matthiasm@0: matthiasm@0: float one_over_N = 1.0/frameCount; matthiasm@0: // update means of complex tuning variables matthiasm@0: m_meanTuning0 *= float(frameCount-1)*one_over_N; matthiasm@0: m_meanTuning1 *= float(frameCount-1)*one_over_N; matthiasm@0: m_meanTuning2 *= float(frameCount-1)*one_over_N; matthiasm@0: matthiasm@0: for (int iTone = 0; iTone < 160; iTone = iTone + 3) { matthiasm@0: m_meanTuning0 += nm[iTone + 0]*one_over_N; matthiasm@0: m_meanTuning1 += nm[iTone + 1]*one_over_N; matthiasm@0: m_meanTuning2 += nm[iTone + 2]*one_over_N; Chris@23: float ratioOld = 0.997; matthiasm@3: m_localTuning0 *= ratioOld; m_localTuning0 += nm[iTone + 0] * (1 - ratioOld); matthiasm@3: m_localTuning1 *= ratioOld; m_localTuning1 += nm[iTone + 1] * (1 - ratioOld); matthiasm@3: m_localTuning2 *= ratioOld; m_localTuning2 += nm[iTone + 2] * (1 - ratioOld); matthiasm@0: } matthiasm@0: matthiasm@0: // if (m_tuneLocal) { Chris@23: // local tuning Chris@23: float localTuningImag = sinvalue * m_localTuning1 - sinvalue * m_localTuning2; Chris@23: float localTuningReal = m_localTuning0 + cosvalue * m_localTuning1 + cosvalue * m_localTuning2; Chris@23: float normalisedtuning = atan2(localTuningImag, localTuningReal)/(2*M_PI); Chris@23: m_localTuning.push_back(normalisedtuning); Chris@23: float tuning440 = 440 * pow(2,normalisedtuning/12); Chris@23: f10.values.push_back(tuning440); Chris@23: // cerr << tuning440 << endl; matthiasm@0: // } matthiasm@0: Chris@23: Feature f1; // logfreqspec Chris@23: f1.hasTimestamp = true; matthiasm@0: f1.timestamp = timestamp; Chris@23: for (size_t iNote = 0; iNote < nNote; iNote++) { Chris@23: f1.values.push_back(nm[iNote]); Chris@23: } matthiasm@0: Chris@23: FeatureSet fs; Chris@23: fs[1].push_back(f1); matthiasm@3: fs[8].push_back(f10); matthiasm@0: matthiasm@0: // deletes matthiasm@0: delete[] magnitude; matthiasm@0: delete[] nm; matthiasm@0: matthiasm@0: m_fl.push_back(f1); // remember note magnitude for getRemainingFeatures Chris@23: char * pPath; Chris@23: pPath = getenv ("VAMP_PATH"); matthiasm@7: matthiasm@7: Chris@23: return fs; matthiasm@0: } matthiasm@0: matthiasm@0: NNLSChroma::FeatureSet matthiasm@0: NNLSChroma::getRemainingFeatures() matthiasm@0: { Chris@23: if (debug_on) cerr << "--> getRemainingFeatures" << endl; Chris@23: FeatureSet fsOut; Chris@23: if (m_fl.size() == 0) return fsOut; Chris@23: int nChord = m_chordnames.size(); Chris@23: // Chris@23: /** Calculate Tuning Chris@23: calculate tuning from (using the angle of the complex number defined by the Chris@23: cumulative mean real and imag values) Chris@23: **/ Chris@23: float meanTuningImag = sinvalue * m_meanTuning1 - sinvalue * m_meanTuning2; Chris@23: float meanTuningReal = m_meanTuning0 + cosvalue * m_meanTuning1 + cosvalue * m_meanTuning2; Chris@23: float cumulativetuning = 440 * pow(2,atan2(meanTuningImag, meanTuningReal)/(24*M_PI)); Chris@23: float normalisedtuning = atan2(meanTuningImag, meanTuningReal)/(2*M_PI); Chris@23: int intShift = floor(normalisedtuning * 3); Chris@23: float intFactor = normalisedtuning * 3 - intShift; // intFactor is a really bad name for this matthiasm@1: Chris@23: char buffer0 [50]; matthiasm@1: Chris@23: sprintf(buffer0, "estimated tuning: %0.1f Hz", cumulativetuning); matthiasm@1: Chris@23: // cerr << "normalisedtuning: " << normalisedtuning << '\n'; matthiasm@1: Chris@23: // push tuning to FeatureSet fsOut Chris@23: Feature f0; // tuning Chris@23: f0.hasTimestamp = true; Chris@23: f0.timestamp = Vamp::RealTime::frame2RealTime(0, lrintf(m_inputSampleRate));; Chris@23: f0.label = buffer0; Chris@23: fsOut[0].push_back(f0); matthiasm@1: Chris@23: /** Tune Log-Frequency Spectrogram Chris@23: calculate a tuned log-frequency spectrogram (f2): use the tuning estimated above (kinda f0) to Chris@23: perform linear interpolation on the existing log-frequency spectrogram (kinda f1). Chris@23: **/ Chris@23: cerr << endl << "[NNLS Chroma Plugin] Tuning Log-Frequency Spectrogram ... "; matthiasm@13: Chris@23: float tempValue = 0; Chris@23: float dbThreshold = 0; // relative to the background spectrum Chris@23: float thresh = pow(10,dbThreshold/20); Chris@23: // cerr << "tune local ? " << m_tuneLocal << endl; Chris@23: int count = 0; matthiasm@1: Chris@23: for (FeatureList::iterator i = m_fl.begin(); i != m_fl.end(); ++i) { Chris@23: Feature f1 = *i; Chris@23: Feature f2; // tuned log-frequency spectrum Chris@23: f2.hasTimestamp = true; Chris@23: f2.timestamp = f1.timestamp; Chris@23: f2.values.push_back(0.0); f2.values.push_back(0.0); // set lower edge to zero matthiasm@1: Chris@23: if (m_tuneLocal) { Chris@23: intShift = floor(m_localTuning[count] * 3); Chris@23: intFactor = m_localTuning[count] * 3 - intShift; // intFactor is a really bad name for this Chris@23: } matthiasm@1: Chris@23: // cerr << intShift << " " << intFactor << endl; matthiasm@1: Chris@23: for (unsigned k = 2; k < f1.values.size() - 3; ++k) { // interpolate all inner bins Chris@23: tempValue = f1.values[k + intShift] * (1-intFactor) + f1.values[k+intShift+1] * intFactor; Chris@23: f2.values.push_back(tempValue); Chris@23: } matthiasm@1: Chris@23: f2.values.push_back(0.0); f2.values.push_back(0.0); f2.values.push_back(0.0); // upper edge Chris@23: vector runningmean = SpecialConvolution(f2.values,hw); Chris@23: vector runningstd; Chris@23: for (int i = 0; i < 256; i++) { // first step: squared values into vector (variance) Chris@23: runningstd.push_back((f2.values[i] - runningmean[i]) * (f2.values[i] - runningmean[i])); Chris@23: } Chris@23: runningstd = SpecialConvolution(runningstd,hw); // second step convolve Chris@23: for (int i = 0; i < 256; i++) { Chris@23: runningstd[i] = sqrt(runningstd[i]); // square root to finally have running std Chris@23: if (runningstd[i] > 0) { Chris@23: // f2.values[i] = (f2.values[i] / runningmean[i]) > thresh ? Chris@23: // (f2.values[i] - runningmean[i]) / pow(runningstd[i],m_paling) : 0; Chris@23: f2.values[i] = (f2.values[i] - runningmean[i]) > 0 ? Chris@23: (f2.values[i] - runningmean[i]) / pow(runningstd[i],m_paling) : 0; Chris@23: } Chris@23: if (f2.values[i] < 0) { Chris@23: cerr << "ERROR: negative value in logfreq spectrum" << endl; Chris@23: } Chris@23: } Chris@23: fsOut[2].push_back(f2); Chris@23: count++; Chris@23: } Chris@23: cerr << "done." << endl; matthiasm@1: Chris@23: /** Semitone spectrum and chromagrams Chris@23: Semitone-spaced log-frequency spectrum derived from the tuned log-freq spectrum above. the spectrum Chris@23: is inferred using a non-negative least squares algorithm. Chris@23: Three different kinds of chromagram are calculated, "treble", "bass", and "both" (which means Chris@23: bass and treble stacked onto each other). Chris@23: **/ Chris@23: if (m_dictID == 1) { Chris@23: cerr << "[NNLS Chroma Plugin] Mapping to semitone spectrum and chroma ... "; Chris@23: } else { Chris@23: cerr << "[NNLS Chroma Plugin] Performing NNLS and mapping to chroma ... "; Chris@23: } matthiasm@13: matthiasm@1: Chris@23: vector > chordogram; Chris@23: vector > scoreChordogram; Chris@23: vector chordchange = vector(fsOut[2].size(),0); Chris@23: vector oldchroma = vector(12,0); Chris@23: vector oldbasschroma = vector(12,0); Chris@23: count = 0; matthiasm@9: Chris@23: for (FeatureList::iterator it = fsOut[2].begin(); it != fsOut[2].end(); ++it) { Chris@23: Feature f2 = *it; // logfreq spectrum Chris@23: Feature f3; // semitone spectrum Chris@23: Feature f4; // treble chromagram Chris@23: Feature f5; // bass chromagram Chris@23: Feature f6; // treble and bass chromagram matthiasm@1: Chris@23: f3.hasTimestamp = true; Chris@23: f3.timestamp = f2.timestamp; matthiasm@1: Chris@23: f4.hasTimestamp = true; Chris@23: f4.timestamp = f2.timestamp; matthiasm@1: Chris@23: f5.hasTimestamp = true; Chris@23: f5.timestamp = f2.timestamp; matthiasm@1: Chris@23: f6.hasTimestamp = true; Chris@23: f6.timestamp = f2.timestamp; matthiasm@1: Chris@29: float b[256]; matthiasm@1: Chris@23: bool some_b_greater_zero = false; Chris@23: float sumb = 0; Chris@23: for (int i = 0; i < 256; i++) { Chris@23: // b[i] = m_dict[(256 * count + i) % (256 * 84)]; Chris@23: b[i] = f2.values[i]; Chris@23: sumb += b[i]; Chris@23: if (b[i] > 0) { Chris@23: some_b_greater_zero = true; Chris@23: } Chris@23: } matthiasm@1: Chris@23: // here's where the non-negative least squares algorithm calculates the note activation x matthiasm@1: Chris@23: vector chroma = vector(12, 0); Chris@23: vector basschroma = vector(12, 0); Chris@23: float currval; Chris@23: unsigned iSemitone = 0; matthiasm@1: Chris@23: if (some_b_greater_zero) { Chris@23: if (m_dictID == 1) { Chris@23: for (unsigned iNote = 2; iNote < nNote - 2; iNote += 3) { Chris@23: currval = 0; Chris@23: currval += b[iNote + 1 + -1] * 0.5; Chris@23: currval += b[iNote + 1 + 0] * 1.0; Chris@23: currval += b[iNote + 1 + 1] * 0.5; Chris@23: f3.values.push_back(currval); Chris@23: chroma[iSemitone % 12] += currval * treblewindow[iSemitone]; Chris@23: basschroma[iSemitone % 12] += currval * basswindow[iSemitone]; Chris@23: iSemitone++; Chris@23: } matthiasm@1: Chris@23: } else { Chris@29: float x[84+1000]; Chris@23: for (int i = 1; i < 1084; ++i) x[i] = 1.0; Chris@23: vector signifIndex; Chris@23: int index=0; Chris@23: sumb /= 84.0; Chris@23: for (unsigned iNote = 2; iNote < nNote - 2; iNote += 3) { Chris@23: float currval = 0; Chris@23: currval += b[iNote + 1 + -1]; Chris@23: currval += b[iNote + 1 + 0]; Chris@23: currval += b[iNote + 1 + 1]; Chris@23: if (currval > 0) signifIndex.push_back(index); Chris@23: f3.values.push_back(0); // fill the values, change later Chris@23: index++; Chris@23: } Chris@29: float rnorm; Chris@29: float w[84+1000]; Chris@29: float zz[84+1000]; Chris@23: int indx[84+1000]; Chris@23: int mode; Chris@23: int dictsize = 256*signifIndex.size(); Chris@23: // cerr << "dictsize is " << dictsize << "and values size" << f3.values.size()<< endl; Chris@29: float *curr_dict = new float[dictsize]; Chris@23: for (unsigned iNote = 0; iNote < signifIndex.size(); ++iNote) { Chris@23: for (unsigned iBin = 0; iBin < 256; iBin++) { Chris@23: curr_dict[iNote * 256 + iBin] = 1.0 * m_dict[signifIndex[iNote] * 256 + iBin]; Chris@23: } Chris@23: } Chris@29: nnls(curr_dict, nNote, nNote, signifIndex.size(), b, x, &rnorm, w, zz, indx, &mode); Chris@23: delete [] curr_dict; Chris@23: for (unsigned iNote = 0; iNote < signifIndex.size(); ++iNote) { Chris@23: f3.values[signifIndex[iNote]] = x[iNote]; Chris@23: // cerr << mode << endl; Chris@23: chroma[signifIndex[iNote] % 12] += x[iNote] * treblewindow[signifIndex[iNote]]; Chris@23: basschroma[signifIndex[iNote] % 12] += x[iNote] * basswindow[signifIndex[iNote]]; Chris@23: } Chris@23: } Chris@23: } matthiasm@13: matthiasm@10: matthiasm@12: matthiasm@13: Chris@23: f4.values = chroma; Chris@23: f5.values = basschroma; Chris@23: chroma.insert(chroma.begin(), basschroma.begin(), basschroma.end()); // just stack the both chromas Chris@23: f6.values = chroma; matthiasm@1: Chris@23: if (m_doNormalizeChroma > 0) { Chris@23: vector chromanorm = vector(3,0); Chris@23: switch (int(m_doNormalizeChroma)) { Chris@23: case 0: // should never end up here Chris@23: break; Chris@23: case 1: Chris@23: chromanorm[0] = *max_element(f4.values.begin(), f4.values.end()); Chris@23: chromanorm[1] = *max_element(f5.values.begin(), f5.values.end()); Chris@23: chromanorm[2] = max(chromanorm[0], chromanorm[1]); Chris@23: break; Chris@23: case 2: Chris@23: for (vector::iterator it = f4.values.begin(); it != f4.values.end(); ++it) { Chris@23: chromanorm[0] += *it; Chris@23: } Chris@23: for (vector::iterator it = f5.values.begin(); it != f5.values.end(); ++it) { Chris@23: chromanorm[1] += *it; Chris@23: } Chris@23: for (vector::iterator it = f6.values.begin(); it != f6.values.end(); ++it) { Chris@23: chromanorm[2] += *it; Chris@23: } Chris@23: break; Chris@23: case 3: Chris@23: for (vector::iterator it = f4.values.begin(); it != f4.values.end(); ++it) { Chris@23: chromanorm[0] += pow(*it,2); Chris@23: } Chris@23: chromanorm[0] = sqrt(chromanorm[0]); Chris@23: for (vector::iterator it = f5.values.begin(); it != f5.values.end(); ++it) { Chris@23: chromanorm[1] += pow(*it,2); Chris@23: } Chris@23: chromanorm[1] = sqrt(chromanorm[1]); Chris@23: for (vector::iterator it = f6.values.begin(); it != f6.values.end(); ++it) { Chris@23: chromanorm[2] += pow(*it,2); Chris@23: } Chris@23: chromanorm[2] = sqrt(chromanorm[2]); Chris@23: break; Chris@23: } Chris@23: if (chromanorm[0] > 0) { Chris@23: for (int i = 0; i < f4.values.size(); i++) { Chris@23: f4.values[i] /= chromanorm[0]; Chris@23: } Chris@23: } Chris@23: if (chromanorm[1] > 0) { Chris@23: for (int i = 0; i < f5.values.size(); i++) { Chris@23: f5.values[i] /= chromanorm[1]; Chris@23: } Chris@23: } Chris@23: if (chromanorm[2] > 0) { Chris@23: for (int i = 0; i < f6.values.size(); i++) { Chris@23: f6.values[i] /= chromanorm[2]; Chris@23: } Chris@23: } matthiasm@13: Chris@23: } matthiasm@13: Chris@23: // local chord estimation Chris@23: vector currentChordSalience; Chris@23: float tempchordvalue = 0; Chris@23: float sumchordvalue = 0; matthiasm@9: Chris@23: for (int iChord = 0; iChord < nChord; iChord++) { Chris@23: tempchordvalue = 0; Chris@23: for (int iBin = 0; iBin < 12; iBin++) { Chris@23: tempchordvalue += m_chorddict[24 * iChord + iBin] * chroma[iBin]; Chris@23: } Chris@23: for (int iBin = 12; iBin < 24; iBin++) { Chris@23: tempchordvalue += m_chorddict[24 * iChord + iBin] * chroma[iBin]; Chris@23: } Chris@23: sumchordvalue+=tempchordvalue; Chris@23: currentChordSalience.push_back(tempchordvalue); Chris@23: } Chris@23: if (sumchordvalue > 0) { Chris@23: for (int iChord = 0; iChord < nChord; iChord++) { Chris@23: currentChordSalience[iChord] /= sumchordvalue; Chris@23: } Chris@23: } else { Chris@23: currentChordSalience[nChord-1] = 1.0; Chris@23: } Chris@23: chordogram.push_back(currentChordSalience); matthiasm@1: Chris@23: fsOut[3].push_back(f3); Chris@23: fsOut[4].push_back(f4); Chris@23: fsOut[5].push_back(f5); Chris@23: fsOut[6].push_back(f6); Chris@23: count++; Chris@23: } Chris@23: cerr << "done." << endl; matthiasm@13: matthiasm@10: Chris@23: /* Simple chord estimation Chris@23: I just take the local chord estimates ("currentChordSalience") and average them over time, then Chris@23: take the maximum. Very simple, don't do this at home... Chris@23: */ Chris@23: cerr << "[NNLS Chroma Plugin] Chord Estimation ... "; Chris@23: count = 0; Chris@23: int halfwindowlength = m_inputSampleRate / m_stepSize; Chris@23: vector chordSequence; Chris@23: for (FeatureList::iterator it = fsOut[6].begin(); it != fsOut[6].end(); ++it) { // initialise the score chordogram Chris@23: vector temp = vector(nChord,0); Chris@23: scoreChordogram.push_back(temp); Chris@23: } Chris@23: for (FeatureList::iterator it = fsOut[6].begin(); it < fsOut[6].end()-2*halfwindowlength-1; ++it) { Chris@23: int startIndex = count + 1; Chris@23: int endIndex = count + 2 * halfwindowlength; matthiasm@10: Chris@23: float chordThreshold = 2.5/nChord;//*(2*halfwindowlength+1); matthiasm@10: Chris@23: vector chordCandidates; Chris@23: for (unsigned iChord = 0; iChord < nChord-1; iChord++) { Chris@23: // float currsum = 0; Chris@23: // for (unsigned iFrame = startIndex; iFrame < endIndex; ++iFrame) { Chris@23: // currsum += chordogram[iFrame][iChord]; Chris@23: // } Chris@23: // if (currsum > chordThreshold) chordCandidates.push_back(iChord); Chris@23: for (unsigned iFrame = startIndex; iFrame < endIndex; ++iFrame) { Chris@23: if (chordogram[iFrame][iChord] > chordThreshold) { Chris@23: chordCandidates.push_back(iChord); Chris@23: break; Chris@23: } Chris@23: } Chris@23: } Chris@23: chordCandidates.push_back(nChord-1); Chris@23: // cerr << chordCandidates.size() << endl; Chris@23: Chris@23: float maxval = 0; // will be the value of the most salient *chord change* in this frame Chris@23: float maxindex = 0; //... and the index thereof Chris@23: unsigned bestchordL = nChord-1; // index of the best "left" chord Chris@23: unsigned bestchordR = nChord-1; // index of the best "right" chord Chris@23: Chris@23: for (int iWF = 1; iWF < 2*halfwindowlength; ++iWF) { Chris@23: // now find the max values on both sides of iWF Chris@23: // left side: Chris@23: float maxL = 0; Chris@23: unsigned maxindL = nChord-1; Chris@23: for (unsigned kChord = 0; kChord < chordCandidates.size(); kChord++) { Chris@23: unsigned iChord = chordCandidates[kChord]; Chris@23: float currsum = 0; Chris@23: for (unsigned iFrame = 0; iFrame < iWF-1; ++iFrame) { Chris@23: currsum += chordogram[count+iFrame][iChord]; matthiasm@10: } Chris@23: if (iChord == nChord-1) currsum *= 0.8; Chris@23: if (currsum > maxL) { Chris@23: maxL = currsum; Chris@23: maxindL = iChord; Chris@23: } Chris@23: } Chris@23: // right side: Chris@23: float maxR = 0; Chris@23: unsigned maxindR = nChord-1; Chris@23: for (unsigned kChord = 0; kChord < chordCandidates.size(); kChord++) { Chris@23: unsigned iChord = chordCandidates[kChord]; Chris@23: float currsum = 0; Chris@23: for (unsigned iFrame = iWF-1; iFrame < 2*halfwindowlength; ++iFrame) { Chris@23: currsum += chordogram[count+iFrame][iChord]; Chris@23: } Chris@23: if (iChord == nChord-1) currsum *= 0.8; Chris@23: if (currsum > maxR) { Chris@23: maxR = currsum; Chris@23: maxindR = iChord; Chris@23: } Chris@23: } Chris@23: if (maxL+maxR > maxval) { Chris@23: maxval = maxL+maxR; Chris@23: maxindex = iWF; Chris@23: bestchordL = maxindL; Chris@23: bestchordR = maxindR; Chris@23: } matthiasm@3: Chris@23: } Chris@23: // cerr << "maxindex: " << maxindex << ", bestchordR is " << bestchordR << ", of frame " << count << endl; Chris@23: // add a score to every chord-frame-point that was part of a maximum Chris@23: for (unsigned iFrame = 0; iFrame < maxindex-1; ++iFrame) { Chris@23: scoreChordogram[iFrame+count][bestchordL]++; Chris@23: } Chris@23: for (unsigned iFrame = maxindex-1; iFrame < 2*halfwindowlength; ++iFrame) { Chris@23: scoreChordogram[iFrame+count][bestchordR]++; Chris@23: } Chris@23: if (bestchordL != bestchordR) chordchange[maxindex+count] += (halfwindowlength - abs(maxindex-halfwindowlength)) * 2.0 / halfwindowlength; Chris@23: count++; Chris@23: } Chris@23: // cerr << "******* agent finished *******" << endl; Chris@23: count = 0; Chris@23: for (FeatureList::iterator it = fsOut[6].begin(); it != fsOut[6].end(); ++it) { Chris@23: float maxval = 0; // will be the value of the most salient chord in this frame Chris@23: float maxindex = 0; //... and the index thereof Chris@23: for (unsigned iChord = 0; iChord < nChord; iChord++) { Chris@23: if (scoreChordogram[count][iChord] > maxval) { Chris@23: maxval = scoreChordogram[count][iChord]; Chris@23: maxindex = iChord; Chris@23: // cerr << iChord << endl; Chris@23: } Chris@23: } Chris@23: chordSequence.push_back(maxindex); Chris@23: // cerr << "before modefilter, maxindex: " << maxindex << endl; Chris@23: count++; Chris@23: } Chris@23: // cerr << "******* mode filter done *******" << endl; matthiasm@10: matthiasm@3: Chris@23: // mode filter on chordSequence Chris@23: count = 0; Chris@23: string oldChord = ""; Chris@23: for (FeatureList::iterator it = fsOut[6].begin(); it != fsOut[6].end(); ++it) { Chris@23: Feature f6 = *it; Chris@23: Feature f7; // chord estimate Chris@23: f7.hasTimestamp = true; Chris@23: f7.timestamp = f6.timestamp; Chris@23: Feature f8; // chord estimate Chris@23: f8.hasTimestamp = true; Chris@23: f8.timestamp = f6.timestamp; matthiasm@17: Chris@23: vector chordCount = vector(nChord,0); Chris@23: int maxChordCount = 0; Chris@23: int maxChordIndex = nChord-1; Chris@23: string maxChord; Chris@23: int startIndex = max(count - halfwindowlength/2,0); Chris@23: int endIndex = min(int(chordogram.size()), count + halfwindowlength/2); Chris@23: for (int i = startIndex; i < endIndex; i++) { Chris@23: chordCount[chordSequence[i]]++; Chris@23: if (chordCount[chordSequence[i]] > maxChordCount) { Chris@23: // cerr << "start index " << startIndex << endl; Chris@23: maxChordCount++; Chris@23: maxChordIndex = chordSequence[i]; Chris@23: maxChord = m_chordnames[maxChordIndex]; Chris@23: } Chris@23: } Chris@23: // chordSequence[count] = maxChordIndex; Chris@23: // cerr << maxChordIndex << endl; Chris@23: f8.values.push_back(chordchange[count]/(halfwindowlength*2)); Chris@23: // cerr << chordchange[count] << endl; Chris@23: fsOut[9].push_back(f8); Chris@23: if (oldChord != maxChord) { Chris@23: oldChord = maxChord; matthiasm@3: Chris@23: // char buffer1 [50]; Chris@23: // if (maxChordIndex < nChord - 1) { Chris@23: // sprintf(buffer1, "%s%s", notenames[maxChordIndex % 12 + 12], chordtypes[maxChordIndex]); Chris@23: // } else { Chris@23: // sprintf(buffer1, "N"); Chris@23: // } Chris@23: // f7.label = buffer1; Chris@23: f7.label = m_chordnames[maxChordIndex]; Chris@23: fsOut[7].push_back(f7); Chris@23: } Chris@23: count++; Chris@23: } Chris@23: Feature f7; // last chord estimate Chris@23: f7.hasTimestamp = true; Chris@23: f7.timestamp = fsOut[6][fsOut[6].size()-1].timestamp; Chris@23: f7.label = "N"; Chris@23: fsOut[7].push_back(f7); Chris@23: cerr << "done." << endl; Chris@23: // // musicity Chris@23: // count = 0; Chris@23: // int oldlabeltype = 0; // start value is 0, music is 1, speech is 2 Chris@23: // vector musicityValue; Chris@23: // for (FeatureList::iterator it = fsOut[4].begin(); it != fsOut[4].end(); ++it) { Chris@23: // Feature f4 = *it; Chris@23: // Chris@23: // int startIndex = max(count - musicitykernelwidth/2,0); Chris@23: // int endIndex = min(int(chordogram.size()), startIndex + musicitykernelwidth - 1); Chris@23: // float chromasum = 0; Chris@23: // float diffsum = 0; Chris@23: // for (int k = 0; k < 12; k++) { Chris@23: // for (int i = startIndex + 1; i < endIndex; i++) { Chris@23: // chromasum += pow(fsOut[4][i].values[k],2); Chris@23: // diffsum += abs(fsOut[4][i-1].values[k] - fsOut[4][i].values[k]); Chris@23: // } Chris@23: // } Chris@23: // diffsum /= chromasum; Chris@23: // musicityValue.push_back(diffsum); Chris@23: // count++; Chris@23: // } Chris@23: // Chris@23: // float musicityThreshold = 0.44; Chris@23: // if (m_stepSize == 4096) { Chris@23: // musicityThreshold = 0.74; Chris@23: // } Chris@23: // if (m_stepSize == 4410) { Chris@23: // musicityThreshold = 0.77; Chris@23: // } Chris@23: // Chris@23: // count = 0; Chris@23: // for (FeatureList::iterator it = fsOut[4].begin(); it != fsOut[4].end(); ++it) { Chris@23: // Feature f4 = *it; Chris@23: // Feature f8; // musicity Chris@23: // Feature f9; // musicity segmenter Chris@23: // Chris@23: // f8.hasTimestamp = true; Chris@23: // f8.timestamp = f4.timestamp; Chris@23: // f9.hasTimestamp = true; Chris@23: // f9.timestamp = f4.timestamp; Chris@23: // Chris@23: // int startIndex = max(count - musicitykernelwidth/2,0); Chris@23: // int endIndex = min(int(chordogram.size()), startIndex + musicitykernelwidth - 1); Chris@23: // int musicityCount = 0; Chris@23: // for (int i = startIndex; i <= endIndex; i++) { Chris@23: // if (musicityValue[i] > musicityThreshold) musicityCount++; Chris@23: // } Chris@23: // bool isSpeech = (2 * musicityCount > endIndex - startIndex + 1); Chris@23: // Chris@23: // if (isSpeech) { Chris@23: // if (oldlabeltype != 2) { Chris@23: // f9.label = "Speech"; Chris@23: // fsOut[9].push_back(f9); Chris@23: // oldlabeltype = 2; Chris@23: // } Chris@23: // } else { Chris@23: // if (oldlabeltype != 1) { Chris@23: // f9.label = "Music"; Chris@23: // fsOut[9].push_back(f9); Chris@23: // oldlabeltype = 1; Chris@23: // } Chris@23: // } Chris@23: // f8.values.push_back(musicityValue[count]); Chris@23: // fsOut[8].push_back(f8); Chris@23: // count++; Chris@23: // } Chris@23: return fsOut; matthiasm@0: matthiasm@0: } matthiasm@0: