drumduino.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. unsigned long g_lastTime = 0;
  14. uint64_t g_timeSum = 0;
  15. uint64_t g_frameTime = 0;
  16. bool readNextFrame(std::shared_ptr<Serial>& serial, DrumduinoProc& proc)
  17. {
  18. AGAIN:
  19. auto available = serial->available();
  20. if(available < sizeof(byte) + sizeof(byte) + sizeof(unsigned long) + PORT_CNT * CHAN_CNT) {
  21. return false;
  22. }
  23. byte sentinel;
  24. serial->readBytes(&sentinel, sizeof(sentinel));
  25. if(sentinel != 0xf0) {
  26. goto AGAIN;
  27. }
  28. byte manufacturer;
  29. serial->readBytes(&manufacturer, sizeof(manufacturer));
  30. unsigned long time1;
  31. serial->readBytes((byte*)&time1, sizeof(time1));
  32. unsigned long time2;
  33. serial->readBytes((byte*)&time2, sizeof(time2));
  34. auto timeDiff1 = time1 - g_lastTime;
  35. auto timeDiff2 = time2 - time1;
  36. g_lastTime = time2;
  37. g_frameTime = timeDiff1 + timeDiff2;
  38. g_timeSum += g_frameTime;
  39. auto& frame = proc.frameBuffer[proc.frameCounter % BufferSize];
  40. serial->readBytes(frame.data(), frame.size());
  41. return true;
  42. }
  43. void sendSysexPrescalerThrottle(std::shared_ptr<Serial>& serial, byte prescaler, byte throttle)
  44. {
  45. byte msg[] = {0xf0, 42, prescaler, throttle, 0xF7};
  46. serial->write(msg, sizeof(msg));
  47. }
  48. void midiNoteOn(std::shared_ptr<MidiOut>& midiOut, byte channel, byte note, byte velocity)
  49. {
  50. byte data[] = {0x90 | channel, 0x7f & note , 0x7f & velocity };
  51. std::vector<byte> message(sizeof(data));
  52. memcpy(message.data(), data, message.size());
  53. midiOut->send(message);
  54. }
  55. void processFrame(DrumduinoProc& proc, const Settings& settings, std::function<void(size_t channel, byte maxValue, byte sumValue)> fnOnNote)
  56. {
  57. const auto& lastFrame = proc.frameBuffer[(proc.frameCounter - 1) % BufferSize];
  58. const auto& currentFrame = proc.frameBuffer[proc.frameCounter % BufferSize];
  59. for(auto channel = 0; channel < PORT_CNT * CHAN_CNT; ++channel) {
  60. const auto& lastValue = lastFrame[mapChannels(channel)];
  61. const auto& currentValue = currentFrame[mapChannels(channel)];
  62. auto& state = proc.states[channel];
  63. auto& triggerFrame = proc.triggers[channel];
  64. auto& maxValue = proc.maxs[channel];
  65. auto& sumValue = proc.sums[channel];
  66. const auto& channelSettings = settings.channelSettings[channel];
  67. switch(channelSettings.type) {
  68. case TypePiezo: {
  69. switch(state) {
  70. // In this state we wait for a signal to trigger
  71. case StateAwait: {
  72. STATE_AGAIN:
  73. if(currentValue < lastValue + channelSettings.threshold) {
  74. break;
  75. }
  76. state = StateScan;
  77. triggerFrame = proc.frameCounter;
  78. maxValue = currentValue;
  79. sumValue = currentValue;
  80. //fallthrough
  81. }
  82. // In this state we measure the value for the given time period to get the max value
  83. case StateScan: {
  84. if(proc.frameCounter < triggerFrame + channelSettings.scanTime) {
  85. maxValue = std::max(currentValue, maxValue);
  86. sumValue += currentValue;
  87. break;
  88. }
  89. fnOnNote(channel, maxValue, sumValue / channelSettings.scanTime);
  90. state = StateMask;
  91. //fallthrough
  92. }
  93. // In this state we do nothing to prevent retriggering
  94. case StateMask: {
  95. if(proc.frameCounter < triggerFrame + channelSettings.scanTime + channelSettings.maskTime) {
  96. break;
  97. }
  98. state = StateAwait;
  99. goto STATE_AGAIN;
  100. }
  101. default: {
  102. throw std::exception("not a valid state!");
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. Drumduino::Drumduino(QWidget* parent)
  110. : QMainWindow(parent)
  111. {
  112. ui.setupUi(this);
  113. // Setup Channel Settings
  114. for(auto channel = 0; channel < PORT_CNT * CHAN_CNT; ++channel) {
  115. _settings.channelSettings[channel].note = channel;
  116. }
  117. // Setup Channels
  118. for(auto port = 0; port < PORT_CNT; ++port) {
  119. auto wgtPort = ui.tabWidget->widget(ui.tabWidget->addTab(new PortTab(), "Port " + QString::number(port)));
  120. for(auto pin = 0; pin < CHAN_CNT; ++pin) {
  121. auto channel = port * CHAN_CNT + pin;
  122. auto wgtChannel = new Channel(channel, _settings.channelSettings[channel], wgtPort);
  123. wgtPort->layout()->addWidget(wgtChannel);
  124. _channels[channel] = wgtChannel;
  125. for(auto dial : wgtChannel->findChildren<QDial*>()) {
  126. dial->installEventFilter(this);
  127. }
  128. }
  129. }
  130. // action Save
  131. connect(ui.actionSave, &QAction::triggered, [this]() {
  132. auto fileName = QFileDialog::getSaveFileName(this, "Save", 0, tr("drumduino (*.edrum)"));
  133. QFile file(fileName);
  134. file.open(QIODevice::WriteOnly);
  135. file.write((const char*)&_settings, sizeof(_settings));
  136. file.close();
  137. });
  138. // action Load
  139. connect(ui.actionLoad, &QAction::triggered, [this]() {
  140. auto fileName = QFileDialog::getOpenFileName(this, "Open", 0, tr("drumduino (*.edrum)"));
  141. QFile file(fileName);
  142. file.open(QIODevice::ReadOnly);
  143. file.read((char*)&_settings, sizeof(_settings));
  144. file.close();
  145. for(auto& channel : _channels) {
  146. channel->update();
  147. }
  148. });
  149. connect(this, &Drumduino::updateChannelProgess, this, &Drumduino::slotUpdateChannelProgress, Qt::QueuedConnection);
  150. try {
  151. //_serial = std::make_shared<Serial>(L"COM3", 115200);
  152. _serial = std::make_shared<Serial>(L"COM3", 2000000);
  153. _midiOut = std::make_shared<MidiOut>(1);
  154. _drumduinoThread = new DrumduinoThread(this, [this]() {
  155. if(readNextFrame(_serial, _proc)) {
  156. processFrame(_proc, _settings, [this](size_t channel, byte maxValue, byte sumValue) {
  157. const auto& channelSettings = _settings.channelSettings[channel];
  158. auto calcValue = 0;
  159. if(channelSettings.sum) {
  160. calcValue = calcCurve(channelSettings.curve, sumValue * 2);
  161. }
  162. else {
  163. calcValue = calcCurve(channelSettings.curve, maxValue);
  164. }
  165. midiNoteOn(_midiOut, _settings.midiChannel, channelSettings.note, calcValue);
  166. emit updateChannelProgess(channel, maxValue, sumValue, calcValue);
  167. });
  168. #if 1
  169. _proc.stateBuffer[_proc.frameCounter % BufferSize] = _proc.states;
  170. #endif
  171. ++_proc.frameCounter;
  172. }
  173. });
  174. _drumduinoThread->start();
  175. #if 1
  176. {
  177. std::array<QCustomPlot*, PORT_CNT* CHAN_CNT> plots;
  178. for(auto port = 0; port < PORT_CNT; ++port) {
  179. auto wgtPort = ui.tabWidget->widget(ui.tabWidget->addTab(new PortTab(), "Graph_Port " + QString::number(port)));
  180. auto table = new QTableWidget(CHAN_CNT, 1, wgtPort);
  181. table->horizontalHeader()->setStretchLastSection(true);
  182. table->verticalHeader()->setMinimumHeight(100);
  183. table->horizontalHeader()->setVisible(false);
  184. wgtPort->layout()->addWidget(table);
  185. for(auto pin = 0; pin < CHAN_CNT; ++pin) {
  186. auto channel = port * CHAN_CNT + pin;
  187. auto wgtPlot = new QCustomPlot(table);
  188. wgtPlot->setBackground(qApp->palette().button());
  189. QPen pen(qApp->palette().midlight().color());
  190. pen.setStyle(Qt::PenStyle::DotLine);
  191. wgtPlot->xAxis->grid()->setPen(pen);
  192. wgtPlot->yAxis->grid()->setPen(pen);
  193. wgtPlot->xAxis2->grid()->setPen(pen);
  194. wgtPlot->yAxis2->grid()->setPen(pen);
  195. wgtPlot->xAxis->setBasePen(qApp->palette().windowText().color());
  196. wgtPlot->yAxis->setBasePen(qApp->palette().windowText().color());
  197. wgtPlot->xAxis2->setBasePen(qApp->palette().windowText().color());
  198. wgtPlot->yAxis2->setBasePen(qApp->palette().windowText().color());
  199. wgtPlot->xAxis->setTickPen(qApp->palette().windowText().color());
  200. wgtPlot->yAxis->setTickPen(qApp->palette().windowText().color());
  201. wgtPlot->xAxis2->setTickPen(qApp->palette().windowText().color());
  202. wgtPlot->yAxis2->setTickPen(qApp->palette().windowText().color());
  203. auto curve = wgtPlot->addGraph();
  204. curve->setPen(QPen(qApp->palette().buttonText().color()));
  205. table->setRowHeight(pin, 127);
  206. table->setCellWidget(pin, 0, wgtPlot);
  207. wgtPlot->xAxis->setRange(0, BufferSize);
  208. wgtPlot->yAxis->setRange(0, 127);
  209. wgtPlot->yAxis2->setRange(0, 2);
  210. wgtPlot->yAxis2->setVisible(true);
  211. auto stateGraph = wgtPlot->addGraph(wgtPlot->xAxis, wgtPlot->yAxis2);
  212. stateGraph->setPen(QPen(qApp->palette().highlight().color()));
  213. stateGraph->setLineStyle(QCPGraph::LineStyle::lsStepLeft);
  214. plots[port * CHAN_CNT + pin] = wgtPlot;
  215. }
  216. }
  217. QTimer* timer = new QTimer(this);
  218. connect(timer, &QTimer::timeout, [this, plots]() {
  219. auto currentIndex = _proc.frameCounter % BufferSize;
  220. QVector<qreal> x(BufferSize);
  221. QVector<qreal> y(BufferSize);
  222. QVector<qreal> s(BufferSize);
  223. for(auto i = 0; i < BufferSize; ++i) {
  224. x[i] = i;
  225. }
  226. for(auto i = 0; i < PORT_CNT * CHAN_CNT; ++i) {
  227. if(plots[i]->isVisible()) {
  228. auto channel = mapChannels(i);
  229. plots[i]->yAxis->setLabel(QString(_settings.channelSettings[i].name));
  230. plots[i]->xAxis->setRange(x.front(), x.back());
  231. for(auto k = 0; k < BufferSize; ++k) {
  232. y[k] = _proc.frameBuffer[k][channel];
  233. s[k] = _proc.stateBuffer[k][i];
  234. }
  235. plots[i]->graph(0)->setData(x, y);
  236. plots[i]->graph(1)->setData(x, s);
  237. plots[i]->replot();
  238. }
  239. }
  240. setWindowTitle("drumduino - sampling each channel at " + QString::number(double(g_frameTime)) + "Hz");
  241. });
  242. timer->start(1000 / 12);
  243. }
  244. #endif
  245. }
  246. catch(...)
  247. {}
  248. }
  249. Drumduino::~Drumduino()
  250. {
  251. if(_drumduinoThread) {
  252. _drumduinoThread->stop();
  253. _drumduinoThread->wait();
  254. }
  255. }
  256. void Drumduino::slotUpdateChannelProgress(size_t channel, byte maxValue, byte sumValue, byte calcValue)
  257. {
  258. _channels[channel]->triggered(maxValue, sumValue, calcValue);
  259. }