From 463e7d971d92c5a0f27f006d890e504cad5671aa Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 12 Feb 2017 10:13:23 -0500 Subject: [PATCH] Make EXPERIMENTAL_FIND_NOTES work again --- src/TrackArtist.cpp | 137 ++++++++++++++++---------------- src/prefs/SpectrogramSettings.h | 4 +- src/prefs/SpectrumPrefs.cpp | 12 +-- src/prefs/WaveformPrefs.cpp | 2 +- 4 files changed, 78 insertions(+), 77 deletions(-) diff --git a/src/TrackArtist.cpp b/src/TrackArtist.cpp index 7d4385d33..9e1122231 100644 --- a/src/TrackArtist.cpp +++ b/src/TrackArtist.cpp @@ -2155,8 +2155,8 @@ void TrackArtist::DrawClipSpectrum(WaveTrackCache &waveTrackCache, #ifdef EXPERIMENTAL_FIND_NOTES const bool &fftFindNotes = settings.fftFindNotes; - const bool &findNotesMinA = settings.findNotesMinA; - const bool &numberOfMaxima = settings.numberOfMaxima; + const double &findNotesMinA = settings.findNotesMinA; + const int &numberOfMaxima = settings.numberOfMaxima; const bool &findNotesQuantize = settings.findNotesQuantize; #endif #ifdef EXPERIMENTAL_FFT_Y_GRID @@ -2267,6 +2267,8 @@ void TrackArtist::DrawClipSpectrum(WaveTrackCache &waveTrackCache, #endif #ifdef EXPERIMENTAL_FIND_NOTES + float log2 = logf( 2.0f ), + lmin = logf( minFreq ), lmax = logf( maxFreq ), scale = lmax - lmin, lmins = lmin, lmaxs = lmax ; @@ -2290,6 +2292,71 @@ void TrackArtist::DrawClipSpectrum(WaveTrackCache &waveTrackCache, #pragma omp parallel for #endif for (int xx = 0; xx < hiddenMid.width; ++xx) { +#ifdef EXPERIMENTAL_FIND_NOTES + int maximas = 0; + const int x0 = half * xx; + if (fftFindNotes) { + for (int i = maxTableSize - 1; i >= 0; i--) + indexes[i] = -1; + + // Build a table of (most) values, put the index in it. + for (int i = (int)(i0); i < (int)(i1); i++) { + float freqi = freq[x0 + (int)(i)]; + int value = (int)((freqi + gain + range) / range*(maxTableSize - 1)); + if (value < 0) + value = 0; + if (value >= maxTableSize) + value = maxTableSize - 1; + indexes[value] = i; + } + // Build from the indices an array of maxima. + for (int i = maxTableSize - 1; i >= 0; i--) { + int index = indexes[i]; + if (index >= 0) { + float freqi = freq[x0 + index]; + if (freqi < findNotesMinA) + break; + + bool ok = true; + for (int m = 0; m < maximas; m++) { + // Avoid to store very close maxima. + float maxm = maxima[m]; + if (maxm / index < minDistance && index / maxm < minDistance) { + ok = false; + break; + } + } + if (ok) { + maxima[maximas++] = index; + if (maximas >= numberOfMaxima) + break; + } + } + } + +// The f2pix helper macro converts a frequency into a pixel coordinate. +#define f2pix(f) (logf(f)-lmins)/(lmaxs-lmins)*hiddenMid.height + + // Possibly quantize the maxima frequencies and create the pixel block limits. + for (int i = 0; i < maximas; i++) { + int index = maxima[i]; + float f = float(index)*bin2f; + if (findNotesQuantize) + { + f = expf((int)(log(f / 440) / log2 * 12 - 0.5) / 12.0f*log2) * 440; + maxima[i] = f*f2bin; + } + float f0 = expf((log(f / 440) / log2 * 24 - 1) / 24.0f*log2) * 440; + maxima0[i] = f2pix(f0); + float f1 = expf((log(f / 440) / log2 * 24 + 1) / 24.0f*log2) * 440; + maxima1[i] = f2pix(f1); + } + } + + int it = 0; + bool inMaximum = false; +#endif //EXPERIMENTAL_FIND_NOTES + for (int yy = 0; yy < hiddenMid.height; ++yy) { const float bin = bins[yy]; const float nextBin = bins[yy+1]; @@ -2300,72 +2367,6 @@ void TrackArtist::DrawClipSpectrum(WaveTrackCache &waveTrackCache, clip->mSpecPxCache->values[xx * hiddenMid.height + yy] = value; } else { - // Do we need this legacy experiment still? -#ifdef EXPERIMENTAL_FIND_NOTES - int maximas = 0; - const int x0 = half * x; - if (fftFindNotes) { - for (int i = maxTableSize - 1; i >= 0; i--) - indexes[i] = -1; - - // Build a table of (most) values, put the index in it. - for (int i = (int)(i0); i < (int)(i1); i++) { - float freqi = freq[x0 + (int)(i)]; - int value = (int)((freqi + gain + range) / range*(maxTableSize - 1)); - if (value < 0) - value = 0; - if (value >= maxTableSize) - value = maxTableSize - 1; - indexes[value] = i; - } - // Build from the indices an array of maxima. - for (int i = maxTableSize - 1; i >= 0; i--) { - int index = indexes[i]; - if (index >= 0) { - float freqi = freq[x0 + index]; - if (freqi < findNotesMinA) - break; - - bool ok = true; - for (int m = 0; m < maximas; m++) { - // Avoid to store very close maxima. - float maxm = maxima[m]; - if (maxm / index < minDistance && index / maxm < minDistance) { - ok = false; - break; - } - } - if (ok) { - maxima[maximas++] = index; - if (maximas >= numberOfMaxima) - break; - } - } - } - -// The f2pix helper macro converts a frequency into a pixel coordinate. -#define f2pix(f) (logf(f)-lmins)/(lmaxs-lmins)*hiddenMid.height - - // Possibly quantize the maxima frequencies and create the pixel block limits. - for (int i = 0; i < maximas; i++) { - int index = maxima[i]; - float f = float(index)*bin2f; - if (findNotesQuantize) - { - f = expf((int)(log(f / 440) / log2 * 12 - 0.5) / 12.0f*log2) * 440; - maxima[i] = f*f2bin; - } - float f0 = expf((log(f / 440) / log2 * 24 - 1) / 24.0f*log2) * 440; - maxima0[i] = f2pix(f0); - float f1 = expf((log(f / 440) / log2 * 24 + 1) / 24.0f*log2) * 440; - maxima1[i] = f2pix(f1); - } - } - int it = 0; - int oldBin0 = -1; - bool inMaximum = false; -#endif //EXPERIMENTAL_FIND_NOTES - float value; #ifdef EXPERIMENTAL_FIND_NOTES diff --git a/src/prefs/SpectrogramSettings.h b/src/prefs/SpectrogramSettings.h index 02e527147..5eb8eea3c 100644 --- a/src/prefs/SpectrogramSettings.h +++ b/src/prefs/SpectrogramSettings.h @@ -138,8 +138,8 @@ public: #ifdef EXPERIMENTAL_FIND_NOTES bool fftFindNotes; - bool findNotesMinA; - bool numberOfMaxima; + double findNotesMinA; + int numberOfMaxima; bool findNotesQuantize; #endif //EXPERIMENTAL_FIND_NOTES diff --git a/src/prefs/SpectrumPrefs.cpp b/src/prefs/SpectrumPrefs.cpp index 21cfd8dcf..5693778d0 100644 --- a/src/prefs/SpectrumPrefs.cpp +++ b/src/prefs/SpectrumPrefs.cpp @@ -107,7 +107,7 @@ void SpectrumPrefs::Populate(size_t windowSize) //------------------------- Main section -------------------- // Now construct the GUI itself. - ShuttleGui S(this, eIsCreating); + ShuttleGui S(this, eIsCreatingFromPrefs); PopulateOrExchange(S); // ----------------------- End of main section -------------- } @@ -265,18 +265,18 @@ void SpectrumPrefs::PopulateOrExchange(ShuttleGui & S) { mFindNotesMinA = S.TieNumericTextBox(_("Minimum Amplitude (dB):"), - mTempSettings.fftFindNotes, + mTempSettings.findNotesMinA, 8); mFindNotesN = S.TieNumericTextBox(_("Max. Number of Notes (1..128):"), - mTempSettings.findNotesMinA, + mTempSettings.numberOfMaxima, 8); } S.EndTwoColumn(); S.TieCheckBox(_("&Find Notes"), - mTempSettings.numberOfMaxima); + mTempSettings.fftFindNotes); S.TieCheckBox(_("&Quantize Notes"), mTempSettings.findNotesQuantize); @@ -353,7 +353,7 @@ bool SpectrumPrefs::Validate() } #endif //EXPERIMENTAL_FIND_NOTES - ShuttleGui S(this, eIsGettingFromDialog); + ShuttleGui S(this, eIsSavingToPrefs); PopulateOrExchange(S); // Delegate range checking to SpectrogramSettings class @@ -376,7 +376,7 @@ bool SpectrumPrefs::Apply() static_cast(mWt->GetLink()) : nullptr; - ShuttleGui S(this, eIsGettingFromDialog); + ShuttleGui S(this, eIsSavingToPrefs); PopulateOrExchange(S); diff --git a/src/prefs/WaveformPrefs.cpp b/src/prefs/WaveformPrefs.cpp index 4081c916b..925b3cdd7 100644 --- a/src/prefs/WaveformPrefs.cpp +++ b/src/prefs/WaveformPrefs.cpp @@ -64,7 +64,7 @@ void WaveformPrefs::Populate() //------------------------- Main section -------------------- // Now construct the GUI itself. - ShuttleGui S(this, eIsCreating); + ShuttleGui S(this, eIsCreatingFromPrefs); PopulateOrExchange(S); // ----------------------- End of main section -------------- }