Skip to content

Commit

Permalink
Adding phosphor decay effect to audio scope.
Browse files Browse the repository at this point in the history
Last four traces are now stored in traces drawn at trace_intensity.
Gadgetoid#14
  • Loading branch information
Kevin J Walters committed Jan 10, 2024
1 parent cf17cae commit cedf5ca
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
26 changes: 19 additions & 7 deletions effect/effect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

#pragma once

#include <functional>
#include <array>
#include <cmath>
#include <string>
#include <cstdint>
#include <cstddef>
#include <functional>
#include <string>
#include "limits.h"


Expand Down Expand Up @@ -195,18 +196,29 @@ class EffectAudioscopeTuner : public EffectTuner {
64, 64, 64,
Display::WIDTH,
Display::HEIGHT - font6.height,
'r') { };
'r'),
// traces initialised in start()
trace_intensity({8, 20, 51, 128}),
trace_idx(0) { };
virtual ~EffectAudioscopeTuner() { };

protected:
void updateDisplay(void) override;
void start(void) override;
void init(void) override;

private:
constexpr static int8_t Y_MID_POS = (Display::HEIGHT - 1) / 2;
constexpr static float y_scale = float(Display::HEIGHT) / (1 - 2 * INT16_MIN);
constexpr static int8_t OFF_SCREEN = INT8_MIN;
constexpr static size_t TRACE_NUM = 4;

EffectText freq_text;

void updateDisplay(void) override;
void start(void) override;
void init(void) override;
std::array<std::array<int8_t, Display::WIDTH>, TRACE_NUM> traces;
std::array<uint8_t, TRACE_NUM> trace_intensity;
size_t trace_idx; // indicates oldest trace

void drawTraces(void);
};


Expand Down
37 changes: 31 additions & 6 deletions effect/effect_audioscope_tuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,48 @@ void EffectAudioscopeTuner::updateDisplay(void) {
}
}

// TODO add multiple fading passes
auto &new_trace = traces[trace_idx];
for (uint8_t x=0; x < Display::WIDTH; x++) {
int32_t value = window(signal, 5, waveform_idx); // returns 0 beyond end of buffer
int32_t y = Y_MID_POS - int32_t(roundf(value * y_scale));
waveform_idx += 10;
if (y >=0 && y < Display::HEIGHT) {
display.set_pixel(x, y,
0u, 128u, 0u);
new_trace[x] = (y >=0 && y < Display::HEIGHT) ? y : OFF_SCREEN;
waveform_idx += 1000 / Display::WIDTH; // TODO tune this based on sample rate
}
trace_idx = (trace_idx + 1) % traces.size();
drawTraces();
}

void EffectAudioscopeTuner::drawTraces(void) {
size_t t_idx = trace_idx;
uint8_t intensity_idx = 0;
for (size_t idx=0; idx < traces.size(); idx++) {
const auto &trace = traces[t_idx++];
for (uint8_t x=0; x < Display::WIDTH; x++) {
int32_t y = trace[x];
if (y >=0 && y < Display::HEIGHT) {
display.set_pixel(x, y,
0u, trace_intensity[idx], 0u);
}
}
if (t_idx >= traces.size()) {
t_idx = 0; // wrap back to the start
}
}
}
}

void EffectAudioscopeTuner::start(void) {
display.clear();

// default step is 1, other effects may have changed it
tuner.setStep(4);

// For visual effect, initialise it falling towards 0 scope pos
int8_t start_row = Y_MID_POS - ((Y_MID_POS + 1) / 2);
for (auto &trace : traces) {
for (auto &tx : trace) {
tx = start_row;
}
}
}

void EffectAudioscopeTuner::init(void) {
Expand Down

0 comments on commit cedf5ca

Please sign in to comment.