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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
/* less_trivial_synth.c
Disposable Hosted Soft Synth API
Constructed by Chris Cannam, Steve Harris and Sean Bolton
This is an example DSSI synth plugin written by Steve Harris.
This example file is in the public domain.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <stdio.h>
#include "dssi.h"
#include "ladspa.h"
#include "saw.h"
#define LTS_OUTPUT 0
#define LTS_FREQ 1
#define LTS_ATTACK 2
#define LTS_DECAY 3
#define LTS_SUSTAIN 4
#define LTS_RELEASE 5
#define LTS_TIMBRE 6
#define LTS_COUNT 7 /* must be 1 + highest value above */
#define POLYPHONY 74
#define MIDI_NOTES 128
#define STEP_SIZE 16
#define GLOBAL_GAIN 0.25f
#define TABLE_MODULUS 1024
#define TABLE_SIZE (TABLE_MODULUS + 1)
#define TABLE_MASK (TABLE_MODULUS - 1)
#define FP_IN(x) (x.part.in & TABLE_MASK)
#define FP_FR(x) ((float)x.part.fr * 0.0000152587890625f)
#define FP_OMEGA(w) ((double)TABLE_MODULUS * 65536.0 * (w));
#define LERP(f,a,b) ((a) + (f) * ((b) - (a)))
long int lrintf (float x);
static LADSPA_Descriptor *ltsLDescriptor = NULL;
static DSSI_Descriptor *ltsDDescriptor = NULL;
float *table[2];
typedef enum {
inactive = 0,
attack,
decay,
sustain,
release
} state_t;
typedef union {
uint32_t all;
struct {
#ifdef WORDS_BIGENDIAN
uint16_t in;
uint16_t fr;
#else
uint16_t fr;
uint16_t in;
#endif
} part;
} fixp;
typedef struct {
state_t state;
int note;
float amp;
float env;
float env_d;
fixp phase;
int counter;
int next_event;
} voice_data;
typedef struct {
LADSPA_Data tune;
LADSPA_Data attack;
LADSPA_Data decay;
LADSPA_Data sustain;
LADSPA_Data release;
LADSPA_Data timbre;
LADSPA_Data pitch;
} synth_vals;
typedef struct {
LADSPA_Data *output;
LADSPA_Data *tune;
LADSPA_Data *attack;
LADSPA_Data *decay;
LADSPA_Data *sustain;
LADSPA_Data *release;
LADSPA_Data *timbre;
LADSPA_Data pitch;
voice_data data[POLYPHONY];
int note2voice[MIDI_NOTES];
fixp omega[MIDI_NOTES];
float fs;
} LTS;
static void runLTS(LADSPA_Handle instance, unsigned long sample_count,
snd_seq_event_t * events, unsigned long EventCount);
static void run_voice(LTS *p, synth_vals *vals, voice_data *d,
LADSPA_Data *out, unsigned int count);
int pick_voice(const voice_data *data);
const LADSPA_Descriptor *ladspa_descriptor(unsigned long index)
{
switch (index) {
case 0:
return ltsLDescriptor;
default:
return NULL;
}
}
const DSSI_Descriptor *dssi_descriptor(unsigned long index)
{
switch (index) {
case 0:
return ltsDDescriptor;
default:
return NULL;
}
}
static void cleanupLTS(LADSPA_Handle instance)
{
free(instance);
}
static void connectPortLTS(LADSPA_Handle instance, unsigned long port,
LADSPA_Data * data)
{
LTS *plugin;
plugin = (LTS *) instance;
switch (port) {
case LTS_OUTPUT:
plugin->output = data;
break;
case LTS_FREQ:
plugin->tune = data;
break;
case LTS_ATTACK:
plugin->attack = data;
break;
case LTS_DECAY:
plugin->decay = data;
break;
case LTS_SUSTAIN:
plugin->sustain = data;
break;
case LTS_RELEASE:
plugin->release = data;
break;
case LTS_TIMBRE:
plugin->timbre = data;
break;
}
}
static LADSPA_Handle instantiateLTS(const LADSPA_Descriptor * descriptor,
unsigned long s_rate)
{
unsigned int i;
LTS *plugin_data = (LTS *) malloc(sizeof(LTS));
plugin_data->fs = s_rate;
for (i=0; i<MIDI_NOTES; i++) {
plugin_data->omega[i].all =
FP_OMEGA(pow(2.0, (i-69.0) / 12.0) / (double)s_rate);
}
return (LADSPA_Handle) plugin_data;
}
static void activateLTS(LADSPA_Handle instance)
{
LTS *plugin_data = (LTS *) instance;
unsigned int i;
for (i=0; i<POLYPHONY; i++) {
plugin_data->data[i].state = inactive;
}
for (i=0; i<MIDI_NOTES; i++) {
plugin_data->note2voice[i] = 0;
}
plugin_data->pitch = 1.0f;
}
static void runLTSWrapper(LADSPA_Handle instance,
unsigned long sample_count)
{
runLTS(instance, sample_count, NULL, 0);
}
static void runLTS(LADSPA_Handle instance, unsigned long sample_count,
snd_seq_event_t *events, unsigned long event_count)
{
LTS *plugin_data = (LTS *) instance;
LADSPA_Data *const output = plugin_data->output;
synth_vals vals;
voice_data *data = plugin_data->data;
unsigned long i;
unsigned long pos;
unsigned long count;
unsigned long event_pos;
unsigned long voice;
vals.tune = *(plugin_data->tune);
vals.attack = *(plugin_data->attack) * plugin_data->fs;
vals.decay = *(plugin_data->decay) * plugin_data->fs;
vals.sustain = *(plugin_data->sustain) * 0.01f;
vals.release = *(plugin_data->release) * plugin_data->fs;
vals.pitch = plugin_data->pitch;
for (pos = 0, event_pos = 0; pos < sample_count; pos += STEP_SIZE) {
vals.timbre = LERP(0.99f, vals.timbre, *(plugin_data->timbre));
while (event_pos < event_count
&& pos >= events[event_pos].time.tick) {
if (events[event_pos].type == SND_SEQ_EVENT_NOTEON) {
snd_seq_ev_note_t n = events[event_pos].data.note;
if (n.velocity > 0) {
const int voice = pick_voice(data);
plugin_data->note2voice[n.note] = voice;
data[voice].note = n.note;
data[voice].amp = sqrtf(n.velocity * .0078740157f) *
GLOBAL_GAIN;
data[voice].state = attack;
data[voice].env = 0.0;
data[voice].env_d = 1.0f / vals.attack;
data[voice].phase.all = 0;
data[voice].counter = 0;
data[voice].next_event = vals.attack;
} else {
const int voice = plugin_data->note2voice[n.note];
data[voice].state = release;
data[voice].env_d = -vals.sustain / vals.release;
data[voice].counter = 0;
data[voice].next_event = vals.release;
}
} else if (events[event_pos].type == SND_SEQ_EVENT_NOTEOFF) {
snd_seq_ev_note_t n = events[event_pos].data.note;
const int voice = plugin_data->note2voice[n.note];
if (data[voice].state != inactive) {
data[voice].state = release;
data[voice].env_d = -data[voice].env / vals.release;
data[voice].counter = 0;
data[voice].next_event = vals.release;
}
} else if (events[event_pos].type == SND_SEQ_EVENT_PITCHBEND) {
vals.pitch =
powf(2.0f, (float)(events[event_pos].data.control.value)
* 0.0001220703125f * 0.166666666f);
plugin_data->pitch = vals.pitch;
}
event_pos++;
}
count = (sample_count - pos) > STEP_SIZE ? STEP_SIZE :
sample_count - pos;
for (i=0; i<count; i++) {
output[pos + i] = 0.0f;
}
for (voice = 0; voice < POLYPHONY; voice++) {
if (data[voice].state != inactive) {
run_voice(plugin_data, &vals, &data[voice], output + pos,
count);
}
}
}
}
static void run_voice(LTS *p, synth_vals *vals, voice_data *d, LADSPA_Data *out, unsigned int count)
{
unsigned int i;
for (i=0; i<count; i++) {
d->phase.all += lrintf((float)p->omega[d->note].all * vals->tune *
vals->pitch);
d->env += d->env_d;
out[i] += LERP(vals->timbre,
LERP(FP_FR(d->phase), table[0][FP_IN(d->phase)],
table[0][FP_IN(d->phase) + 1]),
LERP(FP_FR(d->phase), table[1][FP_IN(d->phase)],
table[1][FP_IN(d->phase) + 1])) *
d->amp * d->env;
}
d->counter += count;
if (d->counter >= d->next_event) {
switch (d->state) {
case inactive:
break;
case attack:
d->state = decay;
d->env_d = (vals->sustain - 1.0f) / vals->decay;
d->counter = 0;
d->next_event = vals->decay;
break;
case decay:
d->state = sustain;
d->env_d = 0.0f;
d->counter = 0;
d->next_event = INT_MAX;
break;
case sustain:
d->counter = 0;
break;
case release:
d->state = inactive;
break;
default:
d->state = inactive;
break;
}
}
}
int getControllerLTS(LADSPA_Handle instance, unsigned long port)
{
switch (port) {
case LTS_ATTACK:
return DSSI_CC(0x49);
case LTS_DECAY:
return DSSI_CC(0x4b);
case LTS_SUSTAIN:
return DSSI_CC(0x4f);
case LTS_RELEASE:
return DSSI_CC(0x48);
case LTS_TIMBRE:
return DSSI_CC(0x01);
}
return DSSI_NONE;
}
/* find the voice that is least relevant (low note priority)*/
int pick_voice(const voice_data *data)
{
unsigned int i;
int highest_note = 0;
int highest_note_voice = 0;
/* Look for an inactive voice */
for (i=0; i<POLYPHONY; i++) {
if (data[i].state == inactive) {
return i;
}
}
/* otherwise find for the highest note and replace that */
for (i=0; i<POLYPHONY; i++) {
if (data[i].note > highest_note) {
highest_note = data[i].note;
highest_note_voice = i;
}
}
return highest_note_voice;
}
void _init()
{
unsigned int i;
char **port_names;
float *sin_table;
LADSPA_PortDescriptor *port_descriptors;
LADSPA_PortRangeHint *port_range_hints;
sin_table = malloc(sizeof(float) * TABLE_SIZE);
for (i=0; i<TABLE_SIZE; i++) {
sin_table[i] = sin(2.0 * M_PI * (double)i / (double)TABLE_MODULUS);
}
table[0] = sin_table;
table[1] = saw_table;
ltsLDescriptor =
(LADSPA_Descriptor *) malloc(sizeof(LADSPA_Descriptor));
if (ltsLDescriptor) {
ltsLDescriptor->UniqueID = 24;
ltsLDescriptor->Label = "LTS";
ltsLDescriptor->Properties = 0;
ltsLDescriptor->Name = "Less Trivial synth";
ltsLDescriptor->Maker = "Steve Harris <steve@plugin.org.uk>";
ltsLDescriptor->Copyright = "Public Domain";
ltsLDescriptor->PortCount = LTS_COUNT;
port_descriptors = (LADSPA_PortDescriptor *)
calloc(ltsLDescriptor->PortCount, sizeof
(LADSPA_PortDescriptor));
ltsLDescriptor->PortDescriptors =
(const LADSPA_PortDescriptor *) port_descriptors;
port_range_hints = (LADSPA_PortRangeHint *)
calloc(ltsLDescriptor->PortCount, sizeof
(LADSPA_PortRangeHint));
ltsLDescriptor->PortRangeHints =
(const LADSPA_PortRangeHint *) port_range_hints;
port_names = (char **) calloc(ltsLDescriptor->PortCount, sizeof(char *));
ltsLDescriptor->PortNames = (const char **) port_names;
/* Parameters for output */
port_descriptors[LTS_OUTPUT] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
port_names[LTS_OUTPUT] = "Output";
port_range_hints[LTS_OUTPUT].HintDescriptor = 0;
/* Parameters for tune */
port_descriptors[LTS_FREQ] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
port_names[LTS_FREQ] = "A tuning (Hz)";
port_range_hints[LTS_FREQ].HintDescriptor = LADSPA_HINT_DEFAULT_440 |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
port_range_hints[LTS_FREQ].LowerBound = 410;
port_range_hints[LTS_FREQ].UpperBound = 460;
/* Parameters for attack */
port_descriptors[LTS_ATTACK] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
port_names[LTS_ATTACK] = "Attack time (s)";
port_range_hints[LTS_ATTACK].HintDescriptor =
LADSPA_HINT_DEFAULT_LOW |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
port_range_hints[LTS_ATTACK].LowerBound = 0.01f;
port_range_hints[LTS_ATTACK].UpperBound = 1.0f;
/* Parameters for decay */
port_descriptors[LTS_DECAY] = port_descriptors[LTS_ATTACK];
port_names[LTS_DECAY] = "Decay time (s)";
port_range_hints[LTS_DECAY].HintDescriptor =
port_range_hints[LTS_ATTACK].HintDescriptor;
port_range_hints[LTS_DECAY].LowerBound =
port_range_hints[LTS_ATTACK].LowerBound;
port_range_hints[LTS_DECAY].UpperBound =
port_range_hints[LTS_ATTACK].UpperBound;
/* Parameters for sustain */
port_descriptors[LTS_SUSTAIN] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
port_names[LTS_SUSTAIN] = "Sustain level (%)";
port_range_hints[LTS_SUSTAIN].HintDescriptor =
LADSPA_HINT_DEFAULT_HIGH |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
port_range_hints[LTS_SUSTAIN].LowerBound = 0.0f;
port_range_hints[LTS_SUSTAIN].UpperBound = 100.0f;
/* Parameters for release */
port_descriptors[LTS_RELEASE] = port_descriptors[LTS_ATTACK];
port_names[LTS_RELEASE] = "Release time (s)";
port_range_hints[LTS_RELEASE].HintDescriptor =
LADSPA_HINT_DEFAULT_MIDDLE | LADSPA_HINT_LOGARITHMIC |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
port_range_hints[LTS_RELEASE].LowerBound =
port_range_hints[LTS_ATTACK].LowerBound;
port_range_hints[LTS_RELEASE].UpperBound =
port_range_hints[LTS_ATTACK].UpperBound * 4.0f;
/* Parameters for timbre */
port_descriptors[LTS_TIMBRE] = port_descriptors[LTS_ATTACK];
port_names[LTS_TIMBRE] = "Timbre";
port_range_hints[LTS_TIMBRE].HintDescriptor =
LADSPA_HINT_DEFAULT_MIDDLE |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
port_range_hints[LTS_TIMBRE].LowerBound = 0.0f;
port_range_hints[LTS_TIMBRE].UpperBound = 1.0f;
ltsLDescriptor->activate = activateLTS;
ltsLDescriptor->cleanup = cleanupLTS;
ltsLDescriptor->connect_port = connectPortLTS;
ltsLDescriptor->deactivate = NULL;
ltsLDescriptor->instantiate = instantiateLTS;
ltsLDescriptor->run = runLTSWrapper;
ltsLDescriptor->run_adding = NULL;
ltsLDescriptor->set_run_adding_gain = NULL;
}
ltsDDescriptor = (DSSI_Descriptor *) malloc(sizeof(DSSI_Descriptor));
if (ltsDDescriptor) {
ltsDDescriptor->DSSI_API_Version = 1;
ltsDDescriptor->LADSPA_Plugin = ltsLDescriptor;
ltsDDescriptor->configure = NULL;
ltsDDescriptor->get_program = NULL;
ltsDDescriptor->get_midi_controller_for_port = getControllerLTS;
ltsDDescriptor->select_program = NULL;
ltsDDescriptor->run_synth = runLTS;
ltsDDescriptor->run_synth_adding = NULL;
ltsDDescriptor->run_multiple_synths = NULL;
ltsDDescriptor->run_multiple_synths_adding = NULL;
}
}
void _fini()
{
if (ltsLDescriptor) {
free((LADSPA_PortDescriptor *) ltsLDescriptor->PortDescriptors);
free((char **) ltsLDescriptor->PortNames);
free((LADSPA_PortRangeHint *) ltsLDescriptor->PortRangeHints);
free(ltsLDescriptor);
}
if (ltsDDescriptor) {
free(ltsDDescriptor);
}
}
|