{"repo_name": "sapf", "file_name": "/sapf/src/main.cpp", "inference_info": {"prefix_code": "// SAPF - Sound As Pure Form\n// Copyright (C) 2019 James McCartney\n//\n// This program is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// This program is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with this program. If not, see .\n\n#include \"VM.hpp\"\n#include \n#include \n#include \n#include \n#include \"primes.hpp\"\n#include \n#include \n#include \n#include \"Manta.h\"\n\n", "suffix_code": ";\n\nManta* manta();\nManta* manta()\n{\n\tstatic MyManta* sManta = new MyManta();\n\treturn sManta;\n}\n\n/* issue:\n\n[These comments are very old and I have not checked if they are still relevant.]\n\nTableData alloc should use new\n\nbugs:\n\nitd should have a tail time. currently the ugen stops as soon as its input, cutting off the delayed signal.\n\n+ should not stop until both inputs stop?\nother additive binops: - avg2 sumsq\n\nno, use a operator \n\n---\n\nadsrg (gate a d s r --> out) envelope generator with gate. \nadsr (dur a d s r --> out) envelope generator with duration. \nevgg - (gate levels times curves suspt --> out) envelope generator with gate. suspt is the index of the sustain level. \nevg - (dur levels times curves suspt --> out) envelope generator with duration. suspt is the index of the sustain level.\n\nblip (freq phase nharm --> out) band limited impulse oscillator.\ndsf1 (freq phase nharm lharm hmul --> out) sum of sines oscillator.\n\nformant (freq formfreq bwfreq --> out) formant oscillator\n\nsvf (in freq rq --> [lp hp bp bs]) state variable filter.\nmoogf (in freq rq --> out) moog ladder low pass filter.\n\n*/\n\nextern void AddCoreOps();\nextern void AddMathOps();\nextern void AddStreamOps();\nextern void AddLFOps();\nextern void AddUGenOps();\nextern void AddSetOps();\nextern void AddRandomOps();\nextern void AddMidiOps();\n\nconst char* gVersionString = \"0.1.21\";\n\nstatic void usage()\n{\n\tfprintf(stdout, \"sapf [-r sample-rate][-p prelude-file]\\n\");\n\tfprintf(stdout, \"\\n\");\n\tfprintf(stdout, \"sapf [-h]\\n\");\n\tfprintf(stdout, \" print this help\\n\");\n\tfprintf(stdout, \"\\n\");\t\n}\n\nint main (int argc, const char * argv[]) \n{\n\tpost(\"------------------------------------------------\\n\");\t\n\tpost(\"A tool for the expression of sound as pure form.\\n\");\t\n\tpost(\"------------------------------------------------\\n\");\t\n\tpost(\"--- version %s\\n\", gVersionString);\n\t\n\tfor (int i = 1; i < argc;) {\n\t\tint c = argv[i][0];\n\t\tif (c == '-') {\n\t\t\tc = argv[i][1];\n\t\t\tswitch (c) {\n\t\t\t\tcase 'r' : {\n\t\t\t\t\tif (argc <= i+1) { post(\"expected sample rate after -r\\n\"); return 1; }\n\t\t\t\t\t\t\n\t\t\t\t\tdouble sr = atof(argv[i+1]);\n\t\t\t\t\tif (sr < 1000. || sr > 768000.) { post(\"sample rate out of range.\\n\"); return 1; }\n\t\t\t\t\tvm.setSampleRate(sr);\n\t\t\t\t\tpost(\"sample rate set to %g\\n\", vm.ar.sampleRate);\n\t\t\t\t\ti += 2;\n\t\t\t\t} break;\n\t\t\t\tcase 'p' : {\n\t\t\t\t\tif (argc <= i+1) { post(\"expected prelude file name after -p\\n\"); return 1; }\n\t\t\t\t\tvm.prelude_file = argv[i+1];\n\t\t\t\t\ti += 2;\n\t\t\t\t} break;\n\t\t\t\tcase 'h' : {\n\t\t\t\t\tusage();\n\t\t\t\t\texit(0);\n\t\t\t\t} break;\n\t\t\t\tdefault: \n\t\t\t\t\tpost(\"unrecognized option -%c\\n\", c);\n\t\t\t}\n\t\t} else {\n\t\t\tpost(\"expected option, got \\\"%s\\\"\\n\", argv[i]);\n\t\t\t++i;\n\t\t}\n\t}\n\t\n\t\n\tvm.addBifHelp(\"Argument Automapping legend:\");\n\tvm.addBifHelp(\" a - as is. argument is not automapped.\");\n\tvm.addBifHelp(\" z - argument is expected to be a signal or scalar, streams are auto mapped.\");\n\tvm.addBifHelp(\" k - argument is expected to be a scalar, signals and streams are automapped.\");\n\tvm.addBifHelp(\"\");\n\t\n\tAddCoreOps();\n\tAddMathOps();\n\tAddStreamOps();\n AddRandomOps();\n\tAddUGenOps();\n\tAddMidiOps();\n AddSetOps();\n\t\n\t\n\tvm.log_file = getenv(\"SAPF_LOG\");\n\tif (!vm.log_file) {\n\t\tconst char* home_dir = getenv(\"HOME\");\n\t\tchar logfilename[PATH_MAX];\n\t\tsnprintf(logfilename, PATH_MAX, \"%s/sapf-log.txt\", home_dir);\n\t\tvm.log_file = strdup(logfilename);\n\t}\n\t\n\t__block Thread th;\n\n\tauto m = manta();\n\ttry {\n\t\tm->Connect();\n\t} catch(...) {\n\t}\n\tprintf(\"Manta %s connected.\\n\", m->IsConnected() ? \"is\" : \"IS NOT\");\n\n\tdispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\n\t\t/*** see at bottom for better way ***/\n\t\twhile(true) {\n\t\t\ttry {\n\t\t\t\tMantaUSB::HandleEvents();\n\t\t\t\tusleep(5000);\n\t\t\t} catch(...) {\n\t\t\t\tsleep(1);\n\t\t\t}\n\t\t}\n\t});\n\t\n\tif (!vm.prelude_file) {\n\t\tvm.prelude_file = getenv(\"SAPF_PRELUDE\");\n\t}\n\tif (vm.prelude_file) {\n\t\tloadFile(th, vm.prelude_file);\n\t}\n\t\n\tdispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\n\t\tth.repl(stdin, vm.log_file);\n\t\texit(0);\n\t});\n\t\n\tCFRunLoopRun();\n\t\n\treturn 0;\n}\n\n", "middle_code": "class MyManta : public Manta\n{\n\tvirtual void PadEvent(int row, int column, int id, int value) {\n\t\tprintf(\"pad %d %d %d %d\\n\", row, column, id, value);\n\t}\n\tvirtual void SliderEvent(int id, int value) {\n\t\tprintf(\"slider %d %d\\n\", id, value);\n\t}\n\tvirtual void ButtonEvent(int id, int value) {\n\t\tprintf(\"button %d %d\\n\", id, value);\n\t}\n\tvirtual void PadVelocityEvent(int row, int column, int id, int velocity) {\n\t\tprintf(\"pad vel %d %d %d %d\\n\", row, column, id, velocity);\n\t}\n\tvirtual void ButtonVelocityEvent(int id, int velocity) {\n\t\tprintf(\"button vel %d %d\\n\", id, velocity);\n\t}\n\tvirtual void FrameEvent(uint8_t *frame) {}\n\tvirtual void DebugPrint(const char *fmt, ...) {}\n}", "code_description": null, "fill_type": "CLASS_TYPE", "language_type": "cpp", "sub_task_type": null}, "context_code": [["/sapf/src/CoreOps.cpp", "// SAPF - Sound As Pure Form\n// Copyright (C) 2019 James McCartney\n//\n// This program is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// This program is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with this program. If not, see .\n\n#include \"VM.hpp\"\n#include \"Parser.hpp\"\n#include \"clz.hpp\"\n#include \n#include \n\n////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n// stack shufflers\n#pragma mark STACK OPS\n\nstatic void clear_(Thread& th, Prim* prim)\n{\n\tth.clearStack();\n}\n\nstatic void cleard_(Thread& th, Prim* prim)\n{\n\tV v = th.top();\n\tth.clearStack();\n\tth.push(v);\n}\n\nstatic void stackDepth_(Thread& th, Prim* prim)\n{\n\tth.push(th.stackDepth());\n}\n\nstatic void ba_(Thread& th, Prim* prim)\n{\n\tif (th.stackDepth() < 2)\n\t\tthrow errStackUnderflow;\n\tV* sp = &th.top();\n\tV b = sp[0];\n\tsp[0] = sp[-1];\n\tsp[-1] = b;\n}\n\nstatic void bac_(Thread& th, Prim* prim) \n{\n\t// swapd\n\tif (th.stackDepth() < 3)\n\t\tthrow errStackUnderflow;\n\tV* sp = &th.top();\n\tV a = sp[-2];\n\tV b = sp[-1];\n\tV c = sp[0];\n\tsp[-2] = b;\n\tsp[-1] = a;\n\tsp[0] = c;\n}\n\nstatic void cab_(Thread& th, Prim* prim)\n{\n\t// rrot\n\tif (th.stackDepth() < 3)\n\t\tthrow errStackUnderflow;\n\tV* sp = &th.top();\n\tV a = sp[-2];\n\tV b = sp[-1];\n\tV c = sp[0];\n\tsp[-2] = c;\n\tsp[-1] = a;\n\tsp[0] = b;\n}\n\nstatic void bca_(Thread& th, Prim* prim)\n{\n\t// rot\n\tif (th.stackDepth() < 3)\n\t\tthrow errStackUnderflow;\n\tV* sp = &th.top();\n\tV a = sp[-2];\n\tV b = sp[-1];\n\tV c = sp[0];\n\tsp[-2] = b;\n\tsp[-1] = c;\n\tsp[0] = a;\n}\n\nstatic void cba_(Thread& th, Prim* prim)\n{\n\t// reverse top 3\n\tif (th.stackDepth() < 3)\n\t\tthrow errStackUnderflow;\n\tV* sp = &th.top();\n\tV a = sp[-2];\n\tV b = sp[-1];\n\tV c = sp[0];\n\tsp[-2] = c;\n\tsp[-1] = b;\n\tsp[0] = a;\n}\n\nstatic void aa_(Thread& th, Prim* prim)\n{\n\t// dup\n\tV v = th.top();\n\tth.push(v);\n}\n\nstatic void aaa_(Thread& th, Prim* prim)\n{\n\t// dup\n\tV v = th.top();\n\tth.push(v);\n\tth.push(v);\n}\n\nstatic void aba_(Thread& th, Prim* prim)\n{\n\t// over\n\tif (th.stackDepth() < 2)\n\t\tthrow errStackUnderflow;\n\n\tV* sp = &th.top();\n\tth.push(sp[-1]);\n}\n\nstatic void bab_(Thread& th, Prim* prim)\n{\n\t// tuck\n\tif (th.stackDepth() < 2)\n\t\tthrow errStackUnderflow;\n\n\tV* sp = &th.top();\n\tV a = sp[-1];\n\tV b = sp[0];\n\tth.push(b);\n\n\tsp[-1] = b;\n\tsp[0] = a;\n}\n\nstatic void aab_(Thread& th, Prim* prim)\n{\n\t// tuck\n\tif (th.stackDepth() < 2)\n\t\tthrow errStackUnderflow;\n \n\tV* sp = &th.top();\n\tV a = sp[-1];\n\tV b = sp[0];\n\tth.push(b);\t\n\tsp[0] = a;\n}\n\nstatic void aabb_(Thread& th, Prim* prim)\n{\n\t// tuck\n\tif (th.stackDepth() < 2)\n\t\tthrow errStackUnderflow;\n \n\tV* sp = &th.top();\n\tV a = sp[-1];\n\tV b = sp[0];\n\tth.push(b);\t\n\tsp[0] = a;\n\tth.push(b);\t\n}\n\nstatic void abab_(Thread& th, Prim* prim)\n{\n\tif (th.stackDepth() < 2)\n\t\tthrow errStackUnderflow;\n\n\tV* sp = &th.top();\n\tV a = sp[-1];\n\tV b = sp[0];\n\tth.push(a);\t\n\tth.push(b);\t\n}\n\nstatic void nip_(Thread& th, Prim* prim)\n{\n\tif (th.stackDepth() < 2)\n\t\tthrow errStackUnderflow;\n\n\tV* sp = &th.top();\n\tV b = sp[0];\n\tsp[-1] = b;\n\tth.pop();\n}\n\n\nstatic void pop_(Thread& th, Prim* prim)\n{\n\tth.pop();\n}\n\n////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n#pragma mark INHERIT\n\nstatic bool hasItem(int64_t size, Table** a, Table* item)\n{\n\tfor (int64_t i = 0; iIdentical(item))\n\t\t\treturn true;\n\t}\n\treturn false;\n}\n\nstatic void Envir_merge2(Thread& th, int64_t asize, Table** a, int64_t bsize, Table** b, int64_t& csize, Table** c0)\n{\n\n\tTable** c = c0;\n\tTable** aend = a + asize;\n\tTable** bend = b + bsize;\n\twhile (a < aend && b < bend) {\t\n\t\tif ((*a)->Identical(*b)) {\n\t\t\t*c++ = *a++;\n\t\t\tb++;\n\t\t} else if (!hasItem(bend-b-1, b+1, *a)) {\n\t\t\t*c++ = *a++;\n\t\t} else if (!hasItem(aend-a-1, a+1, *b)) {\n\t\t\t*c++ = *b++;\n\t\t} else {\n\t\t\tthrow errInconsistentInheritance;\n\t\t}\n\t}\n\twhile (a < aend) { *c++ = *a++; }\n\twhile (b < bend) { *c++ = *b++; }\n\tcsize = c - c0;\n}\n\t\nstatic int64_t Envir_toVec(O list, int64_t maxSize, Table** vec)\n{\n\tint64_t i = 0;\n\tfor (; list && i < maxSize-1;) {\n\t\tvec[i++] = ((Form*)list)->mTable();\n\t\tlist = ((Form*)list)->mNextForm();\n\t}\n\treturn i;\n}\n\nstatic P