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