-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmidi_exporter.hpp
More file actions
95 lines (78 loc) · 3.88 KB
/
midi_exporter.hpp
File metadata and controls
95 lines (78 loc) · 3.88 KB
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
// This file is part of Noteahead.
// Copyright (C) 2025 Jussi Lind <jussi.lind@iki.fi>
//
// Noteahead 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 3 of the License, or
// (at your option) any later version.
// Noteahead 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 Noteahead. If not, see <http://www.gnu.org/licenses/>.
#ifndef MIDI_EXPORTER_HPP
#define MIDI_EXPORTER_HPP
#include <cstdint>
#include <limits>
#include <map>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
namespace noteahead {
class AutomationService;
class Event;
class Instrument;
class MidiCcData;
class MixerService;
class NoteData;
class PitchBendData;
class SideChainService;
class Song;
class MidiExporter {
public:
using AutomationServiceS = std::shared_ptr<AutomationService>;
using ByteVector = std::vector<char>;
using EventS = std::shared_ptr<Event>;
using InstrumentS = std::shared_ptr<Instrument>;
using MixerServiceS = std::shared_ptr<MixerService>;
using SideChainServiceS = std::shared_ptr<SideChainService>;
using SongS = std::shared_ptr<Song>;
using SongW = std::weak_ptr<Song>;
struct ActiveTracks {
std::map<size_t, InstrumentS> trackToInstrument;
std::map<size_t, std::string> trackToPortName;
std::map<std::string, uint8_t> portNameToIndex;
};
MidiExporter(AutomationServiceS automationService, MixerServiceS mixerService, SideChainServiceS sidechainService);
void exportTo(std::string fileName, SongW songW, size_t startPosition = 0, size_t endPosition = std::numeric_limits<size_t>::max()) const;
private:
struct TrackProcessingState {
std::map<size_t, ByteVector> allTracksData;
std::map<size_t, uint32_t> lastTicks;
std::map<size_t, uint8_t> trackToChannelMap;
};
void writeMidiHeader(std::ostream & out, const SongS & song, size_t numNoteTracks) const;
ActiveTracks discoverActiveTracks(const SongS& song, const std::vector<EventS> & events) const;
std::vector<EventS> filterEvents(const std::vector<EventS> & events, MixerServiceS mixerService) const;
std::map<size_t, ByteVector> buildTrackData(const SongS& song, const std::vector<EventS> & events, const ActiveTracks & activeTracks) const;
TrackProcessingState initializeTracks(const SongS& song, const ActiveTracks & activeTracks) const;
TrackProcessingState processEvents(TrackProcessingState state, const std::vector<EventS> & events) const;
std::map<size_t, ByteVector> finalizeTracks(TrackProcessingState state) const;
ByteVector initializeTrack(const SongS& song, size_t trackIndex, const ActiveTracks & activeTracks) const;
void writeNoteOnEvent(ByteVector & data, uint8_t channel, const NoteData & noteData) const;
void writeNoteOffEvent(ByteVector & data, uint8_t channel, const NoteData & noteData) const;
void writeControlChangeEvent(ByteVector & data, uint8_t channel, const MidiCcData & ccData) const;
void writePitchBendEvent(ByteVector & data, uint8_t channel, const PitchBendData & pitchBendData) const;
void writeProgramChangeEvent(ByteVector & data, uint8_t channel, uint8_t patch) const;
void writeTempoTrack(std::ostream& out, const SongS& song) const;
void writeNoteTracks(std::ostream & out, const std::map<size_t, ByteVector> & allTracksData) const;
static void sortEvents(std::vector<EventS> & events);
AutomationServiceS m_automationService;
MixerServiceS m_mixerService;
SideChainServiceS m_sideChainService;
};
} // namespace noteahead
#endif // MIDI_EXPORTER_HPP