diff --git a/include/editsample.h b/include/editsample.h index 7f2e5df..be97ce2 100644 --- a/include/editsample.h +++ b/include/editsample.h @@ -1,7 +1,8 @@ -#ifndef EDITSAMPLE_H -#define EDITSAMPLE_H +#pragma once #include +#include +#include namespace Ui { class EditSample; @@ -12,11 +13,20 @@ class EditSample : public QDialog Q_OBJECT public: - explicit EditSample(QWidget *parent = nullptr); + explicit EditSample(const std::string& audioFile, QWidget *parent = nullptr); ~EditSample(); -private: - Ui::EditSample *ui; -}; +public slots: + void play(); + void stop(); -#endif // EDITSAMPLE_H +private: + Ui::EditSample* ui; + + const std::string audioFile; + uint64_t getTimeInfo(const QTimeEdit* time) const; + + uint64_t currentposition = 0; + + void setCurrentPosition(uint64_t); +}; diff --git a/include/sound.h b/include/sound.h index 10afd43..f887366 100644 --- a/include/sound.h +++ b/include/sound.h @@ -19,12 +19,14 @@ public: 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); bool addDefaultDevice(); bool addDeviceWithName(const std::string& name); void stopAll(); void reset(); SampleReader* openFile(const std::string& name); + std::vector getOutputs(); static std::string FOLDER; private: diff --git a/include/sounddevice.h b/include/sounddevice.h index 701d753..ce01ec8 100644 --- a/include/sounddevice.h +++ b/include/sounddevice.h @@ -21,6 +21,8 @@ public: void startDevice(); void cleanupDecoders(); //fertige decoder löschen + std::string getName() const; + // callback void sound_callback(void* outbuffer, ma_uint32 frameCount); diff --git a/src/editsample.cpp b/src/editsample.cpp index 96cd9d8..d9985f5 100644 --- a/src/editsample.cpp +++ b/src/editsample.cpp @@ -1,10 +1,58 @@ #include "editsample.h" #include "ui_editsample.h" -EditSample::EditSample(QWidget *parent) : QDialog(parent), ui(new Ui::EditSample) { +#include "sound.h" +#include +#include + +EditSample::EditSample(const std::string& audioFile_, QWidget *parent) : QDialog(parent), ui(new Ui::EditSample), audioFile(audioFile_) { ui->setupUi(this); + + std::vector sounddevices = Sound::instance().getOutputs(); + for(const std::string& dev : sounddevices) { + QString qdeviceName = QString::fromStdString(dev); + ui->soundOutputselect->addItem(qdeviceName, qdeviceName); + } + + QObject::connect(ui->playButton, SIGNAL( clicked() ), this, SLOT( play() )); + QObject::connect(ui->stopButton, SIGNAL( clicked() ), this, SLOT( stop() )); } EditSample::~EditSample() { delete ui; } + +void EditSample::play() { + // get audio device + std::string devicename = ui->soundOutputselect->currentText().toStdString(); + + // get timing info + uint64_t startTime = getTimeInfo(ui->startTime); + uint64_t lengthTime = getTimeInfo(ui->lengthTime); + + // 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; + Sound::instance().addPlayback(devicename, audioFile, 1.0, startTime, lengthTime); +} + +void EditSample::stop() { + Sound::instance().stopAll(); +} + +uint64_t EditSample::getTimeInfo(const QTimeEdit* time) const { + QTime timedata = time->time(); + return ((((( + (timedata.hour() * 60) + + timedata.minute()) * 60) + + timedata.second()) * 1000) + + timedata.msec()); +} + +void EditSample::setCurrentPosition(uint64_t cp) { + currentposition = cp; + +} \ No newline at end of file diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0b4fbdb..0e3ce0d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -151,10 +151,11 @@ void MainWindow::addSample() { std::string file = qfile.toStdString(); file = file.substr(file.rfind('/')+1); Log::info << "selected file: " << file; - } - EditSample esample(this); - esample.exec(); + //spawn edit dialog + EditSample esample(file, this); + esample.exec(); + } } diff --git a/src/sound.cpp b/src/sound.cpp index 2db208f..031d08b 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -74,6 +74,19 @@ void Sound::addPlayback(const std::string& name, float volume, uint64_t beginms, } } +void Sound::addPlayback(const std::string& audioDeviceName, const std::string& name, float volume, uint64_t beginms, uint64_t endms) { + 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); + break; + } + } +} + bool Sound::addDefaultDevice() { SoundDevice* sd = SoundDevice::createDevice(&context); if(sd) { @@ -113,6 +126,16 @@ SampleReader* Sound::openFile(const std::string& name) { return SampleReader::createSampleReader(FOLDER + name); } +std::vector Sound::getOutputs() { + std::lock_guard lock(devicesMutex); + std::vector out; + out.reserve(devices.size()); + for(SoundDevice* sd : devices) { + out.push_back(sd->getName()); + } + return out; +} + Sound::Sound() { init(); startBackgroundThread(); diff --git a/src/sounddevice.cpp b/src/sounddevice.cpp index e662fca..87103da 100644 --- a/src/sounddevice.cpp +++ b/src/sounddevice.cpp @@ -40,6 +40,9 @@ static ma_uint64 readDecoderandAdd(ma_decoder* decoder, float volume, unsigned i return frameCountRead; } +std::string SoundDevice::getName() const { + return device.playback.name; +} void SoundDevice::sound_callback(void* outbuffer, ma_uint32 frameCount) { ma_mutex_lock(data.mutex); diff --git a/ui/editsample.ui b/ui/editsample.ui index 88d9046..57b8e90 100644 --- a/ui/editsample.ui +++ b/ui/editsample.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 300 + 465 + 320 @@ -26,13 +26,6 @@ - - - - false - - - @@ -65,21 +58,57 @@ + + + + + + AudioDevice: + + + + + + + false + + + + + + + + + + + Play + + + + + + + Stop + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Save + + + false + + + + + - - - - Qt::Horizontal - - - QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Save - - - false - - - @@ -98,8 +127,8 @@ accept() - 248 - 254 + 257 + 305 157 @@ -114,8 +143,8 @@ reject() - 316 - 260 + 325 + 305 286 @@ -123,5 +152,21 @@ + + buttonBox + rejected() + stopButton + click() + + + 407 + 299 + + + 132 + 301 + + +