[650cae3] | 1 | /* |
---|
| 2 | Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org> |
---|
| 3 | |
---|
| 4 | This file is part of aubio. |
---|
| 5 | |
---|
| 6 | aubio is free software: you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | (at your option) any later version. |
---|
| 10 | |
---|
| 11 | aubio is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
| 22 | #include "parameter.h" |
---|
| 23 | |
---|
| 24 | #define AUBIO_PARAM_MAX_STEPS 2000 |
---|
| 25 | #define AUBIO_PARAM_MIN_STEPS 1 |
---|
| 26 | |
---|
| 27 | struct _aubio_parameter_t { |
---|
| 28 | smpl_t current_value; |
---|
| 29 | smpl_t target_value; |
---|
| 30 | smpl_t increment; |
---|
| 31 | |
---|
| 32 | smpl_t max_value; |
---|
| 33 | smpl_t min_value; |
---|
| 34 | |
---|
| 35 | uint_t steps; |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | aubio_parameter_t * new_aubio_parameter (smpl_t min_value, smpl_t max_value, uint_t steps ) |
---|
| 39 | { |
---|
| 40 | aubio_parameter_t * param = AUBIO_NEW(aubio_parameter_t); |
---|
| 41 | |
---|
| 42 | param->min_value = min_value; |
---|
| 43 | param->max_value = max_value; |
---|
| 44 | param->steps = steps; |
---|
| 45 | |
---|
| 46 | param->current_value = param->min_value; |
---|
| 47 | param->target_value = param->current_value; |
---|
| 48 | param->increment = 0.; |
---|
| 49 | |
---|
| 50 | return param; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | uint_t aubio_parameter_set_target_value ( aubio_parameter_t * param, smpl_t value ) |
---|
| 54 | { |
---|
| 55 | uint_t err = AUBIO_OK; |
---|
| 56 | if (value < param->min_value ) { |
---|
| 57 | param->target_value = param->min_value; |
---|
| 58 | err = AUBIO_FAIL; |
---|
| 59 | } else if ( value > param->max_value ) { |
---|
| 60 | param->target_value = param->max_value; |
---|
| 61 | err = AUBIO_FAIL; |
---|
| 62 | } else { |
---|
| 63 | param->target_value = value; |
---|
| 64 | } |
---|
| 65 | param->increment = ( param->target_value - param->current_value ) / param->steps; |
---|
| 66 | return err; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | uint_t aubio_parameter_set_current_value ( aubio_parameter_t * param, smpl_t value ) |
---|
| 70 | { |
---|
| 71 | uint_t err = AUBIO_OK; |
---|
| 72 | if (value < param->min_value ) { |
---|
| 73 | param->current_value = param->min_value; |
---|
| 74 | err = AUBIO_FAIL; |
---|
| 75 | } else if ( value > param->max_value ) { |
---|
| 76 | param->current_value = param->max_value; |
---|
| 77 | err = AUBIO_FAIL; |
---|
| 78 | } else { |
---|
| 79 | param->current_value = value; |
---|
| 80 | } |
---|
| 81 | param->target_value = param->current_value; |
---|
| 82 | param->increment = 0; |
---|
| 83 | return err; |
---|
| 84 | } |
---|
| 85 | |
---|
[55d1fa4] | 86 | smpl_t aubio_parameter_get_current_value ( const aubio_parameter_t * s ) |
---|
[650cae3] | 87 | { |
---|
| 88 | return s->current_value; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | smpl_t aubio_parameter_get_next_value ( aubio_parameter_t * s ) |
---|
| 92 | { |
---|
| 93 | if ( ABS(s->current_value - s->target_value) > ABS(s->increment) ) { |
---|
| 94 | s->current_value += s->increment; |
---|
| 95 | } else { |
---|
| 96 | s->current_value = s->target_value; |
---|
| 97 | } |
---|
| 98 | return s->current_value; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | uint_t aubio_parameter_set_steps ( aubio_parameter_t * param, uint_t steps ) |
---|
| 102 | { |
---|
| 103 | if (steps < AUBIO_PARAM_MIN_STEPS || steps > AUBIO_PARAM_MAX_STEPS) { |
---|
| 104 | return AUBIO_FAIL; |
---|
| 105 | } |
---|
| 106 | param->steps = steps; |
---|
| 107 | param->increment = ( param->target_value - param->current_value ) / param->steps; |
---|
| 108 | return AUBIO_OK; |
---|
| 109 | } |
---|
| 110 | |
---|
[55d1fa4] | 111 | uint_t aubio_parameter_get_steps ( const aubio_parameter_t * param ) |
---|
[650cae3] | 112 | { |
---|
| 113 | return param->steps; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | uint_t aubio_parameter_set_min_value ( aubio_parameter_t * param, smpl_t min_value ) |
---|
| 117 | { |
---|
| 118 | param->min_value = min_value; |
---|
| 119 | return AUBIO_OK; |
---|
| 120 | } |
---|
| 121 | |
---|
[55d1fa4] | 122 | smpl_t aubio_parameter_get_min_value ( const aubio_parameter_t * param ) |
---|
[650cae3] | 123 | { |
---|
| 124 | return param->min_value; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | uint_t aubio_parameter_set_max_value ( aubio_parameter_t * param, smpl_t max_value ) |
---|
| 128 | { |
---|
| 129 | param->max_value = max_value; |
---|
| 130 | return AUBIO_OK; |
---|
| 131 | } |
---|
| 132 | |
---|
[55d1fa4] | 133 | smpl_t aubio_parameter_get_max_value ( const aubio_parameter_t * param ) |
---|
[650cae3] | 134 | { |
---|
| 135 | return param->max_value; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void del_aubio_parameter ( aubio_parameter_t * param ) |
---|
| 139 | { |
---|
| 140 | AUBIO_FREE(param); |
---|
| 141 | } |
---|