Hibiki/panako/units.go

30 lines
671 B
Go
Raw Normal View History

package panako
import "math"
type Hertz float64
2022-07-16 15:09:55 +00:00
// Cent Twelve-tone equal temperament divides the Octave into 12 semitones of 100 Cent each.
2022-02-05 18:12:36 +00:00
type Cent int64
2022-07-16 15:09:55 +00:00
// Octave The interval between one musical pitch and another with double its frequency
type Octave int
2022-07-16 15:09:55 +00:00
const CentsPerOctave = 1200
func (h Hertz) DifferenceInCent(b Hertz) Cent {
return Cent(CentsPerOctave * math.Log2(float64(b/h)))
}
2022-07-16 15:09:55 +00:00
func (h Hertz) DifferenceInHertz(b Hertz) Hertz {
return b - h
}
2022-07-16 15:09:55 +00:00
func (h Hertz) Octaves(octaves Octave) Hertz {
return h * Hertz(math.Pow(2, float64(octaves)))
}
2022-07-16 15:09:55 +00:00
func (h Hertz) Cents(cents Cent) Hertz {
return h * Hertz(math.Pow(2, float64(cents)/CentsPerOctave))
}