annotate projects/heavy/envelopeTrigger/ControlBinop.c @ 162:c3e8226a5651 heavy-updated

- added additional flags to C rules (-DNDEBUG, -mfpu=neon) - sample-accurate envelope triggering pd/heavy example
author chnrx <chris.heinrichs@gmail.com>
date Thu, 12 Nov 2015 14:59:46 +0000
parents
children
rev   line source
chris@162 1 /**
chris@162 2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
chris@162 3 *
chris@162 4 * Permission to use, copy, modify, and/or distribute this software for any
chris@162 5 * purpose with or without fee is hereby granted, provided that the above
chris@162 6 * copyright notice and this permission notice appear in all copies.
chris@162 7 *
chris@162 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
chris@162 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
chris@162 10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
chris@162 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
chris@162 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
chris@162 13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
chris@162 14 * PERFORMANCE OF THIS SOFTWARE.
chris@162 15 */
chris@162 16
chris@162 17 #include "ControlBinop.h"
chris@162 18
chris@162 19 hv_size_t cBinop_init(ControlBinop *o, float k) {
chris@162 20 o->k = k;
chris@162 21 return 0;
chris@162 22 }
chris@162 23
chris@162 24 static float cBinop_perform_op(BinopType op, float f, const float k) {
chris@162 25 switch (op) {
chris@162 26 case HV_BINOP_ADD: return f + k;
chris@162 27 case HV_BINOP_SUBTRACT: return f - k;
chris@162 28 case HV_BINOP_MULTIPLY: return f * k;
chris@162 29 case HV_BINOP_DIVIDE: return (k != 0.0f) ? f / k : 0.0f;
chris@162 30 case HV_BINOP_INT_DIV: return (float) ((int) f / (int) k);
chris@162 31 case HV_BINOP_MOD_BIPOLAR: return (float) ((int) f % (int) k);
chris@162 32 case HV_BINOP_MOD_UNIPOLAR: {
chris@162 33 f = (k == 0.0f) ? 0.0f : (float) ((int) f % (int) k);
chris@162 34 return (f < 0.0f) ? f + fabsf(k) : f;
chris@162 35 }
chris@162 36 case HV_BINOP_BIT_LEFTSHIFT: return (float) (((int) f) << ((int) k));
chris@162 37 case HV_BINOP_BIT_RIGHTSHIFT: return (float) (((int) f) >> ((int) k));
chris@162 38 case HV_BINOP_BIT_AND: return (float) ((int) f & (int) k);
chris@162 39 case HV_BINOP_BIT_XOR: return (float) ((int) f ^ (int) k);
chris@162 40 case HV_BINOP_BIT_OR: return (float) ((int) f | (int) k);
chris@162 41 case HV_BINOP_EQ: return (f == k) ? 1.0f : 0.0f;
chris@162 42 case HV_BINOP_NEQ: return (f != k) ? 1.0f : 0.0f;
chris@162 43 case HV_BINOP_LOGICAL_AND: return ((f == 0.0f) || (k == 0.0f)) ? 0.0f : 1.0f;
chris@162 44 case HV_BINOP_LOGICAL_OR: return ((f == 0.0f) && (k == 0.0f)) ? 0.0f : 1.0f;
chris@162 45 case HV_BINOP_LESS_THAN: return (f < k) ? 1.0f : 0.0f;
chris@162 46 case HV_BINOP_LESS_THAN_EQL: return (f <= k) ? 1.0f : 0.0f;
chris@162 47 case HV_BINOP_GREATER_THAN: return (f > k) ? 1.0f : 0.0f;
chris@162 48 case HV_BINOP_GREATER_THAN_EQL: return (f >= k) ? 1.0f : 0.0f;
chris@162 49 case HV_BINOP_MAX: return hv_max_f(f, k);
chris@162 50 case HV_BINOP_MIN: return hv_min_f(f, k);
chris@162 51 case HV_BINOP_POW: return (f > 0.0f) ? powf(f, k) : 0.0f;
chris@162 52 case HV_BINOP_ATAN2: return ((f == 0.0f) && (k == 0.0f)) ? 0.0f : atan2f(f, k);
chris@162 53 default: return 0.0f;
chris@162 54 }
chris@162 55 }
chris@162 56
chris@162 57 void cBinop_onMessage(HvBase *_c, ControlBinop *o, BinopType op, int letIn,
chris@162 58 const HvMessage *const m,
chris@162 59 void (*sendMessage)(HvBase *, int, const HvMessage *const)) {
chris@162 60 switch (letIn) {
chris@162 61 case 0: {
chris@162 62 if (msg_isFloat(m, 0)) {
chris@162 63 // Note(joe): supporting Pd's ability to perform operations of packs
chris@162 64 // of floats is likely to not be supported in the future.
chris@162 65 if (msg_isFloat(m, 1)) o->k = msg_getFloat(m, 1);
chris@162 66 HvMessage *n = HV_MESSAGE_ON_STACK(1);
chris@162 67 float f = cBinop_perform_op(op, msg_getFloat(m, 0), o->k);
chris@162 68 msg_initWithFloat(n, msg_getTimestamp(m), f);
chris@162 69 sendMessage(_c, 0, n);
chris@162 70 }
chris@162 71 break;
chris@162 72 }
chris@162 73 case 1: {
chris@162 74 if (msg_isFloat(m, 0)) {
chris@162 75 o->k = msg_getFloat(m, 0);
chris@162 76 }
chris@162 77 break;
chris@162 78 }
chris@162 79 default: break;
chris@162 80 }
chris@162 81 }
chris@162 82
chris@162 83 void cBinop_k_onMessage(HvBase *_c, void *o, BinopType op, const float k,
chris@162 84 int letIn, const HvMessage *const m,
chris@162 85 void (*sendMessage)(HvBase *, int, const HvMessage *const)) {
chris@162 86 if (msg_isFloat(m, 0)) {
chris@162 87 // NOTE(mhroth): Heavy does not support sending bangs to binop objects to return the previous output
chris@162 88 float f = (msg_isFloat(m, 1)) ? msg_getFloat(m, 1) : k;
chris@162 89 HvMessage *n = HV_MESSAGE_ON_STACK(1);
chris@162 90 f = cBinop_perform_op(op, msg_getFloat(m, 0), f);
chris@162 91 msg_initWithFloat(n, msg_getTimestamp(m), f);
chris@162 92 sendMessage(_c, 0, n);
chris@162 93 }
chris@162 94 }