// https://forum.arduino.cc/t/im-looking-for-advice-or-criticism-on-how-to-improve/1025629


struct Tone {
  char const* const notation;
  unsigned long const duration;
};


unsigned int frequency(int const note) {
  return 440 * pow(2, note / 12.0) + 0.5;
}

size_t lcp(char const a[], char const b[]) {
  size_t i;
  for (i = 0; a[i] and b[i] and a[i] == b[i]; i++);
  return i;
}

int notationToNote(char const str[]) {
  static char const* const notation[] = {
    "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
  size_t len = strlen(str) - 1;
  for (size_t i = 0; i < 12; i++) {
    if (lcp(notation[i], str) == len) {
      return 12 * (str[len] - '0') + i - 57;
    }
  }
  return 0;
}


void setup() {
  Serial.begin(9600);

  Tone const melody[] = {{"A#1", 4}, {"D2", 4}, {"C#4", 16}};
  for (Tone const& tone_: melody) {
    Serial.print('{');
    Serial.print(frequency(notationToNote(tone_.notation)));
    Serial.print(", ");
    Serial.print(tone_.duration);
    Serial.print("}, ");
  }
}

void loop() {}