File: path_oscillo.c

package info (click to toggle)
lebiniou 3.67.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 28,670; makefile: 1,276; sh: 602; awk: 432; xml: 261; javascript: 23
file content (278 lines) | stat: -rw-r--r-- 7,602 bytes parent folder | download | duplicates (3)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 *  Copyright 1994-2022 Olivier Girondel
 *  Copyright 2019-2022 Laurent Marsac
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can rediswindowing_factoribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is diswindowing_factoributed 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 lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Plot selected path, oscillo version
 */

#include "context.h"
#include "paths.h"
#include "parameters.h"
#include "path.h"
#include "oscillo.h"
#include "pthread_utils.h"


uint32_t options = BO_GFX | BO_SFX | BO_SCHEMES;
uint32_t version = 0;
enum LayerMode mode = LM_OVERLAY;

char dname[] = "Path oscillo";
char desc[]  = "Path oscillo";

static Porteuse_t *P = NULL;

static int    plot_length  = 50; /* length of path to plot on each run */
static double volume_scale = 1;
static int    oscillo_length_factor = 1; /* used to define oscillo sampling */
static double windowing_factor = 0.2; /* Tukey window constant, between 0 and 1 */
static pthread_mutex_t mutex;


void
init_path(uint16_t id)
{
  if (!xpthread_mutex_lock(&mutex)) {
    xfree(path);

    path_length = paths->paths[id]->size;
    path        = xcalloc(path_length, sizeof(Path_point_t));

    Path_scale_and_center(path, paths->paths[id]->data, path_length, scale);
    xpthread_mutex_unlock(&mutex);
  }
}


json_t *
get_parameters(const uint8_t fetch_all)
{
  json_t *params = get_parameters_path();

  plugin_parameters_add_double(params, BPP_VOLUME_SCALE, volume_scale, 0.1, 10, 0.1, "Volume scale");
  plugin_parameters_add_int(params, BPP_LENGTH, plot_length, 1, 1000, 1, "Length");
  plugin_parameters_add_int(params, BPP_OSCILLO_LENGTH_FACTOR, oscillo_length_factor, 1, 10, 1, "Oscilloscope length factor"); // to check, compare to length
  plugin_parameters_add_double(params, BPP_WINDOWING_FACTOR, windowing_factor, 0, 1, 0.01, "Windowing factor");

  return params;
}


void
set_parameters(const Context_t *ctx, const json_t *in_parameters)
{
  uint8_t reinit_path = 0;

  reinit_path |= set_parameters_path(ctx, in_parameters);
  plugin_parameter_parse_double_range(in_parameters, BPP_VOLUME_SCALE, &volume_scale);
  plugin_parameter_parse_int_range(in_parameters, BPP_LENGTH, &plot_length);
  plugin_parameter_parse_int_range(in_parameters, BPP_OSCILLO_LENGTH_FACTOR, &oscillo_length_factor);
  plugin_parameter_parse_double_range(in_parameters, BPP_WINDOWING_FACTOR, &windowing_factor);

  if (reinit_path) {
    init_path(path_id);
  }
}


json_t *
parameters(const Context_t *ctx, const json_t *in_parameters, const uint8_t fetch_all)
{
  if (NULL != in_parameters) {
    set_parameters(ctx, in_parameters);
  }

  return get_parameters(fetch_all);
}


uint8_t
create(Context_t *ctx)
{
  if (NULL == paths) {
    return 0;
  } else {
    xpthread_mutex_init(&mutex, NULL);
    init_path(path_id);

    return 1;
  }
}


void
destroy(Context_t *ctx)
{
  Porteuse_delete(P);
  xfree(path);
  xpthread_mutex_destroy(&mutex);
}


void
init_oscillo(Context_t *ctx, uint16_t length)
{
  Point2d_t last;

  /* reinit path if selection changed */
  if (path_idx == 0) {
    if (path_id_changed) {
      init_path(path_id);
      path_id_changed = 0;
    }

    last.x = path[path_length - 1].x;
    last.y = path[path_length - 1].y;
  } else {
    /* used to connect to to previous run */
    last.x = path[path_idx - 1].x;
    last.y = path[path_idx - 1].y;
  }

  /* reset plot_length if too big (usefull for osd) */
  plot_length = MIN((uint32_t)plot_length, path_length);

  /* if end of path is crossed durring this round, reduce length
     so that the for loop ends exactly at path_length-1 */
  length = MIN((unsigned int)plot_length, path_length - path_idx);

  /* ensure oscillo_length_factor * length <= ctx->input->size/2 */
  if (length > 0) {
    oscillo_length_factor = MAX(MIN((uint32_t)oscillo_length_factor, ctx->input->size/2 / length), 1);
  }

  if (!xpthread_mutex_lock(&mutex)) {
    Porteuse_delete(P);
    P = Porteuse_new(length * oscillo_length_factor, A_MONO);

    /* oscillo */
    Transform_t t;
    memset(&t, 0, sizeof(t));

    t.v_j_factor = HMAXY * volume_scale;

    /* we want to divide "ctx->input->size" input in "length * oscillo_length_factor" overlapping windows */
    /* estimation of window overlap and size for color computation */
    uint32_t wo = ctx->input->size >> 1; /* overlap */
    uint32_t ws = floor((double)(ctx->input->size - wo) / (double)(length * oscillo_length_factor)) + wo;

    uint16_t r           = floor((double)P->size * windowing_factor);
    uint16_t factor_orig = t.v_j_factor;

    Point2d_t next;

    if (path[path_idx].connect == 0) {
      last.x = path[path_idx].x;
      last.y = path[path_idx].y;
    }
    P->origin = last;

    /* for each point to plot in "length" */
    for (uint16_t l = 0; l < length; l++, path_idx++) {
      uint16_t next_path_idx = (path_idx + 1) % path_length;
      next.x = path[next_path_idx].x;
      next.y = path[next_path_idx].y;

      float dist_coef = 1 / (float)oscillo_length_factor;

      Point2d_t diff_to_next = p2d_sub(&next, &last);
      last = next;

      for (uint16_t l2 = 0; l2 < (unsigned int)oscillo_length_factor; l2++) {
        uint16_t i = l * oscillo_length_factor + l2;

        P->connect[i] = path[path_idx].connect;

        /* compute vector used for current alpha and next origin */
        t.v_i = diff_to_next;
        if (path[next_path_idx].connect == 0) {
          if (l2 != (unsigned int)oscillo_length_factor - 1) {
            t.v_i.x = 0;
            t.v_i.y = 0;
          }
        } else {
          t.v_i.x *= dist_coef;
          t.v_i.y *= dist_coef;
        }

        /* Fix usefull when i == 0 and last and next are identical */
        if (fabs(t.v_i.x) < 1e-6 && fabs(t.v_i.y) < 1e-6) {
          t.v_i.x += 0.01;
        }

        /* tukey win */
        double tc;
        if (i < r / 2) {
          tc = cos(2 * M_PI * (i - r / 2) / r) / 2.0 + 0.5;
        } else if (i > P->size - r / 2) {
          tc = cos(2 * M_PI * (i - 1.0 + r / 2) / r) / 2.0 + 0.5;
        } else {
          tc = 1.0;
        }

        t.v_j_factor = floor((double)factor_orig * tc);

        uint32_t start = i * (ws - wo);
        uint32_t end = (i == length * oscillo_length_factor - 1) ? ctx->input->size : start + ws;
        end = MIN(start + ws, ctx->input->size);

        double win_avg = compute_avg_abs(ctx->input->data[A_MONO], start, end);

        P->color[i] = MIN(1.0, color_scale * win_avg) * PIXEL_MAXVAL;
        P->trans[i] = t;
      }
    }
    xpthread_mutex_unlock(&mutex);
  }

  if (path_idx == path_length) {
    path_idx = 0;
  }

  Porteuse_init_alpha(P);
}

void
run(Context_t *ctx)
{
  Buffer8_t *dst = passive_buffer(ctx);

  Buffer8_clear(dst);
  init_oscillo(ctx, plot_length);
  Porteuse_draw(P, ctx, 2);
}


void
on_switch_on(Context_t *ctx)
{
  // generic path parameters
  path_id = Shuffler_get(paths->shuffler);
  path_idx    = 0;
  color_scale = 1;
  scale = 1;
  // path_oscillo parameters
  plot_length   = 50;
  volume_scale = 1;
  oscillo_length_factor = 1;
  windowing_factor = 0.2;
  // re-init
  init_path(path_id);
}