-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMidiRecorder.cpp
125 lines (114 loc) · 4.81 KB
/
MidiRecorder.cpp
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
/*
* MIT License
*
* Copyright (c) 2019-2023 Christof Ruch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "MidiRecorder.h"
#include "Settings.h"
#include "StreamLogger.h"
MidiRecorder::MidiRecorder(juce::AudioDeviceManager& deviceManager) : isRecording_(false), deviceManager_(deviceManager)
{
// Just enable *all* Midi devices. Not sure if that's smart in the long run, but hey...
auto devices = juce::MidiInput::getAvailableDevices();
for (int i = 0; i < devices.size(); i++) {
enableMidiInput(devices[i]);
}
clocker_ = std::make_shared<MidiClocker>();
}
void MidiRecorder::saveToFile(juce::String filename)
{
juce::Time now = juce::Time::getCurrentTime();
juce::File directory = Settings::instance().getSessionStorageDir();
juce::File midFile = directory.getNonexistentChildFile(juce::String(filename) + now.formatted("-%Y-%m-%d-%H-%M-%S"), ".mid", false);
juce::MidiFile midiFile;
for (auto track : recorded_) {
if (track.second.getNumEvents() > 0) {
track.second.updateMatchedPairs();
midiFile.addTrack(track.second);
}
}
juce::FileOutputStream outputStream(midFile, 16384);
// midiFile.setSmpteTimeFormat(25, 400); // Tenths of milliseconds at 25 fps
midiFile.setTicksPerQuarterNote(96);
midiFile.writeTo(outputStream);
}
std::weak_ptr<MidiClocker> MidiRecorder::getClocker()
{
return clocker_;
}
MidiRecorder::~MidiRecorder()
{
// Remove all registered callbacks
for (auto callback : callbacks_) {
deviceManager_.removeMidiInputDeviceCallback(callback.first, callback.second);
}
saveToFile("sessionMidi");
}
void MidiRecorder::startRecording()
{
recordingStartTime_ = juce::Time::getMillisecondCounterHiRes() / 1000.0;
isRecording_ = true;
}
void MidiRecorder::handleIncomingMidiMessage(juce::MidiInput* source, const juce::MidiMessage& message)
{
if (!message.isActiveSense() && !message.isMidiClock()) {
StreamLogger::instance() << message.getTimeStamp() << " " << message.getDescription() << std::endl;
juce::MidiMessage relativeTimestampMessage = message;
double deltaSeconds = message.getTimeStamp() - recordingStartTime_;
double deltaTicksPerBeat = deltaSeconds * 120 * 96 / 60; // 96 Ticks per quarter note at 120 bpm
relativeTimestampMessage.setTimeStamp(deltaTicksPerBeat);
recorded_[source->getName()].addEvent(relativeTimestampMessage);
}
else if (message.isMidiClock()) {
if (clocker_) {
clocker_->processClockMessage(source->getName(), message);
}
}
}
void MidiRecorder::handlePartialSysexMessage(juce::MidiInput* source, const juce::uint8* messageData, int numBytesSoFar, double timestamp)
{
// What to do with this one?
ignoreUnused(source, messageData, numBytesSoFar, timestamp);
}
void MidiRecorder::enableMidiInput(juce::MidiDeviceInfo device)
{
// Only enable and register once
if (!deviceManager_.isMidiInputDeviceEnabled(device.identifier)) {
deviceManager_.setMidiInputDeviceEnabled(device.identifier, true);
}
if (callbacks_.find(device.identifier) == callbacks_.end()) {
deviceManager_.addMidiInputDeviceCallback(device.identifier, this);
callbacks_[device.identifier] = this;
}
if (recorded_.find(device.identifier) == recorded_.end()) {
recorded_[device.identifier] = juce::MidiMessageSequence();
}
}
void MidiRecorder::disableMidiInput(juce::MidiDeviceInfo input)
{
if (deviceManager_.isMidiInputDeviceEnabled(input.identifier)) {
deviceManager_.setMidiInputDeviceEnabled(input.identifier, false);
}
if (callbacks_.find(input.identifier) == callbacks_.end()) {
deviceManager_.removeMidiInputDeviceCallback(input.identifier, callbacks_[input.identifier]);
callbacks_.erase(input.identifier);
}
}