changeset 137:65a488d8c1bb midi

Might help to actually commit these files
author Chris Cannam
date Mon, 13 Oct 2014 11:28:24 +0100
parents 2260947be4aa
children a4bee1a374b4
files runner/MIDIFeatureWriter.cpp runner/MIDIFeatureWriter.h
diffstat 2 files changed, 168 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runner/MIDIFeatureWriter.cpp	Mon Oct 13 11:28:24 2014 +0100
@@ -0,0 +1,93 @@
+/* -*- 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 filename = getOutputFilename(trackId, transform.getIdentifier());
+    if (filename == "") {
+	throw FailedToOpenOutputStream(trackId, transform.getIdentifier());
+    }
+
+    //!!! implement!
+}
+
+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 11:28:24 2014 +0100
@@ -0,0 +1,75 @@
+/* -*- 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 startFrame, int endFrame) 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, float> SampleRateMap; // NoteData uses sample timing
+    SampleRateMap m_rates;
+
+    typedef map<TransformId, int> ChannelMap;
+    ChannelMap m_channels;
+    
+    typedef map<QString, int> LastChannelMap;
+    LastChannelMap m_lastChannels;
+};
+
+#endif
+