From cd0e36773992e26985bdec1f7a5341f83fa3e521 Mon Sep 17 00:00:00 2001
From: deva <deva>
Date: Fri, 15 Jul 2011 13:02:33 +0000
Subject: New input/output plugin architecture. New LV2 plugin.

---
 drumgizmo/input/midifile/Makefile.am               |  27 +++
 .../input/midifile/audioinputenginemidifile.cc     |  44 ++++
 .../input/midifile/audioinputenginemidifile.h      |  41 ++++
 drumgizmo/input/midifile/midifile.cc               | 235 +++++++++++++++++++++
 drumgizmo/input/midifile/midimap.cc                |  96 +++++++++
 drumgizmo/input/midifile/midimap.h                 |  43 ++++
 6 files changed, 486 insertions(+)
 create mode 100644 drumgizmo/input/midifile/Makefile.am
 create mode 100644 drumgizmo/input/midifile/audioinputenginemidifile.cc
 create mode 100644 drumgizmo/input/midifile/audioinputenginemidifile.h
 create mode 100644 drumgizmo/input/midifile/midifile.cc
 create mode 100644 drumgizmo/input/midifile/midimap.cc
 create mode 100644 drumgizmo/input/midifile/midimap.h

(limited to 'drumgizmo/input/midifile')

diff --git a/drumgizmo/input/midifile/Makefile.am b/drumgizmo/input/midifile/Makefile.am
new file mode 100644
index 0000000..30a2db4
--- /dev/null
+++ b/drumgizmo/input/midifile/Makefile.am
@@ -0,0 +1,27 @@
+
+midifilesources = \
+	midifile.cc \
+	midimap.cc
+
+if HAVE_INPUT_MIDIFILE
+
+midifileltlibs = libmidifile.la
+midifilebuildsources = $(midifilesources)
+
+else
+
+midifileltlibs =
+midifilebuildsources =
+
+endif
+
+EXTRA_DIST = $(midifilesources)
+
+lib_LTLIBRARIES = $(midifileltlibs)
+
+libdir = $(INPUT_PLUGIN_DIR)
+
+INCLUDES = -I$(top_srcdir)/include $(SMF_CFLAGS)
+libmidifile_la_LDFLAGS = $(SMF_LIBS)
+libmidifile_la_LIBADD =
+libmidifile_la_SOURCES = $(midifilebuildsources)
diff --git a/drumgizmo/input/midifile/audioinputenginemidifile.cc b/drumgizmo/input/midifile/audioinputenginemidifile.cc
new file mode 100644
index 0000000..878ca1b
--- /dev/null
+++ b/drumgizmo/input/midifile/audioinputenginemidifile.cc
@@ -0,0 +1,44 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ *            audioinputenginemidifile.cc
+ *
+ *  Sun Feb 27 11:43:32 CET 2011
+ *  Copyright 2011 Bent Bisballe Nyeng
+ *  deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ *  This file is part of DrumGizmo.
+ *
+ *  DrumGizmo is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  DrumGizmo 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with DrumGizmo; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+#include "audioinputenginemidifile.h"
+
+#ifdef TEST_AUDIOINPUTENGINEMIDIFILE
+//Additional dependency files
+//deps:
+//Required cflags (autoconf vars may be used)
+//cflags:
+//Required link options (autoconf vars may be used)
+//libs:
+#include "test.h"
+
+TEST_BEGIN;
+
+// TODO: Put some testcode here (see test.h for usable macros).
+
+TEST_END;
+
+#endif/*TEST_AUDIOINPUTENGINEMIDIFILE*/
diff --git a/drumgizmo/input/midifile/audioinputenginemidifile.h b/drumgizmo/input/midifile/audioinputenginemidifile.h
new file mode 100644
index 0000000..6bb9ff6
--- /dev/null
+++ b/drumgizmo/input/midifile/audioinputenginemidifile.h
@@ -0,0 +1,41 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ *            audioinputenginemidifile.h
+ *
+ *  Sun Feb 27 11:43:32 CET 2011
+ *  Copyright 2011 Bent Bisballe Nyeng
+ *  deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ *  This file is part of DrumGizmo.
+ *
+ *  DrumGizmo is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  DrumGizmo 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with DrumGizmo; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+#ifndef __DRUMGIZMO_AUDIOINPUTENGINEMIDIFILE_H__
+#define __DRUMGIZMO_AUDIOINPUTENGINEMIDIFILE_H__
+
+#include "audioinputengine.h"
+
+class AudioInputEngineMidiFile : public AudioInputEngine {
+public:
+  AudioInputEngineMidiFile() {}
+  ~AudioInputEngineMidiFile() {}
+
+  bool init(EventQueue *eventqueue, DrumKit *drumkit) { return true; }
+  void run(size_t pos, size_t len) {}
+};
+
+#endif/*__DRUMGIZMO_AUDIOINPUTENGINEMIDIFILE_H__*/
diff --git a/drumgizmo/input/midifile/midifile.cc b/drumgizmo/input/midifile/midifile.cc
new file mode 100644
index 0000000..7b17028
--- /dev/null
+++ b/drumgizmo/input/midifile/midifile.cc
@@ -0,0 +1,235 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ *            dummy.cc
+ *
+ *  Sat Apr 30 21:11:54 CEST 2011
+ *  Copyright 2011 Bent Bisballe Nyeng
+ *  deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ *  This file is part of DrumGizmo.
+ *
+ *  DrumGizmo is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  DrumGizmo 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with DrumGizmo; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+#include <stdlib.h>
+
+#include <string>
+#include <event.h>
+
+#include <smf.h>
+
+#include "midimap.h"
+
+#define NOTE_ON 0x90
+
+class MidiFile {
+public:
+  MidiFile();
+  ~MidiFile() {}
+
+  bool init(int instruments, char *inames[]);
+
+  void setParm(std::string parm, std::string value);
+
+  bool start();
+  void stop();
+  
+  void pre();
+  event_t *run(size_t pos, size_t len, size_t *nevents);
+  void post();
+
+private:
+  smf_t *smf;
+  smf_event_t *cur_event;
+
+  MidiMap mmap;
+
+  // Parameters
+  std::string filename;
+  float speed;
+  int track;
+  std::string midimapfile;
+};
+
+MidiFile::MidiFile()
+{
+ cur_event = NULL;
+ smf = NULL;
+ 
+ filename = "";
+ speed = 1.0;
+ track = 0;
+}
+ 
+bool MidiFile::init(int instruments, char *inames[])
+{
+  smf = smf_load(filename.c_str());
+
+  mmap.load(midimapfile);
+
+  return true;
+}
+
+void MidiFile::setParm(std::string parm, std::string value)
+{
+  if(parm == "file") filename = value;
+  if(parm == "speed") speed = atof(value.c_str());
+  if(parm == "track") track = atoi(value.c_str());
+  if(parm == "midimap") midimapfile = value;
+}
+
+bool MidiFile::start()
+{
+  return true;
+}
+
+void MidiFile::stop()
+{
+}
+
+void MidiFile::pre()
+{
+}
+
+event_t *MidiFile::run(size_t pos, size_t len, size_t *nevents)
+{
+  event_t *evs = NULL;
+  size_t nevs = 0;
+
+  double cur_max_time = (double)(pos + len) / (44100.0 / speed);
+  //  double cur_min_time = (double)(pos) / (44100.0 / speed);
+
+  if(!cur_event) cur_event = smf_get_next_event(smf);
+
+  while(cur_event && cur_event->time_seconds < cur_max_time) {
+    if(!smf_event_is_metadata(cur_event)) {
+      if( (cur_event->midi_buffer_length == 3) &&
+          ((cur_event->midi_buffer[0] & NOTE_ON) == NOTE_ON) &&
+          cur_event->track_number == track && cur_event->midi_buffer[2] > 0) {
+        
+        if(evs == NULL) evs = (event_t *)malloc(sizeof(event_t) * 1000);
+
+        int key = cur_event->midi_buffer[1];
+        int velocity = cur_event->midi_buffer[2];
+
+        evs[nevs].type = TYPE_ONSET;
+        size_t evpos = cur_event->time_seconds * (44100.0 / speed);
+        evs[nevs].offset = evpos - pos;
+        /*
+        printf("evpos: %d, sec: %f, pos: %d, len: %d, max_time: %f\n",
+               evpos, cur_event->time_seconds, pos, len, cur_max_time);
+        */
+        evs[nevs].instrument = mmap.lookup(key);
+        evs[nevs].velocity = velocity / 127.0;
+        
+        nevs++;
+        if(nevs > 999) {
+          printf("PANIC!\n");
+          break;
+        }
+      }
+    }
+    
+    cur_event = smf_get_next_event(smf);
+  }
+
+  if(!cur_event) {
+    if(evs == NULL) evs = (event_t *)malloc(sizeof(event_t) * 1000);
+    evs[nevs].type = TYPE_STOP;
+    evs[nevs].offset = len - 1;
+    nevs++;
+  }
+
+  *nevents = nevs;
+
+  return evs;
+}
+
+void MidiFile::post()
+{
+}
+
+extern "C" {
+  void *create()
+  {
+    return new MidiFile();
+  }
+  
+  void destroy(void *h)
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    delete midifile;
+  }
+
+  bool init(void *h, int i, char *inames[])
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    return midifile->init(i, inames);
+  }
+
+  void setparm(void *h, const char *parm, const char *value)
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    midifile->setParm(parm, value);
+  }
+
+  bool start(void *h)
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    return midifile->start();
+  }
+
+  void stop(void *h)
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    midifile->stop();
+  }
+
+  void pre(void *h)
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    midifile->pre();
+  }
+
+  event_t *run(void *h, size_t pos, size_t len, size_t *nev)
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    return midifile->run(pos, len, nev);
+  }
+
+  void post(void *h)
+  {
+    MidiFile *midifile = (MidiFile*)h;
+    midifile->post();
+  }
+}
+
+#ifdef TEST_AUDIOINPUTENGINEMIDIFILE
+//Additional dependency files
+//deps:
+//Required cflags (autoconf vars may be used)
+//cflags:
+//Required link options (autoconf vars may be used)
+//libs:
+#include "test.h"
+
+TEST_BEGIN;
+
+// TODO: Put some testcode here (see test.h for usable macros).
+
+TEST_END;
+
+#endif/*TEST_AUDIOINPUTENGINEMIDIFILE*/
diff --git a/drumgizmo/input/midifile/midimap.cc b/drumgizmo/input/midifile/midimap.cc
new file mode 100644
index 0000000..ccb9b3c
--- /dev/null
+++ b/drumgizmo/input/midifile/midimap.cc
@@ -0,0 +1,96 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ *            midimap.cc
+ *
+ *  Mon Jun 13 21:36:30 CEST 2011
+ *  Copyright 2011 Bent Bisballe Nyeng
+ *  deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ *  This file is part of DrumGizmo.
+ *
+ *  DrumGizmo is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  DrumGizmo 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with DrumGizmo; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+#include "midimap.h"
+
+/*
+
+  35 = 1
+  36-51 = 2            - TODO!
+  51, 53 = 3           - TODO!
+  54, 56-59, 62 = 4    - TODO!
+
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+MidiMap::MidiMap()
+{
+}
+
+bool MidiMap::load(std::string file)
+{
+  FILE *fp = fopen(file.c_str(), "r");
+  if(!fp) return false;
+
+  std::string line;
+  while(!feof(fp)) {
+    int c = fgetc(fp);
+    //    printf("[%c]\n", c);
+    if(c == '\n') {
+      if(line != "") {
+        int from = atoi(line.substr(0, line.find('=')).c_str());
+        int to = atoi(line.substr(line.find('=')+1,
+                                  line.length()-line.find('=')+1).c_str());
+        map[from] = to;
+        // printf("Line: '%s', from: %d to: %d\n", line.c_str(), from, to);
+        
+      }
+      line = "";
+    } else {
+      if((c >= '0' && c <= '9') || c == ',' || c == '-' || c == '=') {
+        line += (char)c;
+      } else {
+        if(c != '\t' && c != ' ') return false; // Parse error.
+      }
+    }
+  }
+
+  return true;
+}
+
+int MidiMap::lookup(int note)
+{
+  return map[note];
+}
+
+#ifdef TEST_MIDIMAP
+//Additional dependency files
+//deps:
+//Required cflags (autoconf vars may be used)
+//cflags:
+//Required link options (autoconf vars may be used)
+//libs:
+#include "test.h"
+
+TEST_BEGIN;
+
+// TODO: Put some testcode here (see test.h for usable macros).
+
+TEST_END;
+
+#endif/*TEST_MIDIMAP*/
diff --git a/drumgizmo/input/midifile/midimap.h b/drumgizmo/input/midifile/midimap.h
new file mode 100644
index 0000000..183d2d3
--- /dev/null
+++ b/drumgizmo/input/midifile/midimap.h
@@ -0,0 +1,43 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ *            midimap.h
+ *
+ *  Mon Jun 13 21:36:29 CEST 2011
+ *  Copyright 2011 Bent Bisballe Nyeng
+ *  deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ *  This file is part of DrumGizmo.
+ *
+ *  DrumGizmo is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  DrumGizmo 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with DrumGizmo; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+#ifndef __DRUMGIZMO_MIDIMAP_H__
+#define __DRUMGIZMO_MIDIMAP_H__
+
+#include <map>
+#include <string>
+
+class MidiMap {
+public:
+  MidiMap();
+  bool load(std::string file);
+  int lookup(int note);
+
+private:
+  std::map<int, int> map;
+};
+
+#endif/*__DRUMGIZMO_MIDIMAP_H__*/
-- 
cgit v1.2.3