Skip to content

Commit

Permalink
Fine-tune audio effect, add back smoothing
Browse files Browse the repository at this point in the history
  • Loading branch information
davepl committed Jul 28, 2024
1 parent e28a2ea commit 3087273
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
3 changes: 1 addition & 2 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ extern RemoteDebug Debug; // Let everyone in the project know about it
#define ENABLE_REMOTE 1 // IR Remote Control
#define ENABLE_AUDIO 1 // Listen for audio from the microphone and process it
#define SCALE_AUDIO_EXPONENTIAL 0
#define ENABLE_AUDIO_SMOOTHING 1
#define EFFECT_PERSISTENCE_CRITICAL 1 // Require effects serialization to succeed

#define DEFAULT_EFFECT_INTERVAL (MILLIS_PER_SECOND * 60 * 2)
Expand Down Expand Up @@ -1292,7 +1291,7 @@ extern RemoteDebug Debug; // Let everyone in the project know about it
#define AUDIO_PEAK_REMOTE_TIMEOUT 1000.0f // How long after remote PeakData before local microphone is used again
#endif
#ifndef ENABLE_AUDIO_SMOOTHING
#define ENABLE_AUDIO_SMOOTHING 0
#define ENABLE_AUDIO_SMOOTHING 1
#endif
#ifndef BARBEAT_ENHANCE
#define BARBEAT_ENHANCE 0.3 // How much the SpectrumAnalyzer "pulses" with the music
Expand Down
27 changes: 15 additions & 12 deletions include/soundanalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,26 @@ class PeakData
case MESMERIZERMIC:
{
static constexpr std::array<float, 16> Scalars16 = {0.4, .5, 0.75, 1.0, 0.6, 0.6, 0.8, 0.8, 1.2, 1.5, 3.0, 3.0, 3.0, 3.0, 3.5, 2.5}; // {0.08, 0.12, 0.3, 0.35, 0.35, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.4, 1.4, 1.0, 1.0, 1.0};
float result = (NUM_BANDS == 16) ? Scalars16[i] : map(i, 0, NUM_BANDS - 1, 1.0, 1.0);
float result = (NUM_BANDS == 16) ? Scalars16[i] : 1.0;
return result;
}
case PCREMOTE:
{

static constexpr std::array<float, 16> Scalars16 = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
float result = (NUM_BANDS == 16) ? Scalars16[i] : map(i, 0, NUM_BANDS - 1, 1.0, 1.0);
float result = (NUM_BANDS == 16) ? Scalars16[i] : 1.0;
return result;
}
case M5PLUS2:
{
static constexpr std::array<float, 16> Scalars16 = {0.3, .5, 0.8, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.7, 0.7, 0.7};
float result = (NUM_BANDS == 16) ? Scalars16[i] : map(i, 0, NUM_BANDS - 1, 1.0, 1.0);
static constexpr std::array<float, 16> Scalars16 = {0.5, 1.0, 2.5, 2.2, 1.5, 2.0, 2.0, 2.0, 1.5, 1.5, 1.5, 1.5, 1.0, 0.8, 1.0, 1.0};
float result = (NUM_BANDS == 16) ? Scalars16[i] : 1.0;
return result;
}
default:
{
static constexpr std::array<float, 16> Scalars16 = {0.5, .5, 0.8, 1.0, 1.5, 1.2, 1.5, 1.6, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 5.0, 2.5};
float result = (NUM_BANDS == 16) ? Scalars16[i] : map(i, 0, NUM_BANDS - 1, 1.0, 1.0);
float result = (NUM_BANDS == 16) ? Scalars16[i] : 1.0;
return result;
}
}
Expand Down Expand Up @@ -198,7 +198,7 @@ class SoundAnalyzer : public AudioVariables
// school that you need to sample at double the frequency you want to process, so 24000 is 12K

static constexpr size_t SAMPLING_FREQUENCY = 20000;
static constexpr size_t LOWEST_FREQ = 40;
static constexpr size_t LOWEST_FREQ = 100;
static constexpr size_t HIGHEST_FREQ = SAMPLING_FREQUENCY / 2;

static constexpr size_t _sampling_period_us = PERIOD_FROM_FREQ(SAMPLING_FREQUENCY);
Expand Down Expand Up @@ -455,16 +455,19 @@ class SoundAnalyzer : public AudioVariables
}
else
{
// uses geometric spacing to calculate the upper frequency for each of the 12 bands, starting with a frequency of 200 Hz
// and ending with a frequency of 12.5 kHz. The spacing ratio r is calculated as the 11th root of the ratio of the maximum
// frequency to the minimum frequency, and each upper frequency is calculated as f1 * r^(i+1).

// Calculate the logarithmic spacing for the frequency bands
float f1 = LOWEST_FREQ;
float f2 = HIGHEST_FREQ;
float r = pow(f2 / f1, 1.0 / (NUM_BANDS - 1));

// Calculate the ratio based on logarithmic scale
float log_f1 = log10(f1);
float log_f2 = log10(f2);
float delta = (log_f2 - log_f1) / (NUM_BANDS - 1);

for (int i = 0; i < NUM_BANDS; i++)
{
_cutOffsBand[i] = round(f1 * pow(r, i + 1));
// Calculate the upper frequency for each band
_cutOffsBand[i] = round(pow(10, log_f1 + delta * (i + 1)));
debugV("BAND %d: %d\n", i, _cutOffsBand[i]);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ void CurrentEffectSummary(bool bRedraw)

// Draw the spectrum analyzer bars

int spectrumTop = topMargin + ySizeVU + 1; // Start at the bottom of the VU meter
int bandHeight = display.height() - spectrumTop - display.BottomMargin;
const int spectrumTop = topMargin + ySizeVU + 1; // Start at the bottom of the VU meter
const int bandHeight = display.height() - spectrumTop - display.BottomMargin;

for (int iBand = 0; iBand < NUM_BANDS; iBand++)
{
Expand All @@ -363,14 +363,14 @@ void CurrentEffectSummary(bool bRedraw)
auto val = min(1.0f, g_Analyzer._peak2Decay[iBand]);
assert(bandHeight * val <= bandHeight);
display.fillRect(iBand * bandWidth, spectrumTop + topSection, bandWidth - 1, bandHeight - topSection, color16);
for (int iLine = spectrumTop; iLine <= spectrumTop + bandHeight; iLine += display.width() / 40)
display.drawFastHLine(iBand * bandWidth, iLine, bandWidth, BLACK16);
}

display.EndFrame();

// Draw horizontal lines so the bars look like they are made of segments

// for (int iLine = spectrumTop; iLine <= spectrumTop + bandHeight; iLine += display.height() / 25)
// display.drawLine(0, iLine, display.width()-1, iLine, BLACK16);
display.EndFrame();

#endif
}

Expand Down

0 comments on commit 3087273

Please sign in to comment.