drumduino.cpp 16 KB

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