1 | #include "aubio_priv.h" |
---|
2 | #include "fmat.h" |
---|
3 | #include "tensor.h" |
---|
4 | |
---|
5 | aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape) |
---|
6 | { |
---|
7 | aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t); |
---|
8 | uint_t items_per_row = 1; |
---|
9 | uint_t i; |
---|
10 | |
---|
11 | if ((sint_t)ndim <= 0) goto failure; |
---|
12 | for (i = 0; i < ndim; i++) { |
---|
13 | if ((sint_t)shape[i] <= 0) goto failure; |
---|
14 | } |
---|
15 | |
---|
16 | c->ndim = ndim; |
---|
17 | c->shape[0] = shape[0]; |
---|
18 | for (i = 1; i < ndim; i++) { |
---|
19 | c->shape[i] = shape[i]; |
---|
20 | items_per_row *= shape[i]; |
---|
21 | } |
---|
22 | c->size = items_per_row * shape[0]; |
---|
23 | c->buffer = AUBIO_ARRAY(smpl_t, c->size); |
---|
24 | c->data = AUBIO_ARRAY(smpl_t*, shape[0]); |
---|
25 | for (i = 0; i < c->shape[0]; i++) { |
---|
26 | c->data[i] = c->buffer + i * items_per_row; |
---|
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) { |
---|
48 | if (!c || !o) return AUBIO_FAIL; |
---|
49 | o->length = c->size; |
---|
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) { |
---|
55 | if (!o || !c) return AUBIO_FAIL; |
---|
56 | c->ndim = 1; |
---|
57 | c->shape[0] = o->length; |
---|
58 | c->data = &o->data; |
---|
59 | c->buffer = o->data; |
---|
60 | c->size = o->length; |
---|
61 | return AUBIO_OK; |
---|
62 | } |
---|
63 | |
---|
64 | uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) { |
---|
65 | if (!c || !o) return AUBIO_FAIL; |
---|
66 | o->height = c->shape[0]; |
---|
67 | o->length = c->size / c->shape[0]; |
---|
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) { |
---|
73 | if (!o || !c) return AUBIO_FAIL; |
---|
74 | c->ndim = 2; |
---|
75 | c->shape[0] = o->height; |
---|
76 | c->shape[1] = o->length; |
---|
77 | c->size = o->height * o->length; |
---|
78 | c->data = o->data; |
---|
79 | c->buffer = o->data[0]; |
---|
80 | return AUBIO_OK; |
---|
81 | } |
---|
82 | |
---|
83 | smpl_t aubio_tensor_max(aubio_tensor_t *t) |
---|
84 | { |
---|
85 | uint_t i; |
---|
86 | smpl_t max = -1000000; |
---|
87 | for (i = 0; i < t->size; i++) { |
---|
88 | max = MAX(t->data[0][i], max); |
---|
89 | } |
---|
90 | return max; |
---|
91 | } |
---|
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 | } |
---|