feat: refactoring

This commit is contained in:
2023-06-05 16:56:55 +04:00
parent 07cb8f8003
commit 059ea3db5b
7 changed files with 54 additions and 74 deletions

View File

@@ -2,38 +2,35 @@ module SoundGen.Synth
open Settings
open Oscillator
open SoundGen.Oscillator
let private getHzBySemitones semi =
pitchStandard * (2. ** (1. / 12.)) ** semi
let getSemitoneShift (rootNote : string) (targetNote : string) : int =
let private getSemitoneShiftInternal (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 pitchClasses =
[| "C"; "C#"; "D"; "D#"; "E"; "F"; "F#"; "G"; "G#"; "A"; "A#"; "B" |]
// 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 rootPitchClass =
Array.findIndex ((=) (rootNote.Substring(0, rootNote.Length - 1))) pitchClasses
// 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 targetPitchClass =
Array.findIndex ((=) (targetNote.Substring(0, targetNote.Length - 1))) pitchClasses
// Calculate the semitone shift using the formula
(targetNoteNum - rootNoteNum) * 12 + (targetPitchClass - rootPitchClass)
let private freq hz duration (osc: OscillatorParameter list) =
let samples =
seq { 0.0 .. (duration * sampleRate) }
let samples = seq { 0.0 .. (duration * sampleRate) }
let attack =
let samplesToRise =
(sampleRate * (0.001 * attackMs))
let samplesToRise = (sampleRate * (0.001 * attackMs))
let risingDelta = 1. / samplesToRise
let mutable i = 0.
@@ -45,22 +42,18 @@ let private freq hz duration (osc: OscillatorParameter list) =
}
let output =
Seq.map
(fun x ->
multiosc { Oscillators = osc; Sample = x }
|> (*) volume)
samples
Seq.map (fun x -> multiosc { Oscillators = osc; Sample = x } |> (*) volume) samples
let adsrLength = Seq.length output
let attackArray =
attack |> Seq.take adsrLength
let attackArray = attack |> Seq.take adsrLength
let release = Seq.rev attackArray
Seq.zip3 release attackArray output
|> Seq.map (fun (x, y, z) -> (x * y * z))
Seq.zip3 release attackArray output |> Seq.map (fun (x, y, z) -> (x * y * z))
let getSemitoneShift (targetNote: string) : int =
getSemitoneShiftInternal "A4" targetNote
let note semitone beats =
let hz = getHzBySemitones (semitone)
@@ -68,6 +61,6 @@ let note semitone beats =
freq
hz
(beats * beatDuration)
[ { Osc = Saw; Freq = hz / 4. };
{ Osc = Saw; Freq = hz+0.5 };
{ Osc = Saw; Freq = hz-1. } ]
[ { Osc = Saw; Freq = hz / 4. }
{ Osc = Saw; Freq = hz + 0.5 }
{ Osc = Saw; Freq = hz - 1. } ]