comparison projects/heavy/samphold/ControlBinop.c @ 160:5bcf04234f80 heavy-updated

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