0.1.0 - Initial commit
This commit is contained in:
44
lib/audio/notes.go
Normal file
44
lib/audio/notes.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package audio
|
||||
|
||||
import "math"
|
||||
|
||||
// Converts note name to frequency in Hz
|
||||
func noteToFreq(note string) float64 {
|
||||
noteFreqs := map[string]float64{
|
||||
"C": 16.35,
|
||||
"C#": 17.32,
|
||||
"D": 18.35,
|
||||
"D#": 19.45,
|
||||
"E": 20.60,
|
||||
"F": 21.83,
|
||||
"F#": 23.12,
|
||||
"G": 24.50,
|
||||
"G#": 25.96,
|
||||
"A": 27.50,
|
||||
"A#": 29.14,
|
||||
"B": 30.87,
|
||||
}
|
||||
|
||||
if len(note) < 2 {
|
||||
return 440.0
|
||||
}
|
||||
|
||||
noteName := note[:1]
|
||||
if len(note) > 2 && (note[1] == '#' || note[1] == 'b') {
|
||||
noteName = note[:2]
|
||||
}
|
||||
|
||||
octave := 4
|
||||
if len(note) > 1 {
|
||||
if note[len(note)-1] >= '0' && note[len(note)-1] <= '9' {
|
||||
octave = int(note[len(note)-1] - '0')
|
||||
}
|
||||
}
|
||||
|
||||
baseFreq, exists := noteFreqs[noteName]
|
||||
if !exists {
|
||||
return 440.0
|
||||
}
|
||||
|
||||
return baseFreq * math.Pow(2, float64(octave))
|
||||
}
|
||||
Reference in New Issue
Block a user