-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
253 lines (223 loc) · 12.5 KB
/
main.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
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
#include <loguru.hpp>
#include "HomeDir.h"
#include "UsbMidiInputPortListProvider.h"
#include "UsbMidiOutputPortListProvider.h"
#include "UsbMidiPortNotifier.h"
#include "Midi1Input.h"
#include "Midi1Output.h"
#include "MusicDeviceHolder.h"
#include "MidiOpenedInputPortsModel.h"
#include "MidiOpenedOutputPortsModel.h"
#include "Push2Pads.h"
#include "QmlSceneLoader.h"
#include "DeviceDescriptionLoader.h"
#include "SelectedSoundDevice.h"
#include "MidiRouter.h"
#include "MidiDumper.h"
#include "MidiDumperModel.h"
#include "MidiChannelMappingBridge.h"
#include "ParameterMapper.h"
#ifdef __SIMULATION__MODE__
#include "FpSimulation.h"
#endif
#include "Push2Device.h"
#include "Push2Topology.h"
#include "Push2LedOfWidget.h"
#include "FpButton.h"
#include "QuickViewOnFpRenderMedium.h"
#include "ValueCircle.h"
#include "MappingCurveDrawing.h"
#include "MappingCurveQt.h"
#include "Push2DeviceQtAdapter.h"
#include "Push2DeviceOverlay.h"
#include "MusicScalesModel.h"
#include "MusicScalesBaseNoteModel.h"
#include "Push2PadsBridge.h"
#include "SoundDeviceSelectionModel.h"
#include "VoiceSelectionModel.h"
#include "SoundDeviceAndVoiceSelection.h"
#include "ControllerDeviceAndPresetSelection.h"
#include "SoundDeviceParameter.h"
#include "ControllerDeviceParameter.h"
#include "TempoHandler.h"
#include "TempoHandlerBridge.h"
#include "TapTempoHandler.h"
#include "TapTempoHandlerBridge.h"
#include "TransportControl.h"
#include "TransportControlBridge.h"
#include "SettingsSaverBridge.h"
#include "sigwatch.h"
#include "ThreadedLoop.h"
#include <unistd.h>
#include <iostream>
#include <QGuiApplication>
#include <QTimer>
#include <cmath>
//TODO remove later
#include <QQmlContext>
/*
#include <csignal>
void signalHandler(int id)
{
printf("*********- SIGNAL Caught ********* %d\n", id);
}
*/
int main(int argc, char *argv[])
{
util::setAppName("push2Control");
loguru::init(argc, argv);
loguru::add_file("/tmp/push2Control.log", loguru::Truncate, loguru::Verbosity_MAX);
// std::signal(SIGINT, signalHandler); // overwrite loggers signal handler
if(!midi::PortNotifiers::instance().init())
{
return -1;
}
#ifdef __SIMULATION__MODE__
push2device::Push2Device push2Device("127.0.0.1:50051");
#else
push2device::Push2Device push2Device;
#endif
MusicDeviceHolder musicDeviceHolder;
auto pPush2Pads = std::make_unique<push2::Pads>(push2Device);
auto &push2Pads = *pPush2Pads.get();
musicDeviceHolder.addMidiInputMedium(std::move(pPush2Pads));
#ifdef __INSERT_DUMMY_MIDI_DEVICES__
musicDeviceHolder.insertMusicDeviceDummies();
#endif
midi::PortNotifiers::instance().update();
#ifndef __SIMULATION__MODE__
push2Device.init(1000);
#endif
util::ThreadedLoop midiOutThread([&musicDeviceHolder](){
auto start = std::chrono::high_resolution_clock::now();
for(auto& musicDevice : musicDeviceHolder.musicDevices){
musicDevice.second->processMidiOutBuffers();
}
auto end = std::chrono::high_resolution_clock::now();
const std::chrono::nanoseconds diff = end - start;
const std::chrono::nanoseconds PERIOD(1000000);
if(diff < PERIOD)
{
std::this_thread::sleep_for(PERIOD - diff);
}
});
TempoHandler tempoHandler(musicDeviceHolder.musicDevices);
TapTempoHandler tapTempoHandler(tempoHandler);
util::ThreadedLoop tempoThread([&tempoHandler](){
tempoHandler.nextTimeSlot();
std::this_thread::sleep_for(std::chrono::microseconds(10));
});
tempoHandler.start();
TransportControl transportControl(musicDeviceHolder.transportCmdDrains);
// ************************************************************************************************************
// ******************** PUSH2::QT *****************************************************************************
// ************************************************************************************************************
QGuiApplication app(argc, argv);
UnixSignalWatcher sigwatch;
sigwatch.watchForSignal(SIGINT);
sigwatch.watchForSignal(SIGTERM);
QObject::connect(&sigwatch, SIGNAL(unixSignal(int)), &app, SLOT(quit()));
qmlRegisterType<ValueCircle>("ValueCircle",1,0,"ValueCircle");
qmlRegisterType<push2::qt::MappingCurveDrawing>("MappingCurveDrawing",1,0,"MappingCurveDrawing");
qRegisterMetaType<push2::qt::MappingCurve>("MappingCurve");
app.setApplicationName("Push2");
const auto displayId = fp::Push2Topology::Display::Id::eDisplay;
//SelectedSoundDevice selectedSoundDevice;
QuickViewOnFpRenderMedium fboQuickView(push2Device.getRenderMedium(fp::Widget(displayId)));
push2::qt::Push2Device push2DeviceQtAdapter(push2Device, &app);
push2DeviceQtAdapter.setObjectName("push2DeviceQtAdapter");
fboQuickView.fboQuickView.rootContext()->setContextProperty("push2Device", &push2DeviceQtAdapter);
qmlRegisterType<push2::qt::Push2DeviceOverlay>("push2.enums", 1, 0, "Push2DeviceOverlay");
midi::Router midiRouter(musicDeviceHolder);
midi::Dumper midiDumper(musicDeviceHolder);
ParameterMapper parameterMapper(musicDeviceHolder);
push2::qt::MidiChannelMappingBridge midiChannelMappingBridge(push2Device, midiRouter);
push2::qt::MidiDumperModel midiDumperModel(midiDumper);
utils::SettingsFnCollection settingsFnCollection;
settingsFnCollection.addLoaderFn(std::bind(&TransportControl::load, &transportControl, "", "Settings.json", "transport_control"));
settingsFnCollection.addSaverFn(std::bind(&TransportControl::save, &transportControl, "", "Settings.json", "transport_control"));
settingsFnCollection.addLoaderFn(std::bind(&midi::Router::load, &midiRouter, "", "Settings.json", "midi_routing"));
settingsFnCollection.addSaverFn(std::bind(&midi::Router::save, &midiRouter, "", "Settings.json", "midi_routing"));
settingsFnCollection.addLoaderFn(std::bind(&ParameterMapper::load, ¶meterMapper, "", "Settings.json", "parameter_mapping"));
settingsFnCollection.addSaverFn(std::bind(&ParameterMapper::save, ¶meterMapper, "", "Settings.json", "parameter_mapping"));
settingsFnCollection.invokeLoaders();
push2::qt::MidiOpenedInputPortsModel openedInputPortsModel(musicDeviceHolder);
push2::qt::MidiOpenedOutputPortsModel openedOutputPortsModel(musicDeviceHolder, midiRouter);
fboQuickView.fboQuickView.rootContext()->setContextProperty("midiOpenedInputPortsModel", &openedInputPortsModel);
fboQuickView.fboQuickView.rootContext()->setContextProperty("midiOpenedOutputPortsModel", &openedOutputPortsModel);
fboQuickView.fboQuickView.rootContext()->setContextProperty("midiChannelMappingBridge", &midiChannelMappingBridge);
fboQuickView.fboQuickView.rootContext()->setContextProperty("midiDumperModel", &midiDumperModel);
push2::qt::SoundDeviceAndVoiceSelection soundDeviceAndVoiceSelection(musicDeviceHolder);
fboQuickView.fboQuickView.rootContext()->setContextProperty("soundDeviceAndVoiceSelection", &soundDeviceAndVoiceSelection);
push2::qt::ControllerDeviceAndPresetSelection controllerDeviceAndPresetSelection(musicDeviceHolder);
fboQuickView.fboQuickView.rootContext()->setContextProperty("controllerDeviceAndPresetSelection", &controllerDeviceAndPresetSelection);
push2::qt::SoundDeviceParameter soundDeviceParameter(musicDeviceHolder, parameterMapper);
fboQuickView.fboQuickView.rootContext()->setContextProperty("soundDeviceParameter", &soundDeviceParameter);
push2::qt::ControllerDeviceParameter controllerDeviceParameter(musicDeviceHolder, parameterMapper);
fboQuickView.fboQuickView.rootContext()->setContextProperty("controllerDeviceParameter", &controllerDeviceParameter);
push2::qt::MusicScalesModel musicScalesModel(push2Pads);
push2::qt::MusicScalesBaseNoteModel musicScalesBaseNoteModel;
push2::qt::PadsBridge push2PadsBridge(push2Pads);
push2::qt::TempoHandlerBridge tempoHandlerBridge(tempoHandler);
push2::qt::TapTempoHandlerBridge tapTempoHandlerBridge(tapTempoHandler);
push2::qt::TransportControlBridge transportControlBridge(musicDeviceHolder.transportCmdDrains, transportControl);
push2::qt::SettingsSaverBridge settingsSaverBridge(settingsFnCollection);
fboQuickView.fboQuickView.rootContext()->setContextProperty("musicScalesModel", &musicScalesModel);
fboQuickView.fboQuickView.rootContext()->setContextProperty("musicScalesBaseNoteModel", &musicScalesBaseNoteModel);
fboQuickView.fboQuickView.rootContext()->setContextProperty("push2Pads", &push2PadsBridge);
fboQuickView.fboQuickView.rootContext()->setContextProperty("tempoHandler", &tempoHandlerBridge);
fboQuickView.fboQuickView.rootContext()->setContextProperty("tapTempoHandler", &tapTempoHandlerBridge);
fboQuickView.fboQuickView.rootContext()->setContextProperty("transportControl", &transportControlBridge);
fboQuickView.fboQuickView.rootContext()->setContextProperty("settingsSaver", &settingsSaverBridge);
qmlRegisterUncreatableMetaObject(push2::qt::FpWidget::staticMetaObject, "push2.enums", 1, 0, "FpWidget", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::ButtonPressState::staticMetaObject, "push2.enums", 1, 0, "ButtonPressState", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::Button3dPressState::staticMetaObject, "push2.enums", 1, 0, "Button3dPressState", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::EncoderTouchState::staticMetaObject, "push2.enums", 1, 0, "EncoderTouchState", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::TouchSurfaceTouchState::staticMetaObject, "push2.enums", 1, 0, "TouchSurfaceTouchState", "Not creatable as it is an enum type");
qRegisterMetaType<push2::qt::FpWidget::Idx>("FpWidget::Idx");
qRegisterMetaType<push2::qt::ButtonPressState::Value>("ButtonPressState::Value");
qRegisterMetaType<push2::qt::Button3dPressState::Value>("Button3dPressState::Value");
qRegisterMetaType<push2::qt::EncoderTouchState::Value>("EncoderTouchState::Value");
qRegisterMetaType<push2::qt::TouchSurfaceTouchState::Value>("TouchSurfaceTouchState::Value");
qmlRegisterUncreatableMetaObject(push2::qt::Led::staticMetaObject, "push2.enums", 1, 0, "Led", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::Button3d::staticMetaObject, "push2.enums", 1, 0, "Button3d", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::Button::staticMetaObject, "push2.enums", 1, 0, "Button", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::Encoder::staticMetaObject, "push2.enums", 1, 0, "Encoder", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::TouchSurface::staticMetaObject, "push2.enums", 1, 0, "TouchSurface", "Not creatable as it is an enum type");
qRegisterMetaType<push2::qt::Button::Id>("Button::Id");
qRegisterMetaType<push2::qt::Button3d::Id>("Button3d::Id");
qRegisterMetaType<push2::qt::Encoder::Id>("Encoder::Id");
qRegisterMetaType<push2::qt::Led::Id>("Led::Id");
qRegisterMetaType<push2::qt::TouchSurface::Id>("TouchSurface::Id");
qmlRegisterUncreatableMetaObject(push2::qt::MusicNote::staticMetaObject, "push2.enums", 1, 0, "MusicNote", "Not creatable as it is an enum type");
qmlRegisterUncreatableMetaObject(push2::qt::MusicScales::staticMetaObject, "push2.enums", 1, 0, "MusicScales", "Not creatable as it is an enum type");
qRegisterMetaType<push2::qt::MusicNote::Value>("MusicNote::Value");
qRegisterMetaType<push2::qt::MusicScales::Value>("MusicScales::Value");
push2::QmlSceneLoader qmlSceneLoader(fboQuickView);
if(musicDeviceHolder.soundDevices.size() > 0)
{
soundDeviceAndVoiceSelection.setCurrentDeviceIndex(0);
}
if(musicDeviceHolder.controllerDevices.size() > 0)
{
controllerDeviceAndPresetSelection.setCurrentDeviceIndex(0);
}
#ifndef __SIMULATION__MODE__
QTimer displayRefreshTimer;
QObject::connect(&displayRefreshTimer, &QTimer::timeout, [&push2Device](){
push2Device.getRenderMedium(fp::Widget(displayId))->flushFrameBuffer();
});
displayRefreshTimer.start(900);
#endif
#ifdef __SIMULATION__MODE__
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&push2Device](){
push2Device.updateInputs();
});
timer.start(10);
#endif
push2Pads.updateLeds();
const auto appRet = app.exec();
settingsFnCollection.invokeSavers();
return appRet;
}