rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_audio.sc -rw-r--r-- 3.7 KiB View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months 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
using import itertools
using import glm
using import .testaudio
using import Capture
using import ..tukan.dsp.utils
import ..tukan.dsp.svf
let svf = tukan.dsp.svf

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

inline index (x a rest...)
    static-if (va-empty? rest...) a
    else
        ? (x < 1.0) a
            this-function (x - 1.0) rest...

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

fn line2 (t)
    "f32 <- (f32)"
    let a = (seq t 2.0)
    let ax ay = a.x a.y
    let ax = (% ax 4.0)
    let q =
        octave 2
            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 * (% (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 = (% ax 4.0)
    let q =
        octave 0
            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 * (% (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 - (% 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 (% idx 4.0) (C 5) (C+ 5) (C 5) (C+ 5)
    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 -2 note
                        pitch

let lpfilter =
    embed
        let dsp = (svf.Filter)
        'set-resonance dsp 0.2
        'set-mode dsp svf.Mode.Low
        # TODO: fix capture losing reference
        let &dsp = (& dsp)
        capture (t s) {&dsp}
            let dsp = (@ &dsp)
            'set-cutoff dsp
                logmix 1000.0 10000.0
                    (sin (t * 2.0)) * 0.5 + 0.5
            'process dsp s

inline music (t)
    #let t =
        + t
            (sine t) * 0.003
    stereo
        lpfilter t
            volume -3.0
                +
                    bassline t
                    kick t
                    volume -1.0
                        hhat t
                    volume -1.0
                        snare t
                    line2 t
                    #tap-delay t
                        taps = 8
                        delay = (/ 8.0)
                        decay = -1.0
                        inline (t)
                            line2 t

test-audio
    map
        inline (samplerate numsamples ...)
            let t = ((numsamples as f32) / samplerate)
            music t
    time =
        ? main-module? 3600 2