2019-08-03 08:34:29 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
|
2013-10-19 22:31:07 -04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2019-08-03 08:34:29 -04:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2013-10-19 22:31:07 -04:00
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
2019-08-03 08:34:29 -04:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2013-10-19 22:31:07 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2013-12-13 14:26:30 -05:00
|
|
|
#include <time.h>
|
2013-10-19 22:31:07 -04:00
|
|
|
|
|
|
|
/* LV2 */
|
2023-05-06 10:18:24 -04:00
|
|
|
#ifdef HAVE_LV2_1_18_6
|
|
|
|
#include <lv2/atom/atom.h>
|
2023-05-06 12:28:35 -04:00
|
|
|
#include <lv2/core/lv2.h>
|
2023-05-06 10:18:24 -04:00
|
|
|
#include <lv2/urid/urid.h>
|
|
|
|
#include <lv2/midi/midi.h>
|
|
|
|
#else
|
|
|
|
#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
|
|
|
|
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
|
|
|
|
#include <lv2/lv2plug.in/ns/ext/urid/urid.h>
|
|
|
|
#include <lv2/lv2plug.in/ns/ext/midi/midi.h>
|
|
|
|
#endif
|
2013-10-19 22:31:07 -04:00
|
|
|
|
2013-10-20 11:24:59 -04:00
|
|
|
#define RSY_URI "https://community.ardour.org/node/7596"
|
2013-10-19 22:31:07 -04:00
|
|
|
|
|
|
|
/* the synth interface */
|
|
|
|
static void * synth_alloc (void);
|
|
|
|
static void synth_init (void *, double rate);
|
|
|
|
static void synth_free (void *);
|
2013-10-26 10:42:27 -04:00
|
|
|
static void synth_parse_midi (void *, const uint8_t *data, const size_t size);
|
2013-10-19 22:31:07 -04:00
|
|
|
static uint32_t synth_sound (void *, uint32_t written, uint32_t nframes, float **out);
|
|
|
|
|
|
|
|
#include "rsynth.c"
|
|
|
|
|
|
|
|
typedef enum {
|
2019-02-28 14:56:23 -05:00
|
|
|
RSY_MIDIIN = 0,
|
|
|
|
RSY_OUTL,
|
|
|
|
RSY_OUTR
|
2013-10-19 22:31:07 -04:00
|
|
|
} PortIndex;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-02-28 14:56:23 -05:00
|
|
|
const LV2_Atom_Sequence* midiin;
|
|
|
|
float* outL;
|
|
|
|
float* outR;
|
2013-10-19 22:31:07 -04:00
|
|
|
|
2019-02-28 14:56:23 -05:00
|
|
|
LV2_URID_Map* map;
|
|
|
|
LV2_URID midi_MidiEvent;
|
2013-10-19 22:31:07 -04:00
|
|
|
|
2019-02-28 14:56:23 -05:00
|
|
|
double SampleRateD;
|
|
|
|
void *synth;
|
|
|
|
bool xmas;
|
2013-10-19 22:31:07 -04:00
|
|
|
} RSynth;
|
|
|
|
|
|
|
|
/* main LV2 */
|
|
|
|
|
|
|
|
static LV2_Handle
|
2019-02-28 14:56:23 -05:00
|
|
|
instantiate (const LV2_Descriptor* descriptor,
|
|
|
|
double rate,
|
|
|
|
const char* bundle_path,
|
|
|
|
const LV2_Feature* const* features)
|
2013-10-19 22:31:07 -04:00
|
|
|
{
|
2019-02-28 14:56:23 -05:00
|
|
|
(void) descriptor; /* unused variable */
|
|
|
|
(void) bundle_path; /* unused variable */
|
|
|
|
|
|
|
|
if (rate < 8000) {
|
|
|
|
fprintf(stderr, "RSynth.lv2 error: unsupported sample-rate (must be > 8k)\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RSynth* self = (RSynth*)calloc(1, sizeof(RSynth));
|
|
|
|
if(!self) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->SampleRateD = rate;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i=0; features[i]; ++i) {
|
|
|
|
if (!strcmp(features[i]->URI, LV2_URID__map)) {
|
|
|
|
self->map = (LV2_URID_Map*)features[i]->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self->map) {
|
|
|
|
fprintf(stderr, "RSynth.lv2 error: Host does not support urid:map\n");
|
|
|
|
free(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->midi_MidiEvent = self->map->map(self->map->handle, LV2_MIDI__MidiEvent);
|
|
|
|
|
|
|
|
self->synth = synth_alloc();
|
|
|
|
synth_init(self->synth, rate);
|
2013-10-19 22:31:07 -04:00
|
|
|
|
2020-02-25 22:03:10 -05:00
|
|
|
if (getenv("ITSXMAS")) {
|
|
|
|
printf("reasonable synth.lv2 says: happy holidays!\n");
|
|
|
|
self->xmas = true;
|
2019-02-28 14:56:23 -05:00
|
|
|
}
|
2013-12-13 14:26:30 -05:00
|
|
|
|
2019-02-28 14:56:23 -05:00
|
|
|
return (LV2_Handle)self;
|
2013-10-19 22:31:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 14:56:23 -05:00
|
|
|
connect_port (LV2_Handle handle,
|
|
|
|
uint32_t port,
|
|
|
|
void* data)
|
2013-10-19 22:31:07 -04:00
|
|
|
{
|
|
|
|
RSynth* self = (RSynth*)handle;
|
|
|
|
|
|
|
|
switch ((PortIndex)port) {
|
|
|
|
case RSY_MIDIIN:
|
|
|
|
self->midiin = (const LV2_Atom_Sequence*)data;
|
|
|
|
break;
|
|
|
|
case RSY_OUTL:
|
|
|
|
self->outL = (float*)data;
|
|
|
|
break;
|
|
|
|
case RSY_OUTR:
|
|
|
|
self->outR = (float*)data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 14:56:23 -05:00
|
|
|
run (LV2_Handle handle, uint32_t n_samples)
|
2013-10-19 22:31:07 -04:00
|
|
|
{
|
2019-02-28 14:56:23 -05:00
|
|
|
RSynth* self = (RSynth*)handle;
|
|
|
|
float* audio[2];
|
|
|
|
|
|
|
|
audio[0] = self->outL;
|
|
|
|
audio[1] = self->outR;
|
|
|
|
|
|
|
|
uint32_t written = 0;
|
|
|
|
|
|
|
|
/* Process incoming MIDI events */
|
|
|
|
if (self->midiin) {
|
|
|
|
LV2_Atom_Event const* ev = (LV2_Atom_Event const*)((uintptr_t)((&(self->midiin)->body) + 1)); // lv2_atom_sequence_begin
|
|
|
|
while( // !lv2_atom_sequence_is_end
|
|
|
|
(const uint8_t*)ev < ((const uint8_t*) &(self->midiin)->body + (self->midiin)->atom.size)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (ev->body.type == self->midi_MidiEvent) {
|
2016-06-26 11:15:46 -04:00
|
|
|
#ifdef DEBUG_MIDI_EVENT // debug midi messages in synth -- not rt-safe(!)
|
2019-02-28 14:56:23 -05:00
|
|
|
printf ("%5d (%d):", ev->time.frames, ev->body.size);
|
|
|
|
for (uint8_t i = 0; i < ev->body.size; ++i) {
|
|
|
|
printf (" %02x", ((const uint8_t*)(ev+1))[i]);
|
|
|
|
}
|
|
|
|
printf ("\n");
|
2016-06-26 11:15:46 -04:00
|
|
|
#endif
|
2019-02-28 14:56:23 -05:00
|
|
|
if (written + BUFFER_SIZE_SAMPLES < ev->time.frames
|
|
|
|
&& ev->time.frames < n_samples) {
|
|
|
|
/* first synthesize sound up until the message timestamp */
|
|
|
|
written = synth_sound(self->synth, written, ev->time.frames, audio);
|
|
|
|
}
|
|
|
|
/* send midi message to synth */
|
|
|
|
if (self->xmas) {
|
|
|
|
synth_parse_xmas(self->synth, (const uint8_t*)(ev+1), ev->body.size);
|
|
|
|
} else {
|
|
|
|
synth_parse_midi(self->synth, (const uint8_t*)(ev+1), ev->body.size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ev = (LV2_Atom_Event const*) // lv2_atom_sequence_next()
|
|
|
|
((uintptr_t)((const uint8_t*)ev + sizeof(LV2_Atom_Event) + ((ev->body.size + 7) & ~7)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* synthesize [remaining] sound */
|
|
|
|
synth_sound(self->synth, written, n_samples, audio);
|
2013-10-19 22:31:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup(LV2_Handle handle)
|
|
|
|
{
|
2019-02-28 14:56:23 -05:00
|
|
|
RSynth* self = (RSynth*)handle;
|
|
|
|
synth_free(self->synth);
|
|
|
|
free(handle);
|
2013-10-19 22:31:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const void*
|
|
|
|
extension_data(const char* uri)
|
|
|
|
{
|
2019-02-28 14:56:23 -05:00
|
|
|
(void) uri; /* unused variable */
|
|
|
|
return NULL;
|
2013-10-19 22:31:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const LV2_Descriptor descriptor = {
|
2019-02-28 14:56:23 -05:00
|
|
|
RSY_URI,
|
|
|
|
instantiate,
|
|
|
|
connect_port,
|
|
|
|
NULL,
|
|
|
|
run,
|
|
|
|
NULL,
|
|
|
|
cleanup,
|
|
|
|
extension_data
|
2013-10-19 22:31:07 -04:00
|
|
|
};
|
|
|
|
|
2014-01-17 10:10:03 -05:00
|
|
|
#if defined(COMPILER_MSVC)
|
|
|
|
__declspec(dllexport)
|
|
|
|
#else
|
|
|
|
__attribute__ ((visibility ("default")))
|
|
|
|
#endif
|
2013-10-19 22:31:07 -04:00
|
|
|
const LV2_Descriptor*
|
2013-10-27 12:03:49 -04:00
|
|
|
lv2_descriptor(uint32_t idx)
|
2013-10-19 22:31:07 -04:00
|
|
|
{
|
2019-02-28 14:56:23 -05:00
|
|
|
switch (idx) {
|
|
|
|
case 0:
|
|
|
|
return &descriptor;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-19 22:31:07 -04:00
|
|
|
}
|