diff --git a/SoundGen/Fx.fs b/SoundGen/Fx.fs index aed3846..5727e1c 100644 --- a/SoundGen/Fx.fs +++ b/SoundGen/Fx.fs @@ -9,14 +9,21 @@ type Saturator = { Gain: float } let saturate (param: Saturator, x: float) = tanh (param.Gain * x) -let sineWaveShape x = - x + 2.8 * sin (2. * x) +let sineWaveShape gain factor x = + x + gain * sin (factor * x) + +let reverb (buffer:float seq) = + let delayMilliseconds = 50.; // 500 is half a second + let delaySamples = (delayMilliseconds * 48.0) |> int; // assumes 44100 Hz sample rate + let decay = 0.5 + let mutable newBuffer = Array.ofSeq buffer + + for i in 0..Seq.length buffer do + let smpl = newBuffer.[i] * decay + if Array.length newBuffer > (i + delaySamples) then + newBuffer.[i + delaySamples] <- smpl + else newBuffer <- Array.append newBuffer (smpl |> Array.singleton) + + newBuffer + -// -// let process(effects:Effect list, sound:float seq) = -// let mutable output = sound -// effects -// |> List.iter((fun eff -> -// match eff with -// |Saturator -> output <- (output |> Seq.map saturate eff))) -// diff --git a/SoundGen/Oscillator.fs b/SoundGen/Oscillator.fs index fd9f3b3..d3c0659 100644 --- a/SoundGen/Oscillator.fs +++ b/SoundGen/Oscillator.fs @@ -1,4 +1,5 @@ module SoundGen.Oscillator + open System open Settings @@ -10,6 +11,9 @@ let sineosc hz x = let squareosc hz x = let sign v : float = if v > 0.0 then 1. else -1. x |> sineosc hz |> sign -let triangleosc (hz:float) (x:float) = 1. - Math.Abs((pos hz x) - 0.5) * 4. + +let triangleosc (hz: float) (x: float) = 1. - Math.Abs((pos hz x) - 0.5) * 4. let sawosc hz x = (pos hz x) * 2. - 1. -let sinesquareosc hz x = sawosc (hz / 4.) x + squareosc (hz/2.) x + +let sinesquareosc hz x = + sawosc (hz / 4.) x + squareosc (hz / 2.) x diff --git a/SoundGen/Program.fs b/SoundGen/Program.fs index 892aec9..338a937 100644 --- a/SoundGen/Program.fs +++ b/SoundGen/Program.fs @@ -55,8 +55,9 @@ let writeToFile (ms: MemoryStream) = ms.WriteTo(fs) song -|> Seq.map (fun x -> - let x1 = sineWaveShape x - saturate ({ Gain = 1.0 }, x1)) +// |> Seq.map (fun x -> +// let x1 = sineWaveShape 2.8 2. x +// saturate ({ Gain = 1.0 }, x1)) +//|> reverb |> createWAV |> writeToFile