From 1ebc965066452c9c780e879c1920a2bb24fb5c28 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 20 Dec 2021 15:59:03 +0100 Subject: [PATCH] cursors --- include/editsample.h | 2 ++ include/samplereader.h | 1 + include/soundview.h | 14 ++++++++++++-- src/editsample.cpp | 19 +++++++++++++++++++ src/samplereader.cpp | 4 ++++ src/soundview.cpp | 39 +++++++++++++++++++++++++++++++++------ 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/include/editsample.h b/include/editsample.h index f4c642b..4ea245d 100644 --- a/include/editsample.h +++ b/include/editsample.h @@ -24,6 +24,8 @@ private slots: void setStartToCurrent(); void setStopToCurrent(); + void timesChanged(); + private: Ui::EditSample* ui; diff --git a/include/samplereader.h b/include/samplereader.h index 0600d71..2ff5c32 100644 --- a/include/samplereader.h +++ b/include/samplereader.h @@ -16,6 +16,7 @@ public: void setWidth(uint32_t w); float readSample(uint32_t pos); + uint64_t getLength(); // length of the sample in ms private: ma_decoder decoder; diff --git a/include/soundview.h b/include/soundview.h index cfa032f..ce1a523 100644 --- a/include/soundview.h +++ b/include/soundview.h @@ -17,12 +17,22 @@ public: void paintEvent(QPaintEvent*) override; void resizeEvent(QResizeEvent*) override; + +public slots: + void updatePosition(unsigned long newpos); + void updateStartPosition(unsigned long newpos); + void updateEndPosition(unsigned long newpos); + private: + void drawCursor(uint64_t pos, QPen pen); + QGraphicsScene scene; SampleReader* samples = nullptr; std::string file; - uint64_t offset = 0; - uint64_t length = 0; + + uint64_t currentPosition = 0; + uint64_t startPos = 0; + uint64_t endPos = 0; }; \ No newline at end of file diff --git a/src/editsample.cpp b/src/editsample.cpp index 2aadf8b..d51f07f 100644 --- a/src/editsample.cpp +++ b/src/editsample.cpp @@ -15,12 +15,17 @@ EditSample::EditSample(const std::string& audioFile_, QWidget *parent) : QDialog ui->soundOutputselect->addItem(qdeviceName, qdeviceName); } + ui->graphicsView->loadFile(audioFile); + QObject::connect(ui->playButton, SIGNAL( clicked() ), this, SLOT( play() )); QObject::connect(ui->stopButton, SIGNAL( clicked() ), this, SLOT( stop() )); QObject::connect(ui->starttoCurrentButton, SIGNAL( clicked() ), this, SLOT( setStartToCurrent() )); QObject::connect(ui->stoptoCurrentButton, SIGNAL( clicked() ), this, SLOT( setStopToCurrent() )); + QObject::connect(ui->startTime, SIGNAL( editingFinished() ), this, SLOT( timesChanged() )); + QObject::connect(ui->lengthTime, SIGNAL( editingFinished() ), this, SLOT( timesChanged() )); + setCurrentPosition(0); } @@ -42,6 +47,9 @@ void EditSample::play() { } Log::info << "play audio: " << std::quoted(audioFile) << " on " << std::quoted(devicename) << " with " << startTime << " and " << lengthTime; + // stop all other sounds first + Sound::instance().stopAll(); + // start new Sound::instance().addPlayback(devicename, audioFile, 1.0, startTime, lengthTime, std::bind(&EditSample::setCurrentPosition, this, std::placeholders::_1)); } @@ -66,6 +74,14 @@ void EditSample::setStopToCurrent() { } } +void EditSample::timesChanged() { + uint64_t start = getTimeInfo(ui->startTime); + uint64_t length = getTimeInfo(ui->lengthTime); + + ui->graphicsView->updateStartPosition(start); + ui->graphicsView->updateEndPosition(length == 0 ? 0 : (start + length)); +} + uint64_t EditSample::getTimeInfo(const QTimeEdit* time) const { QTime timedata = time->time(); return ((((( @@ -100,4 +116,7 @@ void EditSample::setCurrentPosition(uint64_t cp) { // set text async QString input = QString::fromStdString("Current: " + formatedTime); QMetaObject::invokeMethod(ui->currentPosLabel, "setText", Qt::QueuedConnection, Q_ARG(QString, input)); + + // update cursor in view + QMetaObject::invokeMethod(ui->graphicsView, "updatePosition", Qt::QueuedConnection, Q_ARG(unsigned long, currentposition)); } \ No newline at end of file diff --git a/src/samplereader.cpp b/src/samplereader.cpp index afc594d..c2db4f7 100644 --- a/src/samplereader.cpp +++ b/src/samplereader.cpp @@ -51,4 +51,8 @@ float SampleReader::readSample(uint32_t pos) { if(avg > 1) avg = 1; else if(avg < 0) avg = 0; return avg; +} + +uint64_t SampleReader::getLength() { + return decoderSize / (decoder.outputSampleRate / 1000); } \ No newline at end of file diff --git a/src/soundview.cpp b/src/soundview.cpp index e95724a..805331a 100644 --- a/src/soundview.cpp +++ b/src/soundview.cpp @@ -30,15 +30,22 @@ void SoundView::redraw() { //Log::info << "scene Size: " << scene.width() << " " << scene.height() << " " << width() << " " << height(); + // red center line scene.addLine(0, h/2, scene.width()-4, h/2, QPen(Qt::red)); -/* - for(uint32_t x = 0; x < width(); ++x) { - float s = samples->readSample(x); - scene.addLine(x, h/2, x, s*h, QPen(Qt::white)); - //Log::info << "Line: " << x << " " << h/2; + // cursor + if(samples) { + drawCursor(currentPosition, QPen(Qt::blue)); + drawCursor(startPos, QPen(Qt::yellow)); + drawCursor(endPos == 0 ? samples->getLength() : endPos, QPen(Qt::yellow)); + + /* + for(uint32_t x = 0; x < width(); ++x) { + float s = samples->readSample(x); + scene.addLine(x, h/2, x, s*h, QPen(Qt::white)); + //Log::info << "Line: " << x << " " << h/2; + } */ } - */ } void SoundView::paintEvent(QPaintEvent* event) { @@ -54,3 +61,23 @@ void SoundView::resizeEvent(QResizeEvent* event) { samples->setWidth(event->size().width()); } } + +void SoundView::updatePosition(unsigned long newpos) { + currentPosition = newpos; + update(); +} + +void SoundView::updateStartPosition(unsigned long newpos) { + startPos = newpos; + update(); +} + +void SoundView::updateEndPosition(unsigned long newpos) { + endPos = newpos; + update(); +} + +void SoundView::drawCursor(uint64_t pos, QPen pen) { + uint32_t cursorXpos = (pos * (scene.width()-4)) / samples->getLength(); + scene.addLine(cursorXpos, 0, cursorXpos, scene.height() -4, pen); +}