diff --git a/SoundGen/Oscillator.fs b/SoundGen/Oscillator.fs new file mode 100644 index 0000000..b26d006 --- /dev/null +++ b/SoundGen/Oscillator.fs @@ -0,0 +1,33 @@ +module SoundGen.Oscillator + +open Settings + +let sineosc hz x = + x + |> (*) (2. * System.Math.PI * hz / sampleRate) + |> sin + +let squareosc hz x = + let sign v : float = if v > 0.0 then 1. else -1. + x |> sineosc hz |> sign + +let triangleosc hz x = + let fullPeriodTime = 1.0 / hz + let localTime = x % fullPeriodTime + let value = localTime / fullPeriodTime + + if (value < 0.25) then + value * 4. + else if (value < 0.75) then + 2.0 - (value * 4.0) + else + value * 4. - 4.0 + +let sawosc hz x = + let fullPeriodTime = 1.0 / hz; + let localTime = x % fullPeriodTime + + ((localTime / fullPeriodTime) * 2. - 1.0); + +let sinesquareosc hz x = + sineosc (hz/4.) x + squareosc hz x \ No newline at end of file diff --git a/SoundGen/SoundGen.fsproj b/SoundGen/SoundGen.fsproj index 5ff2e23..b1f3b32 100644 --- a/SoundGen/SoundGen.fsproj +++ b/SoundGen/SoundGen.fsproj @@ -7,6 +7,7 @@ + @@ -15,6 +16,7 @@ + diff --git a/SoundGen/Synth.fs b/SoundGen/Synth.fs index 82ca0ca..4cdc8bf 100644 --- a/SoundGen/Synth.fs +++ b/SoundGen/Synth.fs @@ -1,5 +1,6 @@ module SoundGen.Synth open Settings +open Oscillator let getHzBySemitones semi = pitchStandard * (2. ** (1. / 12.)) ** semi @@ -52,8 +53,7 @@ let freq hz duration = Seq.map (fun x -> x - |> (*) (2. * System.Math.PI * hz / sampleRate) - |> sin + |> sinesquareosc hz |> (*) volume ) samples