feat: getSemitoneShift function
This commit is contained in:
@@ -7,31 +7,22 @@ open SoundGen.Oscillator
|
|||||||
let private getHzBySemitones semi =
|
let private getHzBySemitones semi =
|
||||||
pitchStandard * (2. ** (1. / 12.)) ** semi
|
pitchStandard * (2. ** (1. / 12.)) ** semi
|
||||||
|
|
||||||
let private getSemitonesByNote (str: string) =
|
let private getSemitoneShift (rootNote : string) (targetNote : string) : int =
|
||||||
let defaultOctave = 4
|
// 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 =
|
// Extract the note number and pitch class for the root note
|
||||||
[ "A"
|
let rootNoteNum = int (rootNote.Substring(rootNote.Length - 1))
|
||||||
"A#"
|
let rootPitchClass = Array.findIndex ((=) (rootNote.Substring(0, rootNote.Length - 1))) pitchClasses
|
||||||
"B"
|
|
||||||
"C"
|
|
||||||
"C#"
|
|
||||||
"D"
|
|
||||||
"D#"
|
|
||||||
"E"
|
|
||||||
"F"
|
|
||||||
"F#"
|
|
||||||
"G"
|
|
||||||
"G#" ]
|
|
||||||
|> List.toArray
|
|
||||||
|
|
||||||
let octave =
|
// Extract the note number and pitch class for the target note
|
||||||
str.Substring(str.Length - 1) |> int
|
let targetNoteNum = int (targetNote.Substring(targetNote.Length - 1))
|
||||||
|
let targetPitchClass = Array.findIndex ((=) (targetNote.Substring(0, targetNote.Length - 1))) pitchClasses
|
||||||
|
|
||||||
let noteShift =
|
// Calculate the semitone shift using the formula
|
||||||
Array.findIndex (fun e -> e = str.Substring(0, str.Length - 1)) notes
|
(targetNoteNum - rootNoteNum) * 12 + (targetPitchClass - rootPitchClass)
|
||||||
|
|
||||||
(octave - defaultOctave - 1) * 12 + noteShift
|
|
||||||
|
|
||||||
|
|
||||||
let private freq hz duration (osc: OscillatorParameter list) =
|
let private freq hz duration (osc: OscillatorParameter list) =
|
||||||
@@ -75,6 +66,6 @@ let note semitone beats =
|
|||||||
freq
|
freq
|
||||||
hz
|
hz
|
||||||
(beats * beatDuration)
|
(beats * beatDuration)
|
||||||
[ { Osc = Sine; Freq = hz / 4. };
|
[ { Osc = Saw; Freq = hz / 4. };
|
||||||
{ Osc = Saw; Freq = (hz + 1.) };
|
{ Osc = Saw; Freq = hz+0.5 };
|
||||||
{ Osc = Saw; Freq = (hz - 1.3) } ]
|
{ Osc = Saw; Freq = hz-1. } ]
|
||||||
|
|||||||
Reference in New Issue
Block a user