From 4edafbe664d4c2418f07c1e59216f6e78abead03 Mon Sep 17 00:00:00 2001 From: HiveBeats <38073817+HiveBeats@users.noreply.github.com> Date: Tue, 1 Nov 2022 11:19:46 +0400 Subject: [PATCH] feat: private functions && fix reverb --- SoundGen/Fx.fs | 27 ++++++++++++++------------- SoundGen/Oscillator.fs | 2 +- SoundGen/PCMWave.fs | 4 ++-- SoundGen/Program.fs | 2 +- SoundGen/Synth.fs | 14 +++++++------- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/SoundGen/Fx.fs b/SoundGen/Fx.fs index 5727e1c..99f4853 100644 --- a/SoundGen/Fx.fs +++ b/SoundGen/Fx.fs @@ -1,5 +1,5 @@ module SoundGen.Fx - +open Settings type Effect = | Saturator | Reverb @@ -9,21 +9,22 @@ type Saturator = { Gain: float } let saturate (param: Saturator, x: float) = tanh (param.Gain * x) -let sineWaveShape gain factor x = - x + gain * sin (factor * x) +let sineWaveShape gain factor x = x + gain * sin (factor * x) + +let reverb (buffer: float seq) = + let delayMilliseconds = 500.//((1./8.) * beatDuration) // 500 is half a second + + let delaySamples = + (delayMilliseconds * 48.0) |> int // assumes 44100 Hz sample rate -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 + + for i in 0 .. Seq.length buffer do if Array.length newBuffer > (i + delaySamples) then + let smpl = newBuffer.[i] * decay newBuffer.[i + delaySamples] <- smpl - else newBuffer <- Array.append newBuffer (smpl |> Array.singleton) - - newBuffer - + //else newBuffer <- Array.append newBuffer (smpl |> Array.singleton) + Seq.zip buffer newBuffer + |> Seq.map (fun (x, y) -> x + y * 0.8) diff --git a/SoundGen/Oscillator.fs b/SoundGen/Oscillator.fs index e53ad61..fcd7ca9 100644 --- a/SoundGen/Oscillator.fs +++ b/SoundGen/Oscillator.fs @@ -18,7 +18,7 @@ type GenerationParameter = -let pos hz x = (hz * x / sampleRate) % 1. +let private pos hz x = (hz * x / sampleRate) % 1. let sineosc hz x = x |> (*) (2. * Math.PI * hz / sampleRate) |> sin diff --git a/SoundGen/PCMWave.fs b/SoundGen/PCMWave.fs index a16639c..80975f2 100644 --- a/SoundGen/PCMWave.fs +++ b/SoundGen/PCMWave.fs @@ -2,10 +2,10 @@ module SoundGen.PCMWave open Settings open System.IO -let toInt16Sample sample = sample |> (*) 32767. |> int16 +let private toInt16Sample sample = sample |> (*) 32767. |> int16 -let pack (d: int16 []) = +let private pack (d: int16 []) = let stream = new MemoryStream() let writer = diff --git a/SoundGen/Program.fs b/SoundGen/Program.fs index d6b847e..af1ab46 100644 --- a/SoundGen/Program.fs +++ b/SoundGen/Program.fs @@ -55,9 +55,9 @@ let writeToFile (ms: MemoryStream) = ms.WriteTo(fs) song +//|> reverb |> Seq.map (fun x -> let x1 = sineWaveShape 1.5 2.5 x saturate ({ Gain = 1.0 }, x1)) -//|> reverb |> createWAV |> writeToFile diff --git a/SoundGen/Synth.fs b/SoundGen/Synth.fs index 2190b3d..f8283ff 100644 --- a/SoundGen/Synth.fs +++ b/SoundGen/Synth.fs @@ -4,10 +4,10 @@ open Settings open Oscillator open SoundGen.Oscillator -let getHzBySemitones semi = +let private getHzBySemitones semi = pitchStandard * (2. ** (1. / 12.)) ** semi -let getSemitonesByNote (str: string) = +let private getSemitonesByNote (str: string) = let defaultOctave = 4 let notes = @@ -34,7 +34,7 @@ let getSemitonesByNote (str: string) = (octave - defaultOctave - 1) * 12 + noteShift -let freq hz duration (osc: OscillatorParameter list) = +let private freq hz duration (osc: OscillatorParameter list) = let samples = seq { 0.0 .. (duration * sampleRate) } @@ -70,11 +70,11 @@ let freq hz duration (osc: OscillatorParameter list) = let note semitone beats = - let hz = getHzBySemitones (semitone - 12.) + let hz = getHzBySemitones (semitone) freq hz (beats * beatDuration) - [ { Osc = Sine; Freq = hz / 4. } - { Osc = Saw; Freq = (hz + 1.) } - { Osc = Saw; Freq = (hz - 1.3) } ] + [ { Osc = Sine; Freq = hz / 4. }; + { Osc = Saw; Freq = (hz + 1.) }; + { Osc = Saw; Freq = (hz - 1.3) } ]