-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grain.fs
167 lines (149 loc) · 5.04 KB
/
Grain.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module Grain
open System
open MathNet.Numerics
open MathNet.Numerics.Random
type Grain = {
Start: int
Bound: int
Pos: int
}
let makeEnv maxLen =
let win = Window.Tukey(maxLen)
let env (g: Grain) =
let grainPos = g.Pos - g.Start
let grainLen = g.Bound - g.Start
let midGrainPos = grainLen / 2
let nRemaining = grainLen - grainPos
if grainPos < midGrainPos then
win.[grainPos]
else
win.[maxLen - nRemaining]
env
let makeGrainMaker minLen maxLen (source: int [] []) =
let rng = SystemRandomSource.Default
let newStart () =
let extent = source.Length - maxLen
Math.Round((float extent) * rng.NextDouble()) |> int
let newLen () =
let extra = (maxLen - minLen) |> float
minLen + (Math.Round((float extra) * (rng.NextDouble())) |> int)
fun () ->
let start = newStart ()
{
Start = start
Bound = start + (newLen ())
Pos = start
}
let advanceGrain env makeGrain (g: Grain) (source: int [] []): (int [] * Grain) =
let sample =
source.[g.Pos]
|> Array.map (fun chan ->
Math.Round((env g) * (float chan)) |> int
)
let nextPos = g.Pos + 1
if nextPos >= g.Bound then
sample, (makeGrain ())
else
sample, {
g with Pos = g.Pos + 1
}
let Int16Scale = (int Int16.MaxValue) - (int Int16.MinValue) |> float
// https://ccrma.stanford.edu/~jos/pasp/Soft_Clipping.html
let softClip nTracks smpl =
let s = (float smpl) / Int16Scale
let softScale =
0.8 *
if nTracks > 2 then
3.0 / (float nTracks)
else
1.0
let clipped =
if s < -1.0 then
-(2.0 / 3.0)
else if s > 1.0 then
2.0 / 3.0
else
s - (s * s * s) / 3.0
clipped * softScale * Int16Scale
let mix (samples: int [] []) (weights: option<float []>) =
let nTracks = samples.Length
samples.[1..]
|> Array.fold (fun (acc: int []) (next: int []) ->
let safeNext =
if acc.Length <> next.Length then
eprintfn "acc.Length:%d next.Length:%d" acc.Length next.Length
eprintfn "samples:%A" samples
Array.zeroCreate<int> acc.Length
else
next
acc
|> Array.mapi (fun i s -> s + safeNext.[i])
) samples.[0]
|> Array.map ((softClip nTracks) >> Math.Round >> int)
let advanceGrains env makeGrain (grains: Grain []) (source: int [] []): (int [] * Grain []) =
let samples, nextGrains =
grains
|> Array.map (fun g -> advanceGrain env makeGrain g source)
|> Array.unzip
mix samples None, nextGrains
type Envs = (Grain -> float) []
type GrainMakers = (unit -> Grain) []
type Sources = int [] [] [] // .[source].[time].[channel]
type ExpiringTarget = {
Target: float []
TTL: int
}
let makeExpiringTarget nWeights minTTL maxTTL =
let rng = SystemRandomSource.Default
let weights = Array.init nWeights (fun _ -> rng.NextDouble())
let rint (x: float) = (Math.Round(x)) |> int
let ttl = minTTL + rint ((rng.NextDouble()) * (maxTTL - minTTL |> double))
let sumWeights = weights |> Array.sum
{
Target = weights |> Array.map (fun x -> x / sumWeights)
TTL = ttl
}
let nextExpiringtarget (e: ExpiringTarget) minTTL maxTTL =
if e.TTL = 0 then
makeExpiringTarget e.Target.Length minTTL maxTTL
else
{ e with TTL = e.TTL - 1 }
type SourceMix = {
CurrWeights: float []
ExpiringTarget: ExpiringTarget
}
let nextSourceMix minTTL maxTTL currSourceMix =
let nextTarget = nextExpiringtarget currSourceMix.ExpiringTarget minTTL maxTTL
let inertia =
currSourceMix.CurrWeights
|> Array.map (fun x -> x * 0.9)
let pull =
currSourceMix.ExpiringTarget.Target
|> Array.map (fun x -> x * 0.1)
{
CurrWeights = Array.init pull.Length (fun i -> inertia.[i] + pull.[i])
ExpiringTarget = nextTarget
}
let makeSourceMix nSources minTTL maxTTL =
{
CurrWeights = Array.zeroCreate nSources
ExpiringTarget = makeExpiringTarget nSources minTTL maxTTL
}
let granulate (sr: int) (envs: Envs) (grainMakers: GrainMakers) (nGrains: int) (sources: Sources) =
let grainsPerSource =
grainMakers
|> Array.map (fun makeGrain -> Array.init nGrains (fun _ -> makeGrain ()))
let minTTL = sr // / 10
let maxTTL = sr * 5
let sourceMix = makeSourceMix sources.Length minTTL maxTTL
let nextSrcMix = nextSourceMix minTTL maxTTL
let gen (currGrainsPerSource, currSourceMix) =
let samples, nextGrainsPerSource =
currGrainsPerSource
|> Array.mapi (fun i currGrains ->
advanceGrains envs.[i] grainMakers.[i] currGrains sources.[i]
)
|> Array.unzip
let sample = mix samples (Some(currSourceMix.CurrWeights))
Some(sample, (nextGrainsPerSource, nextSrcMix currSourceMix))
Seq.unfold gen (grainsPerSource, sourceMix)