saimple sample "editing" working

This commit is contained in:
mrbesen 2021-12-20 01:30:04 +01:00
parent 7246ab94b7
commit 7f3445de54
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
8 changed files with 171 additions and 37 deletions

View File

@ -1,7 +1,8 @@
#ifndef EDITSAMPLE_H
#define EDITSAMPLE_H
#pragma once
#include <QDialog>
#include <QTimeEdit>
#include <string>
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);
};

View File

@ -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<std::string> getOutputs();
static std::string FOLDER;
private:

View File

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

View File

@ -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 <Log.h>
#include <iomanip>
EditSample::EditSample(const std::string& audioFile_, QWidget *parent) : QDialog(parent), ui(new Ui::EditSample), audioFile(audioFile_) {
ui->setupUi(this);
std::vector<std::string> 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;
}

View File

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

View File

@ -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<std::mutex> 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<std::string> Sound::getOutputs() {
std::lock_guard<std::mutex> lock(devicesMutex);
std::vector<std::string> out;
out.reserve(devices.size());
for(SoundDevice* sd : devices) {
out.push_back(sd->getName());
}
return out;
}
Sound::Sound() {
init();
startBackgroundThread();

View File

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

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>465</width>
<height>320</height>
</rect>
</property>
<property name="windowTitle">
@ -26,13 +26,6 @@
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="soundOutputselect">
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="timeRow">
<item>
@ -65,21 +58,57 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="soundOutputselectLayout">
<item>
<widget class="QLabel" name="soundOutputselectLabel">
<property name="text">
<string>AudioDevice:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="soundOutputselect">
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="buttonLayout">
<item>
<widget class="QPushButton" name="playButton">
<property name="text">
<string>Play</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="stopButton">
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
<property name="centerButtons">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
<property name="centerButtons">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
@ -98,8 +127,8 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
<x>257</x>
<y>305</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
@ -114,8 +143,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
<x>325</x>
<y>305</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
@ -123,5 +152,21 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>stopButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>407</x>
<y>299</y>
</hint>
<hint type="destinationlabel">
<x>132</x>
<y>301</y>
</hint>
</hints>
</connection>
</connections>
</ui>