Skip to content

Commit

Permalink
findCross migrating from EffectAudioscopeTuner to Tuner class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin J Walters committed Jan 7, 2024
1 parent 3ee1393 commit bcd13b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
3 changes: 0 additions & 3 deletions effect/effect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ class EffectAudioscopeTuner : public EffectTuner {
void updateDisplay(void) override;
void start(void) override;
void init(void) override;

private:
int32_t findCross(const std::vector<int16_t> &signal, bool pos_edge = true, size_t sample_mass = 20'000);
};


Expand Down
46 changes: 2 additions & 44 deletions effect/effect_audioscope_tuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ void EffectAudioscopeTuner::updateDisplay(void) {
int32_t waveform_idx = 0;
if (frequency != 0.0f) {
// TODO consder a fixed 5 char of text at 5x5 + 4*5 position
if (frequency < 99999.49f) {
if (frequency < 99'999.49f) {
freq_text.text(std::to_string(int32_t(roundf(tuner.frequency))),
5);
}
int32_t zc_idx = findCross(signal);
int32_t zc_idx = tuner.findCross();
if (zc_idx >= 0) {
waveform_idx = zc_idx;
}
Expand Down Expand Up @@ -57,45 +57,3 @@ void EffectAudioscopeTuner::init(void) {
}
}

// Move this to Tuner??
int32_t EffectAudioscopeTuner::findCross(const std::vector<int16_t> &signal, bool pos_edge, size_t sample_mass) {
uint32_t seq_neg_mass = 0, seq_pos_mass = 0;
size_t seq_neg_count = 0, seq_pos_count = 0;
size_t matching_edge = 0;
bool found_pre_cross = false;

auto prev_elem = signal[0];
int32_t idx = -1;
for (idx = 0 ; idx < signal.size(); idx++) {
auto elem = signal[idx];
// count sequential positive and negative values
if (elem > 0) {
seq_pos_mass = (prev_elem > 0) ? seq_pos_mass + elem : 0;
seq_pos_count = (prev_elem > 0) ? seq_pos_count + 1 : 0;
} else if (elem < 0) {
seq_neg_mass = (prev_elem < 0) ? seq_neg_mass - elem : 0;
seq_neg_count = (prev_elem < 0) ? seq_neg_count + 1 : 0;
}

// we have found it if there's a sequential sequence of one sign
// followed by the other sign possibly with some indecision in between
if (pos_edge) {
found_pre_cross = found_pre_cross || (seq_neg_mass >= sample_mass);
if (found_pre_cross && seq_pos_mass >= sample_mass) {
matching_edge = seq_pos_count;
break;
}
} else {
found_pre_cross = found_pre_cross || (seq_pos_mass >= sample_mass);
if (found_pre_cross && seq_neg_mass >= sample_mass) {
matching_edge = seq_neg_count;
break;
}
}
prev_elem = elem;
}

// -1 if nothing was found or backtrack to start of zero cross if possible
return (matching_edge > 0) ? (idx >= matching_edge ? idx - matching_edge: 0) : -1;
}

41 changes: 41 additions & 0 deletions effect/lib/tuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,44 @@ void Tuner::setSampleRate(uint32_t rate) {
}
}

int32_t Tuner::findCross(bool pos_edge, size_t sample_mass) {
uint32_t seq_neg_mass = 0, seq_pos_mass = 0;
size_t seq_neg_count = 0, seq_pos_count = 0;
size_t matching_edge = 0;
bool found_pre_cross = false;

auto prev_elem = sample_array[0];
int32_t idx = -1;
for (idx = 0 ; idx < sample_array.size(); idx++) {
auto elem = sample_array[idx];
// count sequential positive and negative values
if (elem > 0) {
seq_pos_mass = (prev_elem > 0) ? seq_pos_mass + elem : 0;
seq_pos_count = (prev_elem > 0) ? seq_pos_count + 1 : 0;
} else if (elem < 0) {
seq_neg_mass = (prev_elem < 0) ? seq_neg_mass - elem : 0;
seq_neg_count = (prev_elem < 0) ? seq_neg_count + 1 : 0;
}

// we have found it if there's a sequential sequence of one sign
// followed by the other sign possibly with some indecision in between
if (pos_edge) {
found_pre_cross = found_pre_cross || (seq_neg_mass >= sample_mass);
if (found_pre_cross && seq_pos_mass >= sample_mass) {
matching_edge = seq_pos_count;
break;
}
} else {
found_pre_cross = found_pre_cross || (seq_pos_mass >= sample_mass);
if (found_pre_cross && seq_neg_mass >= sample_mass) {
matching_edge = seq_neg_count;
break;
}
}
prev_elem = elem;
}

// -1 if nothing was found or backtrack to start of zero cross if possible
return (matching_edge > 0) ? (idx >= matching_edge ? idx - matching_edge: 0) : -1;
}

1 change: 1 addition & 0 deletions effect/lib/tuner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ class Tuner {
void process();
void setSampleRate(uint32_t rate);
const std::vector<int16_t>& getSamples(void) { return sample_array; };
int32_t findCross(bool pos_edge = true, size_t sample_mass = 20'000);
};

0 comments on commit bcd13b9

Please sign in to comment.