1
1

drumduino.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. #include "stdafx.h"
  2. #include "drumduino.h"
  3. #include "porttab.h"
  4. #include "channel.h"
  5. #include "curve.h"
  6. size_t mapChannels(size_t channel)
  7. {
  8. size_t port = channel / CHAN_CNT;
  9. size_t chan = channel % CHAN_CNT;
  10. const size_t pinMapping[8] = {2, 4, 1, 6, 0, 7, 3, 5};
  11. return port * CHAN_CNT + pinMapping[chan];
  12. }
  13. bool readNextFrame(std::shared_ptr<Serial>& serial, DrumduinoProc& proc)
  14. {
  15. AGAIN:
  16. auto available = serial->available();
  17. if(available < sizeof(byte) + sizeof(byte) + sizeof(unsigned long) + PORT_CNT * CHAN_CNT) {
  18. return false;
  19. }
  20. byte sentinel;
  21. serial->readBytes(&sentinel, 1);
  22. if(sentinel != 0xf0) {
  23. goto AGAIN;
  24. }
  25. byte manufacturer;
  26. serial->readBytes(&manufacturer, 1);
  27. unsigned long time;
  28. serial->readBytes((byte*)&time, sizeof(unsigned long));
  29. auto& frame = proc.frameBuffer[proc.frameCounter % BufferSize];
  30. serial->readBytes(frame.data(), frame.size());
  31. return true;
  32. }
  33. void sendSysexPrescalerThrottle(std::shared_ptr<Serial>& serial, byte prescaler, byte throttle)
  34. {
  35. byte msg[] = {0xf0, 42, prescaler, throttle, 0xF7};
  36. serial->write(msg, sizeof(msg));
  37. }
  38. void midiNoteOn(std::shared_ptr<MidiOut>& midiOut, byte channel, byte note, byte velocity)
  39. {
  40. byte data[] = {0x90 | channel, 0x7f & note , 0x7f & velocity };
  41. std::vector<byte> message(sizeof(data));
  42. memcpy(message.data(), data, message.size());
  43. midiOut->send(message);
  44. }
  45. void processFrame(std::shared_ptr<MidiOut>& midiOut, DrumduinoProc& proc, const Settings& settings)
  46. {
  47. const auto& lastFrame = proc.frameBuffer[(proc.frameCounter - 1) % BufferSize];
  48. const auto& currentFrame = proc.frameBuffer[proc.frameCounter % BufferSize];
  49. for(auto channel = 0; channel < PORT_CNT * CHAN_CNT; ++channel) {
  50. const auto& lastValue = lastFrame[mapChannels(channel)];
  51. const auto& currentValue = currentFrame[mapChannels(channel)];
  52. auto& state = proc.states[channel];
  53. auto& triggerFrame = proc.triggers[channel];
  54. auto& maxValue = proc.maxs[channel];
  55. const auto& channelSettings = settings.channelSettings[channel];
  56. switch(channelSettings.type) {
  57. case TypePiezo: {
  58. switch(state) {
  59. // In this state we wait for a signal to trigger
  60. case StateAwait: {
  61. STATE_AGAIN:
  62. if(currentValue < lastValue + channelSettings.thresold) {
  63. break;
  64. }
  65. state = StateScan;
  66. triggerFrame = proc.frameCounter;
  67. maxValue = currentValue;
  68. //fallthrough
  69. }
  70. // In this state we measure the value for the given time period to get the max value
  71. case StateScan: {
  72. if(proc.frameCounter < triggerFrame + channelSettings.scanTime) {
  73. maxValue = std::max(currentValue, maxValue);
  74. break;
  75. }
  76. midiNoteOn(midiOut, settings.midiChannel, channelSettings.note, maxValue);
  77. state = StateMask;
  78. //fallthrough
  79. }
  80. // In this state we do nothing to prevent retriggering
  81. case StateMask: {
  82. if(proc.frameCounter < triggerFrame + channelSettings.scanTime + channelSettings.maskTime) {
  83. break;
  84. }
  85. state = StateAwait;
  86. goto STATE_AGAIN;
  87. }
  88. default: {
  89. throw std::exception("not a valid state!");
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. Drumduino::Drumduino(QWidget* parent)
  97. : QMainWindow(parent)
  98. {
  99. ui.setupUi(this);
  100. ui.cbPrescaler->setCurrentIndex(_settings.prescaler);
  101. ui.sbThrottle->setValue(_settings.throttle);
  102. connect(ui.cbPrescaler, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {
  103. _settings.prescaler = index;
  104. sendSysexPrescalerThrottle(_serial, _settings.prescaler, _settings.throttle);
  105. });
  106. connect(ui.sbThrottle, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this](int value) {
  107. _settings.throttle = value;
  108. sendSysexPrescalerThrottle(_serial, _settings.prescaler, _settings.throttle);
  109. });
  110. // Setup Channel Settings
  111. for(auto channel = 0; channel < PORT_CNT * CHAN_CNT; ++channel) {
  112. _settings.channelSettings[channel].note = channel;
  113. }
  114. // Setup Channels
  115. for(auto port = 0; port < PORT_CNT; ++port) {
  116. auto wgtPort = ui.tabWidget->widget(ui.tabWidget->addTab(new PortTab(), "Port " + QString::number(port)));
  117. for(auto pin = 0; pin < CHAN_CNT; ++pin) {
  118. auto channel = port * CHAN_CNT + pin;
  119. auto wgtChannel = new Channel(channel, _settings.channelSettings[channel], wgtPort);
  120. wgtPort->layout()->addWidget(wgtChannel);
  121. _channels[channel] = wgtChannel;
  122. for(auto dial : wgtChannel->findChildren<QDial*>()) {
  123. dial->installEventFilter(this);
  124. }
  125. }
  126. }
  127. // action Save
  128. connect(ui.actionSave, &QAction::triggered, [this]() {
  129. auto fileName = QFileDialog::getSaveFileName(this, "Save", 0, tr("drumduino (*.edrum)"));
  130. QFile file(fileName);
  131. file.open(QIODevice::WriteOnly);
  132. file.write((const char*)&_settings, sizeof(_settings));
  133. file.close();
  134. });
  135. // action Load
  136. connect(ui.actionLoad, &QAction::triggered, [this]() {
  137. auto fileName = QFileDialog::getOpenFileName(this, "Open", 0, tr("drumduino (*.edrum)"));
  138. QFile file(fileName);
  139. file.open(QIODevice::ReadOnly);
  140. file.read((char*)&_settings, sizeof(_settings));
  141. file.close();
  142. for(auto& channel : _channels) {
  143. channel->update();
  144. }
  145. });
  146. _serial = std::make_shared<Serial>(L"COM3", 115200);
  147. _midiOut = std::make_shared<MidiOut>(1);
  148. _drumduinoThread = new DrumduinoThread(this, [this]() {
  149. if(readNextFrame(_serial, _proc)) {
  150. processFrame(_midiOut, _proc, _settings);
  151. #if 1
  152. _proc.stateBuffer[_proc.frameCounter % BufferSize] = _proc.states;
  153. #endif
  154. ++_proc.frameCounter;
  155. }
  156. });
  157. _drumduinoThread->start();
  158. #if 1
  159. {
  160. std::array<QCustomPlot*, PORT_CNT* CHAN_CNT> plots;
  161. for(auto port = 0; port < PORT_CNT; ++port) {
  162. auto wgtPort = ui.tabWidget->widget(ui.tabWidget->addTab(new PortTab(), "Graph_Port " + QString::number(port)));
  163. auto table = new QTableWidget(CHAN_CNT, 1, wgtPort);
  164. table->horizontalHeader()->setStretchLastSection(true);
  165. table->verticalHeader()->setMinimumHeight(100);
  166. wgtPort->layout()->addWidget(table);
  167. for(auto pin = 0; pin < CHAN_CNT; ++pin) {
  168. auto channel = port * CHAN_CNT + pin;
  169. auto wgtPlot = new QCustomPlot(table);
  170. wgtPlot->addGraph();
  171. table->setRowHeight(pin, 127);
  172. table->setCellWidget(pin, 0, wgtPlot);
  173. wgtPlot->xAxis->setRange(0, BufferSize);
  174. wgtPlot->yAxis->setRange(0, 127);
  175. wgtPlot->yAxis2->setRange(0, 2);
  176. wgtPlot->yAxis2->setVisible(true);
  177. auto stateGraph = wgtPlot->addGraph(wgtPlot->xAxis, wgtPlot->yAxis2);
  178. stateGraph->setPen(QPen(Qt::red));
  179. stateGraph->setLineStyle(QCPGraph::LineStyle::lsStepLeft);
  180. plots[port * CHAN_CNT + pin] = wgtPlot;
  181. }
  182. }
  183. QTimer* timer = new QTimer(this);
  184. connect(timer, &QTimer::timeout, [this, plots]() {
  185. auto currentIndex = _proc.frameCounter % BufferSize;
  186. QVector<qreal> x(BufferSize);
  187. QVector<qreal> y(BufferSize);
  188. QVector<qreal> s(BufferSize);
  189. for(auto i = 0; i < BufferSize; ++i) {
  190. x[i] = i;
  191. }
  192. for(auto i = 0; i < PORT_CNT * CHAN_CNT; ++i) {
  193. if(plots[i]->isVisible()) {
  194. auto channel = mapChannels(i);
  195. plots[i]->xAxis->setRange(x.front(), x.back());
  196. for(auto k = 0; k < BufferSize; ++k) {
  197. y[k] = _proc.frameBuffer[k][channel];
  198. s[k] = _proc.stateBuffer[k][i];
  199. }
  200. plots[i]->graph(0)->setData(x, y);
  201. plots[i]->graph(1)->setData(x, s);
  202. plots[i]->replot();
  203. }
  204. }
  205. });
  206. timer->start(1000 / 12);
  207. }
  208. #endif
  209. #if 0
  210. return;
  211. _startTime = QDateTime::currentMSecsSinceEpoch();
  212. for(auto i = 0; i < 5; ++i) {
  213. _settings.channelSettings[0].type = TypePiezo;
  214. }
  215. for(auto i = 0; i < PORT_CNT; ++i) {
  216. ui.tabWidget->addTab(new PortTab(), "Port_" + QString::number(i));
  217. auto tab = ui.tabWidget->widget(i);
  218. auto table = tab->findChild<QTableWidget*>("tableWidget");
  219. table->setRowCount(8);
  220. table->setColumnCount(9);
  221. QStringList headers;
  222. headers << "type" << "note" << "thresold" << "scanTime" << "maskTime" << "CurveType" << "CurveValue" << "CurveForm" << "Graph";
  223. table->setHorizontalHeaderLabels(headers);
  224. }
  225. for(auto channel = 0; channel < PORT_CNT * CHAN_CNT; ++channel) {
  226. _settings.channelSettings[channel].note = 35 + channel;
  227. auto tab = ui.tabWidget->widget(channel / 8);
  228. auto table = tab->findChild<QTableWidget*>("tableWidget");
  229. table->setRowHeight(channel % 8, 110);
  230. _plots.push_back(new QCustomPlot(table));
  231. auto valueGraph = _plots.back()->addGraph();
  232. valueGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssDisc, 3));
  233. _plots.back()->xAxis->setRange(0, BufferSize);
  234. _plots.back()->yAxis->setRange(0, 256);
  235. _plots.back()->yAxis2->setRange(0, 2);
  236. _plots.back()->yAxis2->setVisible(true);
  237. auto stateGraph = _plots.back()->addGraph(_plots.back()->xAxis, _plots.back()->yAxis2);
  238. stateGraph->setPen(QPen(Qt::red));
  239. stateGraph->setLineStyle(QCPGraph::LineStyle::lsStepLeft);
  240. auto wgtType = new QComboBox(table);
  241. auto wgtNote = new QSpinBox(table);
  242. auto wgtThresold = new QSpinBox(table);
  243. auto wgtScanTime = new QSpinBox(table);
  244. auto wgtMaskTime = new QSpinBox(table);
  245. auto wgtCurveType = new QComboBox(table);
  246. auto wgtCurveValue = new QSpinBox(table);
  247. QStringList types;
  248. types << "Disabled" << "Piezo";
  249. wgtType->addItems(types);
  250. QStringList curveTypes;
  251. curveTypes << "Normal" << "Exp" << "Log" << "Sigma" << "Flat" << "eXTRA",
  252. wgtCurveType->addItems(curveTypes);
  253. auto curveForm = new QCustomPlot(table);
  254. curveForm->addGraph();
  255. curveForm->xAxis->setRange(0, 127);
  256. curveForm->yAxis->setRange(0, 127);
  257. table->setCellWidget(channel % 8, 0, wgtType);
  258. table->setCellWidget(channel % 8, 1, wgtNote);
  259. table->setCellWidget(channel % 8, 2, wgtThresold);
  260. table->setCellWidget(channel % 8, 3, wgtScanTime);
  261. table->setCellWidget(channel % 8, 4, wgtMaskTime);
  262. table->setCellWidget(channel % 8, 5, wgtCurveType);
  263. table->setCellWidget(channel % 8, 6, wgtCurveValue);
  264. table->setCellWidget(channel % 8, 7, curveForm);
  265. table->setCellWidget(channel % 8, 8, _plots.back());
  266. wgtType->setCurrentIndex(_settings.channelSettings[channel].type);
  267. wgtNote->setValue(_settings.channelSettings[channel].note);
  268. wgtThresold->setValue(_settings.channelSettings[channel].thresold);
  269. wgtScanTime->setValue(_settings.channelSettings[channel].scanTime);
  270. wgtMaskTime->setValue(_settings.channelSettings[channel].maskTime);
  271. wgtCurveType->setCurrentIndex(_settings.channelSettings[channel].curveType);
  272. wgtCurveValue->setMaximum(256);
  273. wgtCurveValue->setValue(_settings.channelSettings[channel].curveValue);
  274. auto fnReplotCurveForm = [this, channel, curveForm]() {
  275. QVector<qreal> x(127);
  276. QVector<qreal> y(127);
  277. for(auto i = 0; i < 127; ++i) {
  278. x[i] = i;
  279. y[i] = calcCurve(_settings.channelSettings[channel].curveType, i, _settings.channelSettings[channel].curveValue);
  280. }
  281. curveForm->graph(0)->setData(x, y);
  282. curveForm->replot();
  283. };
  284. fnReplotCurveForm();
  285. connect(wgtType, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this, channel](int i) mutable { _settings.channelSettings[channel].type = (Type)i; });
  286. connect(wgtNote, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this, channel](int i) mutable { _settings.channelSettings[channel].note = i; });
  287. connect(wgtThresold, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this, channel](int i) mutable { _settings.channelSettings[channel].thresold = i; });
  288. connect(wgtScanTime, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this, channel](int i) mutable { _settings.channelSettings[channel].scanTime = i; });
  289. connect(wgtMaskTime, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this, channel](int i) mutable { _settings.channelSettings[channel].maskTime = i; });
  290. connect(wgtCurveType, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this, channel, curveForm, fnReplotCurveForm](int v) mutable {
  291. _settings.channelSettings[channel].curveType = (Curve)v;
  292. fnReplotCurveForm();
  293. });
  294. connect(wgtCurveValue, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this, channel, fnReplotCurveForm](int i) mutable {
  295. _settings.channelSettings[channel].curveValue = i;
  296. fnReplotCurveForm();
  297. });
  298. }
  299. _serial = std::make_shared<Serial>(L"COM3", 115200);
  300. _midiOut = std::make_shared<MidiOut>(1);
  301. _workerThread = new WorkerThread(this);
  302. _workerThread->start();
  303. #if 0
  304. {
  305. QTimer* timer = new QTimer(this);
  306. _lasttime = QDateTime::currentMSecsSinceEpoch();
  307. connect(timer, &QTimer::timeout, this, &drumduino::serialRead);
  308. timer->start(1);
  309. }
  310. #endif
  311. {
  312. QTimer* timer = new QTimer(this);
  313. connect(timer, &QTimer::timeout, this, &drumduino::updateGraph);
  314. timer->start(1000 / 12);
  315. }
  316. ui.chkUpdateGraph->QCheckBox::setCheckState(_updateGraph ? Qt::Checked : Qt::Unchecked);
  317. connect(ui.chkUpdateGraph, &QCheckBox::stateChanged, [this](int s) {
  318. _updateGraph = s == Qt::Checked;
  319. });
  320. #endif
  321. }
  322. Drumduino::~Drumduino()
  323. {
  324. _drumduinoThread->stop();
  325. _drumduinoThread->wait();
  326. }
  327. #if 0
  328. void Drumduino::serialRead()
  329. {
  330. AGAIN:
  331. auto available = _serial->available();
  332. if(available < 1 + PORT_CNT * CHAN_CNT) {
  333. return;
  334. }
  335. byte sentinel;
  336. _serial->readBytes(&sentinel, 1);
  337. if(sentinel != 0xff) {
  338. goto AGAIN;
  339. }
  340. //now we have a full frame
  341. std::array<byte, PORT_CNT* CHAN_CNT> frame;
  342. _serial->readBytes(frame.data(), frame.size());
  343. auto currentIndex = _currentFrame % BufferSize;
  344. handleFrame(frame, currentIndex);
  345. for(size_t i = 0; i < PORT_CNT * CHAN_CNT; ++i) {
  346. _stateBuffer[i][currentIndex] = _states[i];
  347. _maxVal[i] = std::max(_maxVal[i], frame[i]);
  348. }
  349. ++_currentFrame;
  350. // goto AGAIN;
  351. }
  352. void Drumduino::updateGraph()
  353. {
  354. if(!_updateGraph) {
  355. return;
  356. }
  357. auto currentIndex = _currentFrame % BufferSize;
  358. QVector<qreal> x(BufferSize);
  359. QVector<qreal> y(BufferSize);
  360. QVector<qreal> s(BufferSize);
  361. for(auto i = 0; i < BufferSize; ++i) {
  362. x[i] = i;
  363. }
  364. for(auto i = 0; i < PORT_CNT * CHAN_CNT; ++i) {
  365. _plots[i]->xAxis->setRange(x.front(), x.back());
  366. //if(_plots[i]->isVisible()) {
  367. for(auto k = 0; k < BufferSize; ++k) {
  368. y[k] = _frameBuffer[i][k];
  369. s[k] = _stateBuffer[i][k];
  370. }
  371. _plots[i]->graph(0)->setData(x, y);
  372. _plots[i]->graph(1)->setData(x, s);
  373. _plots[i]->replot();
  374. //}
  375. }
  376. }
  377. #endif
  378. #if 0
  379. void Drumduino::handleFrame(const std::array<byte, PORT_CNT* CHAN_CNT>& frame, const uint64_t currentIndex)
  380. {
  381. auto fnMidiNoteOn = [this](size_t channel, byte newValue) {
  382. const auto& channelSettings = _settings.channelSettings[channel];
  383. auto note = channelSettings.note;
  384. auto velocity = calcCurve(channelSettings.curveType, newValue / 2, channelSettings.curveValue);
  385. byte data[] = {0x90 | channel, 0x7f & note , 0x7f & velocity };
  386. std::vector<byte> message(sizeof(data));
  387. memcpy(message.data(), data, message.size());
  388. _midiOut->send(message);
  389. };
  390. for(auto channel = 0; channel < PORT_CNT * CHAN_CNT; ++channel) {
  391. //const auto curTime = QDateTime::currentMSecsSinceEpoch();
  392. const auto curTime = _currentFrame;
  393. const auto newValue = frame[mapChannels(channel)];
  394. auto& lastValue = _frameBuffer[channel][(currentIndex - 1) % BufferSize];
  395. auto& nextValue = _frameBuffer[channel][currentIndex];
  396. auto& state = _states[channel];
  397. auto& triggerFrame = _triggers[channel];
  398. auto& maxValue = _max[channel];
  399. const auto& channelSettings = _settings.channelSettings[channel];
  400. switch(channelSettings.type) {
  401. case TypePiezo: {
  402. switch(state) {
  403. // In this state we wait for a signal to trigger
  404. case StateAwait: {
  405. if(newValue > lastValue + channelSettings.thresold) {
  406. state = StateScan;
  407. triggerFrame = curTime;
  408. maxValue = newValue;
  409. if(channelSettings.scanTime == 0) {
  410. fnMidiNoteOn(channel, maxValue);
  411. }
  412. }
  413. break;
  414. }
  415. // In this state we measure the value for the given time period to get the max value
  416. case StateScan: {
  417. if(curTime > triggerFrame + channelSettings.scanTime) {
  418. if(channelSettings.scanTime != 0) {
  419. fnMidiNoteOn(channel, maxValue);
  420. }
  421. state = StateMask;
  422. }
  423. else {
  424. maxValue = std::max(newValue, maxValue);
  425. }
  426. break;
  427. }
  428. // In this state we do nothing to prevent retriggering
  429. case StateMask: {
  430. if(curTime > triggerFrame + channelSettings.scanTime + channelSettings.maskTime) {
  431. state = StateAwait;
  432. }
  433. break;
  434. }
  435. default: {
  436. state = StateAwait;
  437. }
  438. }
  439. nextValue = newValue;
  440. }
  441. }
  442. }
  443. }
  444. #endif