159f422c8560ad0b0e1b3ebe59c5b08358fcdfb9
[ardour.git] / libs / plugins / reasonablesynth.lv2 / lv2.c
1 /* reasonable simple synth
2  *
3  * Copyright (C) 2013 Robin Gareus <robin@gareus.org>
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  */
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25
26 /* LV2 */
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"
31
32 #define RSY_URI "https://community.ardour.org/node/7596"
33
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);
40
41 #include "rsynth.c"
42
43 typedef enum {
44   RSY_MIDIIN = 0,
45   RSY_OUTL,
46   RSY_OUTR
47 } PortIndex;
48
49 typedef struct {
50   const LV2_Atom_Sequence* midiin;
51   float* outL;
52   float* outR;
53
54   LV2_URID_Map* map;
55   LV2_URID midi_MidiEvent;
56
57   double SampleRateD;
58   void *synth;
59 } RSynth;
60
61 /* main LV2 */
62
63 static LV2_Handle
64 instantiate(const LV2_Descriptor*     descriptor,
65             double                    rate,
66             const char*               bundle_path,
67             const LV2_Feature* const* features)
68 {
69   if (rate < 8000) {
70     fprintf(stderr, "RSynth.lv2 error: unsupported sample-rate (must be > 8k)\n");
71     return NULL;
72   }
73   RSynth* self = (RSynth*)calloc(1, sizeof(RSynth));
74   if(!self) {
75     return NULL;
76   }
77
78   self->SampleRateD = rate;
79
80   int i;
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;
84     }
85   }
86
87   if (!self->map) {
88     fprintf(stderr, "RSynth.lv2 error: Host does not support urid:map\n");
89     free(self);
90     return NULL;
91   }
92
93   self->midi_MidiEvent = self->map->map(self->map->handle, LV2_MIDI__MidiEvent);
94
95   self->synth = synth_alloc();
96   synth_init(self->synth, rate);
97
98   return (LV2_Handle)self;
99 }
100
101 static void
102 connect_port(LV2_Handle handle,
103              uint32_t   port,
104              void*      data)
105 {
106   RSynth* self = (RSynth*)handle;
107
108   switch ((PortIndex)port) {
109     case RSY_MIDIIN:
110       self->midiin = (const LV2_Atom_Sequence*)data;
111       break;
112     case RSY_OUTL:
113       self->outL = (float*)data;
114       break;
115     case RSY_OUTR:
116       self->outR = (float*)data;
117       break;
118   }
119 }
120
121 static void
122 run(LV2_Handle handle, uint32_t n_samples)
123 {
124   RSynth* self = (RSynth*)handle;
125   float* audio[2];
126
127   audio[0] = self->outL;
128   audio[1] = self->outR;
129
130   uint32_t written = 0;
131
132   /* Process incoming MIDI events */
133   if (self->midiin) {
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);
141         }
142         /* send midi message to synth */
143         synth_parse_midi(self->synth, (uint8_t*)(ev+1), ev->body.size);
144       }
145       ev = lv2_atom_sequence_next(ev);
146     }
147   }
148
149   /* synthesize [remaining] sound */
150   synth_sound(self->synth, written, n_samples, audio);
151 }
152
153 static void
154 cleanup(LV2_Handle handle)
155 {
156   RSynth* self = (RSynth*)handle;
157   synth_free(self->synth);
158   free(handle);
159 }
160
161 static const void*
162 extension_data(const char* uri)
163 {
164   return NULL;
165 }
166
167 static const LV2_Descriptor descriptor = {
168   RSY_URI,
169   instantiate,
170   connect_port,
171   NULL,
172   run,
173   NULL,
174   cleanup,
175   extension_data
176 };
177
178 LV2_SYMBOL_EXPORT
179 const LV2_Descriptor*
180 lv2_descriptor(uint32_t index)
181 {
182   switch (index) {
183   case 0:
184     return &descriptor;
185   default:
186     return NULL;
187   }
188 }
189
190 /* vi:set ts=8 sts=2 sw=2: */