wip: oscillators

This commit is contained in:
HiveBeats
2022-10-31 10:43:53 +04:00
parent df6a6da423
commit 6da2dcfa6a
3 changed files with 37 additions and 2 deletions

33
SoundGen/Oscillator.fs Normal file
View File

@@ -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

View File

@@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Settings.fs" /> <Compile Include="Settings.fs" />
<Compile Include="Oscillator.fs" />
<Compile Include="Synth.fs" /> <Compile Include="Synth.fs" />
<Compile Include="Fx.fs" /> <Compile Include="Fx.fs" />
<Compile Include="PCMWave.fs" /> <Compile Include="PCMWave.fs" />
@@ -15,6 +16,7 @@
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,5 +1,6 @@
module SoundGen.Synth module SoundGen.Synth
open Settings open Settings
open Oscillator
let getHzBySemitones semi = let getHzBySemitones semi =
pitchStandard * (2. ** (1. / 12.)) ** semi pitchStandard * (2. ** (1. / 12.)) ** semi
@@ -52,8 +53,7 @@ let freq hz duration =
Seq.map Seq.map
(fun x -> (fun x ->
x x
|> (*) (2. * System.Math.PI * hz / sampleRate) |> sinesquareosc hz
|> sin
|> (*) volume |> (*) volume
) )
samples samples