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