Merge branch 'windows+cc' into cairocanvas
[ardour.git] / libs / plugins / reasonablesynth.lv2 / lv2.c
index a32fe200cecf413c4318658713e8b9ad79a9148a..6b9c81d8dd06a65df303dcfc9acdfde0a5480d7e 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <time.h>
 
 /* LV2 */
 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
@@ -56,6 +57,7 @@ typedef struct {
 
   double SampleRateD;
   void *synth;
+  bool xmas;
 } RSynth;
 
 /* main LV2 */
@@ -66,6 +68,9 @@ instantiate(const LV2_Descriptor*     descriptor,
             const char*               bundle_path,
             const LV2_Feature* const* features)
 {
+  (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;
@@ -95,6 +100,16 @@ instantiate(const LV2_Descriptor*     descriptor,
   self->synth = synth_alloc();
   synth_init(self->synth, rate);
 
+
+  struct tm date;
+  time_t now;
+  time(&now);
+  localtime_r(&now, &date);
+  if (getenv("ITSXMAS") || (date.tm_mon == 11 /*dec*/ && date.tm_mday == 25)) {
+    printf("reasonable synth.lv2 says: happy holidays!\n");
+    self->xmas = true;
+  }
+
   return (LV2_Handle)self;
 }
 
@@ -131,18 +146,26 @@ run(LV2_Handle handle, uint32_t n_samples)
 
   /* Process incoming MIDI events */
   if (self->midiin) {
-    LV2_Atom_Event const* ev = (LV2_Atom_Event const*) ((&(self->midiin)->body) + 1); // lv2_atom_sequence_begin
-    while( (const uint8_t*)ev < ((const uint8_t*) &(self->midiin)->body + (self->midiin)->atom.size) ) {
+    LV2_Atom_Event const* ev = (LV2_Atom_Event const*)((&(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) {
-       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 */
-       synth_parse_midi(self->synth, (const uint8_t*)(ev+1), ev->body.size);
+        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*)((const uint8_t*)ev + sizeof(LV2_Atom_Event) + ((ev->body.size + 7) & ~7));
+      ev = (LV2_Atom_Event const*) // lv2_atom_sequence_next()
+        ((const uint8_t*)ev + sizeof(LV2_Atom_Event) + ((ev->body.size + 7) & ~7));
     }
   }
 
@@ -161,6 +184,7 @@ cleanup(LV2_Handle handle)
 static const void*
 extension_data(const char* uri)
 {
+  (void) uri; /* unused variable */
   return NULL;
 }
 
@@ -177,9 +201,9 @@ static const LV2_Descriptor descriptor = {
 
 LV2_SYMBOL_EXPORT
 const LV2_Descriptor*
-lv2_descriptor(uint32_t index)
+lv2_descriptor(uint32_t idx)
 {
-  switch (index) {
+  switch (idx) {
   case 0:
     return &descriptor;
   default:
@@ -187,4 +211,4 @@ lv2_descriptor(uint32_t index)
   }
 }
 
-/* vi:set ts=8 sts=2 sw=2: */
+/* vi:set ts=8 sts=2 sw=2 et: */