annotate projects/heavy/envelopeTrigger/HvTable.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 "HvTable.h"
chris@162 18
chris@162 19 hv_size_t hTable_init(HvTable *o, int length) {
chris@162 20 o->length = length;
chris@162 21 // true size of the table is always an integer multple of HV_N_SIMD
chris@162 22 o->size = (length + HV_N_SIMD_MASK) & ~HV_N_SIMD_MASK;
chris@162 23 // add an extra length for mirroring
chris@162 24 o->allocated = o->size + HV_N_SIMD;
chris@162 25 o->head = 0;
chris@162 26 hv_size_t numBytes = o->allocated * sizeof(float);
chris@162 27 o->buffer = (float *) hv_malloc(numBytes);
chris@162 28 hv_memset(o->buffer, numBytes);
chris@162 29 return numBytes;
chris@162 30 }
chris@162 31
chris@162 32 hv_size_t hTable_initWithData(HvTable *o, int length, const float *const data) {
chris@162 33 o->length = length;
chris@162 34 o->size = (length + HV_N_SIMD_MASK) & ~HV_N_SIMD_MASK;
chris@162 35 o->allocated = o->size + HV_N_SIMD;
chris@162 36 o->head = 0;
chris@162 37 hv_size_t numBytes = o->size * sizeof(float);
chris@162 38 o->buffer = (float *) hv_malloc(numBytes);
chris@162 39 hv_memset(o->buffer, numBytes);
chris@162 40 hv_memcpy(o->buffer, data, length*sizeof(float));
chris@162 41 return numBytes;
chris@162 42 }
chris@162 43
chris@162 44 hv_size_t hTable_initWithFinalData(HvTable *o, int length, float *data) {
chris@162 45 o->length = length;
chris@162 46 o->size = length;
chris@162 47 o->allocated = length;
chris@162 48 o->buffer = data;
chris@162 49 o->head = 0;
chris@162 50 return 0;
chris@162 51 }
chris@162 52
chris@162 53 void hTable_free(HvTable *o) {
chris@162 54 hv_free(o->buffer);
chris@162 55 }
chris@162 56
chris@162 57 int hTable_resize(HvTable *o, hv_uint32_t newLength) {
chris@162 58 // TODO(mhroth): update context with memory allocated by table
chris@162 59 // NOTE(mhroth): mirrored bytes are not necessarily carried over
chris@162 60 const hv_uint32_t oldBytes = (hv_uint32_t) (o->size * sizeof(float));
chris@162 61 const hv_uint32_t newSize = (newLength + HV_N_SIMD_MASK) & ~HV_N_SIMD_MASK;
chris@162 62 const hv_uint32_t newAllocated = newSize + HV_N_SIMD;
chris@162 63 const hv_uint32_t newBytes = (hv_uint32_t) (newAllocated * sizeof(float));
chris@162 64 float *b = (float *) hv_realloc(o->buffer, newBytes);
chris@162 65 hv_assert(b != NULL); // error while reallocing!
chris@162 66 if (newSize > o->size) hv_clear_buffer(b+o->size, newAllocated-o->size); // clear new parts of the buffer
chris@162 67 if (b != o->buffer) {
chris@162 68 // the buffer has been reallocated, ensure that it is on a 32-byte boundary
chris@162 69 if ((((uintptr_t) (const void *) b) & 0x10) == 0) {
chris@162 70 o->buffer = b;
chris@162 71 } else {
chris@162 72 float *c = (float *) hv_malloc(newBytes);
chris@162 73 hv_assert(c != NULL);
chris@162 74 hv_clear_buffer(c, newLength);
chris@162 75 const hv_size_t min = hv_min_ui(o->size, newLength);
chris@162 76 hv_memcpy(c, b, min * sizeof(float));
chris@162 77 hv_free(b);
chris@162 78 o->buffer = c;
chris@162 79 }
chris@162 80 }
chris@162 81 o->length = newLength;
chris@162 82 o->size = newSize;
chris@162 83 o->allocated = newAllocated;
chris@162 84 return (int) (newBytes - oldBytes);
chris@162 85 }
chris@162 86
chris@162 87 void hTable_onMessage(HvBase *_c, HvTable *o, int letIn, const HvMessage *const m,
chris@162 88 void (*sendMessage)(HvBase *, int, const HvMessage *const)) {
chris@162 89 if (msg_compareSymbol(m,0,"resize") && msg_isFloat(m,1) && msg_getFloat(m,1) >= 0.0f) {
chris@162 90 hTable_resize(o, (int) hv_ceil_f(msg_getFloat(m,1))); // apply ceil to ensure that tables always have enough space
chris@162 91
chris@162 92 // send out the new size of the table
chris@162 93 HvMessage *n = HV_MESSAGE_ON_STACK(1);
chris@162 94 msg_initWithFloat(n, msg_getTimestamp(m), (float) hTable_getSize(o));
chris@162 95 sendMessage(_c, 0, n);
chris@162 96 }
chris@162 97
chris@162 98 else if (msg_compareSymbol(m,0,"mirror")) {
chris@162 99 hv_memcpy(o->buffer+o->size, o->buffer, HV_N_SIMD*sizeof(float));
chris@162 100 }
chris@162 101 }