doubleclick sample to play

This commit is contained in:
mrbesen 2021-12-22 12:52:25 +01:00
parent 0d4f585d41
commit a13f57f4a8
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
11 changed files with 89 additions and 17 deletions

View File

@ -34,6 +34,7 @@ private slots:
void itemSelected();
void itemChanged(QTreeWidgetItem* item, int column);
void itemDoubleClicked(QTreeWidgetItem* item);
void dialogButtonPressed(QAbstractButton* btn);
private:

View File

@ -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();
};

View File

@ -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();

View File

@ -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<void(uint64_t)> callback = {});
void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t length = 0, std::function<void(uint64_t)> callback = {});
void startDevice();
void cleanupDecoders(); //fertige decoder löschen

View File

@ -3,6 +3,7 @@
#include "addnewwhat.h"
#include "editsample.h"
#include "sound.h"
#include <QFileDialog>
@ -189,6 +190,14 @@ void ButtonManager::itemChanged(QTreeWidgetItem* item, int column) {
}
}
void ButtonManager::itemDoubleClicked(QTreeWidgetItem* item) {
if(SampleItem* sample = dynamic_cast<SampleItem*>(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) {

View File

@ -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<ButtonItem*>(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<SampleItem*>(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));
}

View File

@ -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();

View File

@ -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<std::mutex> 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<std::mutex> 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;
}
}

View File

@ -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();

View File

@ -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<void(uint64_t)> callback) {
void SoundDevice::addPlayback(const std::string& name, float volume, uint64_t beginms, uint64_t length, std::function<void(uint64_t)> 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;
}

View File

@ -230,6 +230,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonTreeWidget</sender>
<signal>itemDoubleClicked(QTreeWidgetItem*,int)</signal>
<receiver>ButtonManager</receiver>
<slot>itemDoubleClicked(QTreeWidgetItem*)</slot>
<hints>
<hint type="sourcelabel">
<x>55</x>
<y>33</y>
</hint>
<hint type="destinationlabel">
<x>62</x>
<y>2</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>addButton()</slot>
@ -240,5 +256,6 @@
<slot>itemSelected()</slot>
<slot>dialogButtonPressed(QAbstractButton*)</slot>
<slot>itemChanged(QTreeWidgetItem*,int)</slot>
<slot>itemDoubleClicked(QTreeWidgetItem*)</slot>
</slots>
</ui>