From 1d055261b4144dbf86b2658437015b15d4dd9bff Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 4 Sep 2022 00:32:56 +0100 Subject: initial --- examples/example1.csd | 133 +++++++++++++++++++++++++++++++++++++++++++++++ examples/example2.csd | 120 ++++++++++++++++++++++++++++++++++++++++++ examples/example3.csd | 54 +++++++++++++++++++ examples/example4.csd | 57 ++++++++++++++++++++ examples/example5.csd | 112 +++++++++++++++++++++++++++++++++++++++ examples/supplement.json | 10 ++++ 6 files changed, 486 insertions(+) create mode 100644 examples/example1.csd create mode 100644 examples/example2.csd create mode 100644 examples/example3.csd create mode 100644 examples/example4.csd create mode 100644 examples/example5.csd create mode 100644 examples/supplement.json (limited to 'examples') diff --git a/examples/example1.csd b/examples/example1.csd new file mode 100644 index 0000000..5ba2436 --- /dev/null +++ b/examples/example1.csd @@ -0,0 +1,133 @@ +/* + csound-json example 1 + + load data, examine and modify, print and output to file + +*/ + + + Released into the public domain under the Unlicense license + http://unlicense.org/ + + +-odac +-d + + +sr = 44100 +ksmps = 64 +nchnls = 2 +0dbfs = 1 + + +; base JSON string +gSjson = {{ + { + "instruments": { + "oscil1": [ + [0, 2, 440], + [2, 4, 880], + [4, 2, 220] + ], + "oscil2": [ + [6, 2, 880, "word"], + [8, 2, 220] + ] + }, + "details": { + "createdby": "Richard Knight", + "createdon": "2022-09-02" + } + } +}} + +instr boot + + ; load JSON data and print the type detected + iJson jsonloads gSjson + prints sprintf("1. Loaded; type = %s\n", jsontype:S(iJson)) + + ; query using JSONPath, print type found and dump string + iqueried jsonpath iJson, "$.instruments['oscil2'][0][:]" + prints sprintf("2. Path query 1; type = %s\n", jsontype:S(iqueried)) + prints sprintf("3. Dumped path query 1:\n%s\n\n", jsondumps(iqueried)) + + ; output JSONPath query result to numeric array (strings result in 0) + prints "4. Path query 1 to numeric array:\n" + idata[] jsonarrval iqueried + printarray idata + + ; output JSONPath query result to string array + prints "\n5. Path query 1 to string array:\n" + Sarray[] jsonarrval iqueried + index = 0 + while (index < lenarray(Sarray)) do + prints sprintf("%s ", Sarray[index]) + index += 1 + od + prints "\n\n" + + ; query with JSON Pointer, print type and dump string + iqueried jsonptr iJson, "/instruments/oscil1" + prints sprintf("6. Pointer query 1; type = %s\n", jsontype:S(iqueried)) + prints sprintf("7. output:\n%s\n\n", jsondumps(iqueried)) + + ; query with JSON Pointer, print type (should be numeric), convert and print + iqueried jsonptr iJson, "/instruments/oscil1/0/2" + prints sprintf("8. Pointer query 2; type = %s\n", jsontype:S(iqueried)) + inum = strtod(jsondumps(iqueried)) + prints sprintf("9. output as string, converted to numeric in csound: %d\n\n", inum) + + ; query with JSON Pointer to get instruments object + iinstruments1 jsonptr iJson, "/instruments" + + ; load supplementary data from file and extract instruments object with JSON Pointer query + iJson2 jsonload "supplement.json" + prints sprintf("10. File loaded; type = %s\n", jsontype:S(iJson2)) + iinstruments2 jsonptr iJson2, "/instruments" + + + ; merge the two instruments objects + jsonmerge iinstruments1, iinstruments2, 1 + prints sprintf("11. Merged instruments; dumped data:\n%s\n\n", jsondumps(iinstruments1)) + + ; insert the merged object back to the "instruments" key + jsoninsert iJson, "instruments", iinstruments1 + prints sprintf("12. Inserted instruments back to original; dumped data:\n%s\n\n", jsondumps(iJson)) + + ; create a new empty JSON object and insert some values with keys + iJson jsoninit + jsoninsertval iJson, "hello", "world" + jsoninsertval iJson, "numeric", 3 + + ; create JSON object from string and insert with key + jsoninsert iJson, "array", jsonloads("[1,2,3]") + + ; insert array values + iarraydata[] fillarray 3, 2, 1, 0 + jsoninsertval iJson, "arraycs", iarraydata + + ; insert string array values + Sarraydata[] fillarray "hello", "world", "again" + jsoninsertval iJson, "stringarraycs", Sarraydata + + iJsonarray[] fillarray jsonloads("{\"a\": \"b\"}"), jsonloads("{\"c\": 321, \"e\": 123}") + jsoninsert iJson, "subobject", iJsonarray + + ; use JSON Pointer to replace a value + jsonptrrplval iJson, "/hello", "goodbye" + + ; use JSONPath to replace value + jsonpathrplval iJson, "$.subobject[0]['a']", "this is a" + + prints sprintf("13. Inserted to new; dumped data:\n%s\n\n", jsondumps(iJson)) + + jsondump iJson, "example_output.json" + prints "14. Wrote to output file\n" +endin + + + +i"boot" 0 1 + + diff --git a/examples/example2.csd b/examples/example2.csd new file mode 100644 index 0000000..fd03ec3 --- /dev/null +++ b/examples/example2.csd @@ -0,0 +1,120 @@ +/* + csound-json example 2 + + merge objects, play score based on contents + +*/ + + + Released into the public domain under the Unlicense license + http://unlicense.org/ + + +-d +-m0 + + + +sr = 44100 +ksmps = 64 +nchnls = 2 +0dbfs = 1 + + +; base JSON string +gSjson = {{ + { + "instruments": { + "oscil1": [ + [0, 2, 440], + [2, 4, 880], + [4, 2, 220] + ], + "oscil2": [ + [6, 2, 330], + [8, 2, 660] + ] + }, + "details": { + "createdby": "Richard Knight", + "createdon": "2022-09-02" + } + } +}} + + +instr boot + + ; load the string and the supplementary file + iJbase jsonloads gSjson + iJsupplement jsonload "supplement.json" + + ; get the instruments objects and merge them + iJinstrs1 jsongetval iJbase, "instruments" + iJinstrs2 jsongetval iJsupplement, "instruments" + jsonmerge iJinstrs1, iJinstrs2, 1 + + ; get keys of the resulting instruments + Skeys[] jsonkeys iJinstrs1 + + ; loop through the keys + indexi = 0 + while (indexi < lenarray(Skeys)) do + Sinstrument = Skeys[indexi] + + ; get the relevant instrument object + iJscore jsongetval iJinstrs1, Sinstrument + + ; score items are retrieved as handles to JSON arrays, in a Csound array + iJscorelines[] jsonarr iJscore + + ; loop through each of the JSON arrays + indexs = 0 + while (indexs < lenarray(iJscorelines)) do + + ; get the JSON array values and format/call the scoreline accordingly + isc[] jsonarrval iJscorelines[indexs] + Scoreline = sprintf("i\"%s\" %d %d %d\n", Sinstrument, isc[0], isc[1], isc[2]) + prints Scoreline + scoreline_i Scoreline + indexs += 1 + od + indexi += 1 + od +endin + + +/* + The sound producting instruments to be called +*/ +instr oscil1 + ifreq = p4 + kamp linseg 1, p3*0.7, 1, p3*0.3, 0 + aout oscil kamp, ifreq, 1 + outs aout, aout +endin + +instr oscil2 + ifreq = p4 + kamp linseg 1, p3*0.7, 1, p3*0.3, 0 + aout oscil kamp, ifreq, 2 + outs aout, aout +endin + +instr oscil3 + ifreq = p4 + kamp linseg 1, p3*0.7, 1, p3*0.3, 0 + aout oscil kamp, ifreq, 3 + outs aout, aout +endin + + + + +f1 0 16384 10 1 ; Sine +f2 0 16384 10 1 0.5 0.3 0.25 0.2 0.167 0.14 0.125 .111 ; Sawtooth +f3 0 16384 10 1 0 0.3 0 0.2 0 0.14 0 .111 ; Square +f0 15 +i"boot" 0 1 + + \ No newline at end of file diff --git a/examples/example3.csd b/examples/example3.csd new file mode 100644 index 0000000..7d02ea4 --- /dev/null +++ b/examples/example3.csd @@ -0,0 +1,54 @@ +/* + csound-json example 3 + + add values to an object at k-rate and print them after + +*/ + + + Released into the public domain under the Unlicense license + http://unlicense.org/ + + +-d +-m0 + + +sr = 44100 +ksmps = 64 +nchnls = 2 +0dbfs = 1 + + +instr write_values + + ; create empty object + iJson jsoninit + + ; insert some values to the object four times a second + kmetro metro 4 + kindex init 1 + if (kmetro == 1) then + jsoninsertvalk iJson, sprintfk("key%03d", kindex), kindex*random:k(1, 10) + kindex += 1 + endif + + ; print what has been inserted at the end + schedule "print_values", p3, 1, iJson +endin + + +instr print_values + iJson = p4 + + ; dump JSON and print + prints sprintf("%s\n\n", jsondumps(iJson)) +endin + + + + +f0 11 +i"write_values" 0 10 + + \ No newline at end of file diff --git a/examples/example4.csd b/examples/example4.csd new file mode 100644 index 0000000..b6bc78c --- /dev/null +++ b/examples/example4.csd @@ -0,0 +1,57 @@ +/* + csound-json example 4 + + fill JSON object with various data at init time + +*/ + + + Released into the public domain under the Unlicense license + http://unlicense.org/ + + +-d +-m0 + + +sr = 44100 +ksmps = 64 +nchnls = 2 +0dbfs = 1 + + +instr boot + + ; empty object + iJson jsoninit + + ; add key/value combination as "description" object + Skeys[] fillarray "colour", "taste", "smell" + Svalues[] fillarray "blue", "sweet", "vile" + iJsonSub1 jsoninit + jsoninsertval iJsonSub1, Skeys, Svalues + jsoninsert iJson, "description", iJsonSub1 + + ; add key/value combination and additional value to iJsonSub2 + Skeys[] fillarray "height", "width" + ivalues[] fillarray 35.4, 6.41 + iJsonSub2 jsoninit + jsoninsertval iJsonSub2, Skeys, ivalues + jsoninsertval iJsonSub2, "depth", 16.439 + + ; add iJsonSub2 to array along with new specified string objects + iJsonObjects[] fillarray iJsonSub2, jsonloads("{\"not\": \"much\"}"), jsonloads("[1,2,3]") + + ; add all of the above iJsonObjects back into the main object under "measurements" key + jsoninsert iJson, "measurements", iJsonObjects + + ; show the result + prints sprintf("%s\n\n", jsondumps(iJson)) +endin + + + + +i"boot" 0 1 + + \ No newline at end of file diff --git a/examples/example5.csd b/examples/example5.csd new file mode 100644 index 0000000..d30629b --- /dev/null +++ b/examples/example5.csd @@ -0,0 +1,112 @@ +/* + csound-json example 5 + + fill JSON object with various data at init time + +*/ + + + Released into the public domain under the Unlicense license + http://unlicense.org/ + + +-d +-m0 + + +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 + iJson1 jsoninit + iJson2 jsoninit + schedule "run1", 0, 5, iJson1 + schedule "run2", 5, 5, iJson2 + 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 + + + + + +f1 0 64 10 1 ; sine +i"boot" 0 15 + + \ No newline at end of file diff --git a/examples/supplement.json b/examples/supplement.json new file mode 100644 index 0000000..5e20466 --- /dev/null +++ b/examples/supplement.json @@ -0,0 +1,10 @@ +{ + "instruments": { + "oscil3": [ + [10, 1, 123], + [11, 1, 456], + [12, 1, 789], + [13, 1, 432] + ] + } +} \ No newline at end of file -- cgit v1.2.3