package edisyn.synth.tuning;

import edisyn.*;

public abstract class TuningDefinition 
	{
	int bases[] = new int[128];
	int detunes[] = new int[128];
	int rootMIDINote = 0;
	double rootFrequency = 1;
	boolean configured; 
	
	public void setConfigured(boolean val) { configured = val; }
	public boolean isConfigured() { return configured; }
	public int[] getBases() { return bases; }
	public int[] getDetunes() { return detunes; }
	public int getRootMIDINote() { return rootMIDINote; }
	public void setRootMIDINote(int val) { rootMIDINote = val; }
	public double getRootFrequency() { return rootFrequency; }
	public void setRootFrequency(double val) { rootFrequency = val; }
	
	 static double LOG_2 = Math.log(2);
	 static double INV_LOG_2 = 1/LOG_2;
	 static int TWO_TO_THE_14 = 16384; 		// (int) Math.pow(2, 14);

	public static double midiNumberToHz(int m)
	{
		return Math.pow(2, (m - 69) / 12.0) * 440.0;
	}

	public static double hzToMidiNumber(double hz)
	{
		return Math.log(hz/440) * INV_LOG_2 * 12 + 69;
	}

	public static double centsAbove(double f2, double f1) 
	{
		return 1200 * Math.log(f2/f1) * INV_LOG_2;
	}
	
	public static int centsToTicks(double c) 
	{
		return (int)((c * TWO_TO_THE_14 / 100.0) + 0.5);
	}
	
	public void setNoteFrequency(int note_index, double freq)
		{
		int ind = (int)hzToMidiNumber(freq);
		double base = midiNumberToHz(ind);
		double cents = centsAbove(freq, base);
		int ticks = centsToTicks(cents);
		if (ticks == TWO_TO_THE_14)	
			{
			ticks = 0;
			ind++;
		}
		bases[note_index] = ind;
		detunes[note_index] = ticks;
	}
	public abstract void popup(Synth synth);
	public abstract String getMenuName();
}

@@ -81,19 +81,18 @@
 nition %0A
-%09
 %7B%0A
+
 %09int bas
@@ -670,17 +670,16 @@
 l; %7D%0A%09%0A%09
- 
 static d
@@ -706,17 +706,16 @@
 og(2);%0A%09
- 
 static d
@@ -742,17 +742,16 @@
 LOG_2;%0A%09
- 
 static i
@@ -1277,20 +1277,19 @@
 e freq)%0A
-%09
 %09%7B%0A
+
 %09%09int in
@@ -1397,16 +1397,68 @@
  base);%0A
+%09%09while(cents %3C 0) %7B%0A%09%09%09ind--;%0A%09%09%09cents += 100;%0A%09%09%7D%0A
 %09%09int ti
@@ -1518,21 +1518,28 @@
 14)%09%0A%09%09%09
+   
 %7B%0A%09%09%09
+%09
+   
 ticks = 
@@ -1544,16 +1544,20 @@
 = 0;%0A%09%09%09
+%09
+   
 ind++;%0A%09
@@ -1553,24 +1553,28 @@
    ind++;%0A%09%09
+%09   
 %7D%0A%09%09bases%5Bno

Fix bug with weirdness in rare cases