changeset 143:ad425b9096bd

Merge from branch 'midi'
author Chris Cannam
date Mon, 13 Oct 2014 13:53:09 +0100
parents ee56e3e9eeb5 (current diff) 6b62bae0af33 (diff)
children b3d73c08b6ce
files
diffstat 6 files changed, 346 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/.hgsubstate	Mon Oct 13 11:42:54 2014 +0100
+++ b/.hgsubstate	Mon Oct 13 13:53:09 2014 +0100
@@ -1,3 +1,3 @@
 d16f0fd6db6104d87882bc43788a3bb1b0f8c528 dataquay
 879bdc878826bebec67130326f99397c430419b1 sv-dependency-builds
-952005e252668fecdee23fed6227c52ba28cf0a3 svcore
+2104ea2204d2b8e5f19401526d8009ac8eba63ea svcore
--- a/runner.pro	Mon Oct 13 11:42:54 2014 +0100
+++ b/runner.pro	Mon Oct 13 13:53:09 2014 +0100
@@ -53,8 +53,8 @@
 
 TARGET = sonic-annotator
 
-DEPENDPATH += . svcore
-INCLUDEPATH += . dataquay svcore
+DEPENDPATH += . svcore runner
+INCLUDEPATH += . dataquay svcore runner
 
 QMAKE_LIBDIR = svcore $$QMAKE_LIBDIR
 
@@ -84,6 +84,7 @@
         runner/FeatureWriterFactory.h  \
         runner/DefaultFeatureWriter.h \
         runner/FeatureExtractionManager.h \
+        runner/MIDIFeatureWriter.h \
         runner/MultiplexedReader.h
 
 SOURCES += \
@@ -92,6 +93,7 @@
 	runner/FeatureExtractionManager.cpp \
         runner/AudioDBFeatureWriter.cpp \
         runner/FeatureWriterFactory.cpp \
+        runner/MIDIFeatureWriter.cpp \
         runner/MultiplexedReader.cpp
 
 !win32 {
--- a/runner/FeatureWriterFactory.cpp	Mon Oct 13 11:42:54 2014 +0100
+++ b/runner/FeatureWriterFactory.cpp	Mon Oct 13 13:53:09 2014 +0100
@@ -19,6 +19,7 @@
 #include "DefaultFeatureWriter.h"
 #include "rdf/RDFFeatureWriter.h"
 #include "AudioDBFeatureWriter.h"
+#include "MIDIFeatureWriter.h"
 #include "transform/CSVFeatureWriter.h"
 
 set<string>
@@ -29,6 +30,7 @@
     tags.insert("rdf");
     tags.insert("audiodb");
     tags.insert("csv");
+    tags.insert("midi");
     return tags;
 }
 
@@ -43,6 +45,8 @@
         return new AudioDBFeatureWriter();
     } else if (tag == "csv") {
         return new CSVFeatureWriter();
+    } else if (tag == "midi") {
+        return new MIDIFeatureWriter();
     }
 
     return 0;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runner/MIDIFeatureWriter.cpp	Mon Oct 13 13:53:09 2014 +0100
@@ -0,0 +1,156 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Annotator
+    A utility for batch feature extraction from audio files.
+    Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
+    Copyright 2007-2014 QMUL.
+
+    This program 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.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#include "MIDIFeatureWriter.h"
+
+using namespace std;
+using Vamp::Plugin;
+using Vamp::PluginBase;
+
+#include "base/Exceptions.h"
+#include "data/fileio/MIDIFileWriter.h"
+
+MIDIFeatureWriter::MIDIFeatureWriter() :
+    FileFeatureWriter(SupportOneFilePerTrackTransform |
+                      SupportOneFilePerTrack |
+                      SupportOneFileTotal,
+                      "mid")
+{
+}
+
+MIDIFeatureWriter::~MIDIFeatureWriter()
+{
+}
+
+MIDIFeatureWriter::ParameterList
+MIDIFeatureWriter::getSupportedParameters() const
+{
+    ParameterList pl = FileFeatureWriter::getSupportedParameters();
+    return pl;
+}
+
+void
+MIDIFeatureWriter::setParameters(map<string, string> &params)
+{
+    FileFeatureWriter::setParameters(params);
+}
+
+void
+MIDIFeatureWriter::setTrackMetadata(QString, TrackMetadata)
+{
+    cerr << "MIDIFeatureWriter::setTrackMetadata: not supported (yet?)" << endl;
+}
+
+void
+MIDIFeatureWriter::write(QString trackId,
+			 const Transform &transform,
+			 const Plugin::OutputDescriptor& output,
+			 const Plugin::FeatureList& features,
+			 std::string /* summaryType */)
+{
+    QString transformId = transform.getIdentifier();
+
+    QString filename = getOutputFilename(trackId, transformId);
+    if (filename == "") {
+	throw FailedToOpenOutputStream(trackId, transformId);
+    }
+
+    int sampleRate = transform.getSampleRate();
+
+    if (m_rates.find(filename) == m_rates.end()) {
+        m_rates[filename] = sampleRate;
+    }
+
+    if (m_fileTransforms[filename].find(transformId) == 
+        m_fileTransforms[filename].end()) {
+
+        // This transform is new to the file, give it a channel number
+
+        int channel = m_nextChannels[filename];
+        m_nextChannels[filename] = channel + 1;
+
+        m_fileTransforms[filename].insert(transformId);
+        m_channels[transformId] = channel;
+    }
+
+    NoteList notes = m_notes[filename];
+
+    bool freq = (output.unit == "Hz" || 
+                 output.unit == "hz" || 
+                 output.unit == "HZ");
+
+    for (int i = 0; i < (int)features.size(); ++i) {
+
+        const Plugin::Feature &feature(features[i]);
+
+        Vamp::RealTime timestamp = feature.timestamp;
+        int frame = Vamp::RealTime::realTime2Frame(timestamp, sampleRate);
+
+        int duration = 1;
+        if (feature.hasDuration) {
+            duration = Vamp::RealTime::realTime2Frame(feature.duration, sampleRate);
+        }
+        
+        int pitch = 60;
+        if (feature.values.size() > 0) {
+            float pval = feature.values[0];
+            if (freq) {
+                pitch = Pitch::getPitchForFrequency(pval);
+            } else {
+                pitch = int(pval + 0.5);
+            }
+        }
+
+        int velocity = 100;
+        if (feature.values.size() > 1) {
+            float vval = feature.values[1];
+            if (vval < 128) {
+                velocity = int(vval + 0.5);
+            }
+        }
+
+        NoteData note(frame, duration, pitch, velocity);
+
+        note.channel = m_channels[transformId];
+
+        notes.push_back(note);
+    }
+
+    m_notes[filename] = notes;
+}
+
+void
+MIDIFeatureWriter::finish()
+{
+    for (NoteMap::const_iterator i = m_notes.begin(); i != m_notes.end(); ++i) {
+
+	QString filename = i->first;
+	NoteList notes = i->second;
+	float rate = m_rates[filename];
+
+	TrivialNoteExportable exportable(notes);
+
+	{
+	    MIDIFileWriter writer(filename, &exportable, rate);
+	    if (!writer.isOK()) {
+		cerr << "ERROR: Failed to create MIDI writer: " 
+		     << writer.getError() << endl;
+		throw FileOperationFailed(filename, "create MIDI writer");
+	    }
+	    writer.write();
+	}
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runner/MIDIFeatureWriter.h	Mon Oct 13 13:53:09 2014 +0100
@@ -0,0 +1,78 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Annotator
+    A utility for batch feature extraction from audio files.
+
+    Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
+    Copyright 2007-2014 QMUL.
+
+    This program 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.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#ifndef _MIDI_FEATURE_WRITER_H_
+#define _MIDI_FEATURE_WRITER_H_
+
+#include "transform/FileFeatureWriter.h"
+#include "data/model/NoteData.h"
+
+class MIDIFileWriter;
+
+class MIDIFeatureWriter : public FileFeatureWriter
+{
+public:
+    MIDIFeatureWriter();
+    virtual ~MIDIFeatureWriter();
+
+    virtual ParameterList getSupportedParameters() const;
+    virtual void setParameters(map<string, string> &params);
+
+    virtual void setTrackMetadata(QString trackid, TrackMetadata metadata);
+
+    virtual void write(QString trackid,
+                       const Transform &transform,
+                       const Vamp::Plugin::OutputDescriptor &output,
+                       const Vamp::Plugin::FeatureList &features,
+                       std::string summaryType = "");
+
+    virtual void finish();
+
+    virtual QString getWriterTag() const { return "midi"; }
+
+private:
+    class TrivialNoteExportable : public NoteExportable {
+    public:
+	TrivialNoteExportable(NoteList notes) : m_notes(notes) { }
+	virtual NoteList getNotes() const {
+	    return m_notes;
+	}
+	virtual NoteList getNotesWithin(int, int) const {
+	    // Not required by MIDIFileWriter, not supported
+	    return NoteList();
+	}
+    private:
+	NoteList m_notes;
+    };
+
+    typedef map<QString, NoteList> NoteMap; // output filename -> notes
+    NoteMap m_notes;
+    
+    typedef map<QString, set<TransformId> > FileTransformMap;
+    FileTransformMap m_fileTransforms;
+
+    typedef map<QString, float> SampleRateMap; // NoteData uses sample timing
+    SampleRateMap m_rates;
+
+    typedef map<TransformId, int> ChannelMap;
+    ChannelMap m_channels;
+    
+    typedef map<QString, int> NextChannelMap;
+    NextChannelMap m_nextChannels;
+};
+
+#endif
+
--- a/runner/main.cpp	Mon Oct 13 11:42:54 2014 +0100
+++ b/runner/main.cpp	Mon Oct 13 13:53:09 2014 +0100
@@ -211,8 +211,10 @@
     cerr << endl;
 }
 
-void printHelp(QString myname)
+void printHelp(QString myname, QString w)
 {
+    std::string writer = w.toStdString();
+
     printUsage(myname);
 
     QString extensions = AudioFileReaderFactory::getKnownExtensions();
@@ -239,107 +241,115 @@
     cerr << "Playlist files in M3U format are also supported." << endl;
     cerr << endl;
 
-    cerr << "Transformation options:" << endl;
-    cerr << endl;
-    cerr << "  -t, --transform <T> Apply transform described in transform file <T> to" << endl;
-    cerr << "                      all input audio files.  You may supply this option" << endl;
-    cerr << "                      multiple times.  You must supply this option or -T at" << endl;
-    cerr << "                      least once for any work to be done.  Transform format" << endl;
-    cerr << "                      may be SV transform XML or Vamp transform RDF/Turtle." << endl;
-    cerr << "                      See documentation for examples." << endl;
-    cerr << endl;
-    cerr << "  -T, --transforms <T> Apply all transforms described in transform files" << endl;
-    cerr << "                      whose names are listed in text file <T>.  You may supply" << endl;
-    cerr << "                      this option multiple times." << endl;
-    cerr << endl;
-    cerr << "  -d, --default <I>   Apply the default transform for transform id <I>.  This" << endl;
-    cerr << "                      is equivalent to generating a skeleton transform for this" << endl;
-    cerr << "                      id (using the -s option, below) and then applying that," << endl;
-    cerr << "                      unmodified, with the -t option in the normal way.  Note" << endl;
-    cerr << "                      that the results may vary as the implementation's default" << endl;
-    cerr << "                      processing parameters are not guaranteed.  Do not use" << endl;
-    cerr << "                      this in production systems.  You may supply this option" << endl;
-    cerr << "                      multiple times, and mix it with -t and -T." << endl;
-    cerr << endl;
-    cerr << "  -w, --writer <W>    Write output using writer type <W>." << endl;
-    cerr << "                      Supported writer types are: ";
     set<string> writers = FeatureWriterFactory::getWriterTags();
+
+    QString writerText = "Supported writer types are: ";
     for (set<string>::const_iterator i = writers.begin();
          i != writers.end(); ) {
-        cerr << *i;
-        if (++i != writers.end()) cerr << ", ";
-        else cerr << ".";
+        writerText += i->c_str();
+        if (++i != writers.end()) writerText += ", ";
+        else writerText += ".";
     }
-    cerr << endl;
-    cerr << "                      You may supply this option multiple times.  You must" << endl;
-    cerr << "                      supply this option at least once for any work to be done." << endl;
-    cerr << endl;
-    cerr << "  -S, --summary <S>   In addition to the result features, write summary feature" << endl;
-    cerr << "                      of summary type <S>." << endl;
-    cerr << "                      Supported summary types are min, max, mean, median, mode," << endl;
-    cerr << "                      sum, variance, sd, count." << endl;
-    cerr << "                      You may supply this option multiple times." << endl;
-    cerr << endl;
-    cerr << "      --summary-only  Write only summary features; do not write the regular" << endl;
-    cerr << "                      result features." << endl;
-    cerr << endl;
-    cerr << "      --segments <A>,<B>[,...]" << endl;
-    cerr << "                      Summarise in segments, with segment boundaries" << endl;
-    cerr << "                      at A, B, ... seconds." << endl;
-    cerr << endl;
-    cerr << "  -m, --multiplex     If multiple input audio files are given, use mono" << endl;
-    cerr << "                      mixdowns of all files as the input channels for a single" << endl;
-    cerr << "                      invocation of each transform, instead of running the" << endl;
-    cerr << "                      transform against all files separately. The first file" << endl;
-    cerr << "                      will be used for output reference name and sample rate." << endl;
-    cerr << endl;
-    cerr << "  -r, --recursive     If any of the <audio> arguments is found to be a local" << endl;
-    cerr << "                      directory, search the tree starting at that directory" << endl;
-    cerr << "                      for all supported audio files and take all of those as" << endl;
-    cerr << "                      input instead." << endl;
-    cerr << endl;
-    cerr << "  -n, --normalise     Normalise input audio files to signal absolute max = 1.f." << endl;
-    cerr << endl;
-    cerr << "  -f, --force         Continue with subsequent files following an error." << endl;
-    cerr << endl;
-    cerr << "Housekeeping options:" << endl;
-    cerr << endl;
-    cerr << "  -l, --list          List all known transform ids to standard output." << endl;
-    cerr << endl;
-    cerr << "  -s, --skeleton <I>  Generate a skeleton transform file for transform id <I>" << endl;
-    cerr << "                      and write it to standard output." << endl;
-    cerr << endl;
-    cerr << "  -v, --version       Show the version number and exit." << endl;
-    cerr << endl;
-    cerr << "      --minversion <V> Exit with successful return code if the version of" << endl;
-    cerr << "                      " << myname << " is at least <V>, failure otherwise." << endl;
-    cerr << "                      For scripts that depend on certain option support." << endl;
-    cerr << endl;
-    cerr << "  -h, --help          Show help." << endl;
+    writerText = wrap(writerText, 56, 22);
 
-    cerr << endl;
-    cerr << "If no -w (or --writer) options are supplied, either the -l -s -v or -h option" << endl;
-    cerr << "(or long equivalent) must be given instead." << endl;
+    if (writer == "" || writers.find(writer) == writers.end()) {
 
-    for (set<string>::const_iterator i = writers.begin();
-         i != writers.end(); ++i) {
-        FeatureWriter *w = FeatureWriterFactory::createWriter(*i);
+        cerr << "Transformation options:" << endl;
+        cerr << endl;
+        cerr << "  -t, --transform <T> Apply transform described in transform file <T> to" << endl;
+        cerr << "                      all input audio files. You may supply this option" << endl;
+        cerr << "                      multiple times. You must supply this option or -T at" << endl;
+        cerr << "                      least once for any work to be done. Transform format" << endl;
+        cerr << "                      may be SV transform XML or Vamp transform RDF/Turtle." << endl;
+        cerr << "                      See accompanying documentation for transform examples." << endl;
+        cerr << endl;
+        cerr << "  -T, --transforms <T> Apply all transforms described in transform files" << endl;
+        cerr << "                      whose names are listed in text file <T>. You may supply" << endl;
+        cerr << "                      this option multiple times." << endl;
+        cerr << endl;
+        cerr << "  -d, --default <I>   Apply the default transform for transform id <I>. This" << endl;
+        cerr << "                      is equivalent to generating a skeleton transform for this" << endl;
+        cerr << "                      id (using the -s option, below) and then applying that," << endl;
+        cerr << "                      unmodified, with the -t option in the normal way. Note" << endl;
+        cerr << "                      that results may vary, as the implementation's default" << endl;
+        cerr << "                      processing parameters are not guaranteed. Do not use" << endl;
+        cerr << "                      this in production systems. You may supply this option" << endl;
+        cerr << "                      multiple times, and mix it with -t and -T." << endl;
+        cerr << endl;
+        cerr << "  -w, --writer <W>    Write output using writer type <W>." << endl;
+        cerr << "                      " << writerText << endl;
+        cerr << "                      You may supply this option multiple times. You must" << endl;
+        cerr << "                      supply this option at least once for any work to be done." << endl;
+        cerr << endl;
+        cerr << "  -S, --summary <S>   In addition to the result features, write summary feature" << endl;
+        cerr << "                      of summary type <S>." << endl;
+        cerr << "                      Supported summary types are min, max, mean, median, mode," << endl;
+        cerr << "                      sum, variance, sd, count." << endl;
+        cerr << "                      You may supply this option multiple times." << endl;
+        cerr << endl;
+        cerr << "      --summary-only  Write only summary features; do not write the regular" << endl;
+        cerr << "                      result features." << endl;
+        cerr << endl;
+        cerr << "      --segments <A>,<B>[,...]" << endl;
+        cerr << "                      Summarise in segments, with segment boundaries" << endl;
+        cerr << "                      at A, B, ... seconds." << endl;
+        cerr << endl;
+        cerr << "  -m, --multiplex     If multiple input audio files are given, use mono" << endl;
+        cerr << "                      mixdowns of all files as the input channels for a single" << endl;
+        cerr << "                      invocation of each transform, instead of running the" << endl;
+        cerr << "                      transform against all files separately. The first file" << endl;
+        cerr << "                      will be used for output reference name and sample rate." << endl;
+        cerr << endl;
+        cerr << "  -r, --recursive     If any of the <audio> arguments is found to be a local" << endl;
+        cerr << "                      directory, search the tree starting at that directory" << endl;
+        cerr << "                      for all supported audio files and take all of those as" << endl;
+        cerr << "                      input instead." << endl;
+        cerr << endl;
+        cerr << "  -n, --normalise     Normalise input audio files to signal absolute max = 1.f." << endl;
+        cerr << endl;
+        cerr << "  -f, --force         Continue with subsequent files following an error." << endl;
+        cerr << endl;
+        cerr << "Housekeeping options:" << endl;
+        cerr << endl;
+        cerr << "  -l, --list          List all known transform ids to standard output." << endl;
+        cerr << endl;
+        cerr << "  -s, --skeleton <I>  Generate a skeleton transform file for transform id <I>" << endl;
+        cerr << "                      and write it to standard output." << endl;
+        cerr << endl;
+        cerr << "  -v, --version       Show the version number and exit." << endl;
+        cerr << endl;
+        cerr << "      --minversion <V> Exit with successful return code if the version of" << endl;
+        cerr << "                      " << myname << " is at least <V>, failure otherwise." << endl;
+        cerr << "                      For scripts that depend on certain option support." << endl;
+        cerr << endl;
+        cerr << "  -h, --help          Show help." << endl;
+        cerr << "  -h, --help <W>      Show help for writer type W." << endl;
+        cerr << "                      " << writerText << endl;
+
+        cerr << endl;
+        cerr << "If no -w (or --writer) options are supplied, either the -l -s -v or -h option" << endl;
+        cerr << "(or long equivalent) must be given instead." << endl;
+
+    } else {
+
+        FeatureWriter *w = FeatureWriterFactory::createWriter(writer);
         if (!w) {
-            cerr << "  (Internal error: failed to create writer of this type)" << endl;
-            continue;
+            cerr << "  (Internal error: failed to create writer of known type \""
+                 << writer << "\")" << endl;
+            return;
         }
+        cerr << "Additional options for writer type \"" << writer << "\":" << endl;
+        cerr << endl;
         FeatureWriter::ParameterList params = w->getSupportedParameters();
         delete w;
         if (params.empty()) {
-            continue;
+            cerr << "(No special options available for this writer)" << endl;
+            return;
         }
-        cerr << endl;
-        cerr << "Additional options for writer type \"" << *i << "\":" << endl;
-        cerr << endl;
         for (FeatureWriter::ParameterList::const_iterator j = params.begin();
              j != params.end(); ++j) {
-            cerr << "  --" << *i << "-" << j->name << " ";
-            int spaceage = 16 - int(i->length()) - int(j->name.length());
+            cerr << "  --" << writer << "-" << j->name << " ";
+            int spaceage = 16 - int(writer.length()) - int(j->name.length());
             if (j->hasArg) { cerr << "<X> "; spaceage -= 4; }
             for (int k = 0; k < spaceage; ++k) cerr << " ";
             QString s(j->description.c_str());
@@ -474,7 +484,11 @@
         bool last = ((i + 1) == args.size());
         
         if (arg == "-h" || arg == "--help" || arg == "-?") {
-            printHelp(myname);
+            QString writer;
+            if (!last) {
+                writer = args[i+1];
+            }
+            printHelp(myname, writer);
             return 0;
         }