File: spectrogram.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 (136 lines) | stat: -rw-r--r-- 3,185 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
/*
 *  Copyright 1994-2022 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou 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 lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "context.h"
#include "parameters.h"
#include "pthread_utils.h"


uint32_t version = 0;
uint32_t options = BO_SFX | BO_NORANDOM | BO_SCHEMES;
enum LayerMode mode = LM_OVERLAY;
char desc[] = "Voiceprint effect";
char dname[] = "Spectrogram";


static Buffer8_t *my_scr = NULL;
static uint16_t *v_start = NULL, *v_end = NULL;

static double volume_scale = 1;


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

  plugin_parameters_add_double(params, BPP_VOLUME_SCALE, volume_scale, 0.1, 10, 0.1, "Volume scale");

  return params;
}


void
set_parameters(const Context_t *ctx, const json_t *in_parameters)
{
  plugin_parameter_parse_double_range(in_parameters, BPP_VOLUME_SCALE, &volume_scale);
}


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);
}


void
on_switch_on(Context_t *ctx)
{
  /* Initialize parameters */
  volume_scale = 1;
}


static void
init_v(Context_t *ctx)
{
  uint16_t k;
  float da_log;

  da_log = logf(ctx->input->spectrum_size - 1) / logf(10.0);

  for (k = 1; k < ctx->input->spectrum_size; k++) {
    v_start[k] = (uint16_t)(logf((float)k)/logf(10.0) / da_log * MAXY);
    v_end[k]   = (uint16_t)(log1p((float)k)/logf(10.0) / da_log * MAXY); /* log1p(x)=logf(x+1) */
  }
}


void
run(Context_t *ctx)
{
  Buffer8_t *src = my_scr;
  Buffer8_t *dst = passive_buffer(ctx);
  uint16_t k;

  memmove((void *)src->buffer, (const void *)(src->buffer+sizeof(Pixel_t)), BUFFSIZE-1);

  if (!xpthread_mutex_lock(&ctx->input->mutex)) {
    for (k = 1; k < ctx->input->spectrum_size; k++) {
      Pixel_t color1 = (Pixel_t)(255.0 * ctx->input->spectrum_log[A_MONO][k] * volume_scale);
      v_line_nc(src, MAXX, v_start[k], v_end[k] - 1, color1);
    }
    xpthread_mutex_unlock(&ctx->input->mutex);
  }

  v_line_nc(src, MINX, MINY, MAXY, 0);

  Buffer8_copy(src, dst);
  //  Buffer8_add(src, dst, 16);
  //  Buffer8_clear_border(dst);
  //  Buffer8_copy(active_buffer(ctx->biniou8), passive_buffer(ctx->biniou8));
}


int8_t
create(Context_t *ctx)
{
  v_start = xcalloc(ctx->input->spectrum_size, sizeof(uint16_t));
  v_end = xcalloc(ctx->input->spectrum_size, sizeof(uint16_t));

  my_scr = Buffer8_new();

  init_v(ctx);

  return 1;
}


void
destroy(Context_t *ctx)
{
  Buffer8_delete(my_scr);
  xfree(v_start);
  xfree(v_end);
}