'
'   Some routines useful for creating numbers to plug into 
'   Dx7E!2 microtonal pitch tables.
'            In Amigabasic, a Microsoft Basic
'            Danger: no line numbers!
'
'
'    H. Lowengard, June 87  
'
'
' Here is a function which converts frequency to Points.
' there are 4096 points in an octave (the two least significant bits
' are ignored by Yamaha's sound chip, so there are actually 1024
' tones in their octave.)
'    The general formula for converting equally tempered (Octave-based)
'    notes into frequencies is:
'     F= BaseFreq*(2^(NoteNumberIncludingOctaves)
'     this equation is simpler when it is expressed in points,
'     which are specially scaled logorithms:
'  points=Magicbasefreq*(4096*(octave +(relativeNoteNumber/NotesPerOctave)))
' Since GMR didn't supply the Base Constant, we have to solve for it, 
'  reasoning:
'    Since each 'C' in the E!2 system is a factor of 4096,  and
'    a semitone is 1/12th of this, or 341.3333, the 5th A (which is A440) 
'    is 4096*(5+(9/12))  = 23552 points( a is the 9th note, with c=0)
'    Now we know that 440Hz = ?? * 2^(23552/4096)
'    so the magic constant must be 440/(2^(23552/4096)), or  8.1751798
'    Therefore, tuning to 0 points gives you a frequency of 8.175 Hz,
'                      4096 points gives you a frequency of 16.35 Hz,
'              et cetera....
DEF FNHzToPts( PtsToHz ) = LOG( Hertz / 8.175798 )/LOG(2!)*4096!
'
DEF FNPtsToHz( points ) = 8.175798 * (2!^(points/4096!))
'
'  Any Equal temperament  is easy to express in points ,
'  for example 'Cents' have 1200 tones to the ovtave, so
'   one Cent =  4096/1200 = 3.413333 points.
'  Just ratios, on the other hand, must be converted to 
'  logorithmic form. 
'   for example, the just triad 3/2 4/2 5/2 on A440 becomes
'   (in Hertz)  660       880         1100
'   (in Points) 25948.01  27648       28966.62
'
'
'  You can create any octave based equal temperament expressed in points
' by dividing the number of notes per octave into 4096:
' 12ET= 4096/12 = 341.3333 Pts,  19ET=4096/19 = 215.5789 Pts,...  
'  Some scales, like W. Carlos' Beta, are based on divisions of intervals
'  other than Octaves -  for example, equal quarters of 5/4:
' if this 5/4 is based on 'C' (0 points = 8.175 Hz) , we get
' Just E (5/4)  = 5/4 * 8.175798   = 10.21975 Hz = 1318.617 Points
' So dividing that interval into equal quarters gives:
' 1318.617/4 =  329.6542 points. Note how close this is to 
' a semitone (341.333), but  it differs just enough (pun intended)
' to make c-e consonant.
'
'
'
' here is a little test program to play ET scales via the
' sound   command (I will avoid Amiga tricks so IBM BasicA will
' work...)
INPUT " Equal temperament interval in points:", IntInPts
INPUT " Based on  which frequency? " ,BaseFreq
PRINT  " I will play all notes in the range 100 Hz to 1000 hz "
' create the interval constant 
IntConst = 2^(IntInPts/4096)
' find the true lower limit
Lowlimit=BaseFreq
 TestLow:
  IF Lowlimit /IntConst < 100 GOTO DoSoundTable
  Lowlimit=Lowlimit/IntConst
  GOTO TestLow
  
DoSoundTable:
Iota=0
PRINT  "Start of the table "
SoundTable:
 PRINT  Iota,Lowlimit
 SOUND Lowlimit,1.5
 SOUND 0,.5
 Iota=Iota+1
 Lowlimit=Lowlimit*IntConst
 IF Lowlimit < 1000 GOTO SoundTable
PRINT  "End of the Table" 
 END
 
                                                                                  