[1fe822d] | 1 | #include "aubio_priv.h" |
---|
| 2 | #include "fmat.h" |
---|
| 3 | #include "tensor.h" |
---|
| 4 | |
---|
[b508ba6] | 5 | aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape) |
---|
[1fe822d] | 6 | { |
---|
| 7 | aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t); |
---|
[2859f89] | 8 | uint_t items_per_row = 1; |
---|
[1fe822d] | 9 | uint_t i; |
---|
| 10 | |
---|
[b508ba6] | 11 | if ((sint_t)ndim <= 0) goto failure; |
---|
| 12 | for (i = 0; i < ndim; i++) { |
---|
| 13 | if ((sint_t)shape[i] <= 0) goto failure; |
---|
[1fe822d] | 14 | } |
---|
| 15 | |
---|
[b508ba6] | 16 | c->ndim = ndim; |
---|
| 17 | c->shape[0] = shape[0]; |
---|
| 18 | for (i = 1; i < ndim; i++) { |
---|
| 19 | c->shape[i] = shape[i]; |
---|
[83ec87f] | 20 | items_per_row *= shape[i]; |
---|
[1fe822d] | 21 | } |
---|
[83ec87f] | 22 | c->size = items_per_row * shape[0]; |
---|
[2859f89] | 23 | c->buffer = AUBIO_ARRAY(smpl_t, c->size); |
---|
[b508ba6] | 24 | c->data = AUBIO_ARRAY(smpl_t*, shape[0]); |
---|
[2859f89] | 25 | for (i = 0; i < c->shape[0]; i++) { |
---|
| 26 | c->data[i] = c->buffer + i * items_per_row; |
---|
[1fe822d] | 27 | } |
---|
| 28 | |
---|
| 29 | return c; |
---|
| 30 | |
---|
| 31 | failure: |
---|
| 32 | del_aubio_tensor(c); |
---|
| 33 | return NULL; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void del_aubio_tensor(aubio_tensor_t *c) |
---|
| 37 | { |
---|
| 38 | if (c->data) { |
---|
| 39 | if (c->data[0]) { |
---|
| 40 | AUBIO_FREE(c->data[0]); |
---|
| 41 | } |
---|
| 42 | AUBIO_FREE(c->data); |
---|
| 43 | } |
---|
| 44 | AUBIO_FREE(c); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) { |
---|
[0d9ef90] | 48 | if (!c || !o) return AUBIO_FAIL; |
---|
| 49 | o->length = c->size; |
---|
[1fe822d] | 50 | o->data = c->data[0]; |
---|
| 51 | return AUBIO_OK; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) { |
---|
[0d9ef90] | 55 | if (!o || !c) return AUBIO_FAIL; |
---|
[b508ba6] | 56 | c->ndim = 1; |
---|
| 57 | c->shape[0] = o->length; |
---|
[1fe822d] | 58 | c->data = &o->data; |
---|
[0d9ef90] | 59 | c->buffer = o->data; |
---|
[a5199ad] | 60 | c->size = o->length; |
---|
[1fe822d] | 61 | return AUBIO_OK; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) { |
---|
[0d9ef90] | 65 | if (!c || !o) return AUBIO_FAIL; |
---|
[b508ba6] | 66 | o->height = c->shape[0]; |
---|
[0d9ef90] | 67 | o->length = c->size / c->shape[0]; |
---|
[1fe822d] | 68 | o->data = c->data; |
---|
| 69 | return AUBIO_OK; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) { |
---|
[0d9ef90] | 73 | if (!o || !c) return AUBIO_FAIL; |
---|
[6ad67a5] | 74 | c->ndim = 2; |
---|
| 75 | c->shape[0] = o->height; |
---|
| 76 | c->shape[1] = o->length; |
---|
[a5199ad] | 77 | c->size = o->height * o->length; |
---|
[1fe822d] | 78 | c->data = o->data; |
---|
[0d9ef90] | 79 | c->buffer = o->data[0]; |
---|
[1fe822d] | 80 | return AUBIO_OK; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | smpl_t aubio_tensor_max(aubio_tensor_t *t) |
---|
| 84 | { |
---|
| 85 | uint_t i; |
---|
[533187f] | 86 | smpl_t max = t->buffer[0]; |
---|
[83ec87f] | 87 | for (i = 0; i < t->size; i++) { |
---|
[533187f] | 88 | max = MAX(t->buffer[i], max); |
---|
[1fe822d] | 89 | } |
---|
| 90 | return max; |
---|
| 91 | } |
---|
[c61cf97] | 92 | |
---|
| 93 | const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t) { |
---|
| 94 | uint_t i; |
---|
| 95 | if (!t) return NULL; |
---|
| 96 | size_t offset = 2; |
---|
| 97 | static char_t shape_str[STRN_LENGTH]; |
---|
| 98 | char_t shape_str_previous[STRN_LENGTH] = "("; |
---|
| 99 | for (i = 0; i < t->ndim; i++) { |
---|
| 100 | int len = snprintf(shape_str, STRN_LENGTH, "%s%d%s", |
---|
| 101 | shape_str_previous, t->shape[i], (i == t->ndim - 1) ? "" : ", "); |
---|
| 102 | strncpy(shape_str_previous, shape_str, len); |
---|
| 103 | } |
---|
| 104 | snprintf(shape_str, strnlen(shape_str, STRN_LENGTH - offset - 1) + offset, |
---|
| 105 | "%s)", shape_str_previous); |
---|
| 106 | } |
---|