feat: private functions && fix reverb
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
module SoundGen.Fx
|
module SoundGen.Fx
|
||||||
|
open Settings
|
||||||
type Effect =
|
type Effect =
|
||||||
| Saturator
|
| Saturator
|
||||||
| Reverb
|
| Reverb
|
||||||
@@ -9,21 +9,22 @@ type Saturator = { Gain: float }
|
|||||||
|
|
||||||
let saturate (param: Saturator, x: float) = tanh (param.Gain * x)
|
let saturate (param: Saturator, x: float) = tanh (param.Gain * x)
|
||||||
|
|
||||||
let sineWaveShape gain factor x =
|
let sineWaveShape gain factor x = x + gain * sin (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 decay = 0.5
|
||||||
let mutable newBuffer = Array.ofSeq buffer
|
let mutable newBuffer = Array.ofSeq buffer
|
||||||
|
|
||||||
for i in 0..Seq.length buffer do
|
for i in 0 .. Seq.length buffer do
|
||||||
let smpl = newBuffer.[i] * decay
|
|
||||||
if Array.length newBuffer > (i + delaySamples) then
|
if Array.length newBuffer > (i + delaySamples) then
|
||||||
|
let smpl = newBuffer.[i] * decay
|
||||||
newBuffer.[i + delaySamples] <- smpl
|
newBuffer.[i + delaySamples] <- smpl
|
||||||
else newBuffer <- Array.append newBuffer (smpl |> Array.singleton)
|
//else newBuffer <- Array.append newBuffer (smpl |> Array.singleton)
|
||||||
|
|
||||||
newBuffer
|
|
||||||
|
|
||||||
|
|
||||||
|
Seq.zip buffer newBuffer
|
||||||
|
|> Seq.map (fun (x, y) -> x + y * 0.8)
|
||||||
|
|||||||
@@ -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 =
|
let sineosc hz x =
|
||||||
x |> (*) (2. * Math.PI * hz / sampleRate) |> sin
|
x |> (*) (2. * Math.PI * hz / sampleRate) |> sin
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ module SoundGen.PCMWave
|
|||||||
open Settings
|
open Settings
|
||||||
open System.IO
|
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 stream = new MemoryStream()
|
||||||
|
|
||||||
let writer =
|
let writer =
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ let writeToFile (ms: MemoryStream) =
|
|||||||
ms.WriteTo(fs)
|
ms.WriteTo(fs)
|
||||||
|
|
||||||
song
|
song
|
||||||
|
//|> reverb
|
||||||
|> Seq.map (fun x ->
|
|> Seq.map (fun x ->
|
||||||
let x1 = sineWaveShape 1.5 2.5 x
|
let x1 = sineWaveShape 1.5 2.5 x
|
||||||
saturate ({ Gain = 1.0 }, x1))
|
saturate ({ Gain = 1.0 }, x1))
|
||||||
//|> reverb
|
|
||||||
|> createWAV
|
|> createWAV
|
||||||
|> writeToFile
|
|> writeToFile
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ open Settings
|
|||||||
open Oscillator
|
open Oscillator
|
||||||
open SoundGen.Oscillator
|
open SoundGen.Oscillator
|
||||||
|
|
||||||
let getHzBySemitones semi =
|
let private getHzBySemitones semi =
|
||||||
pitchStandard * (2. ** (1. / 12.)) ** semi
|
pitchStandard * (2. ** (1. / 12.)) ** semi
|
||||||
|
|
||||||
let getSemitonesByNote (str: string) =
|
let private getSemitonesByNote (str: string) =
|
||||||
let defaultOctave = 4
|
let defaultOctave = 4
|
||||||
|
|
||||||
let notes =
|
let notes =
|
||||||
@@ -34,7 +34,7 @@ let getSemitonesByNote (str: string) =
|
|||||||
(octave - defaultOctave - 1) * 12 + noteShift
|
(octave - defaultOctave - 1) * 12 + noteShift
|
||||||
|
|
||||||
|
|
||||||
let freq hz duration (osc: OscillatorParameter list) =
|
let private freq hz duration (osc: OscillatorParameter list) =
|
||||||
let samples =
|
let samples =
|
||||||
seq { 0.0 .. (duration * sampleRate) }
|
seq { 0.0 .. (duration * sampleRate) }
|
||||||
|
|
||||||
@@ -70,11 +70,11 @@ let freq hz duration (osc: OscillatorParameter list) =
|
|||||||
|
|
||||||
|
|
||||||
let note semitone beats =
|
let note semitone beats =
|
||||||
let hz = getHzBySemitones (semitone - 12.)
|
let hz = getHzBySemitones (semitone)
|
||||||
|
|
||||||
freq
|
freq
|
||||||
hz
|
hz
|
||||||
(beats * beatDuration)
|
(beats * beatDuration)
|
||||||
[ { Osc = Sine; Freq = hz / 4. }
|
[ { Osc = Sine; Freq = hz / 4. };
|
||||||
{ Osc = Saw; Freq = (hz + 1.) }
|
{ Osc = Saw; Freq = (hz + 1.) };
|
||||||
{ Osc = Saw; Freq = (hz - 1.3) } ]
|
{ Osc = Saw; Freq = (hz - 1.3) } ]
|
||||||
|
|||||||
Reference in New Issue
Block a user