rev: 21c5e6d2f665d0e8b4ff917a51b664c895dab2ed tukan/testing/test_audio.sc -rw-r--r-- 6.0 KiB View raw Log this file
21c5e6d2f665 — Leonard Ritter * renamed project from Liminal to Tukan 2 years ago
                                                                                
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using import glm
import ..tukan.audio
using import ..tukan.sdl
let audio = tukan.audio

let SAMPLESIZE = 44100
let pi2 = (pi * 2.0)

fn hz (c)
    "f32 <- f32"
    13.75 * (2.0 ** ((c + 3.0) / 12.0))

fn fmod (x y)
    x - (trunc (x / y)) * y

fn seq (t s)
    """"f32 f32 <- f32 f32
        returns note index, fract
    vec2
        trunc (t / s)
        fmod t s

fn index (x a rest...)
    if (va-empty? rest...) a
    else
        ? (x < 1.0) a
            index (x - 1.0) rest...

# OPL oscillators

# wave 0: regular sine
fn sine (x)
    "f32 <- (f32)"
    sin (pi2 * x)

# wave 1: half sine + pause
fn sine-2p (x)
    "f32 <- (f32)"
    max (sine x) 0.0

# wave 2: half sine, double frequency
fn sine-22 (x)
    "f32 <- (f32)"
    abs (sine x)

# wave 3: quarter sine + pause, double frequency
fn sine-4p2 (x)
    "f32 <- (f32)"
    ? ((fmod (x * 2.0) 1.0) < 0.5)
        abs (sine x)
        0.0

# wave 4: sine + pause, double frequency
fn sine-p2 (x)
    "f32 <- (f32)"
    ? ((fmod x 1.0) < 0.5)
        sine (x * 2.0)
        0.0

# wave 5: half sine + pause, double frequency
fn sine-2p2 (x)
    "f32 <- (f32)"
    ? ((fmod x 1.0) < 0.5)
        abs (sine (x * 2.0))
        0.0

# wave 6: square wave
fn square (x)
    "f32 <- (f32)"
    ? ((fmod x 1.0) < 0.5) 1.0 -1.0

# wave 7: saw wave
fn saw (x)
    "f32 <- (f32)"
    1.0 - 2.0 * (fmod x 1.0)

let HASHSCALE3 =
    vec3 443.897 441.423 437.195

fn noise (t)
    let p3 = (fmod (vec3 (t * HASHSCALE3)) 1.0)
    let p3 =
        + p3
            p3.z * (p3.y + 19.19)
            p3.x * (p3.z + 19.19)
            p3.y * (p3.x + 19.19)
    (fmod ((p3.x + p3.y) * p3.z) 1.0) * 2.0 - 1.0

let C C+ D D+ E F F+ G G+ A A+ =
    \ 60.0 61.0 62.0 63.0 64.0 65.0 66.0 67.0 68.0 69.0 70.0 71.0

fn stereo (x)
    vec2 x

fn volume (a x)
    x * (2.0 ** a)

fn octave (a x)
    x + (12.0 * a)

fn mix (...)
    + 0.0 ...

fn bpm (b)
    "compute the length of a beat at given rate"
    60.0 / b

fn limit (x n)
    """"a simple amplitude limiter
        n is the value of the first derivative at x=0
    let n =
        if (none? n) 1.0
        else n
    x / ((abs x) + n)

fn tap-delay (t taps delay decay f)
    let loop (i s) = taps 0.0
    if (i > 0)
        let i = (i - 1)
        loop i
            + (s * (2.0 ** decay))
                f (t * (1.0 + (f32 i) * 0.01) - (f32 i) * delay)
    else
        return s

# v = vec4(attack,decay,hold,release), s = sustain amplitude
fn adsr (t v s)
    "f32 <- (f32 vec4 f32)"
    let s =
        if (none? s) 1.0
        else s
    let v =
        vec4
            max 2.2e-05 v.x
            max 2.2e-05 v.y
            v.z
            max 2.2e-05 v.w
    # attack term
    let ta = (t / v.x)
    # decay / sustain amplitude term
    let td = (max s (1.0 - ((t - v.x) * (1.0 - s) / v.y)))
    # length / release term
    let tr =
        1.0 - ((max 0.0 (t - (+ v.x v.y v.z))) / v.w)
    max 0.0 (min ta (tr * td))

fn line2 (t)
    "f32 <- (f32)"
    let a = (seq t 2.0)
    let ax ay = a.x a.y
    let ax = (fmod ax 4.0)
    let q =
        octave 1
            index ax 0.0 1.0 2.0 3.0
    let w = (seq t (/ 4.0))
    let wx wy = w.x w.y
    let n = (30.0 + q + 12.0 * (fmod (trunc (wx * 0.5)) 2.0))
    let vib_f = (0.14 * (hz (0.5 * (sine (6.1 * t)))))
    let vibthz = (vib_f + t * (hz n))
    let amp = (adsr wy (vec4 0.0 0.0 0.0 0.1) 1.0)
    amp * (sine-2p (vibthz + (amp * (sine-22 vibthz))))

fn bassline (t)
    "f32 <- (f32)"
    let a = (seq t 2.0)
    let ax ay = a.x a.y
    let ax = (fmod ax 4.0)
    let q =
        octave -1
            index ax 0.0 -6.0 -1.0 -4.0
    let w = (seq t (/ 8.0))
    let wx wy = w.x w.y
    let n = (30.0 + q + 12.0 * (fmod (trunc (wx * 0.5)) 2.0))
    let vib_f = (0.07 * (hz (0.5 * (sine (6.1 * t)))))
    let amp = (adsr wy (vec4 0.001 0.0 0.0 0.15) 1.0)
    let vibthz = (vib_f + (t * (hz n)))
    amp * (sine-2p (vibthz + (sine-4p2 vibthz)))

fn hhat (t)
    let w = (seq (t - 0.25) (/ 2.0))
    let wx wy = w.x w.y
    let amp = (adsr wy (vec4 0.001 0.0 0.0 0.3) 1.0)
    let w2 = (seq (t - 0.25) (/ 8.0))
    let wx2 wy2 = w2.x w2.y
    let amp2 = (adsr wy2 (vec4 0.001 0.0 0.0 0.15) 1.0)
    amp2 * amp * (noise t)

fn snare (t)
    let w = (seq (t - 0.5) (/ 1.0))
    let wx wy = w.x w.y
    let amp = (adsr wy (vec4 0.001 0.0 0.0 0.15) 1.0)
    # quantize time to reduce frequency
    let ph =
        wy - (fmod wy (1.0 / 8192.0))
    amp * (noise ph)

fn kick (t)
    let q =
        seq t
            bpm 120.0
    let idx vt = q.x q.y
    let note =
        index (fmod idx 4.0) C C+ C C+
    let pitch =
        + 1.0
            adsr vt (vec4 0.001 0.0 0.0 0.1)
    let amp =
        adsr vt (vec4 0.001 0.0 0.0 0.5)
    * amp
        sine
            * vt
                hz
                    *
                        octave -3 note
                        pitch

fn music (t)
    let t =
        + t
            (sine t) * 0.003
    stereo
        volume -2.0
            mix
                kick t
                volume -1.0
                    hhat t
                volume -1.0
                    snare t
                bassline t
                tap-delay t
                    taps = 8
                    delay = (/ 8.0)
                    decay = -1.0
                    fn (t)
                        line2 t

print
    compile-glsl 'fragment
        typify
            fn ()
                let k =
                    music (unconst 0.0)
                return;

fn main ()
    SDL_Init SDL_INIT_AUDIO
    audio.init;
    let SAMPLESIZE = 44100
    fn queue_audio (time)
        var samples @ (2 * SAMPLESIZE) : f32
        for i in (range 0 SAMPLESIZE)
            let t = (time / (u32 audio.samplerate))
            let s = (music t)
            samples @ (i * 2) = s.x
            samples @ (i * 2 + 1) = s.y
            time = time + 1:u32
        audio.queue samples SAMPLESIZE

    var time = 0:u32
    queue_audio time
    audio.play true
    for i in (range 1 32)
        queue_audio time
        SDL_Delay 500
    audio.play-until-empty;
    audio.exit;
    SDL_Quit;

#compile
    typify main
    'dump-module
    'O3
    'no-debug-info

if main-module?
    main;