1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
* MidiExport.cpp - support for Exporting MIDI files
*
* Copyright (c) 2015 Mohamed Abdel Maksoud <mohamed at amaksoud.com>
* Copyright (c) 2017 Hyunjin Song <tteu.ingog/at/gmail.com>
*
* This file is part of LMMS - https://lmms.io
*
* 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.
*
* This program 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 this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include <QDomDocument>
#include <QDir>
#include <QApplication>
#include <QMessageBox>
#include <QProgressDialog>
#include "MidiExport.h"
#include "lmms_math.h"
#include "TrackContainer.h"
#include "BBTrack.h"
#include "InstrumentTrack.h"
#include "LocaleHelper.h"
extern "C"
{
Plugin::Descriptor PLUGIN_EXPORT midiexport_plugin_descriptor =
{
STRINGIFY( PLUGIN_NAME ),
"MIDI Export",
QT_TRANSLATE_NOOP( "pluginBrowser",
"Filter for exporting MIDI-files from LMMS" ),
"Mohamed Abdel Maksoud <mohamed at amaksoud.com> and "
"Hyunjin Song <tteu.ingog/at/gmail.com>",
0x0100,
Plugin::ExportFilter,
NULL,
NULL,
NULL
} ;
}
MidiExport::MidiExport() : ExportFilter( &midiexport_plugin_descriptor)
{
}
MidiExport::~MidiExport()
{
}
bool MidiExport::tryExport(const TrackContainer::TrackList &tracks,
const TrackContainer::TrackList &tracks_BB,
int tempo, int masterPitch, const QString &filename)
{
QFile f(filename);
f.open(QIODevice::WriteOnly);
QDataStream midiout(&f);
InstrumentTrack* instTrack;
BBTrack* bbTrack;
QDomElement element;
int nTracks = 0;
uint8_t buffer[BUFFER_SIZE];
uint32_t size;
for (const Track* track : tracks) if (track->type() == Track::InstrumentTrack) nTracks++;
for (const Track* track : tracks_BB) if (track->type() == Track::InstrumentTrack) nTracks++;
// midi header
MidiFile::MIDIHeader header(nTracks);
size = header.writeToBuffer(buffer);
midiout.writeRawData((char *)buffer, size);
std::vector<std::vector<std::pair<int,int>>> plists;
// midi tracks
for (Track* track : tracks)
{
DataFile dataFile(DataFile::SongProject);
MTrack mtrack;
if (track->type() == Track::InstrumentTrack)
{
mtrack.addName(track->name().toStdString(), 0);
//mtrack.addProgramChange(0, 0);
mtrack.addTempo(tempo, 0);
instTrack = dynamic_cast<InstrumentTrack *>(track);
element = instTrack->saveState(dataFile, dataFile.content());
int base_pitch = 0;
double base_volume = 1.0;
int base_time = 0;
MidiNoteVector pat;
for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
{
if (n.nodeName() == "instrumenttrack")
{
QDomElement it = n.toElement();
// transpose +12 semitones, workaround for #1857
base_pitch = (69 - it.attribute("basenote", "57").toInt());
if (it.attribute("usemasterpitch", "1").toInt())
{
base_pitch += masterPitch;
}
base_volume = LocaleHelper::toDouble(it.attribute("volume", "100"))/100.0;
}
if (n.nodeName() == "pattern")
{
base_time = n.toElement().attribute("pos", "0").toInt();
writePattern(pat, n, base_pitch, base_volume, base_time);
}
}
ProcessBBNotes(pat, INT_MAX);
writePatternToTrack(mtrack, pat);
size = mtrack.writeToBuffer(buffer);
midiout.writeRawData((char *)buffer, size);
}
if (track->type() == Track::BBTrack)
{
bbTrack = dynamic_cast<BBTrack *>(track);
element = bbTrack->saveState(dataFile, dataFile.content());
std::vector<std::pair<int,int>> plist;
for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
{
if (n.nodeName() == "bbtco")
{
QDomElement it = n.toElement();
int pos = it.attribute("pos", "0").toInt();
int len = it.attribute("len", "0").toInt();
plist.push_back(std::pair<int,int>(pos, pos+len));
}
}
std::sort(plist.begin(), plist.end());
plists.push_back(plist);
}
} // for each track
// midi tracks in BB tracks
for (Track* track : tracks_BB)
{
DataFile dataFile(DataFile::SongProject);
MTrack mtrack;
auto itr = plists.begin();
std::vector<std::pair<int,int>> st;
if (track->type() != Track::InstrumentTrack) continue;
mtrack.addName(track->name().toStdString(), 0);
//mtrack.addProgramChange(0, 0);
mtrack.addTempo(tempo, 0);
instTrack = dynamic_cast<InstrumentTrack *>(track);
element = instTrack->saveState(dataFile, dataFile.content());
int base_pitch = 0;
double base_volume = 1.0;
for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
{
if (n.nodeName() == "instrumenttrack")
{
QDomElement it = n.toElement();
// transpose +12 semitones, workaround for #1857
base_pitch = (69 - it.attribute("basenote", "57").toInt());
if (it.attribute("usemasterpitch", "1").toInt())
{
base_pitch += masterPitch;
}
base_volume = LocaleHelper::toDouble(it.attribute("volume", "100")) / 100.0;
}
if (n.nodeName() == "pattern")
{
std::vector<std::pair<int,int>> &plist = *itr;
MidiNoteVector nv, pat;
writePattern(pat, n, base_pitch, base_volume, 0);
// workaround for nested BBTCOs
int pos = 0;
int len = n.toElement().attribute("steps", "1").toInt() * 12;
for (auto it = plist.begin(); it != plist.end(); ++it)
{
while (!st.empty() && st.back().second <= it->first)
{
writeBBPattern(pat, nv, len, st.back().first, pos, st.back().second);
pos = st.back().second;
st.pop_back();
}
if (!st.empty() && st.back().second <= it->second)
{
writeBBPattern(pat, nv, len, st.back().first, pos, it->first);
pos = it->first;
while (!st.empty() && st.back().second <= it->second)
{
st.pop_back();
}
}
st.push_back(*it);
pos = it->first;
}
while (!st.empty())
{
writeBBPattern(pat, nv, len, st.back().first, pos, st.back().second);
pos = st.back().second;
st.pop_back();
}
ProcessBBNotes(nv, pos);
writePatternToTrack(mtrack, nv);
++itr;
}
}
size = mtrack.writeToBuffer(buffer);
midiout.writeRawData((char *)buffer, size);
}
return true;
}
void MidiExport::writePattern(MidiNoteVector &pat, QDomNode n,
int base_pitch, double base_volume, int base_time)
{
// TODO interpret steps="12" muted="0" type="1" name="Piano1" len="2592"
for (QDomNode nn = n.firstChild(); !nn.isNull(); nn = nn.nextSibling())
{
QDomElement note = nn.toElement();
if (note.attribute("len", "0") == "0") continue;
// TODO interpret pan="0" fxch="0" pitchrange="1"
MidiNote mnote;
mnote.pitch = qMax(0, qMin(127, note.attribute("key", "0").toInt() + base_pitch));
// Map from LMMS volume to MIDI velocity
mnote.volume = qMin(qRound(base_volume * LocaleHelper::toDouble(note.attribute("vol", "100")) * (127.0 / 200.0)), 127);
mnote.time = base_time + note.attribute("pos", "0").toInt();
mnote.duration = note.attribute("len", "0").toInt();
pat.push_back(mnote);
}
}
void MidiExport::writePatternToTrack(MTrack &mtrack, MidiNoteVector &nv)
{
for (auto it = nv.begin(); it != nv.end(); ++it)
{
mtrack.addNote(it->pitch, it->volume, it->time / 48.0, it->duration / 48.0);
}
}
void MidiExport::writeBBPattern(MidiNoteVector &src, MidiNoteVector &dst,
int len, int base, int start, int end)
{
if (start >= end) { return; }
start -= base;
end -= base;
std::sort(src.begin(), src.end());
for (auto it = src.begin(); it != src.end(); ++it)
{
for (int time = it->time + ceil((start - it->time) / len)
* len; time < end; time += len)
{
MidiNote note;
note.duration = it->duration;
note.pitch = it->pitch;
note.time = base + time;
note.volume = it->volume;
dst.push_back(note);
}
}
}
void MidiExport::ProcessBBNotes(MidiNoteVector &nv, int cutPos)
{
std::sort(nv.begin(), nv.end());
int cur = INT_MAX, next = INT_MAX;
for (auto it = nv.rbegin(); it != nv.rend(); ++it)
{
if (it->time < cur)
{
next = cur;
cur = it->time;
}
if (it->duration < 0)
{
it->duration = qMin(qMin(-it->duration, next - cur), cutPos - it->time);
}
}
}
void MidiExport::error()
{
//qDebug() << "MidiExport error: " << m_error ;
}
extern "C"
{
// necessary for getting instance out of shared lib
Plugin * PLUGIN_EXPORT lmms_plugin_main( Model *, void * _data )
{
return new MidiExport();
}
}
|