summaryrefslogtreecommitdiff
path: root/src/utils/parameter.c
blob: f8a8b4507f39f7ec64ffd98847aaaae79c24b083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>

  This file is part of aubio.

  aubio is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  aubio is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with aubio.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "config.h"
#include "aubio_priv.h"
#include "parameter.h"

#define AUBIO_PARAM_MAX_STEPS 2000
#define AUBIO_PARAM_MIN_STEPS 1

struct _aubio_parameter_t {
  smpl_t current_value;
  smpl_t target_value;
  smpl_t increment;

  smpl_t max_value;
  smpl_t min_value;

  uint_t steps;
};

aubio_parameter_t * new_aubio_parameter (smpl_t min_value, smpl_t max_value, uint_t steps )
{
  aubio_parameter_t * param = AUBIO_NEW(aubio_parameter_t);

  param->min_value = min_value;
  param->max_value = max_value;
  param->steps = steps;

  param->current_value = param->min_value;
  param->target_value = param->current_value;
  param->increment = 0.;

  return param;
}

uint_t aubio_parameter_set_target_value ( aubio_parameter_t * param, smpl_t value )
{
  uint_t err = AUBIO_OK;
  if (value < param->min_value ) {
    param->target_value = param->min_value;
    err = AUBIO_FAIL;
  } else if ( value > param->max_value ) {
    param->target_value = param->max_value;
    err = AUBIO_FAIL;
  } else {
    param->target_value = value;
  }
  param->increment = ( param->target_value - param->current_value ) / param->steps;
  return err;
}

uint_t aubio_parameter_set_current_value ( aubio_parameter_t * param, smpl_t value )
{
  uint_t err = AUBIO_OK;
  if (value < param->min_value ) {
    param->current_value = param->min_value;
    err = AUBIO_FAIL;
  } else if ( value > param->max_value ) {
    param->current_value = param->max_value;
    err = AUBIO_FAIL;
  } else {
    param->current_value = value;
  }
  param->target_value = param->current_value;
  param->increment = 0;
  return err;
}

smpl_t aubio_parameter_get_current_value ( const aubio_parameter_t * s )
{
  return s->current_value;
}

smpl_t aubio_parameter_get_next_value ( aubio_parameter_t * s )
{
  if ( ABS(s->current_value - s->target_value) > ABS(s->increment) ) {
    s->current_value += s->increment;
  } else {
    s->current_value = s->target_value;
  }
  return s->current_value;
}

uint_t aubio_parameter_set_steps ( aubio_parameter_t * param, uint_t steps )
{
  if (steps < AUBIO_PARAM_MIN_STEPS || steps > AUBIO_PARAM_MAX_STEPS) {
    return AUBIO_FAIL;
  }
  param->steps = steps;
  param->increment = ( param->target_value - param->current_value ) / param->steps;
  return AUBIO_OK;
}

uint_t aubio_parameter_get_steps ( const aubio_parameter_t * param )
{
  return param->steps;
}

uint_t aubio_parameter_set_min_value ( aubio_parameter_t * param, smpl_t min_value )
{
  param->min_value = min_value;
  return AUBIO_OK;
}

smpl_t aubio_parameter_get_min_value ( const aubio_parameter_t * param )
{
  return param->min_value;
}

uint_t aubio_parameter_set_max_value ( aubio_parameter_t * param, smpl_t max_value )
{
  param->max_value = max_value;
  return AUBIO_OK;
}

smpl_t aubio_parameter_get_max_value ( const aubio_parameter_t * param )
{
  return param->max_value;
}

void del_aubio_parameter ( aubio_parameter_t * param )
{
  AUBIO_FREE(param);
}