aboutsummaryrefslogtreecommitdiff
path: root/examples/example5.csd
blob: 3c3f9ba219682362bd52f34ae019572151d2cd0f (plain)
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
/*
    csound-json example 5

    fill JSON object with various data at k-rate and init time
        add to an object loaded from disk
        add f-table to JSON
        print and save to disk

*/
<CsoundSynthesizer>
<CsLicence>
    Released into the public domain under the Unlicense license
    http://unlicense.org/
</CsLicence>
<CsOptions>
-d
-m0
-odac
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 64
nchnls = 2
0dbfs = 1


/*
    Create a random alphabetic string
*/
opcode randstring, S, 0
    Soutput = sprintf("%c%c%c%c%c%c", \
        random(97, 122), random(97, 122),\
        random(97, 122), random(97, 122),\
        random(97, 122), random(97, 122)\
    )
    xout Soutput
endop


instr boot

    ; empty objects
    iJson1 jsoninit
    iJson2 jsoninit

    ; fill with data
    schedule "run1", 0, 5, iJson1
    schedule "run2", 5, 5, iJson2

    ; combine objects
    schedule "process", 10, 1, iJson1, iJson2
    turnoff
endin


instr run1
    iJson = p4
    kdata[] init 4
    kindex init 1
    kmetro metro 5

    ; add random numeric values with incremental key names
    if (kmetro == 1) then
        Skey = sprintfk("sound%03d", kindex)
        kdata[0] = random:k(0, 1)
        kdata[1] = random:k(0, 1)
        kdata[2] = random:k(0, 1)
        kdata[3] = random:k(0, 1)
        jsoninsertvalk iJson, Skey, kdata
        kindex += 1
    endif
endin


instr run2
    iJson = p4
    Skeys[] init 3
    Svalues[] init 3

    ; add random key/value strings
    index = 0
    while (index < 10) do
        Skeys[0] randstring
        Skeys[1] randstring
        Skeys[2] randstring
        Svalues[0] randstring
        Svalues[1] randstring
        Svalues[2] randstring
        jsoninsertval iJson, Skeys, Svalues
        index += 1
    od
endin


instr process

    ; populated objects
    iJsons[] fillarray p4, p5

    ; load file
    iJson jsonload "supplement.json"

    ; insert as array of objects under "added" key
    jsoninsert iJson, "added", iJsons

    ; add f-table sine wave points
    ipoints[] tab2array 1
    jsoninsertval iJson, "sine", ipoints

    ; show and write output
    prints sprintf("%s\n\n", jsondumps(iJson))
    jsondump iJson, "example_output.json"
endin


   
</CsInstruments>
<CsScore>
f1 0 16 10 1    ; sine
i"boot" 0 15
</CsScore>
</CsoundSynthesizer>