1 /* reasonable simple synth
3 * Copyright (C) 2013 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
28 #include "lv2/lv2plug.in/ns/ext/atom/util.h"
29 #include "lv2/lv2plug.in/ns/ext/urid/urid.h"
30 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
32 #define RSY_URI "https://community.ardour.org/node/7596"
34 /* the synth interface */
35 static void * synth_alloc (void);
36 static void synth_init (void *, double rate);
37 static void synth_free (void *);
38 static void synth_parse_midi (void *, uint8_t *data, size_t size);
39 static uint32_t synth_sound (void *, uint32_t written, uint32_t nframes, float **out);
50 const LV2_Atom_Sequence* midiin;
55 LV2_URID midi_MidiEvent;
64 instantiate(const LV2_Descriptor* descriptor,
66 const char* bundle_path,
67 const LV2_Feature* const* features)
70 fprintf(stderr, "RSynth.lv2 error: unsupported sample-rate (must be > 8k)\n");
73 RSynth* self = (RSynth*)calloc(1, sizeof(RSynth));
78 self->SampleRateD = rate;
81 for (i=0; features[i]; ++i) {
82 if (!strcmp(features[i]->URI, LV2_URID__map)) {
83 self->map = (LV2_URID_Map*)features[i]->data;
88 fprintf(stderr, "RSynth.lv2 error: Host does not support urid:map\n");
93 self->midi_MidiEvent = self->map->map(self->map->handle, LV2_MIDI__MidiEvent);
95 self->synth = synth_alloc();
96 synth_init(self->synth, rate);
98 return (LV2_Handle)self;
102 connect_port(LV2_Handle handle,
106 RSynth* self = (RSynth*)handle;
108 switch ((PortIndex)port) {
110 self->midiin = (const LV2_Atom_Sequence*)data;
113 self->outL = (float*)data;
116 self->outR = (float*)data;
122 run(LV2_Handle handle, uint32_t n_samples)
124 RSynth* self = (RSynth*)handle;
127 audio[0] = self->outL;
128 audio[1] = self->outR;
130 uint32_t written = 0;
132 /* Process incoming MIDI events */
134 LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(self->midiin)->body);
135 while(!lv2_atom_sequence_is_end(&(self->midiin)->body, (self->midiin)->atom.size, ev)) {
136 if (ev->body.type == self->midi_MidiEvent) {
137 if (written + BUFFER_SIZE_SAMPLES < ev->time.frames
138 && ev->time.frames < n_samples) {
139 /* first synthesize sound up until the message timestamp */
140 written = synth_sound(self->synth, written, ev->time.frames, audio);
142 /* send midi message to synth */
143 synth_parse_midi(self->synth, (uint8_t*)(ev+1), ev->body.size);
145 ev = lv2_atom_sequence_next(ev);
149 /* synthesize [remaining] sound */
150 synth_sound(self->synth, written, n_samples, audio);
154 cleanup(LV2_Handle handle)
156 RSynth* self = (RSynth*)handle;
157 synth_free(self->synth);
162 extension_data(const char* uri)
167 static const LV2_Descriptor descriptor = {
179 const LV2_Descriptor*
180 lv2_descriptor(uint32_t index)
190 /* vi:set ts=8 sts=2 sw=2: */