rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/testaudio.sc -rw-r--r-- 1.6 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
import ..tukan.audio
using import ..tukan.sdl
using import itertools
let audio = tukan.audio

let BUFFERSIZE = 1024
let NUMBUFFERS = 3

inline audio-queue (srate)
    Collector
        inline () 0:u32
        inline (t) true
        inline (t) t
        inline (src t)
            let buf n = (src)
            if (t != 0:u32)
                # wait for buffer to drain
                while ((audio.queued) > ((NUMBUFFERS * n) as f32))
                    SDL_Delay 0
            audio.queue buf n
            if (t == 0:u32)
                audio.play true
            t + 1:u32

inline audio-buffer-sink (samplecount)
    let samples = (alloca-array f32 (2 * samplecount))
    Collector
        inline () 0
        inline (n) (n < samplecount)
        inline (n)
            _ samples n
        inline (src n)
            let s = (src)
            samples @ (n * 2) = s.x
            samples @ (n * 2 + 1) = s.y
            n + 1

inline test-audio (coll opts...)
    """"f must return a collector that receives the current sample index
        and writes a vec2
    let total-time = (va-option time opts... 3600)
    SDL_Init SDL_INIT_AUDIO

    let samples = (BUFFERSIZE * NUMBUFFERS)

    audio.init (samples = samples)
    print "buffersize:" (deref audio.buffersize)
    ->> (range (audio.samplerate * total-time))
        map
            inline (s)
                _
                    numsamples = s
                    samplerate = ((deref audio.samplerate) as f32)
        coll
        cascade
            audio-buffer-sink BUFFERSIZE
        audio-queue audio.samplerate
    audio.play-until-empty;
    audio.exit;
    SDL_Quit;

do
    let test-audio

    locals;