Branch
Hash :
adb9e235
Author :
Thomas de Grivel
Date :
2023-01-14T18:26:30
license
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
/* rtbuf
* Copyright 2018-2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <float.h>
#include <stdio.h>
#include <strings.h>
#include "../../librtbuf/rtbuf.h"
#include "../../librtbuf/lib.h"
#include "../signal.h"
#include "../signal_type.h"
#include "../music.h"
#include "../music_type.h"
#include "../synth.h"
#include "../synth_type.h"
s_rtbuf_lib_proc_in rtbuf_synth_synth_in[] =
{{ "envelope", RTBUF_SIGNAL_TYPE, 1.0, 0.0, 1.0, 1.0 },
{ "oscillator", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0, 1.0 },
RTBUF_MUSIC_NOTES_IN("note"),
{ 0, 0, 0.0, 0.0, 0.0, 0.0 }};
s_rtbuf_lib_proc_out rtbuf_synth_synth_out[] =
{{ "signal", RTBUF_SIGNAL_TYPE },
{ "note_n", "unsigned int" },
{ 0, 0 }};
int rtbuf_lib_init (s_rtbuf_lib *lib)
{
(void) lib;
rtbuf_music_init();
return 0;
}
s_rtbuf * rtbuf_in_rtbuf (s_rtbuf *rtb, unsigned int in)
{
int i;
assert(in < rtb->proc->in_n);
if ((i = rtb->in[in].rtb) < 0)
return 0;
return &g_rtbuf[i];
}
s_rtbuf * rtbuf_synth_synth_note_envelope (s_rtbuf *rtb, unsigned int i)
{
unsigned int j;
assert(i < RTBUF_MUSIC_NOTE_MAX);
j = RTBUF_SYNTH_SYNTH_IN_NOTE_ENVELOPE(i);
return rtbuf_in_rtbuf(rtb, j);
}
s_rtbuf * rtbuf_synth_synth_note_oscillator (s_rtbuf *rtb,
unsigned int i)
{
unsigned int j;
assert(i < RTBUF_MUSIC_NOTE_MAX);
j = RTBUF_SYNTH_SYNTH_IN_NOTE_OSCILLATOR(i);
return rtbuf_in_rtbuf(rtb, j);
}
void rtbuf_synth_synth_delete_note (s_rtbuf *rtb,
unsigned int i)
{
s_rtbuf *env = rtbuf_synth_synth_note_envelope(rtb, i);
s_rtbuf *osc = rtbuf_synth_synth_note_oscillator(rtb, i);
s_rtbuf_synth_synth_data *data;
data = (s_rtbuf_synth_synth_data*) rtb->data;
if (env || osc) {
printf("synth_synth_delete_note %u\n", i);
if (env)
rtbuf_delete(env);
if (osc)
rtbuf_delete(osc);
data->note_n--;
}
}
void rtbuf_synth_synth_delete_notes (s_rtbuf *rtb)
{
s_rtbuf_synth_synth_data *data;
unsigned int i = 0;
data = (s_rtbuf_synth_synth_data*) rtb->data;
while (i < RTBUF_MUSIC_NOTE_MAX && data->note_n > 0) {
rtbuf_synth_synth_delete_note(rtb, i);
i++;
}
}
s_rtbuf * rtbuf_synth_synth_new_envelope (s_rtbuf *rtb,
unsigned int i)
{
s_rtbuf *ref = rtbuf_in_rtbuf(rtb, RTBUF_SYNTH_SYNTH_IN_ENVELOPE);
int env_i;
s_rtbuf *env;
unsigned int env_vel;
unsigned int env_freq;
unsigned int env_start;
unsigned int env_stop;
s_rtbuf_binding *v = &rtb->in[RTBUF_SYNTH_SYNTH_IN_NOTES];
unsigned int kbd_i = v->rtb;
unsigned int kbd_notes = v->out;
unsigned int note_vel = RTBUF_MUSIC_NOTE_IN_VELOCITY(kbd_notes, i);
unsigned int note_freq = RTBUF_MUSIC_NOTE_IN_FREQUENCY(kbd_notes, i);
unsigned int note_start = RTBUF_MUSIC_NOTE_IN_START(kbd_notes, i);
unsigned int note_stop = RTBUF_MUSIC_NOTE_IN_STOP(kbd_notes, i);
assert(rtb > g_rtbuf);
if (!ref)
return 0;
if((env_i = rtbuf_clone(ref)) < 0)
return 0;
env = &g_rtbuf[env_i];
env_vel = rtbuf_in_find(env, "velocity");
env_freq = rtbuf_in_find(env, "frequency");
env_start = rtbuf_in_find(env, "start");
env_stop = rtbuf_in_find(env, "stop");
rtbuf_bind(kbd_i, note_vel , env, env_vel);
rtbuf_bind(kbd_i, note_freq , env, env_freq);
rtbuf_bind(kbd_i, note_start, env, env_start);
rtbuf_bind(kbd_i, note_stop , env, env_stop);
rtbuf_bind(env_i, 0, rtb, RTBUF_SYNTH_SYNTH_IN_NOTE_ENVELOPE(i));
if (env->proc->start)
env->proc->start(env);
if (env->proc->f)
env->proc->f(env);
/* rtbuf_print_long(env_i); */
printf("synth_synth_new_envelope %u\n", i);
return env;
}
s_rtbuf * rtbuf_synth_synth_new_oscillator (s_rtbuf *rtb,
unsigned int i)
{
s_rtbuf *ref =
rtbuf_in_rtbuf(rtb, RTBUF_SYNTH_SYNTH_IN_OSCILLATOR);
int osc_i;
s_rtbuf *osc;
unsigned int osc_freq;
s_rtbuf_binding *v = &rtb->in[RTBUF_SYNTH_SYNTH_IN_NOTES];
unsigned int kbd_i = v->rtb;
unsigned int kbd_notes = v->out;
unsigned int note_freq = RTBUF_MUSIC_NOTE_IN_FREQUENCY(kbd_notes, i);
assert(rtb > g_rtbuf);
if (!ref)
return 0;
if ((osc_i = rtbuf_clone(ref)) < 0)
return 0;
osc = &g_rtbuf[osc_i];
osc_freq = rtbuf_in_find(osc, "frequency");
rtbuf_bind(kbd_i, note_freq, osc, osc_freq);
rtbuf_bind(osc_i, 0, rtb, RTBUF_SYNTH_SYNTH_IN_NOTE_OSCILLATOR(i));
if (osc->proc->start)
osc->proc->start(osc);
if (osc->proc->f)
osc->proc->f(osc);
/* rtbuf_print_long(osc_i); */
return osc;
}
void rtbuf_synth_synth_update_note_signal (s_rtbuf *rtb,
unsigned int i)
{
s_rtbuf_synth_synth_data *data;
s_rtbuf_signal_fun env;
s_rtbuf_signal_fun osc;
unsigned int j = 0;
double *signal;
assert(rtb);
assert(rtb->data);
assert(i < RTBUF_MUSIC_NOTE_MAX);
rtbuf_signal_fun(rtb, RTBUF_SYNTH_SYNTH_IN_NOTE_ENVELOPE(i), &env);
rtbuf_signal_fun(rtb, RTBUF_SYNTH_SYNTH_IN_NOTE_OSCILLATOR(i), &osc);
data = (s_rtbuf_synth_synth_data *) rtb->data;
signal = data->signal;
while (j < RTBUF_SIGNAL_SAMPLES) {
double e = env.sample_fun(env.signal, j);
double o = osc.sample_fun(osc.signal, j);
e = max(0.0, e);
*signal += e * o;
/* printf(" e=%f o=%f s=%f", e, o, *signal); */
signal++;
j++;
}
}
void rtbuf_synth_synth_update_note (s_rtbuf *rtb,
unsigned int i)
{
s_rtbuf_synth_synth_data *data;
s_rtbuf_music_notes *notes =
rtbuf_music_notes(rtb, RTBUF_SYNTH_SYNTH_IN_NOTES);
s_rtbuf_music_note *note;
assert(notes);
assert(i < RTBUF_MUSIC_NOTE_MAX);
assert(rtb->data);
data = (s_rtbuf_synth_synth_data*) rtb->data;
note = ¬es->note[i];
if (note->velocity > 0.0 && note->start >= 0.0) {
s_rtbuf *env = rtbuf_synth_synth_note_envelope(rtb, i);
s_rtbuf *osc = rtbuf_synth_synth_note_oscillator(rtb, i);
if (!env || !osc) {
if (!env)
env = rtbuf_synth_synth_new_envelope(rtb, i);
if (!osc)
osc = rtbuf_synth_synth_new_oscillator(rtb, i);
data->note_n++;
}
if (env && osc) {
unsigned int env_state_out = rtbuf_proc_out_find(env->proc,
"state");
int env_state = rtbuf_out_int(env, env_state_out,
RTBUF_SYNTH_ENVELOPE_STATE_STARTED);
if (env_state == RTBUF_SYNTH_ENVELOPE_STATE_STARTED)
rtbuf_synth_synth_update_note_signal(rtb, i);
}
}
else
rtbuf_synth_synth_delete_note(rtb, i);
}
int rtbuf_synth_synth (s_rtbuf *rtb)
{
s_rtbuf_music_notes *notes =
rtbuf_music_notes(rtb, RTBUF_SYNTH_SYNTH_IN_NOTES);
s_rtbuf_synth_synth_data *data;
unsigned int i = 0;
unsigned int notes_n = notes ? notes->note_n : 0;
unsigned int n;
data = (s_rtbuf_synth_synth_data*) rtb->data;
n = data->note_n;
rtbuf_signal_zero(data->signal);
while (i < RTBUF_MUSIC_NOTE_MAX && (n > 0 || notes_n > 0)) {
s_rtbuf_music_note *note = notes ? ¬es->note[i] : 0;
if (rtbuf_music_note_p(note))
notes_n--;
if (rtbuf_synth_synth_note_envelope(rtb, i))
n--;
rtbuf_synth_synth_update_note(rtb, i);
i++;
}
return 0;
}
int rtbuf_synth_synth_start (s_rtbuf *rtb)
{
s_rtbuf_synth_synth_data *data;
if (rtb->proc->out_bytes < sizeof(*data))
return 1;
data = (s_rtbuf_synth_synth_data*) rtb->data;
(void) data;
return 0;
}
int rtbuf_synth_synth_stop (s_rtbuf *rtb)
{
rtbuf_synth_synth_delete_notes(rtb);
return 0;
}
s_rtbuf_lib_proc rtbuf_lib_proc =
{ "synth",
rtbuf_synth_synth,
rtbuf_synth_synth_start,
0,
rtbuf_synth_synth_in,
rtbuf_synth_synth_out };
unsigned long rtbuf_lib_ver = RTBUF_LIB_VER;