1
1

drumduino.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.threshold) {
  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, calcCurve(channelSettings.curve, 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. // Setup Channel Settings
  101. for(auto channel = 0; channel < PORT_CNT * CHAN_CNT; ++channel) {
  102. _settings.channelSettings[channel].note = channel;
  103. }
  104. // Setup Channels
  105. for(auto port = 0; port < PORT_CNT; ++port) {
  106. auto wgtPort = ui.tabWidget->widget(ui.tabWidget->addTab(new PortTab(), "Port " + QString::number(port)));
  107. for(auto pin = 0; pin < CHAN_CNT; ++pin) {
  108. auto channel = port * CHAN_CNT + pin;
  109. auto wgtChannel = new Channel(channel, _settings.channelSettings[channel], wgtPort);
  110. wgtPort->layout()->addWidget(wgtChannel);
  111. _channels[channel] = wgtChannel;
  112. for(auto dial : wgtChannel->findChildren<QDial*>()) {
  113. dial->installEventFilter(this);
  114. }
  115. }
  116. }
  117. // action Save
  118. connect(ui.actionSave, &QAction::triggered, [this]() {
  119. auto fileName = QFileDialog::getSaveFileName(this, "Save", 0, tr("drumduino (*.edrum)"));
  120. QFile file(fileName);
  121. file.open(QIODevice::WriteOnly);
  122. file.write((const char*)&_settings, sizeof(_settings));
  123. file.close();
  124. });
  125. // action Load
  126. connect(ui.actionLoad, &QAction::triggered, [this]() {
  127. auto fileName = QFileDialog::getOpenFileName(this, "Open", 0, tr("drumduino (*.edrum)"));
  128. QFile file(fileName);
  129. file.open(QIODevice::ReadOnly);
  130. file.read((char*)&_settings, sizeof(_settings));
  131. file.close();
  132. for(auto& channel : _channels) {
  133. channel->update();
  134. }
  135. });
  136. _serial = std::make_shared<Serial>(L"COM3", 115200);
  137. _midiOut = std::make_shared<MidiOut>(1);
  138. _drumduinoThread = new DrumduinoThread(this, [this]() {
  139. if(readNextFrame(_serial, _proc)) {
  140. processFrame(_midiOut, _proc, _settings);
  141. #if 1
  142. _proc.stateBuffer[_proc.frameCounter % BufferSize] = _proc.states;
  143. #endif
  144. ++_proc.frameCounter;
  145. }
  146. });
  147. _drumduinoThread->start();
  148. #if 1
  149. {
  150. std::array<QCustomPlot*, PORT_CNT* CHAN_CNT> plots;
  151. for(auto port = 0; port < PORT_CNT; ++port) {
  152. auto wgtPort = ui.tabWidget->widget(ui.tabWidget->addTab(new PortTab(), "Graph_Port " + QString::number(port)));
  153. auto table = new QTableWidget(CHAN_CNT, 1, wgtPort);
  154. table->horizontalHeader()->setStretchLastSection(true);
  155. table->verticalHeader()->setMinimumHeight(100);
  156. table->horizontalHeader()->setVisible(false);
  157. wgtPort->layout()->addWidget(table);
  158. for(auto pin = 0; pin < CHAN_CNT; ++pin) {
  159. auto channel = port * CHAN_CNT + pin;
  160. auto wgtPlot = new QCustomPlot(table);
  161. wgtPlot->addGraph();
  162. table->setRowHeight(pin, 127);
  163. table->setCellWidget(pin, 0, wgtPlot);
  164. wgtPlot->xAxis->setRange(0, BufferSize);
  165. wgtPlot->yAxis->setRange(0, 127);
  166. wgtPlot->yAxis2->setRange(0, 2);
  167. wgtPlot->yAxis2->setVisible(true);
  168. auto stateGraph = wgtPlot->addGraph(wgtPlot->xAxis, wgtPlot->yAxis2);
  169. stateGraph->setPen(QPen(Qt::red));
  170. stateGraph->setLineStyle(QCPGraph::LineStyle::lsStepLeft);
  171. plots[port * CHAN_CNT + pin] = wgtPlot;
  172. }
  173. }
  174. QTimer* timer = new QTimer(this);
  175. connect(timer, &QTimer::timeout, [this, plots]() {
  176. auto currentIndex = _proc.frameCounter % BufferSize;
  177. QVector<qreal> x(BufferSize);
  178. QVector<qreal> y(BufferSize);
  179. QVector<qreal> s(BufferSize);
  180. for(auto i = 0; i < BufferSize; ++i) {
  181. x[i] = i;
  182. }
  183. for(auto i = 0; i < PORT_CNT * CHAN_CNT; ++i) {
  184. if(plots[i]->isVisible()) {
  185. auto channel = mapChannels(i);
  186. plots[i]->yAxis->setLabel(QString(_settings.channelSettings[i].name));
  187. plots[i]->xAxis->setRange(x.front(), x.back());
  188. for(auto k = 0; k < BufferSize; ++k) {
  189. y[k] = _proc.frameBuffer[k][channel];
  190. s[k] = _proc.stateBuffer[k][i];
  191. }
  192. plots[i]->graph(0)->setData(x, y);
  193. plots[i]->graph(1)->setData(x, s);
  194. plots[i]->replot();
  195. }
  196. }
  197. });
  198. timer->start(1000 / 12);
  199. }
  200. #endif
  201. }
  202. Drumduino::~Drumduino()
  203. {
  204. _drumduinoThread->stop();
  205. _drumduinoThread->wait();
  206. }