Perceptualtuningkey.c

From 8BitDev.org - Atari 7800 Development Wiki
Revision as of 08:22, 7 November 2015 by MSaarna (talk | contribs) (Created page with "<pre> // perceptualtuningkey.c // By Mike Saarna // Searches the TIA frequncies for a key that has the most in-tune // Major Third, Perfect Fourth, Perfect Fifth, Octave...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
 // perceptualtuningkey.c
 // By Mike Saarna

 // Searches the TIA frequncies for a key that has the most in-tune
 // Major Third, Perfect Fourth, Perfect Fifth, Octave relationships.

 // The program identified A3 as the strongest match, and will do so every 
 // time. However it's provided for those who might wish to validate, tinker,
 // or adapt. Consider this source Public Domain. The author accepts no 
 // liability resulting from the program's use.

 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>

 float TIAfrequencies[] = {
	31.7,	//c=6,f=31 
	32.7,	//c=6,f=30
	33.8, 	//c=6,f=29
	35, 	//c=6,f=28
	36.2, 	//c=6,f=27
	37.6, 	//c=6,f=26
	39, 	//c=6,f=25
	40.6, 	//c=6,f=24
	42.3, 	//c=6,f=23
	44.1, 	//c=6,f=22
	46.1,	//c=6,f=21
	48.3, 	//c=6,f=20
	50.7,	//c=6,f=19
	53.4, 	//c=6,f=18
	56.3, 	//c=6,f=17
	59.7, 	//c=6,f=16
	63.4, 	//c=6,f=15
	67.6, 	//c=6,f=14
	72.4, 	//c=6,f=13
	78, 	//c=6,f=12
	84.5, 	//c=6,f=11
	92.2, 	//c=6,f=10
	101.4, 	//c=6,f=9
	112.7,	//c=6,f=8
	126.8, 	//c=6,f=7
	144.9,	//c=6,f=6
	163.8, 	//c=12,f=31
	169, 	//c=12,f=30
	174.7, 	//c=12,f=29
	180.7, 	//c=12,f=28
	187.1, 	//c=12,f=27
	194.1, 	//c=12,f=26
	201.5,	//c=12,f=25
	209.6, 	//c=12,f=24
	218.3, 	//c=12,f=23
	227.8, 	//c=12,f=22
	238.2, 	//c=12,f=21
	249.5, 	//c=12,f=20
	262, 	//c=12,f=19
	275.8, 	//c=12,f=18
	291.1, 	//c=12,f=17
	308.2,	//c=12,f=16
	327.5, 	//c=12,f=15
	349.3, 	//c=12,f=14
	374.3, 	//c=12,f=13
	403.1, 	//c=12,f=12
	436.7, 	//c=12,f=11
	476.4, 	//c=12,f=10
	491.3, 	//c=4,f=31
	507.1, 	//c=4,f=30
	524,	//c=4,f=29
	542.1, 	//c=4,f=28
	561.4, 	//c=4,f=27
	582.2, 	//c=4,f=26
	604.6, 	//c=4,f=25
	628.8, 	//c=4,f=24
	655,	//c=4,f=23
	683.5, 	//c=4,f=22
	714.5,	//c=4,f=21
	748.6, 	//c=4,f=20
	786, 	//c=4,f=19
	827.4, 	//c=4,f=18
	873.3, 	//c=4,f=17
	924.7, 	//c=4,f=16
	982.5, 	//c=4,f=15
	1048, 	//c=4,f=14
	1122.9, //c=4,f=13
	1209.2,	//c=4,f=12
	1310, 	//c=4,f=11
	1429.1, //c=4,f=10
	1572, 	//c=4,f=9
	1746.7, //c=4,f=8
	1965, 	//c=4,f=7
	2245.7, //c=4,f=6
	2620, 	//c=4,f=5
	3144, 	//c=4,f=4
	3930,	//c=4,f=3
	5240, 	//c=4,f=2
	7860, 	//c=4,f=1
	15720, 	//c=4,f=0
	0 };
 char *TIAparams[] = {
	"c=6,f=31",
	"c=6,f=30",
	"c=6,f=29",
	"c=6,f=28",
	"c=6,f=27",
	"c=6,f=26",
	"c=6,f=25",
	"c=6,f=24",
	"c=6,f=23",
	"c=6,f=22",
	"c=6,f=21",
	"c=6,f=20",
	"c=6,f=19",
	"c=6,f=18",
	"c=6,f=17",
	"c=6,f=16",
	"c=6,f=15",
	"c=6,f=14",
	"c=6,f=13",
	"c=6,f=12",
	"c=6,f=11",
	"c=6,f=10",
	"c=6,f=9",
	"c=6,f=8",
	"c=6,f=7",
	"c=6,f=6",
	"c=12,f=31",
	"c=12,f=30",
	"c=12,f=29",
	"c=12,f=28",
	"c=12,f=27",
	"c=12,f=26",
	"c=12,f=25",
	"c=12,f=24",
	"c=12,f=23",
	"c=12,f=22",
	"c=12,f=21",
	"c=12,f=20",
	"c=12,f=19",
	"c=12,f=18",
	"c=12,f=17",
	"c=12,f=16",
	"c=12,f=15",
	"c=12,f=14",
	"c=12,f=13",
	"c=12,f=12",
	"c=12,f=11",
	"c=12,f=10",
	"c=4,f=31",
	"c=4,f=30",
	"c=4,f=29",
	"c=4,f=28",
	"c=4,f=27",
	"c=4,f=26",
	"c=4,f=25",
	"c=4,f=24",
	"c=4,f=23",
	"c=4,f=22",
	"c=4,f=21",
	"c=4,f=20",
	"c=4,f=19",
	"c=4,f=18",
	"c=4,f=17",
	"c=4,f=16",
	"c=4,f=15",
	"c=4,f=14",
	"c=4,f=13",
	"c=4,f=12",
	"c=4,f=11",
	"c=4,f=10",
	"c=4,f=9",
	"c=4,f=8",
	"c=4,f=7",
	"c=4,f=6",
	"c=4,f=5",
	"c=4,f=4",
	"c=4,f=3",
	"c=4,f=2",
	"c=4,f=1",
	"c=4,f=0",
	0 };


 float findvals[] = {
	0.840909091 * 1,    //a
	0.890909091 * 1,    //a#
	0.943863636 * 1,    //b
	1.0         * 1,    //c        
	1.059454545 * 1,    //c#
	1.122454545 * 1,    //d
	1.189181818 * 1,    //d#
	1.25990909  * 1,    //e
	1.334818182 * 1,    //f
	1.414181818 * 1,    //f#
	1.498272726 * 1,    //g
	1.587363636 * 1,    //g#

	0.840909091 * 2,    //a
	0.890909091 * 2,    //a#
	0.943863636 * 2,    //b
	1.0         * 2,    //c        
	1.059454545 * 2,    //c#
	1.122454545 * 2,    //d
	1.189181818 * 2,    //d#
	1.25990909  * 2,    //e
	1.334818182 * 2,    //f
	1.414181818 * 2,    //f#
	1.498272726 * 2,    //g
	1.587363636 * 2,    //g#

	0.840909091 * 3,    //a
	0.890909091 * 3,    //a#
	0.943863636 * 3,    //b
	1.0         * 3,    //c        
	1.059454545 * 3,    //c#
	1.122454545 * 3,    //d
	1.189181818 * 3,    //d#
	1.25990909  * 3,    //e
	1.334818182 * 3,    //f
	1.414181818 * 3,    //f#
	1.498272726 * 3,    //g
	1.587363636 * 3     //g#


	};

 float findweights[] = {
		// C major

	1.0, 	//a
	1.0,  	//a#
	1.0,  	//b
	32.0, 	//c  - unison
	1.0,  	//c#
	1.0,  	//d
	1.0,  	//d#
	32.0,  	//e  - major third
	32.0,  	//f  - perfect fourth
	1.0,  	//f#
	32.0,  	//g  - perfect fifth
	1.0,  	//g#

	1.0, 	//a
	1.0,  	//a#
	1.0,  	//b
	16.0, 	//c  - unison
	1.0,	//c#
	1.0,	//d
	1.0,	//d#
	16.0,	//e   -major third
	16.0,	//f   -perfect routth
	1.0,	//f#
	16.0,  	//g  - perfect fifth
	1.0,  	//g#

	1.0, 	//a
	1.0,  	//a#
	1.0,  	//b
	8.0, 	//c  - unison
	1.0,	//c#
	1.0,	//d
	1.0,	//d#
	2.0,	//e  -major third
	2.0,	//f  -perfect fourth
	1.0,	//f#
	2.0,  	//g  - perfect fifth
	1.0,  	//g#
	};

 char *intervals[] = {
        "(major sixth)",
	"(minor seventh)",
	"(major seventh)",
	"unison",
	"(minor second)",
	"(major second)",
	"(minor third)",
	"major third",
	"perfect fourth",
	"(tritone)",
	"perfect fifth",
	"(minor sixth)",
	"(major sixth)",
	"(minor seventh)",
	"(major seventh)",
	"octave",
        "(minor second)",
        "(major second)",
        "(minor third)",
        "major third",
        "perfect fourth",
        "(tritone)",
        "perfect fifth",
        "(minor sixth)",
        "(major sixth)",
        "(minor seventh)",
        "(major seventh)",
        "octave",
        "(minor second)",
        "(major second)",
        "(minor third)",
        "major third",
        "perfect fourth",
        "(tritone)",
        "perfect fifth",
        "(minor sixth)",
        "(major sixth)",
        "(minor seventh)",
        "(major seventh)",
        "octave"
};


int main()
{
	int s,t,freq,lastval;
	int tiaindex;
	for(t=0;TIAfrequencies[t]!=0;t++);
	lastval=t;

	float passminimum, totalminimum, target,current;
	float minimumscore; int minimumindex; 

	minimumscore=10000;
	for(s=0;s<(lastval-1);s++)
	{
		printf("Weight for f=%f\n",TIAfrequencies[s]);
		totalminimum=0;
		for(freq=0;freq<28;freq++)
		{
			target=TIAfrequencies[s]*findvals[freq];
			passminimum=100000;
			for(t=0;t<lastval;t++)
			{
				current=fabs(TIAfrequencies[t]-target);
				if(current<passminimum)
				{
					passminimum=current;
					tiaindex=t;
				}
			}
			totalminimum=totalminimum+(passminimum*findweights[freq]);
			printf(" %f\t%f\t%s\t%s\n",TIAfrequencies[tiaindex],(passminimum/TIAfrequencies[s])*100,TIAparams[tiaindex],intervals[freq]);
		}
		printf("   %f distortion : %f Hz  \n\n",totalminimum/TIAfrequencies[s],TIAfrequencies[s]);
		if(minimumscore>(totalminimum/TIAfrequencies[s]))
		{
			minimumscore=totalminimum/TIAfrequencies[s];
			minimumindex=s;
		}
	}
	printf("The least distortion found was %f for %f Hz  \n\n",minimumscore,TIAfrequencies[minimumindex]);
	exit(0);
}