mirror of
https://github.com/LMMS/lmms.git
synced 2026-01-16 18:38:15 -05:00
* clang-tidy: Apply cppcoreguidelines-init-variables everywhere (treating NaNs as zeros) * Initialize msec and tick outside switch * Update plugins/Vestige/Vestige.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update plugins/Vestige/Vestige.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update plugins/Vestige/Vestige.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update plugins/VstEffect/VstEffectControls.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update plugins/VstEffect/VstEffectControls.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update plugins/VstEffect/VstEffectControls.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Use initialization with = * Use tabs * Use static_cast * Update DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Do not use tabs for alignment in src/core/DrumSynth.cpp Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com> * Move x variable inside loop * Use ternary operator for b variable * Revert "Use tabs" This reverts commit 07afd8a83f58b539c3673310b2aad4b63c9198a0. * Remove unnecessary variables in XpressiveView * Simplify initialization in Plugin * Combine declaration and initialization in EqCurve * Combine declaration and initialization in Song * Combine declaration and initialization in AudioAlsa * Combine declaration and initialization in EqCurve (again) * Missed some * Undo changes made to non-LMMS files * Undo indentation changes in SidInstrument.cpp * Combine declaration with assignment in IoHelper * Combine declaration with assignment using auto in Carla * Combine declaration with assignment * Combine declaration with assignment in BasicFilters * Simplify assignments in AudioFileProcessorWaveView::zoom * Simplify out sample variable in BitInvader * Remove sampleLength variable in DelayEffect * Move gain variable in DynamicsProcessor * Combine peak variable declaration with assignment in EqSpectrumView * Move left/right lfo variables in for loop in FlangerEffect * Use ternary operator for group variable in LadspaControlDialog * Combine declaration with assignment in Lb302 * Combine declaration with assignment in MidiExport * Combine declaration with assignment in MidiFile * Combine declaration with assignment in MidiImport * Use ternary operator for vel_adjusted variable in OpulenZ * Move tmpL and dcblkL variables in for loop in ReverbSC * Combine declaration with initialization in SlicerT * Combine declaration with assignment in SaSpectrumView * Combine declaration with assignment in SaWaterfallView * Combine declaration with assignment in StereoEnhancerEffect * Combine declaration with assignment in VibratingString * Combine declaration with assignment in VstEffectControls * Combine declaration with assignment in Xpressive * Combine declaration with assignment in AutomatableModel * Combine declaration with assignment in AutomationClip * Move sample variable in for loop in BandLimitedWave * Combine declaration with assignment in DataFile * Combine declaration with assignment in DrumSynth * Combine declaration with assignment in Effect * Remove redundant assignment to nphsLeft in InstrumentPlayHandle * Combine declaration with assignment in LadspaManager * Combine declaration with assignment in LinkedModelGroups * Combine declaration with assignment in MemoryHelper * Combine declaration with assignment in AudioAlsa * Combine declaration with assignment in AudioFileOgg * Combine declaration with assignment in AudioPortAudio * Combine declaration with assignment in AudioSoundIo * Combine declaration with assignment in Lv2Evbuf * Combine declaration with assignment in Lv2Proc * Combine declaration with assignment in main * Combine declaration with assignment in MidiAlsaRaw * Combine declaration with assignment in MidiAlsaSeq * Combine declaration with assignment in MidiController * Combine declaration with assignment in MidiJack * Combine declaration with assignment in MidiSndio * Combine declaration with assignment in ControlLayout * Combine declaration with assignment in MainWindow * Combine declaration with assignment in ProjectNotes * Use ternary operator for nextValue variable in AutomationClipView * Combine declaration with assignment in AutomationEditor * Move length variable in for-loop in PianoRoll * Combine declaration with assignment in ControllerConnectionDialog * Combine declaration with assignment in Graph * Combine declaration with assignment in LcdFloatSpinBox * Combine declaration with assignment in TimeDisplayWidget * Remove currentNote variable in InstrumentTrack * Combine declaration with assignment in DrumSynth (again) * Use ternary operator for factor variable in BitInvader * Use ternary operator for highestBandwich variable in EqCurve Bandwich? * Move sum variable into for loop in Graph * Fix format in MidiSndio * Fixup a few more * Cleanup error variables * Use ternary operators and combine declaration with initialization * Combine declaration with initialization * Update plugins/LadspaEffect/LadspaControlDialog.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update plugins/OpulenZ/OpulenZ.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update plugins/SpectrumAnalyzer/SaProcessor.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/midi/MidiAlsaRaw.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/gui/MainWindow.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/gui/clips/AutomationClipView.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/gui/editors/AutomationEditor.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/gui/widgets/Fader.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Move static_cast conversion into separate variable * Use real index when interpolating * Remove empty line * Make helpBtn a private member * Move controller type into separate variable * Fix format of DrumSynth::waveform function * Use tabs and static_cast * Remove redundant if branch * Refactor using static_cast/reinterpret_cast * Add std namespace prefix * Store repeated conditional into boolean variable * Cast to int before assigning to m_currentLength * Rename note_frames to noteFrames * Update src/core/Controller.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/core/DrumSynth.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Update src/gui/widgets/Graph.cpp Co-authored-by: Kevin Zander <veratil@gmail.com> * Revert changes that initialized variables redudantly For situations where the initialization is more complex or passed into a function by a pointer, we dont need to do initialization ourselves since it is already done for us, just in a different way. * Remove redundant err variable * Remove explicit check of err variable * Clean up changes and address review * Do not initialize to 0/nullptr when not needed * Wrap condition in parentheses for readability --------- Co-authored-by: Kevin Zander <veratil@gmail.com> Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>
362 lines
9.0 KiB
C++
362 lines
9.0 KiB
C++
/*
|
|
* 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 "MidiExport.h"
|
|
|
|
#include "Engine.h"
|
|
#include "TrackContainer.h"
|
|
#include "DataFile.h"
|
|
#include "InstrumentTrack.h"
|
|
#include "LocaleHelper.h"
|
|
#include "PatternTrack.h"
|
|
|
|
#include "plugin_export.h"
|
|
|
|
namespace lmms
|
|
{
|
|
|
|
|
|
extern "C"
|
|
{
|
|
|
|
Plugin::Descriptor PLUGIN_EXPORT midiexport_plugin_descriptor =
|
|
{
|
|
LMMS_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::Type::ExportFilter,
|
|
nullptr,
|
|
nullptr,
|
|
nullptr,
|
|
} ;
|
|
|
|
}
|
|
|
|
|
|
MidiExport::MidiExport() : ExportFilter( &midiexport_plugin_descriptor)
|
|
{
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MidiExport::tryExport(const TrackContainer::TrackList &tracks,
|
|
const TrackContainer::TrackList &patternStoreTracks,
|
|
int tempo, int masterPitch, const QString &filename)
|
|
{
|
|
QFile f(filename);
|
|
f.open(QIODevice::WriteOnly);
|
|
QDataStream midiout(&f);
|
|
|
|
QDomElement element;
|
|
|
|
|
|
int nTracks = 0;
|
|
auto buffer = std::array<uint8_t, BUFFER_SIZE>{};
|
|
|
|
for (const Track* track : tracks) if (track->type() == Track::Type::Instrument) nTracks++;
|
|
for (const Track* track : patternStoreTracks) if (track->type() == Track::Type::Instrument) nTracks++;
|
|
|
|
// midi header
|
|
MidiFile::MIDIHeader header(nTracks);
|
|
uint32_t size = header.writeToBuffer(buffer.data());
|
|
midiout.writeRawData((char *)buffer.data(), size);
|
|
|
|
std::vector<std::vector<std::pair<int,int>>> plists;
|
|
|
|
// midi tracks
|
|
for (Track* track : tracks)
|
|
{
|
|
DataFile dataFile(DataFile::Type::SongProject);
|
|
MTrack mtrack;
|
|
|
|
if (track->type() == Track::Type::Instrument)
|
|
{
|
|
|
|
mtrack.addName(track->name().toStdString(), 0);
|
|
//mtrack.addProgramChange(0, 0);
|
|
mtrack.addTempo(tempo, 0);
|
|
|
|
auto 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 midiClip;
|
|
|
|
for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
|
|
{
|
|
|
|
if (n.nodeName() == "instrumenttrack")
|
|
{
|
|
QDomElement it = n.toElement();
|
|
base_pitch = (69 - it.attribute("basenote", "69").toInt());
|
|
if (it.attribute("usemasterpitch", "1").toInt())
|
|
{
|
|
base_pitch += masterPitch;
|
|
}
|
|
base_volume = LocaleHelper::toDouble(it.attribute("volume", "100"))/100.0;
|
|
}
|
|
|
|
if (n.nodeName() == "midiclip")
|
|
{
|
|
base_time = n.toElement().attribute("pos", "0").toInt();
|
|
writeMidiClip(midiClip, n, base_pitch, base_volume, base_time);
|
|
}
|
|
|
|
}
|
|
processPatternNotes(midiClip, INT_MAX);
|
|
writeMidiClipToTrack(mtrack, midiClip);
|
|
size = mtrack.writeToBuffer(buffer.data());
|
|
midiout.writeRawData((char *)buffer.data(), size);
|
|
}
|
|
|
|
if (track->type() == Track::Type::Pattern)
|
|
{
|
|
auto patternTrack = dynamic_cast<PatternTrack*>(track);
|
|
element = patternTrack->saveState(dataFile, dataFile.content());
|
|
|
|
std::vector<std::pair<int,int>> plist;
|
|
for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
|
|
{
|
|
|
|
if (n.nodeName() == "patternclip")
|
|
{
|
|
QDomElement it = n.toElement();
|
|
int pos = it.attribute("pos", "0").toInt();
|
|
int len = it.attribute("len", "0").toInt();
|
|
plist.emplace_back(pos, pos+len);
|
|
}
|
|
}
|
|
std::sort(plist.begin(), plist.end());
|
|
plists.push_back(plist);
|
|
|
|
}
|
|
} // for each track
|
|
|
|
// for each instrument in the pattern editor
|
|
for (Track* track : patternStoreTracks)
|
|
{
|
|
DataFile dataFile(DataFile::Type::SongProject);
|
|
MTrack mtrack;
|
|
|
|
// begin at the first pattern track (first pattern)
|
|
auto itr = plists.begin();
|
|
|
|
std::vector<std::pair<int,int>> st;
|
|
|
|
if (track->type() != Track::Type::Instrument) continue;
|
|
|
|
mtrack.addName(track->name().toStdString(), 0);
|
|
//mtrack.addProgramChange(0, 0);
|
|
mtrack.addTempo(tempo, 0);
|
|
|
|
auto instTrack = dynamic_cast<InstrumentTrack *>(track);
|
|
element = instTrack->saveState(dataFile, dataFile.content());
|
|
|
|
int base_pitch = 0;
|
|
double base_volume = 1.0;
|
|
|
|
// for each pattern in the pattern editor
|
|
for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
|
|
{
|
|
if (n.nodeName() == "instrumenttrack")
|
|
{
|
|
QDomElement it = n.toElement();
|
|
base_pitch = (69 - it.attribute("basenote", "69").toInt());
|
|
if (it.attribute("usemasterpitch", "1").toInt())
|
|
{
|
|
base_pitch += masterPitch;
|
|
}
|
|
base_volume = LocaleHelper::toDouble(it.attribute("volume", "100")) / 100.0;
|
|
}
|
|
|
|
if (n.nodeName() == "midiclip")
|
|
{
|
|
std::vector<std::pair<int,int>> &plist = *itr;
|
|
|
|
MidiNoteVector nv, midiClip;
|
|
writeMidiClip(midiClip, n, base_pitch, base_volume, 0);
|
|
|
|
|
|
// FIXME better variable names and comments
|
|
int pos = 0;
|
|
int len = n.toElement().attribute("steps", "1").toInt() * 12;
|
|
|
|
// for each pattern clip of the current pattern track (in song editor)
|
|
for (const auto& position : plist)
|
|
{
|
|
const auto& [start, end] = position;
|
|
while (!st.empty() && st.back().second <= start)
|
|
{
|
|
writePatternClip(midiClip, nv, len, st.back().first, pos, st.back().second);
|
|
pos = st.back().second;
|
|
st.pop_back();
|
|
}
|
|
|
|
if (!st.empty() && st.back().second <= end)
|
|
{
|
|
writePatternClip(midiClip, nv, len, st.back().first, pos, start);
|
|
pos = start;
|
|
while (!st.empty() && st.back().second <= end)
|
|
{
|
|
st.pop_back();
|
|
}
|
|
}
|
|
|
|
st.push_back(position);
|
|
pos = start;
|
|
}
|
|
|
|
while (!st.empty())
|
|
{
|
|
writePatternClip(midiClip, nv, len, st.back().first, pos, st.back().second);
|
|
pos = st.back().second;
|
|
st.pop_back();
|
|
}
|
|
|
|
processPatternNotes(nv, pos);
|
|
writeMidiClipToTrack(mtrack, nv);
|
|
|
|
// next pattern track
|
|
++itr;
|
|
}
|
|
}
|
|
size = mtrack.writeToBuffer(buffer.data());
|
|
midiout.writeRawData((char *)buffer.data(), size);
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiExport::writeMidiClip(MidiNoteVector &midiClip, const 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" mixch="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();
|
|
mnote.type = static_cast<Note::Type>(note.attribute("type", "0").toInt());
|
|
midiClip.push_back(mnote);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MidiExport::writeMidiClipToTrack(MTrack &mtrack, MidiNoteVector &nv)
|
|
{
|
|
for (const auto& note : nv)
|
|
{
|
|
mtrack.addNote(note.pitch, note.volume, note.time / 48.0, note.duration / 48.0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MidiExport::writePatternClip(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 (const auto& srcNote : src)
|
|
{
|
|
for (int time = srcNote.time + ceil((start - srcNote.time) / len) * len; time < end; time += len)
|
|
{
|
|
MidiNote note;
|
|
note.duration = srcNote.duration;
|
|
note.pitch = srcNote.pitch;
|
|
note.time = base + time;
|
|
note.volume = srcNote.volume;
|
|
note.type = srcNote.type;
|
|
dst.push_back(note);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MidiExport::processPatternNotes(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->type == Note::Type::Step)
|
|
{
|
|
it->duration = qMin(qMin(DefaultBeatLength, next - cur), cutPos - it->time);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MidiExport::error()
|
|
{
|
|
//qDebug() << "MidiExport error: " << m_error ;
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
{
|
|
|
|
// necessary for getting instance out of shared lib
|
|
PLUGIN_EXPORT Plugin * lmms_plugin_main( Model *, void * _data )
|
|
{
|
|
return new MidiExport();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} // namespace lmms
|