chris@162: /** chris@162: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@162: * chris@162: * Permission to use, copy, modify, and/or distribute this software for any chris@162: * purpose with or without fee is hereby granted, provided that the above chris@162: * copyright notice and this permission notice appear in all copies. chris@162: * chris@162: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@162: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@162: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@162: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@162: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@162: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@162: * PERFORMANCE OF THIS SOFTWARE. chris@162: */ chris@162: chris@162: #include "ControlBinop.h" chris@162: chris@162: hv_size_t cBinop_init(ControlBinop *o, float k) { chris@162: o->k = k; chris@162: return 0; chris@162: } chris@162: chris@162: static float cBinop_perform_op(BinopType op, float f, const float k) { chris@162: switch (op) { chris@162: case HV_BINOP_ADD: return f + k; chris@162: case HV_BINOP_SUBTRACT: return f - k; chris@162: case HV_BINOP_MULTIPLY: return f * k; chris@162: case HV_BINOP_DIVIDE: return (k != 0.0f) ? f / k : 0.0f; chris@162: case HV_BINOP_INT_DIV: return (float) ((int) f / (int) k); chris@162: case HV_BINOP_MOD_BIPOLAR: return (float) ((int) f % (int) k); chris@162: case HV_BINOP_MOD_UNIPOLAR: { chris@162: f = (k == 0.0f) ? 0.0f : (float) ((int) f % (int) k); chris@162: return (f < 0.0f) ? f + fabsf(k) : f; chris@162: } chris@162: case HV_BINOP_BIT_LEFTSHIFT: return (float) (((int) f) << ((int) k)); chris@162: case HV_BINOP_BIT_RIGHTSHIFT: return (float) (((int) f) >> ((int) k)); chris@162: case HV_BINOP_BIT_AND: return (float) ((int) f & (int) k); chris@162: case HV_BINOP_BIT_XOR: return (float) ((int) f ^ (int) k); chris@162: case HV_BINOP_BIT_OR: return (float) ((int) f | (int) k); chris@162: case HV_BINOP_EQ: return (f == k) ? 1.0f : 0.0f; chris@162: case HV_BINOP_NEQ: return (f != k) ? 1.0f : 0.0f; chris@162: case HV_BINOP_LOGICAL_AND: return ((f == 0.0f) || (k == 0.0f)) ? 0.0f : 1.0f; chris@162: case HV_BINOP_LOGICAL_OR: return ((f == 0.0f) && (k == 0.0f)) ? 0.0f : 1.0f; chris@162: case HV_BINOP_LESS_THAN: return (f < k) ? 1.0f : 0.0f; chris@162: case HV_BINOP_LESS_THAN_EQL: return (f <= k) ? 1.0f : 0.0f; chris@162: case HV_BINOP_GREATER_THAN: return (f > k) ? 1.0f : 0.0f; chris@162: case HV_BINOP_GREATER_THAN_EQL: return (f >= k) ? 1.0f : 0.0f; chris@162: case HV_BINOP_MAX: return hv_max_f(f, k); chris@162: case HV_BINOP_MIN: return hv_min_f(f, k); chris@162: case HV_BINOP_POW: return (f > 0.0f) ? powf(f, k) : 0.0f; chris@162: case HV_BINOP_ATAN2: return ((f == 0.0f) && (k == 0.0f)) ? 0.0f : atan2f(f, k); chris@162: default: return 0.0f; chris@162: } chris@162: } chris@162: chris@162: void cBinop_onMessage(HvBase *_c, ControlBinop *o, BinopType op, int letIn, chris@162: const HvMessage *const m, chris@162: void (*sendMessage)(HvBase *, int, const HvMessage *const)) { chris@162: switch (letIn) { chris@162: case 0: { chris@162: if (msg_isFloat(m, 0)) { chris@162: // Note(joe): supporting Pd's ability to perform operations of packs chris@162: // of floats is likely to not be supported in the future. chris@162: if (msg_isFloat(m, 1)) o->k = msg_getFloat(m, 1); chris@162: HvMessage *n = HV_MESSAGE_ON_STACK(1); chris@162: float f = cBinop_perform_op(op, msg_getFloat(m, 0), o->k); chris@162: msg_initWithFloat(n, msg_getTimestamp(m), f); chris@162: sendMessage(_c, 0, n); chris@162: } chris@162: break; chris@162: } chris@162: case 1: { chris@162: if (msg_isFloat(m, 0)) { chris@162: o->k = msg_getFloat(m, 0); chris@162: } chris@162: break; chris@162: } chris@162: default: break; chris@162: } chris@162: } chris@162: chris@162: void cBinop_k_onMessage(HvBase *_c, void *o, BinopType op, const float k, chris@162: int letIn, const HvMessage *const m, chris@162: void (*sendMessage)(HvBase *, int, const HvMessage *const)) { chris@162: if (msg_isFloat(m, 0)) { chris@162: // NOTE(mhroth): Heavy does not support sending bangs to binop objects to return the previous output chris@162: float f = (msg_isFloat(m, 1)) ? msg_getFloat(m, 1) : k; chris@162: HvMessage *n = HV_MESSAGE_ON_STACK(1); chris@162: f = cBinop_perform_op(op, msg_getFloat(m, 0), f); chris@162: msg_initWithFloat(n, msg_getTimestamp(m), f); chris@162: sendMessage(_c, 0, n); chris@162: } chris@162: }