xmas-egg
[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 #include <time.h>
26
27 /* LV2 */
28 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
29 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
30 #include "lv2/lv2plug.in/ns/ext/urid/urid.h"
31 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
32
33 #define RSY_URI "https://community.ardour.org/node/7596"
34
35 /* the synth interface */
36 static void *   synth_alloc      (void);
37 static void     synth_init       (void *, double rate);
38 static void     synth_free       (void *);
39 static void     synth_parse_midi (void *, const uint8_t *data, const size_t size);
40 static uint32_t synth_sound      (void *, uint32_t written, uint32_t nframes, float **out);
41
42 #include "rsynth.c"
43
44 typedef enum {
45   RSY_MIDIIN = 0,
46   RSY_OUTL,
47   RSY_OUTR
48 } PortIndex;
49
50 typedef struct {
51   const LV2_Atom_Sequence* midiin;
52   float* outL;
53   float* outR;
54
55   LV2_URID_Map* map;
56   LV2_URID midi_MidiEvent;
57
58   double SampleRateD;
59   void *synth;
60   bool xmas;
61 } RSynth;
62
63 /* main LV2 */
64
65 static LV2_Handle
66 instantiate(const LV2_Descriptor*     descriptor,
67             double                    rate,
68             const char*               bundle_path,
69             const LV2_Feature* const* features)
70 {
71   (void) descriptor; /* unused variable */
72   (void) bundle_path; /* unused variable */
73
74   if (rate < 8000) {
75     fprintf(stderr, "RSynth.lv2 error: unsupported sample-rate (must be > 8k)\n");
76     return NULL;
77   }
78   RSynth* self = (RSynth*)calloc(1, sizeof(RSynth));
79   if(!self) {
80     return NULL;
81   }
82
83   self->SampleRateD = rate;
84
85   int i;
86   for (i=0; features[i]; ++i) {
87     if (!strcmp(features[i]->URI, LV2_URID__map)) {
88       self->map = (LV2_URID_Map*)features[i]->data;
89     }
90   }
91
92   if (!self->map) {
93     fprintf(stderr, "RSynth.lv2 error: Host does not support urid:map\n");
94     free(self);
95     return NULL;
96   }
97
98   self->midi_MidiEvent = self->map->map(self->map->handle, LV2_MIDI__MidiEvent);
99
100   self->synth = synth_alloc();
101   synth_init(self->synth, rate);
102
103
104   struct tm date;
105   time_t now;
106   time(&now);
107   localtime_r(&now, &date);
108   if (getenv("ITSXMAS") || (date.tm_mon == 11 /*dec*/ && date.tm_mday == 25)) {
109     printf("reasonable synth.lv2 says: happy holidays!\n");
110     self->xmas = true;
111   }
112
113   return (LV2_Handle)self;
114 }
115
116 static void
117 connect_port(LV2_Handle handle,
118              uint32_t   port,
119              void*      data)
120 {
121   RSynth* self = (RSynth*)handle;
122
123   switch ((PortIndex)port) {
124     case RSY_MIDIIN:
125       self->midiin = (const LV2_Atom_Sequence*)data;
126       break;
127     case RSY_OUTL:
128       self->outL = (float*)data;
129       break;
130     case RSY_OUTR:
131       self->outR = (float*)data;
132       break;
133   }
134 }
135
136 static void
137 run(LV2_Handle handle, uint32_t n_samples)
138 {
139   RSynth* self = (RSynth*)handle;
140   float* audio[2];
141
142   audio[0] = self->outL;
143   audio[1] = self->outR;
144
145   uint32_t written = 0;
146
147   /* Process incoming MIDI events */
148   if (self->midiin) {
149     LV2_Atom_Event const* ev = (LV2_Atom_Event const*)((&(self->midiin)->body) + 1); // lv2_atom_sequence_begin
150     while( // !lv2_atom_sequence_is_end
151         (const uint8_t*)ev < ((const uint8_t*) &(self->midiin)->body + (self->midiin)->atom.size)
152         )
153     {
154       if (ev->body.type == self->midi_MidiEvent) {
155         if (written + BUFFER_SIZE_SAMPLES < ev->time.frames
156             && ev->time.frames < n_samples) {
157           /* first synthesize sound up until the message timestamp */
158           written = synth_sound(self->synth, written, ev->time.frames, audio);
159         }
160         /* send midi message to synth */
161         if (self->xmas) {
162           synth_parse_xmas(self->synth, (const uint8_t*)(ev+1), ev->body.size);
163         } else {
164           synth_parse_midi(self->synth, (const uint8_t*)(ev+1), ev->body.size);
165         }
166       }
167       ev = (LV2_Atom_Event const*) // lv2_atom_sequence_next()
168         ((const uint8_t*)ev + sizeof(LV2_Atom_Event) + ((ev->body.size + 7) & ~7));
169     }
170   }
171
172   /* synthesize [remaining] sound */
173   synth_sound(self->synth, written, n_samples, audio);
174 }
175
176 static void
177 cleanup(LV2_Handle handle)
178 {
179   RSynth* self = (RSynth*)handle;
180   synth_free(self->synth);
181   free(handle);
182 }
183
184 static const void*
185 extension_data(const char* uri)
186 {
187   (void) uri; /* unused variable */
188   return NULL;
189 }
190
191 static const LV2_Descriptor descriptor = {
192   RSY_URI,
193   instantiate,
194   connect_port,
195   NULL,
196   run,
197   NULL,
198   cleanup,
199   extension_data
200 };
201
202 LV2_SYMBOL_EXPORT
203 const LV2_Descriptor*
204 lv2_descriptor(uint32_t idx)
205 {
206   switch (idx) {
207   case 0:
208     return &descriptor;
209   default:
210     return NULL;
211   }
212 }
213
214 /* vi:set ts=8 sts=2 sw=2 et: */