From a13f57f4a88badbc770ca22a1ee831efa478868c Mon Sep 17 00:00:00 2001 From: mrbesen Date: Wed, 22 Dec 2021 12:52:25 +0100 Subject: [PATCH] doubleclick sample to play --- include/buttonmanager.h | 1 + include/buttonmanageritems.h | 12 +++++++++++ include/sound.h | 4 ++-- include/sounddevice.h | 2 +- src/buttonmanager.cpp | 9 +++++++++ src/buttonmanageritems.cpp | 39 +++++++++++++++++++++++++++++++++++- src/editsample.cpp | 5 ----- src/sound.cpp | 8 ++++---- src/soundbutton.cpp | 3 +-- src/sounddevice.cpp | 6 ++++-- ui/buttonmanager.ui | 17 ++++++++++++++++ 11 files changed, 89 insertions(+), 17 deletions(-) diff --git a/include/buttonmanager.h b/include/buttonmanager.h index fb867af..6b89356 100644 --- a/include/buttonmanager.h +++ b/include/buttonmanager.h @@ -34,6 +34,7 @@ private slots: void itemSelected(); void itemChanged(QTreeWidgetItem* item, int column); + void itemDoubleClicked(QTreeWidgetItem* item); void dialogButtonPressed(QAbstractButton* btn); private: diff --git a/include/buttonmanageritems.h b/include/buttonmanageritems.h index a982bfc..3b55f64 100644 --- a/include/buttonmanageritems.h +++ b/include/buttonmanageritems.h @@ -55,6 +55,11 @@ public: const static int TYPE = 1001; + virtual bool hasRemove() const override; + + virtual void remove() override; + + // triggered from the view when the name was changed void nameWasChanged(); @@ -71,9 +76,16 @@ public: const static int TYPE = 1002; + virtual bool hasRemove() const override; + + virtual void remove() override; + Config::SampleConfig& getConfig(); private: Config::RootConfig& conf; uint8_t samplenr = 0; ButtonItem* button = nullptr; + + //called when a sample was moved + void updatePosition(); }; \ No newline at end of file diff --git a/include/sound.h b/include/sound.h index 67c3a0a..50342ad 100644 --- a/include/sound.h +++ b/include/sound.h @@ -21,8 +21,8 @@ public: static Sound& instance(); static void deinitInstance(); - void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t endms = 0); - void addPlayback(const std::string& audioDeviceName, const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t endms = 0, sndcb_func callback = {}); + void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t length = 0); + void addPlayback(const std::string& audioDeviceName, const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t length = 0, sndcb_func callback = {}); bool addDefaultDevice(); bool addDeviceWithName(const std::string& name); void stopAll(); diff --git a/include/sounddevice.h b/include/sounddevice.h index 74b7a10..26c5168 100644 --- a/include/sounddevice.h +++ b/include/sounddevice.h @@ -17,7 +17,7 @@ public: static SoundDevice* createDevice(ma_context* ctx, const ma_device_id* did = NULL); void stop(); - void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t endms = 0, std::function callback = {}); + void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t length = 0, std::function callback = {}); void startDevice(); void cleanupDecoders(); //fertige decoder löschen diff --git a/src/buttonmanager.cpp b/src/buttonmanager.cpp index d012435..bc9a13f 100644 --- a/src/buttonmanager.cpp +++ b/src/buttonmanager.cpp @@ -3,6 +3,7 @@ #include "addnewwhat.h" #include "editsample.h" +#include "sound.h" #include @@ -189,6 +190,14 @@ void ButtonManager::itemChanged(QTreeWidgetItem* item, int column) { } } +void ButtonManager::itemDoubleClicked(QTreeWidgetItem* item) { + if(SampleItem* sample = dynamic_cast(item)) { + const Config::SampleConfig& sampleconf = sample->getConfig(); + Sound::instance().addPlayback(sampleconf.file, sampleconf.volume, sampleconf.offset, sampleconf.length); + } +} + + void ButtonManager::dialogButtonPressed(QAbstractButton* btn) { QDialogButtonBox::ButtonRole role = ui->buttonBox->buttonRole(btn); if(role == QDialogButtonBox::ButtonRole::ResetRole) { diff --git a/src/buttonmanageritems.cpp b/src/buttonmanageritems.cpp index 6ae0e39..3ed8df4 100644 --- a/src/buttonmanageritems.cpp +++ b/src/buttonmanageritems.cpp @@ -134,6 +134,22 @@ ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& co } } +bool ButtonItem::hasRemove() const { + return true; +} + +void ButtonItem::remove() { + auto& rowconf = row->getConfig(); + rowconf.erase(rowconf.begin() + buttonnr); + + for(uint8_t i = buttonnr+1; i <= rowconf.size(); ++i) { + ButtonItem* item = dynamic_cast(mparent->child(i)); + if(item) { + item->buttonnr--; + } + } +} + void ButtonItem::nameWasChanged() { std::string newName = data(1, Qt::ItemDataRole::DisplayRole).toString().toStdString(); getConfig().name = newName; @@ -147,10 +163,31 @@ Config::ButtonConfig& ButtonItem::getConfig() { SampleItem::SampleItem(ButtonItem* parent, uint8_t samplenr, Config::RootConfig& conf) : ButtonManagerItem(parent, TYPE), conf(conf), samplenr(samplenr), button(parent) { const Config::SampleConfig& sample = getConfig(); - setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) samplenr+1)); + updatePosition(); setData(3, Qt::ItemDataRole::DisplayRole, QVariant(QString::fromStdString(sample.file))); } +bool SampleItem::hasRemove() const { + return true; +} + +void SampleItem::remove() { + auto& btnconf = button->getConfig().samples; + btnconf.erase(btnconf.begin() + samplenr); + + for(uint8_t i = samplenr+1; i <= btnconf.size(); ++i) { + SampleItem* item = dynamic_cast(mparent->child(i)); + if(item) { + item->samplenr--; + item->updatePosition(); + } + } +} + Config::SampleConfig& SampleItem::getConfig() { return button->getConfig().samples.at(samplenr); +} + +void SampleItem::updatePosition() { + setData(2, Qt::ItemDataRole::DisplayRole, QVariant((int) samplenr+1)); } \ No newline at end of file diff --git a/src/editsample.cpp b/src/editsample.cpp index e545dd6..936d084 100644 --- a/src/editsample.cpp +++ b/src/editsample.cpp @@ -49,11 +49,6 @@ void EditSample::play() { uint64_t startTime = getStartTime(); uint64_t lengthTime = getLength(); - // convert relative time to absolute - if(lengthTime != 0) { - lengthTime = startTime + lengthTime; - } - Log::info << "play audio: " << std::quoted(audioFile) << " on " << std::quoted(devicename) << " with " << startTime << " and " << lengthTime; // stop all other sounds first Sound::instance().stopAll(); diff --git a/src/sound.cpp b/src/sound.cpp index e993f2f..4ff7cc0 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -64,24 +64,24 @@ void Sound::deinitInstance() { Log::info << "Sound deinited"; } -void Sound::addPlayback(const std::string& name, float volume, uint64_t beginms, uint64_t endms) { +void Sound::addPlayback(const std::string& name, float volume, uint64_t beginms, uint64_t length) { if(volume < 0.00001f) volume = 0.00001f; std::lock_guard lock(devicesMutex); for(SoundDevice* sd : devices) { - sd->addPlayback(name, volume, beginms, endms); + sd->addPlayback(name, volume, beginms, length); } } -void Sound::addPlayback(const std::string& audioDeviceName, const std::string& name, float volume, uint64_t beginms, uint64_t endms, sndcb_func callback) { +void Sound::addPlayback(const std::string& audioDeviceName, const std::string& name, float volume, uint64_t beginms, uint64_t length, sndcb_func callback) { if(volume < 0.00001f) volume = 0.00001f; std::lock_guard lock(devicesMutex); for(SoundDevice* sd : devices) { if(sd->getName() == audioDeviceName) { - sd->addPlayback(name, volume, beginms, endms, callback); + sd->addPlayback(name, volume, beginms, length, callback); break; } } diff --git a/src/soundbutton.cpp b/src/soundbutton.cpp index 37e15e1..890ceed 100644 --- a/src/soundbutton.cpp +++ b/src/soundbutton.cpp @@ -80,9 +80,8 @@ void SoundButton::play() { if(currentSample >= samples.size()) currentSample = 0; - uint64_t endms = (sample.lengthms == 0 ? 0 : sample.startms + sample.lengthms); try { - Sound::instance().addPlayback(sample.file, sample.volume, sample.startms, endms); + Sound::instance().addPlayback(sample.file, sample.volume, sample.startms, sample.lengthms); } catch(const std::exception& e) { Log::error << "Catched Exception when plaing Audio File: " << sample.file; setDisabled(); diff --git a/src/sounddevice.cpp b/src/sounddevice.cpp index 4348473..19cb5ef 100644 --- a/src/sounddevice.cpp +++ b/src/sounddevice.cpp @@ -170,7 +170,7 @@ void SoundDevice::stop() { deviceRunning = false; } -void SoundDevice::addPlayback(const std::string& name, float volume, uint64_t beginms, uint64_t endms, std::function callback) { +void SoundDevice::addPlayback(const std::string& name, float volume, uint64_t beginms, uint64_t length, std::function callback) { cleanupDecoders(); Playback* pb = new Playback(); @@ -193,7 +193,9 @@ void SoundDevice::addPlayback(const std::string& name, float volume, uint64_t be Log::trace << "skip to frame: " << pb->currentFrame; } - if(endms != 0) { + if(length != 0) { + uint64_t endms = beginms + length; + pb->endFrame = (pb->decoder.outputSampleRate * endms) / 1000; Log::trace << "skip endframe: " << pb->endFrame; } diff --git a/ui/buttonmanager.ui b/ui/buttonmanager.ui index 0c8487b..4bb28f7 100644 --- a/ui/buttonmanager.ui +++ b/ui/buttonmanager.ui @@ -230,6 +230,22 @@ + + buttonTreeWidget + itemDoubleClicked(QTreeWidgetItem*,int) + ButtonManager + itemDoubleClicked(QTreeWidgetItem*) + + + 55 + 33 + + + 62 + 2 + + + addButton() @@ -240,5 +256,6 @@ itemSelected() dialogButtonPressed(QAbstractButton*) itemChanged(QTreeWidgetItem*,int) + itemDoubleClicked(QTreeWidgetItem*)