sound callbacks
This commit is contained in:
parent
7f3445de54
commit
77bfc4a59a
|
@ -25,6 +25,8 @@ private:
|
|||
|
||||
const std::string audioFile;
|
||||
uint64_t getTimeInfo(const QTimeEdit* time) const;
|
||||
QTime timeFromMS(uint64_t ms) const;
|
||||
std::string formatTime(const QTime& time) const;
|
||||
|
||||
uint64_t currentposition = 0;
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "miniaudio.h"
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
|
@ -15,11 +16,13 @@
|
|||
|
||||
class Sound {
|
||||
public:
|
||||
using sndcb_func = std::function<void(uint64_t)>;
|
||||
|
||||
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);
|
||||
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 = {});
|
||||
bool addDefaultDevice();
|
||||
bool addDeviceWithName(const std::string& name);
|
||||
void stopAll();
|
||||
|
@ -29,6 +32,7 @@ public:
|
|||
std::vector<std::string> getOutputs();
|
||||
|
||||
static std::string FOLDER;
|
||||
|
||||
private:
|
||||
Sound();
|
||||
~Sound();
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "miniaudio.h"
|
||||
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
@ -16,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);
|
||||
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 startDevice();
|
||||
void cleanupDecoders(); //fertige decoder löschen
|
||||
|
@ -34,6 +35,7 @@ private:
|
|||
uint64_t startFrame = 0;
|
||||
uint64_t endFrame = std::numeric_limits<uint64_t>::max();
|
||||
bool isDone = false;
|
||||
std::function<void(uint64_t)> callback;
|
||||
Playback() {}
|
||||
};
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "sound.h"
|
||||
#include <Log.h>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
EditSample::EditSample(const std::string& audioFile_, QWidget *parent) : QDialog(parent), ui(new Ui::EditSample), audioFile(audioFile_) {
|
||||
ui->setupUi(this);
|
||||
|
@ -16,6 +17,8 @@ EditSample::EditSample(const std::string& audioFile_, QWidget *parent) : QDialog
|
|||
|
||||
QObject::connect(ui->playButton, SIGNAL( clicked() ), this, SLOT( play() ));
|
||||
QObject::connect(ui->stopButton, SIGNAL( clicked() ), this, SLOT( stop() ));
|
||||
|
||||
setCurrentPosition(0);
|
||||
}
|
||||
|
||||
EditSample::~EditSample() {
|
||||
|
@ -36,7 +39,7 @@ void EditSample::play() {
|
|||
}
|
||||
|
||||
Log::info << "play audio: " << std::quoted(audioFile) << " on " << std::quoted(devicename) << " with " << startTime << " and " << lengthTime;
|
||||
Sound::instance().addPlayback(devicename, audioFile, 1.0, startTime, lengthTime);
|
||||
Sound::instance().addPlayback(devicename, audioFile, 1.0, startTime, lengthTime, std::bind(&EditSample::setCurrentPosition, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void EditSample::stop() {
|
||||
|
@ -52,7 +55,24 @@ uint64_t EditSample::getTimeInfo(const QTimeEdit* time) const {
|
|||
+ timedata.msec());
|
||||
}
|
||||
|
||||
QTime EditSample::timeFromMS(uint64_t ms) const {
|
||||
// return QTime().addMSecs(ms);
|
||||
// return QTime(0, 0, 0, ms);
|
||||
return QTime::fromMSecsSinceStartOfDay(ms);
|
||||
}
|
||||
|
||||
std::string EditSample::formatTime(const QTime& time) const {
|
||||
std::ostringstream str;
|
||||
str << std::setw(2) << std::setfill('0')
|
||||
<< time.minute() << ':' << std::setw(2)
|
||||
<< time.second() << '.' << std::setw(3)
|
||||
<< time.msec();
|
||||
return str.str();
|
||||
}
|
||||
|
||||
void EditSample::setCurrentPosition(uint64_t cp) {
|
||||
currentposition = cp;
|
||||
|
||||
QTime time = timeFromMS(cp);
|
||||
std::string formatedTime = formatTime(time);
|
||||
// ui->currentPosLabel->setText(QString::fromStdString("Current: " + formatedTime));
|
||||
}
|
|
@ -74,14 +74,14 @@ 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) {
|
||||
void Sound::addPlayback(const std::string& audioDeviceName, const std::string& name, float volume, uint64_t beginms, uint64_t endms, 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);
|
||||
sd->addPlayback(name, volume, beginms, endms, callback);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,6 +79,10 @@ void SoundDevice::sound_callback(void* outbuffer, ma_uint32 frameCount) {
|
|||
}
|
||||
|
||||
pb->currentFrame += read;
|
||||
|
||||
if(pb->callback) {
|
||||
pb->callback(pb->currentFrame / (pDecoder->outputSampleRate / 1000));
|
||||
}
|
||||
}
|
||||
|
||||
ma_mutex_unlock(data.mutex);
|
||||
|
@ -166,7 +170,7 @@ void SoundDevice::stop() {
|
|||
deviceRunning = false;
|
||||
}
|
||||
|
||||
void SoundDevice::addPlayback(const std::string& name, float volume, uint64_t beginms, uint64_t endms) {
|
||||
void SoundDevice::addPlayback(const std::string& name, float volume, uint64_t beginms, uint64_t endms, std::function<void(uint64_t)> callback) {
|
||||
cleanupDecoders();
|
||||
|
||||
Playback* pb = new Playback();
|
||||
|
@ -180,6 +184,7 @@ void SoundDevice::addPlayback(const std::string& name, float volume, uint64_t be
|
|||
|
||||
pb->volume = volume;
|
||||
pb->isDone = false;
|
||||
pb->callback = callback;
|
||||
|
||||
if(beginms != 0) {
|
||||
pb->startFrame = (pb->decoder.outputSampleRate * beginms) / 1000;
|
||||
|
|
|
@ -26,6 +26,31 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="currentposLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="currentPosLabel">
|
||||
<property name="text">
|
||||
<string>Current: </string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="starttoCurrentButton">
|
||||
<property name="text">
|
||||
<string>SetStart</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="stoptoCurrentButton">
|
||||
<property name="text">
|
||||
<string>SetStop</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="timeRow">
|
||||
<item>
|
||||
|
|
Loading…
Reference in New Issue