source: src/utils/parameter.c @ 650cae3

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 650cae3 was 650cae3, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/utils/parameter.{c,h}: added simple parameter with linear interpolation

  • Property mode set to 100644
File size: 3.5 KB
Line 
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 "config.h"
22#include "aubio_priv.h"
23#include "parameter.h"
24
25#define AUBIO_PARAM_MAX_STEPS 2000
26#define AUBIO_PARAM_MIN_STEPS 1
27
28struct _aubio_parameter_t {
29  smpl_t current_value;
30  smpl_t target_value;
31  smpl_t increment;
32
33  smpl_t max_value;
34  smpl_t min_value;
35
36  uint_t steps;
37};
38
39aubio_parameter_t * new_aubio_parameter (smpl_t min_value, smpl_t max_value, uint_t steps )
40{
41  aubio_parameter_t * param = AUBIO_NEW(aubio_parameter_t);
42
43  param->min_value = min_value;
44  param->max_value = max_value;
45  param->steps = steps;
46
47  param->current_value = param->min_value;
48  param->target_value = param->current_value;
49  param->increment = 0.;
50
51  return param;
52}
53
54uint_t aubio_parameter_set_target_value ( aubio_parameter_t * param, smpl_t value )
55{
56  uint_t err = AUBIO_OK;
57  if (value < param->min_value ) {
58    param->target_value = param->min_value;
59    err = AUBIO_FAIL;
60  } else if ( value > param->max_value ) {
61    param->target_value = param->max_value;
62    err = AUBIO_FAIL;
63  } else {
64    param->target_value = value;
65  }
66  param->increment = ( param->target_value - param->current_value ) / param->steps;
67  return err;
68}
69
70uint_t aubio_parameter_set_current_value ( aubio_parameter_t * param, smpl_t value )
71{
72  uint_t err = AUBIO_OK;
73  if (value < param->min_value ) {
74    param->current_value = param->min_value;
75    err = AUBIO_FAIL;
76  } else if ( value > param->max_value ) {
77    param->current_value = param->max_value;
78    err = AUBIO_FAIL;
79  } else {
80    param->current_value = value;
81  }
82  param->target_value = param->current_value;
83  param->increment = 0;
84  return err;
85}
86
87smpl_t aubio_parameter_get_current_value ( aubio_parameter_t * s )
88{
89  return s->current_value;
90}
91
92smpl_t aubio_parameter_get_next_value ( aubio_parameter_t * s )
93{
94  if ( ABS(s->current_value - s->target_value) > ABS(s->increment) ) {
95    s->current_value += s->increment;
96  } else {
97    s->current_value = s->target_value;
98  }
99  return s->current_value;
100}
101
102uint_t aubio_parameter_set_steps ( aubio_parameter_t * param, uint_t steps )
103{
104  if (steps < AUBIO_PARAM_MIN_STEPS || steps > AUBIO_PARAM_MAX_STEPS) {
105    return AUBIO_FAIL;
106  }
107  param->steps = steps;
108  param->increment = ( param->target_value - param->current_value ) / param->steps;
109  return AUBIO_OK;
110}
111
112uint_t aubio_parameter_get_steps ( aubio_parameter_t * param )
113{
114  return param->steps;
115}
116
117uint_t aubio_parameter_set_min_value ( aubio_parameter_t * param, smpl_t min_value )
118{
119  param->min_value = min_value;
120  return AUBIO_OK;
121}
122
123smpl_t aubio_parameter_get_min_value ( aubio_parameter_t * param )
124{
125  return param->min_value;
126}
127
128uint_t aubio_parameter_set_max_value ( aubio_parameter_t * param, smpl_t max_value )
129{
130  param->max_value = max_value;
131  return AUBIO_OK;
132}
133
134smpl_t aubio_parameter_get_max_value ( aubio_parameter_t * param )
135{
136  return param->max_value;
137}
138
139void del_aubio_parameter ( aubio_parameter_t * param )
140{
141  AUBIO_FREE(param);
142}
Note: See TracBrowser for help on using the repository browser.