lmms
Cross-platform music production software
Loading...
Searching...
No Matches
MidiExport.h
Go to the documentation of this file.
1/*
2 * MidiExport.h - support for exporting MIDI files
3 *
4 * Copyright (c) 2015 Mohamed Abdel Maksoud <mohamed at amaksoud.com>
5 * Copyright (c) 2017 Hyunjin Song <tteu.ingog/at/gmail.com>
6 * Copyright (c) 2020 EmoonX
7 * Copyright (c) 2026 Dalton Messmer <messmer.dalton/at/gmail.com>
8 *
9 * This file is part of LMMS - https://lmms.io
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program (see COPYING); if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA.
25 *
26 */
27
28#ifndef LMMS_MIDI_EXPORT_H
29#define LMMS_MIDI_EXPORT_H
30
31#include "MidiFile.h"
32#include "DataFile.h"
33#include "ExportFilter.h"
34#include "Note.h"
35
36class QDomNode;
37
38namespace lmms
39{
40
43{
44private:
46 struct Note
47 {
49
51 std::uint8_t pitch = 0;
52
54 std::uint8_t volume = 0;
55
58
61
62 Type type = Type::Regular;
63
65 bool operator<(const Note& b) const noexcept
66 {
67 return time < b.time;
68 }
69 };
70
72 class Clip
73 {
74 private:
76 std::vector<Note> m_notes;
77
78 public:
80 void write(const QDomNode& root,
81 int basePitch, double baseVolume, tick_t baseTime);
82
84 void processPatternNotes(tick_t cutPos);
85
87 void writeToTrack(MidiFile::Track& midiTrack) const;
88
90 void writeToPattern(Clip& patternClip,
91 tick_t len, tick_t base, tick_t start, tick_t end);
92 };
93
95 std::uint8_t m_channel = 0;
96
99
101 std::vector<std::vector<std::pair<tick_t, tick_t>>> m_plists;
102
103public:
105 MidiExport();
106 ~MidiExport() override = default;
107
108 // Default Beat Length in ticks for step notes
109 // TODO: The beat length actually varies per note, however the method that
110 // calculates it (InstrumentTrack::beatLen) requires a NotePlayHandle to do
111 // so. While we don't figure out a way to hold the beat length of each note
112 // on its member variables, we will use a default value as a beat length that
113 // will be used as an upper limit of the midi note length. This doesn't worsen
114 // the current logic used for MidiExport because right now the beat length is
115 // not even considered during the generation of the MIDI.
116 static constexpr int DefaultBeatLength = 1500;
117
125 bool tryExport(const TrackContainer::TrackList& tracks,
126 const TrackContainer::TrackList& patternStoreTracks,
127 int tempo, int masterPitch, const std::filesystem::path& filePath) override;
128
129private:
131 void processTrack(Track& track, MidiFile::Track& midiTrack,
132 int tempo, int masterPitch, bool isPattern = false);
133
135 void writePatternClip(Clip& clip, const QDomElement& clipElem,
136 std::uint8_t patternIdx, MidiFile::Track& midiTrack);
137
139 void processPatternTrack(Track& track);
140
142 gui::PluginView* instantiateView(QWidget*) override { return nullptr; }
143};
144
145} // namespace lmms
146
147#endif // LMMS_MIDI_EXPORT_H
Definition DataFile.h:44
DataFile(const QString &fileName)
Definition DataFile.cpp:156
@ SongProject
Definition DataFile.h:52
ExportFilter(const Descriptor *_descriptor)
Definition ExportFilter.h:42
A clip of MIDI notes.
Definition MidiExport.h:73
void writeToTrack(MidiFile::Track &midiTrack) const
Add clip notes to MIDI file track.
Definition MidiExport.cpp:99
void processPatternNotes(tick_t cutPos)
Adjust special duration pattern clip notes by resizing them accordingly.
Definition MidiExport.cpp:108
std::vector< Note > m_notes
List of actual notes.
Definition MidiExport.h:76
void write(const QDomNode &root, int basePitch, double baseVolume, tick_t baseTime)
Append notes from root node to clip.
Definition MidiExport.cpp:69
void writeToPattern(Clip &patternClip, tick_t len, tick_t base, tick_t start, tick_t end)
Write sorted notes to a explicitly repeating pattern clip.
Definition MidiExport.cpp:132
~MidiExport() override=default
void processTrack(Track &track, MidiFile::Track &midiTrack, int tempo, int masterPitch, bool isPattern=false)
Process a given instrument track.
Definition MidiExport.cpp:202
MidiExport()
Explicit constructor for setting plugin descriptor.
Definition MidiExport.cpp:156
void writePatternClip(Clip &clip, const QDomElement &clipElem, std::uint8_t patternIdx, MidiFile::Track &midiTrack)
Build a repeating clip from a normal one and write to MIDI track.
Definition MidiExport.cpp:282
bool tryExport(const TrackContainer::TrackList &tracks, const TrackContainer::TrackList &patternStoreTracks, int tempo, int masterPitch, const std::filesystem::path &filePath) override
Export tracks from a project to a .mid extension MIDI file.
Definition MidiExport.cpp:160
DataFile m_dataFile
DataFile to be used by Qt elements.
Definition MidiExport.h:98
static constexpr int DefaultBeatLength
Definition MidiExport.h:116
gui::PluginView * instantiateView(QWidget *) override
Necessary for lmms_plugin_main().
Definition MidiExport.h:142
std::vector< std::vector< std::pair< tick_t, tick_t > > > m_plists
Matrix containing (start, end) pairs for pattern objects.
Definition MidiExport.h:101
void processPatternTrack(Track &track)
Process a given pattern track.
Definition MidiExport.cpp:325
std::uint8_t m_channel
Current (incremental) track channel number for non drum tracks.
Definition MidiExport.h:95
Represents a MIDI track.
Definition MidiFile.h:182
Type
Definition Note.h:119
std::vector< Track * > TrackList
Definition TrackContainer.h:52
Base-class for all tracks.
Definition Track.h:67
Definition PluginView.h:36
Definition RemotePluginBase.cpp:34
std::int32_t tick_t
Definition LmmsTypes.h:35
A single MIDI note.
Definition MidiExport.h:47
std::uint8_t pitch
The pitch (tone), which can be lower or higher.
Definition MidiExport.h:51
tick_t duration
For how long the note plays.
Definition MidiExport.h:60
Type type
Definition MidiExport.h:62
::lmms::Note::Type Type
Definition MidiExport.h:48
bool operator<(const Note &b) const noexcept
Sort notes by time.
Definition MidiExport.h:65
tick_t time
Absolute time (from song start) when the note starts playing.
Definition MidiExport.h:57
std::uint8_t volume
Volume (loudness).
Definition MidiExport.h:54