feat: getSemitoneShift function

This commit is contained in:
2023-06-04 19:34:53 +04:00
parent 71927731d3
commit 9aba6c1855

View File

@@ -7,31 +7,22 @@ open SoundGen.Oscillator
let private getHzBySemitones semi =
pitchStandard * (2. ** (1. / 12.)) ** semi
let private getSemitonesByNote (str: string) =
let defaultOctave = 4
let private getSemitoneShift (rootNote : string) (targetNote : string) : int =
// Define arrays to map pitch classes to numeric values and vice versa
let pitchClasses = [| "C"; "C#"; "D"; "D#"; "E"; "F"; "F#"; "G"; "G#"; "A"; "A#"; "B" |]
let pitchClassValues = [| 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11 |]
let notes =
[ "A"
"A#"
"B"
"C"
"C#"
"D"
"D#"
"E"
"F"
"F#"
"G"
"G#" ]
|> List.toArray
// Extract the note number and pitch class for the root note
let rootNoteNum = int (rootNote.Substring(rootNote.Length - 1))
let rootPitchClass = Array.findIndex ((=) (rootNote.Substring(0, rootNote.Length - 1))) pitchClasses
let octave =
str.Substring(str.Length - 1) |> int
// Extract the note number and pitch class for the target note
let targetNoteNum = int (targetNote.Substring(targetNote.Length - 1))
let targetPitchClass = Array.findIndex ((=) (targetNote.Substring(0, targetNote.Length - 1))) pitchClasses
let noteShift =
Array.findIndex (fun e -> e = str.Substring(0, str.Length - 1)) notes
// Calculate the semitone shift using the formula
(targetNoteNum - rootNoteNum) * 12 + (targetPitchClass - rootPitchClass)
(octave - defaultOctave - 1) * 12 + noteShift
let private freq hz duration (osc: OscillatorParameter list) =
@@ -75,6 +66,6 @@ let note semitone beats =
freq
hz
(beats * beatDuration)
[ { Osc = Sine; Freq = hz / 4. };
{ Osc = Saw; Freq = (hz + 1.) };
{ Osc = Saw; Freq = (hz - 1.3) } ]
[ { Osc = Saw; Freq = hz / 4. };
{ Osc = Saw; Freq = hz+0.5 };
{ Osc = Saw; Freq = hz-1. } ]