/*
 * LoadTracker
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "common.h"
#include "settings.h"

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

unsigned char freqtbllo[] =
{
  0x17,0x27,0x39,0x4b,0x5f,0x74,0x8a,0xa1,0xba,0xd4,0xf0,0x0e,
  0x2d,0x4e,0x71,0x96,0xbe,0xe8,0x14,0x43,0x74,0xa9,0xe1,0x1c,
  0x5a,0x9c,0xe2,0x2d,0x7c,0xcf,0x28,0x85,0xe8,0x52,0xc1,0x37,
  0xb4,0x39,0xc5,0x5a,0xf7,0x9e,0x4f,0x0a,0xd1,0xa3,0x82,0x6e,
  0x68,0x71,0x8a,0xb3,0xee,0x3c,0x9e,0x15,0xa2,0x46,0x04,0xdc,
  0xd0,0xe2,0x14,0x67,0xdd,0x79,0x3c,0x29,0x44,0x8d,0x08,0xb8,
  0xa1,0xc5,0x28,0xcd,0xba,0xf1,0x78,0x53,0x87,0x1a,0x10,0x71,
  0x42,0x89,0x4f,0x9b,0x74,0xe2,0xf0,0xa6,0x0e,0x33,0x20,0xff,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char freqtblhi[] =
{
  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02,
  0x02,0x02,0x02,0x02,0x02,0x02,0x03,0x03,0x03,0x03,0x03,0x04,
  0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x06,0x06,0x07,0x07,0x08,
  0x08,0x09,0x09,0x0a,0x0a,0x0b,0x0c,0x0d,0x0d,0x0e,0x0f,0x10,
  0x11,0x12,0x13,0x14,0x15,0x17,0x18,0x1a,0x1b,0x1d,0x1f,0x20,
  0x22,0x24,0x27,0x29,0x2b,0x2e,0x31,0x34,0x37,0x3a,0x3e,0x41,
  0x45,0x49,0x4e,0x52,0x57,0x5c,0x62,0x68,0x6e,0x75,0x7c,0x83,
  0x8b,0x93,0x9c,0xa5,0xaf,0xb9,0xc4,0xd0,0xdd,0xea,0xf8,0xff,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

int tuningcount = 0;
double tuning[96];
char tuningname[64];

void calculatefreqtable(const Settings &cfg)
{
  double freq = cfg.ntsc ? NTSCCLOCKRATE : PALCLOCKRATE;
  double basefreq = cfg.basepitch * (16777216.0 / freq) * std::pow(2.0, 0.25) / 32.0;

  if (tuningcount)
  {
    double cyclebasefreq = basefreq;
    double freq = basefreq;
    int c = 0;
    while (c < 96)
    {
      for (int i = 0; i < tuningcount; i++)
      {
        if (c < 96)
        {
          int intfreq = static_cast<int>(freq + 0.5);
          if (intfreq > 0xffff)
              intfreq = 0xffff;
          freqtbllo[c] = intfreq & 0xff;
          freqtblhi[c] = intfreq >> 8;
          freq = cyclebasefreq * tuning[i];
          c++;
        }
      }
      cyclebasefreq = freq;
    }
  }
  else
  {
    for (int c = 0; c < 8*12 ; c++)
    {
        double note = c;
        double freq = basefreq * std::pow(2.0, note/cfg.equaldivisionsperoctave);
        int intfreq = freq + 0.5;
        if (intfreq > 0xffff)
            intfreq = 0xffff;
        freqtbllo[c] = intfreq & 0xff;
        freqtblhi[c] = intfreq >> 8;
    }
  }
}

void readscalatuningfile(const char *filepath, char *specialnotenames)
{
  FILE *scalatuningfile = std::fopen(filepath, "rt");
  if (scalatuningfile)
  {
    char configbuf[MAX_PATHNAME];
    char *configptr;

    // Tuning name
    for (;;)
    {
      if (std::feof(scalatuningfile)) return;
      std::fgets(configbuf, MAX_PATHNAME, scalatuningfile);
      if ((configbuf[0]) && (configbuf[0] != '!') && (configbuf[0] != 13) && (configbuf[0] != 10)) break;
    }
    configptr = configbuf;
    std::sscanf(configptr, "%63[^\t\n]", tuningname);

    // Tuning count
    for (;;)
    {
      if (feof(scalatuningfile)) return;
      std::fgets(configbuf, MAX_PATHNAME, scalatuningfile);
      if ((configbuf[0]) && (configbuf[0] != '!') && (configbuf[0] != 13) && (configbuf[0] != 10)) break;
    }
    configptr = configbuf;
    std::sscanf(configptr, "%d", &tuningcount);

    char strbuf[64];
    char name[3];
    // Tunings
    for (int i = 0; i < tuningcount; i++)
    {
      for (;;)
      {
        if (std::feof(scalatuningfile)) return;
        std::fgets(configbuf, MAX_PATHNAME, scalatuningfile);
        if ((configbuf[0]) && (configbuf[0] != '!') && (configbuf[0] != 13) && (configbuf[0] != 10)) break;
      }
      configptr = configbuf;
      name[0] = '\0';
      std::sscanf(configptr, "%63s %2s", strbuf, name);
      if (!i)
      {
        std::strcpy(specialnotenames, name);
      }
      else
      {
        if (i == tuningcount - 1)
        {
          char *tmp = strdup(specialnotenames);
          std::strcpy(specialnotenames, name);
          std::strcat(specialnotenames, tmp);
          std::free(tmp);
        }
        else
        {
          std::strcat(specialnotenames, name);
        }
      }
      if (!std::strchr(strbuf, '.'))
      {
        double numerator;
        double denominator;
        std::sscanf(strbuf, "%lf", &numerator);
        if (std::strchr(strbuf, '/'))
        {
          std::sscanf(std::strchr(strbuf, '/') + 1, "%lf", &denominator);
          tuning[i] = numerator / denominator;
        }
      }
      else
      {
        double centvalue;
        std::sscanf(configptr, "%lf", &centvalue);
        tuning[i] = std::pow(2.0, centvalue / 1200.0);
      }
    }
    std::fclose(scalatuningfile);
  }
}
