annotate projects/heavy/circularBuffer/ControlBinop.c @ 163:20b52283c7b4 heavy-updated

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