diff options
-rw-r--r-- | src/canvastoolposition.cc | 72 | ||||
-rw-r--r-- | src/canvastoolposition.h | 1 | ||||
-rw-r--r-- | src/canvastoolranges.cc | 61 |
3 files changed, 104 insertions, 30 deletions
diff --git a/src/canvastoolposition.cc b/src/canvastoolposition.cc index 7784720..a90908b 100644 --- a/src/canvastoolposition.cc +++ b/src/canvastoolposition.cc @@ -32,6 +32,8 @@ #include <iostream> +constexpr int diam{8}; + #define mapX(x) canvas.mapX(x) #define mapY(x) canvas.mapY(x) #define unmapX(x) canvas.unmapX(x) @@ -56,6 +58,43 @@ CanvasToolPosition::CanvasToolPosition(Instrument& instrument, bool CanvasToolPosition::mouseMoveEvent(QMouseEvent* event) { + sel_id_t active_range = ranges.active(); + Range act_sel = ranges.get(active_range); +// +// int x1 = mapX(range.pos1); +// int x2 = mapX(range.pos2); +// highlight1 = false; +// highlight2 = false; +// if(x1 == event->x) +// { +// highlight1 = true; +// } +// else if(x2 == event->x) +// { +// highlight2 = true; +// } +// canvas->update(); +// + + if(event->button() != Qt::LeftButton && + event->y() > (canvas.height() - diam)) + { + // Check if a range is being dragged. + QVector<sel_id_t> ids = ranges.ids(); + QVector<sel_id_t>::iterator i = ids.begin(); + while(i != ids.end()) + { + Range sel = ranges.get(*i); + if(abs(event->x() - mapX(sel.pos1)) < diam/2 + || abs(event->x() - mapX(sel.pos2)) < diam/2) + { + canvas.setCursor(Qt::SplitHCursor); + return true; + } + i++; + } + } + return false; } @@ -74,7 +113,6 @@ void drawRange(const Range& range, Canvas& canvas, QPainter& painter) { int x1 = mapX(range.pos1); int x2 = mapX(range.pos2); - if(x1 > x2) { std::swap(x1, x2); @@ -87,17 +125,27 @@ void drawRange(const Range& range, Canvas& canvas, QPainter& painter) // | | // | | constexpr auto len = 8; - painter.drawLine(x1, mapY(-1.0), x1, mapY(1.0)); // left bar - - painter.drawLine(x1, mapY( 0.0), x1 + len, mapY(0.0) + len); // left arrow - painter.drawLine(x1, mapY( 0.0), x1 + len, mapY(0.0) - len); // left arrow - - painter.drawLine(x1, mapY( 0.0), x2, mapY(0.0)); // horizontal line - - painter.drawLine(x2, mapY( 0.0), x2 - len, mapY(0.0) + len); // right arrow - painter.drawLine(x2, mapY( 0.0), x2 - len, mapY(0.0) - len); // right arrow - - painter.drawLine(x2, mapY(-1.0), x2, mapY(1.0)); // right bar + auto arrows_y = canvas.height() - canvas.height() / 8; + auto balls_y = canvas.height() - diam; + if(x1 > 0 && x1 < canvas.width()) + { + painter.drawLine(x1, 0, x1, canvas.height()); // left bar + painter.drawEllipse(x1-diam/2, balls_y, diam, diam); + } + if(((x1 > 0 && x1 < canvas.width()) || (x2 > 0 && x2 < canvas.width())) && + std::abs(x1 - x2) > len) + { + painter.drawLine(x1, arrows_y, x1 + len, arrows_y + len); // left arrow + painter.drawLine(x1, arrows_y, x1 + len, arrows_y - len); // left arrow + painter.drawLine(x1, arrows_y, x2, arrows_y); // horizontal line + painter.drawLine(x2, arrows_y, x2 - len, arrows_y + len); // right arrow + painter.drawLine(x2, arrows_y, x2 - len, arrows_y - len); // right arrow + } + if(x2 > 0 && x2 < canvas.width()) + { + painter.drawLine(x2, 0, x2, canvas.height()); // right bar + painter.drawEllipse(x2-diam/2, balls_y, diam, diam); + } } } diff --git a/src/canvastoolposition.h b/src/canvastoolposition.h index 0265a37..e6a645c 100644 --- a/src/canvastoolposition.h +++ b/src/canvastoolposition.h @@ -69,4 +69,5 @@ private: QColor colPreviewSel; bool show_preview{false}; + int mouse_x{}; }; diff --git a/src/canvastoolranges.cc b/src/canvastoolranges.cc index 9a76163..ce9ae37 100644 --- a/src/canvastoolranges.cc +++ b/src/canvastoolranges.cc @@ -342,49 +342,72 @@ void CanvasToolRanges::doAutoCreateRanges(bool preview) .fadein = 2, .fadeout = ((to - from) / 3) * fadeout }; - +// det er her det foregaard ///////////////////////////////////////////////////////////////////////// if(positionData1 && positionData2) { //TODO: set window in ms instead of samples - int pos_window_size{1000}; // +/- 500 from detected, + int pos_window_size{256}; // +/- 500 from detected, double threshold = 0.1; // 1/3 is about right - float norm{0}; - for(int idx = range.from - pos_window_size / 2; - idx < range.from + pos_window_size / 2; +//////////////////////////////////////////////////////////// + double signal_max{0}; + for(int idx = (int)range.from - pos_window_size / 2; + idx < (int)range.from + pos_window_size / 2; + ++idx) + { + signal_max = + std::max((double)std::abs(positionData1[idx]), signal_max); + } + constexpr int noise_len{128}; +// constexpr double noise_to_threshold_scalar{200}; + double noise_floor{0}; + for(int idx = (int)range.from - pos_window_size / 2 - noise_len; + idx < (int)range.from - pos_window_size / 2; ++idx) { - norm = std::max(std::abs(positionData1[idx]), norm); + noise_floor = + std::max((double)std::abs(positionData1[idx]), noise_floor); } + threshold = noise_floor + (signal_max - noise_floor) / 10; - int pos1{range.from - pos_window_size / 2}; - for(int idx = range.from - pos_window_size / 2; - idx < range.from + pos_window_size / 2; + int pos1{(int)range.from - pos_window_size / 2}; + for(int idx = (int)range.from - pos_window_size / 2; + idx < (int)range.from + pos_window_size / 2; ++idx) { - if(std::abs(positionData1[idx]) / norm > threshold) + if(std::abs(positionData1[idx]) > threshold) { pos1 = idx; range.pos1 = pos1; break; } } - - norm = 0; +//////////////////////////////////////////////////////////// + signal_max = 0; for(int idx = range.from - pos_window_size / 2; idx < range.from + pos_window_size / 2; ++idx) { - norm = std::max(std::abs(positionData1[idx]), norm); + signal_max = + std::max((double)std::abs(positionData1[idx]), signal_max); + } + noise_floor = 0; + for(int idx = (int)range.from - pos_window_size / 2 - noise_len; + idx < (int)range.from - pos_window_size / 2; + ++idx) + { + noise_floor = + std::max((double)std::abs(positionData2[idx]), noise_floor); } + threshold = noise_floor + (signal_max - noise_floor) / 10; - int pos2{range.from + pos_window_size / 2}; - for(int idx = range.from - pos_window_size / 2; - idx < range.from + pos_window_size / 2; + int pos2{(int)range.from + pos_window_size / 2}; + for(int idx = (int)range.from - pos_window_size / 2; + idx < (int)range.from + pos_window_size / 2; ++idx) { - if(std::abs(positionData2[idx]) / norm > threshold) + if(std::abs(positionData2[idx]) > threshold) { pos2 = idx; range.pos2 = pos2; @@ -392,10 +415,12 @@ void CanvasToolRanges::doAutoCreateRanges(bool preview) } } +//////////////////////////////////////////////////////////// + constexpr double speed_of_sound{343100}; // mm/s constexpr double samplerate{48000}; // 48kHz - TODO get from audio - range.position = std::abs(pos1 - pos2) * speed_of_sound / samplerate / 2.0; + range.position = std::abs(pos1 - pos2);// * speed_of_sound / samplerate / 2.0; } ///////////////////////////////////////////////////////////////////////// |