Mercurial > hg > beaglert
comparison projects/heavy/circularBuffer/ControlDelay.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 |
comparison
equal
deleted
inserted
replaced
162:c3e8226a5651 | 163:20b52283c7b4 |
---|---|
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 "ControlDelay.h" | |
18 | |
19 hv_size_t cDelay_init(HvBase *_c, ControlDelay *o, float delayMs) { | |
20 o->delay = ctx_millisecondsToSamples(_c, delayMs); | |
21 hv_memset(o->msgs, __HV_DELAY_MAX_MESSAGES*sizeof(HvMessage *)); | |
22 return 0; | |
23 } | |
24 | |
25 void cDelay_onMessage(HvBase *_c, ControlDelay *o, int letIn, const HvMessage *const m, | |
26 void (*sendMessage)(HvBase *, int, const HvMessage *const)) { | |
27 switch (letIn) { | |
28 case 0: { | |
29 if (msg_compareSymbol(m, 0, "flush")) { | |
30 // send all messages immediately | |
31 for (int i = 0; i < __HV_DELAY_MAX_MESSAGES; i++) { | |
32 HvMessage *n = o->msgs[i]; | |
33 if (n != NULL) { | |
34 msg_setTimestamp(n, msg_getTimestamp(m)); // update the timestamp to now | |
35 sendMessage(_c, 0, n); // send the message | |
36 ctx_cancelMessage(_c, n, sendMessage); // then clear it | |
37 // NOTE(mhroth): there may be a problem here if a flushed message causes a clear message to return | |
38 // to this object in the same step | |
39 } | |
40 } | |
41 hv_memset(o->msgs, __HV_DELAY_MAX_MESSAGES*sizeof(HvMessage *)); | |
42 } else if (msg_compareSymbol(m, 0, "clear")) { | |
43 // cancel (clear) all (pending) messages | |
44 for (int i = 0; i < __HV_DELAY_MAX_MESSAGES; i++) { | |
45 HvMessage *n = o->msgs[i]; | |
46 if (n != NULL) { | |
47 ctx_cancelMessage(_c, n, sendMessage); | |
48 } | |
49 } | |
50 hv_memset(o->msgs, __HV_DELAY_MAX_MESSAGES*sizeof(HvMessage *)); | |
51 } else { | |
52 hv_uint32_t ts = msg_getTimestamp(m); | |
53 msg_setTimestamp((HvMessage *) m, ts+o->delay); // update the timestamp to set the delay | |
54 int i; | |
55 for (i = 0; i < __HV_DELAY_MAX_MESSAGES; i++) { | |
56 if (o->msgs[i] == NULL) { | |
57 o->msgs[i] = ctx_scheduleMessage(_c, m, sendMessage, 0); | |
58 break; | |
59 } | |
60 } | |
61 hv_assert(i < __HV_DELAY_MAX_MESSAGES); // scheduled message limit reached | |
62 msg_setTimestamp((HvMessage *) m, ts); // return to the original timestamp | |
63 } | |
64 break; | |
65 } | |
66 case 1: { | |
67 if (msg_isFloat(m,0)) { | |
68 // set delay in milliseconds | |
69 o->delay = ctx_millisecondsToSamples(_c,msg_getFloat(m,0)); | |
70 } | |
71 break; | |
72 } | |
73 case 2: { | |
74 if (msg_isFloat(m,0)) { | |
75 // set delay in samples | |
76 o->delay = (hv_uint32_t) msg_getFloat(m,0); | |
77 } | |
78 break; | |
79 } | |
80 default: break; | |
81 } | |
82 } | |
83 | |
84 void cDelay_clearExecutingMessage(ControlDelay *o, const HvMessage *const m) { | |
85 for (int i = 0; i < __HV_DELAY_MAX_MESSAGES; ++i) { | |
86 if (o->msgs[i] == m) { | |
87 o->msgs[i] = NULL; | |
88 break; | |
89 } | |
90 } | |
91 } |