gokul/acoustic-guitar-tuner

This code defines the physical pin configuration and footprint layouts for the ATtiny1616 microcontroller and a matching tactile switch component, including their key hardware connection points, dimensions, and 3D models.

Version
1.0.10
License
unset
Stars
1

firmware/test_pitch.cpp

#include "acoustic_tuner_attiny1616/pitch_detector.h"
#include <stdio.h>
#include <stdlib.h>
#include <initializer_list>

int main() {
  int16_t data[tuner::sampleCount];
  const float strings[] = {82.4069f,110,146.832f,195.998f,246.942f,329.628f};
  float maxError = 0;
  int tests = 0;
  for (float base : strings) for (int cents : {-35, 0, 35})
    for (int phase = 0; phase < 5; ++phase) for (int mode = 0; mode < 3; ++mode) {
      const float hz = base * powf(2, cents / 1200.0f);
      for (int i = 0; i < tuner::sampleCount; ++i) {
        const float a = 2 * 3.14159265359f * hz * i / tuner::sampleRate + phase;
        float v = 100 * sinf(a);
        if (mode) v += 70 * sinf(2 * a) + 35 * sinf(3 * a);
        if (mode == 2) v = v * expf(-1.5f * i / tuner::sampleRate) + (rand()%7-3);
        data[i] = lroundf(v);
      }
      const float got = tuner::estimate(data);
      const float error = got > 0 ? fabsf(1200 * log2f(got / hz)) : 9999;
      if (error > maxError) maxError = error;
      if (error > 4) {printf("FAIL %.3f Hz -> %.3f, error %.2f cents\n",hz,got,error);return 1;}
      ++tests;
    }
  for (auto &v : data) v = 0;
  if (tuner::estimate(data) != 0) return 2;
  for (auto &v : data) v = rand()%201-100;
  if (tuner::estimate(data) != 0) return 3;
  printf("PASS %d tone/harmonic/noise cases + silence + random noise; worst error %.3f cents\n",tests,maxError);
}