# HG changeset patch # User Chris Cannam # Date 1413204789 -3600 # Node ID ad425b9096bd668a31126b575a104e6ba2c062f8 # Parent ee56e3e9eeb565c0c25a8cee13cdd82854baa6c0# Parent 6b62bae0af33ffdb6db23ed1a2da2318e702b52e Merge from branch 'midi' diff -r ee56e3e9eeb5 -r ad425b9096bd .hgsubstate --- 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 diff -r ee56e3e9eeb5 -r ad425b9096bd runner.pro --- 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 { diff -r ee56e3e9eeb5 -r ad425b9096bd runner/FeatureWriterFactory.cpp --- 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 @@ -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; diff -r ee56e3e9eeb5 -r ad425b9096bd runner/MIDIFeatureWriter.cpp --- /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 ¶ms) +{ + 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(); + } + } +} + diff -r ee56e3e9eeb5 -r ad425b9096bd runner/MIDIFeatureWriter.h --- /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 ¶ms); + + 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 NoteMap; // output filename -> notes + NoteMap m_notes; + + typedef map > FileTransformMap; + FileTransformMap m_fileTransforms; + + typedef map SampleRateMap; // NoteData uses sample timing + SampleRateMap m_rates; + + typedef map ChannelMap; + ChannelMap m_channels; + + typedef map NextChannelMap; + NextChannelMap m_nextChannels; +}; + +#endif + diff -r ee56e3e9eeb5 -r ad425b9096bd runner/main.cpp --- 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 Apply transform described in transform file 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 Apply all transforms described in transform files" << endl; - cerr << " whose names are listed in text file . You may supply" << endl; - cerr << " this option multiple times." << endl; - cerr << endl; - cerr << " -d, --default Apply the default transform for transform id . 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 Write output using writer type ." << endl; - cerr << " Supported writer types are: "; set writers = FeatureWriterFactory::getWriterTags(); + + QString writerText = "Supported writer types are: "; for (set::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 In addition to the result features, write summary feature" << endl; - cerr << " of summary type ." << 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 ,[,...]" << 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 ,[,...]" << 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