Skip to content

Commit

Permalink
change syntax to avoid typing global so many times
Browse files Browse the repository at this point in the history
  • Loading branch information
David Braun committed Jan 16, 2022
1 parent e8b189e commit a867eee
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 59 deletions.
Binary file modified ChucKDesigner.toe
Binary file not shown.
53 changes: 25 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ Due to some difficulties with codesigning, for the moment you must compile ChucK

The ChucK Audio CHOP's functions:

* `.set_global_float(name: str, val: float)`
* `.set_global_int(name: str, val: int)`
* `.set_global_string(name: str, val: int)`
* `.set_global_float_array(name: str, vals: List[float])` **not numpy arrays!**
* `.set_global_int_array(name: str, vals: List[int])` **not numpy arrays!**
* `.set_global_float_array_value(name: str, index: int, val: float)`
* `.set_global_int_array_value(name: str, index: int, val: int)`
* `.set_global_associative_float_array_value(name: str, key: str, val: float)`
* `.set_global_associative_int_array_value(name: str, key: str, val: int)`
* `.set_float(name: str, val: float)`
* `.set_int(name: str, val: int)`
* `.set_string(name: str, val: int)`
* `.set_float_array(name: str, vals: List[float])` **not numpy arrays!**
* `.set_int_array(name: str, vals: List[int])` **not numpy arrays!**
* `.set_float_array_value(name: str, index: int, val: float)`
* `.set_int_array_value(name: str, index: int, val: int)`
* `.set_associative_float_array_value(name: str, key: str, val: float)`
* `.set_associative_int_array_value(name: str, key: str, val: int)`
* `.broadcast_event(name: str)`
* `.set_log_level(level: int)`

Expand All @@ -93,7 +93,7 @@ This can be seen in the following image:
![Float Example TouchDesigner Screenshot](docs/float_example.png?raw=true "Float Example TouchDesigner Screenshot")

In TouchDesigner, we can execute the Python code
`op('chuckaudio1').set_global_float("freq", 880.)`
`op('chuckaudio1').set_float("freq", 880.)`
This will set the global float variable named "freq" to 880. The code has been written to update the sine oscillator's frequency every 10 milliseconds, so you will immediately hear a change in the frequency. Note that the code below would **not** have led to a change in sound.

```chuck
Expand Down Expand Up @@ -129,32 +129,29 @@ On the ChucK Audio Listener, there is a custom parameter for "Float Variables".

### Python Callbacks in ChucK Listener CHOP

In the example above, we used a `global float freq` and a `global int randInt`. Find the `Float Variables` and `Int Variables` custom parameters on the ChucK Listener CHOP and set them to `freq` and `randInt` respectively. Now `freq` will appear in the `getGlobalFloat` callback, and `randInt` will appear in the `getGlobalInt` callback.
In the example above, we used a `global float freq` and a `global int randInt`. Find the `Float Variables` and `Int Variables` custom parameters on the ChucK Listener CHOP and set them to `freq` and `randInt` respectively. Now `freq` will appear in the `getFloat` callback, and `randInt` will appear in the `getInt` callback.

```python
# This is an example callbacks DAT for a ChucK Audio Operator.
# In all callback methods, "listener" is the ChucK Listener operator doing the callback.

def getGlobalFloat(listener, name, val):
print(f'getGlobalFloat(name="{name}", val={val})')
def getFloat(listener, name, val):
print(f'getFloat(name="{name}", val={val})')

def getGlobalInt(listener, name, val):
print(f'getGlobalInt(name="{name}", val={val})')
def getInt(listener, name, val):
print(f'getInt(name="{name}", val={val})')

def getGlobalString(listener, name, val):
print(f'getGlobalString(name="{name}", val={val})')
def getString(listener, name, val):
print(f'getString(name="{name}", val={val})')

def getGlobalEvent(listener, name):
print(f'getGlobalEvent(name="{name}")')
def getEvent(listener, name):
print(f'getEvent(name="{name}")')

def getGlobalFloatArray(listener, name, vals):
print(f'getGlobalFloatArray(name="{name}", vals={vals})')
def getFloatArray(listener, name, vals):
print(f'getFloatArray(name="{name}", vals={vals})')

def getGlobalIntArray(listener, name, vals):
print(f'getGlobalIntArray(name="{name}", vals={vals})')

def getGlobalEvent(listener, name):
print(f'getGlobalEvent(name="{name}")')
def getIntArray(listener, name, vals):
print(f'getIntArray(name="{name}", vals={vals})')
```

Most of these callbacks are straightforward to understand, but the `Event` type syntax is worth discussing. Let this be the compiled ChucK code:
Expand All @@ -170,7 +167,7 @@ fun void playImpact() {
// chuck enough time so that the buf plays
1::second => now;

// invoke getGlobalEvent("notifier") in TouchDesigner
// invoke getEvent("notifier") in TouchDesigner
notifier.broadcast();
}

Expand All @@ -185,4 +182,4 @@ At the beginning, there will be 2 channels of output by default. In TouchDesigne
op('chuckaudio1').broadcast_event('pulse')
```

This will spork a shred of `playImpact()`, which will play a short sound. After 1 second of playing the sound, ChucK will broadcast an event named "notifier" back to TouchDesigner. This event will show up in the `getGlobalEvent()` method, if "notifier" is in custom parameter `Event Variables`.
This will spork a shred of `playImpact()`, which will play a short sound. After 1 second of playing the sound, ChucK will broadcast an event named "notifier" back to TouchDesigner. This event will show up in the `getEvent()` method, if "notifier" is in custom parameter `Event Variables`.
18 changes: 9 additions & 9 deletions src/ChucKDesignerCHOP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,18 +427,18 @@ pySetLogLevel(PyObject* self, PyObject* args, void*)

static PyMethodDef methods[] =
{
{"set_global_float", (PyCFunction)pySetGlobalFloat, METH_VARARGS, "Set a ChucK global float variable."},
{"set_global_int", (PyCFunction)pySetGlobalInt, METH_VARARGS, "Set a ChucK global float variable."},
{"set_global_string", (PyCFunction)pySetGlobalString, METH_VARARGS, "Set a ChucK global string variable."},
{"set_float", (PyCFunction)pySetGlobalFloat, METH_VARARGS, "Set a ChucK global float variable."},
{"set_int", (PyCFunction)pySetGlobalInt, METH_VARARGS, "Set a ChucK global float variable."},
{"set_string", (PyCFunction)pySetGlobalString, METH_VARARGS, "Set a ChucK global string variable."},

{"set_global_float_array", (PyCFunction)pySetGlobalFloatArray, METH_VARARGS, "Set a ChucK global float array variable."},
{"set_global_int_array", (PyCFunction)pySetGlobalIntArray, METH_VARARGS, "Set a ChucK global int array variable."},
{"set_float_array", (PyCFunction)pySetGlobalFloatArray, METH_VARARGS, "Set a ChucK global float array variable."},
{"set_int_array", (PyCFunction)pySetGlobalIntArray, METH_VARARGS, "Set a ChucK global int array variable."},

{"set_global_float_array_value", (PyCFunction)pySetGlobalFloatArrayValue, METH_VARARGS, "Set a single value in a ChucK global float array variable."},
{"set_global_int_array_value", (PyCFunction)pySetGlobalIntArrayValue, METH_VARARGS, "Set a single value in a ChucK global int array variable."},
{"set_float_array_value", (PyCFunction)pySetGlobalFloatArrayValue, METH_VARARGS, "Set a single value in a ChucK global float array variable."},
{"set_int_array_value", (PyCFunction)pySetGlobalIntArrayValue, METH_VARARGS, "Set a single value in a ChucK global int array variable."},

{"set_global_associative_float_array_value", (PyCFunction)pySetGlobalAssociativeFloatArrayValue, METH_VARARGS, "Set a single value in an associative ChucK global float array variable."},
{"set_global_associative_float_int_value", (PyCFunction)pySetGlobalAssociativeIntArrayValue, METH_VARARGS, "Set a single value in an associative ChucK global int array variable."},
{"set_associative_float_array_value", (PyCFunction)pySetGlobalAssociativeFloatArrayValue, METH_VARARGS, "Set a single value in an associative ChucK global float array variable."},
{"set_associative_float_int_value", (PyCFunction)pySetGlobalAssociativeIntArrayValue, METH_VARARGS, "Set a single value in an associative ChucK global int array variable."},

{"broadcast_event", (PyCFunction)pyBroadcastChuckEvent, METH_VARARGS, "Broadcast an event to ChucK."},

Expand Down
40 changes: 18 additions & 22 deletions src/ChucKListenerCHOP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,28 @@ const char* PythonCallbacksDATStubs =
"# This is an example callbacks DAT for a ChucK Audio Operator.\n"
"# In all callback methods, \"listener\" is the ChucK Listener operator doing the callback.\n"
"\n\n"
"def getGlobalFloat(listener, name, val):\n"
" # print(f'getGlobalFloat(name=\"{name}\", val={val})')\n"
"def getFloat(listener, name, val):\n"
" # print(f'getFloat(name=\"{name}\", val={val})')\n"
" pass\n"
"\n\n"
"def getGlobalInt(listener, name, val):\n"
" # print(f'getGlobalInt(name=\"{name}\", val={val})')\n"
"def getInt(listener, name, val):\n"
" # print(f'getInt(name=\"{name}\", val={val})')\n"
" pass\n"
"\n\n"
"def getGlobalString(listener, name, val):\n"
" # print(f'getGlobalString(name=\"{name}\", val={val})')\n"
"def getString(listener, name, val):\n"
" # print(f'getString(name=\"{name}\", val={val})')\n"
" pass\n"
"\n\n"
"def getGlobalEvent(listener, name):\n"
" # print(f'getGlobalEvent(name=\"{name}\")')\n"
"def getEvent(listener, name):\n"
" # print(f'getEvent(name=\"{name}\")')\n"
" pass\n"
"\n\n"
"def getGlobalFloatArray(listener, name, vals):\n"
" # print(f'getGlobalFloatArray(name=\"{name}\", vals={vals})')\n"
"def getFloatArray(listener, name, vals):\n"
" # print(f'getFloatArray(name=\"{name}\", vals={vals})')\n"
" pass\n"
"\n\n"
"def getGlobalIntArray(listener, name, vals):\n"
" # print(f'getGlobalIntArray(name=\"{name}\", vals={vals})')\n"
" pass\n"
"\n\n"
"def getGlobalEvent(listener, name):\n"
" # print(f'getGlobalEvent(name=\"{name}\")')\n"
"def getIntArray(listener, name, vals):\n"
" # print(f'getIntArray(name=\"{name}\", vals={vals})')\n"
" pass\n"
;

Expand Down Expand Up @@ -329,7 +325,7 @@ ChucKListenerCHOP::execute(CHOP_Output* output,
PyTuple_SET_ITEM(args, 2, PyLong_FromDouble(val));


PyObject *result = myNodeInfo->context->callPythonCallback("getGlobalFloat", args, nullptr, nullptr);
PyObject *result = myNodeInfo->context->callPythonCallback("getFloat", args, nullptr, nullptr);
// callPythonCallback doesn't take ownership of the argts
Py_DECREF(args);

Expand All @@ -353,7 +349,7 @@ ChucKListenerCHOP::execute(CHOP_Output* output,
PyTuple_SET_ITEM(args, 1, PyUnicode_FromString(name));
PyTuple_SET_ITEM(args, 2, PyLong_FromLongLong(val));

PyObject* result = myNodeInfo->context->callPythonCallback("getGlobalInt", args, nullptr, nullptr);
PyObject* result = myNodeInfo->context->callPythonCallback("getInt", args, nullptr, nullptr);
// callPythonCallback doesn't take ownership of the argts
Py_DECREF(args);

Expand All @@ -372,7 +368,7 @@ ChucKListenerCHOP::execute(CHOP_Output* output,
PyTuple_SET_ITEM(args, 1, PyUnicode_FromString(name));
PyTuple_SET_ITEM(args, 2, PyUnicode_FromString(str));

PyObject* result = myNodeInfo->context->callPythonCallback("getGlobalString", args, nullptr, nullptr);
PyObject* result = myNodeInfo->context->callPythonCallback("getString", args, nullptr, nullptr);
// callPythonCallback doesn't take ownership of the argts
Py_DECREF(args);

Expand Down Expand Up @@ -403,7 +399,7 @@ ChucKListenerCHOP::execute(CHOP_Output* output,

PyTuple_SET_ITEM(args, 2, lst);

PyObject *result = myNodeInfo->context->callPythonCallback("getGlobalFloatArray", args, nullptr, nullptr);
PyObject *result = myNodeInfo->context->callPythonCallback("getFloatArray", args, nullptr, nullptr);
// callPythonCallback doesn't take ownership of the argts
Py_DECREF(args);
//Py_DECREF(lst); // todo?
Expand Down Expand Up @@ -435,7 +431,7 @@ ChucKListenerCHOP::execute(CHOP_Output* output,

PyTuple_SET_ITEM(args, 2, lst);

PyObject* result = myNodeInfo->context->callPythonCallback("getGlobalIntArray", args, nullptr, nullptr);
PyObject* result = myNodeInfo->context->callPythonCallback("getIntArray", args, nullptr, nullptr);
// callPythonCallback doesn't take ownership of the argts
Py_DECREF(args);
//Py_DECREF(lst); // todo?
Expand All @@ -454,7 +450,7 @@ ChucKListenerCHOP::execute(CHOP_Output* output,
// The first argument is already set to the 'op' variable, so we set the second argument to our speed value
PyTuple_SET_ITEM(args, 1, PyUnicode_FromString(name));

PyObject* result = myNodeInfo->context->callPythonCallback("getGlobalEvent", args, nullptr, nullptr);
PyObject* result = myNodeInfo->context->callPythonCallback("getEvent", args, nullptr, nullptr);
// callPythonCallback doesn't take ownership of the argts
Py_DECREF(args);

Expand Down

0 comments on commit a867eee

Please sign in to comment.