func New(val, offset int) Interval { octaves := int((val - 1) / 7.0) diatonic := int(mt_math.Mod(float64(val-1), 7)) chromatic := DiatonicToChromatic(diatonic) + offset return Interval{octaves, diatonic, chromatic} }
func qualityInterval(quality Quality) func(int) Interval { return func(val int) Interval { diatonic := int(mt_math.Mod(float64(val-1), 7)) diff := qualityDiff(perfect(diatonic), quality) return New(val, diff) } }
func (i Interval) AddInterval(o Interval) Interval { diatonics := i.Diatonic() + o.Diatonic() diatonicOctaves := diatonics / 7.0 diatonicRemainder := int(mt_math.Mod(float64(diatonics), 7.0)) octaves := i.Octaves() + o.Octaves() + diatonicOctaves chromatic := i.Chromatic() + o.Chromatic() if diatonicOctaves > 0 { chromatic = int(mt_math.Mod(float64(chromatic), 12.0)) } return Interval{ octaves: octaves, diatonic: diatonicRemainder, chromatic: chromatic, } }
func (p Pitch) Name(s NameStrategy) string { semitones := int(mt_math.Mod(float64(p.interval.Semitones()), 12.0)) nameIndex := s.GetMappedIndex(semitones) delta := semitones - interval.DiatonicToChromatic(nameIndex) if delta == 0 { return PitchNames[nameIndex] } return fmt.Sprintf("%s%s", PitchNames[nameIndex], accidentalName(delta+2)) }
func accidentalName(i int) string { return AccidentalNames[int(mt_math.Mod(float64(i), float64(len(AccidentalNames))))] }
C int = iota D E F G A B ) var ( AccidentalNames = [5]string{"bb", "b", "", "#", "x"} PitchNames = [7]string{"C", "D", "E", "F", "G", "A", "B"} namesForFlats = [12]int{0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6} namesForSharps = [12]int{0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6} FlatNames = NameStrategyFunc(func(i int) int { return namesForFlats[int(mt_math.Mod(float64(i), 12))] }) SharpNames = NameStrategyFunc(func(i int) int { return namesForSharps[int(mt_math.Mod(float64(i), 12))] }) ) type NameStrategy interface { GetMappedIndex(int) int } type NameStrategyFunc func(int) int func (f NameStrategyFunc) GetMappedIndex(i int) int { return f(i) } func New(semitone int, accidental int) Pitch { return Pitch{interval.New(semitone+1, accidental)}