funcionando con dmx, controles Ui muestran info pero no actúan sobre el

sonido.
Refactorizado todos lo métodos que interactúan con el sonido a miniaudioengine
This commit is contained in:
snt 2024-04-22 19:14:49 +02:00
parent 7aea8f6cf1
commit 521f1fc6d7
12 changed files with 516 additions and 501 deletions

View file

@ -4,59 +4,50 @@
AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
QGroupBox(parent)
, m_suspendResumeButton(0)
, m_refreshGUI(new QTimer(this))
, m_currentMedia("")
, m_mediaLoaded(false)
, m_volumeIndicator(new QSpinBox)
, m_panIndicator(new QSpinBox)
, m_pitchIndicator(new QSpinBox)
{
this->setTitle(name);
QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *status = new QHBoxLayout;
QGridLayout *status = new QGridLayout;
m_statusLabel = new QLabel;
m_statusLabel->setText(STATUS_LABEL);
m_statusValue = new QLabel;
status->addWidget(m_statusLabel);
status->addWidget(m_statusValue);
m_loopCheck = new QCheckBox();
m_loopCheckLabel = new QLabel;
m_loopCheckLabel->setText("Loop");
connect(m_loopCheck, SIGNAL(stateChanged(int)), this, SLOT(loopChanged(int)));
status->addWidget(m_loopCheck);
status->addWidget(m_loopCheckLabel);
layout->addLayout(status);
QHBoxLayout *folderLoaded = new QHBoxLayout;
status->addWidget(m_statusLabel, 0, 0);
status->addWidget(m_statusValue, 0, 2);
m_folderLabel = new QLabel;
m_folderLabel->setText(FOLDER_LABEL);
m_folderValue = new QLabel;
m_folderValue->setMaximumWidth(200);
folderLoaded->addWidget(m_folderLabel);
folderLoaded->addWidget(m_folderValue);
layout->addLayout(folderLoaded);
QHBoxLayout *fileLoaded = new QHBoxLayout;
status->addWidget(m_folderLabel, 1, 0);
status->addWidget(m_folderValue, 1, 1);
m_fileLabel = new QLabel;
m_fileLabel->setText(FILE_LABEL);
m_fileValue = new QLabel;
m_fileValue->setMaximumWidth(200);
fileLoaded->addWidget(m_fileLabel);
fileLoaded->addWidget(m_fileValue);
layout->addLayout(fileLoaded);
status->addWidget(m_fileLabel, 1, 2);
status->addWidget(m_fileValue, 1, 3);
layout->addLayout(status);
QGridLayout *volumeBox = new QGridLayout;
m_volumeLabel = new QLabel;
m_volumeLabel->setText(tr(VOLUME_LABEL));
m_volumeSlider = new QSlider(Qt::Horizontal);
m_volumeSlider->setMinimum(-100);
m_volumeSlider->setMinimum(0);
m_volumeSlider->setMaximum(100);
m_volumeSlider->setSingleStep(1);
m_volumeIndicator = new QLabel;
m_volumeIndicator->setRange(0, 100);
m_volumeIndicator->setValue(0);
m_volumeIndicator->setMaximumWidth(40);
m_volumeIndicator->setButtonSymbols(QAbstractSpinBox::NoButtons);
volumeBox->addWidget(m_volumeLabel, 0, 0);
volumeBox->addWidget(m_volumeSlider, 0, 1);
volumeBox->addWidget(m_volumeIndicator, 0, 2);
connect(m_volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(volumeChanged(int)));
connect(m_volumeSlider, &QSlider::valueChanged, this, [=] () {
m_volumeIndicator->setText(QString::number(m_volumeSlider->value()));
m_volumeIndicator->setValue(m_volumeSlider->value());
});
m_panLabel = new QLabel;
m_panLabel->setText("Pan");
@ -64,18 +55,34 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
m_panSlider->setMinimum(0);
m_panSlider->setMaximum(255);
m_panSlider->setSingleStep(1);
m_panIndicator->setRange(0, 255);
m_panIndicator->setValue(128);
m_panIndicator->setMaximumWidth(40);
m_panIndicator->setButtonSymbols(QAbstractSpinBox::NoButtons);
connect(m_panSlider, &QSlider::valueChanged, this, [=] () {
m_panIndicator->setValue(m_panSlider->value());
});
connect(m_panSlider, SIGNAL(valueChanged(int)), this, SLOT(panChanged(int)));
volumeBox->addWidget(m_panLabel, 1, 0);
volumeBox->addWidget(m_panSlider, 1, 1);
volumeBox->addWidget(m_panIndicator, 1, 2);
m_pitchLabel = new QLabel;
m_pitchLabel->setText("Pitch");
m_pitchSlider = new QSlider(Qt::Horizontal);
m_pitchSlider->setMinimum(0);
m_pitchSlider->setMaximum(255);
m_pitchSlider->setSingleStep(1);
m_pitchIndicator->setRange(0, 255);
m_pitchIndicator->setValue(128);
m_pitchIndicator->setMaximumWidth(40);
m_pitchIndicator->setButtonSymbols(QAbstractSpinBox::NoButtons);
connect(m_pitchSlider, &QSlider::valueChanged, this, [=] () {
m_pitchIndicator->setValue(m_pitchSlider->value());
});
connect(m_pitchSlider, SIGNAL(valueChanged(int)), this, SLOT(pitchChanged(int)));
volumeBox->addWidget(m_pitchLabel, 2, 0);
volumeBox->addWidget(m_pitchSlider, 2, 1);
volumeBox->addWidget(m_pitchIndicator, 2, 2);
layout->addLayout(volumeBox);
QHBoxLayout *progressTime = new QHBoxLayout;
@ -102,15 +109,12 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
m_progressSlider = new QSlider(Qt::Horizontal);
layout->addWidget(m_progressSlider);
m_suspendResumeButton = new QPushButton(this);
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspendResume()));
layout->addWidget(m_suspendResumeButton);
this->setLayout(layout);
connect(m_refreshGUI, SIGNAL(timeout()), this, SLOT(refreshGUI()));
m_refreshGUI->start(100);
}
AudioLayerWidget::~AudioLayerWidget()
@ -118,123 +122,69 @@ AudioLayerWidget::~AudioLayerWidget()
}
// From UI.
void AudioLayerWidget::volumeChanged(int value)
{
float result;
if (m_mediaLoaded == false)
return;
result = ma_volume_linear_to_db(value);
ma_sound_group_set_volume(&currentSound, result);
(void)value;
// ToDo: call the audio engine
}
void AudioLayerWidget::panChanged(int value)
{
float result;
if (m_mediaLoaded == false)
return;
result = (value / 128.0) - 128;
ma_sound_group_set_pan(&currentSound, result);
(void)value;
// ToDo: call the audio engine
}
void AudioLayerWidget::pitchChanged(int value)
{
float result;
if (m_mediaLoaded == false)
return;
result = (value / 128.0) - 128;
ma_sound_group_set_pitch(&currentSound, result);
(void)value;
// ToDo: call the audio engine
}
void AudioLayerWidget::loopChanged(int value)
{
if (m_mediaLoaded == false)
return;
ma_sound_set_looping(&currentSound, value);
(void)value;
// ToDo: call the audio engine
}
void AudioLayerWidget::toggleSuspendResume()
{
switch (m_status) {
case Status::PlayingLoop:
case Status::PlayingOnce:
this->setPlaybackStatus(Status::Paused);
case Status::Paused:
case Status::Stopped:
this->setPlaybackStatus(Status::PlayingOnce);
}
// ToDo: call the audio engine
}
// from DMX signals
void AudioLayerWidget::setVol(qreal vol)
{
this->volumeChanged(vol);
m_volumeSlider->blockSignals(true);
m_volumeSlider->setValue(vol);
m_volumeIndicator->setText(QString::number(vol));
m_volumeIndicator->setValue(vol);
m_volumeSlider->blockSignals(false);
}
void AudioLayerWidget::setPan(qreal pan)
{
this->panChanged(pan);
m_panSlider->blockSignals(true);
m_panSlider->setValue(pan);
m_panIndicator->setValue(pan);
m_panSlider->blockSignals(false);
}
void AudioLayerWidget::setPitch(qreal pitch)
{
this->pitchChanged(pitch);
m_pitchSlider->blockSignals(true);
m_pitchSlider->setValue(pitch);
m_pitchIndicator->setValue(pitch);
m_pitchSlider->blockSignals(false);
}
void AudioLayerWidget::loadMedia(QString file)
{
ma_result result;
ma_format *format = 0;
ma_uint32 *channels = 0;
ma_uint32 *sampleRate = 0;
if (m_currentMedia.compare(file) == 0 ) {
return;
}
if (!QFile::exists(file)) {
qWarning("Can not access to file %s", file.toLatin1().constData());
return;
}
ma_engine engine = AudioWidget::getInstance()->getEngine();
if (currentSound.ownsDataSource == true)
{
ma_sound_uninit(&currentSound);
}
result = ma_sound_init_from_file(&engine, file.toLatin1(), MA_SOUND_FLAG_NO_SPATIALIZATION | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_STREAM, NULL, NULL, &currentSound);
if (result != MA_SUCCESS) {
qWarning("WARNING: Failed to load sound %s", file.toLatin1().constData());
return;
}
m_currentMedia = file;
float pLength = this->getDuration();
result = ma_sound_get_data_format(&currentSound, format, channels, sampleRate, NULL, 0);
if (result != MA_SUCCESS) {
qWarning("WARNING: Failed to get data format %s", file.toLatin1().constData());
return;
}
m_mediaLoaded = true;
durationChanged(pLength * 1000);
fileLoaded(file);
// Display music informations
std::cout << "File loaded: " << file.toLatin1().constData() << " : " << std::endl;
std::cout << " " << pLength << " seconds";
std::cout << " -- " << sampleRate << " samples/sec";
std::cout << " -- " << format << " format";
std::cout << " -- " << channels << " channels" << std::endl;
}
float AudioLayerWidget::getDuration()
{
float pLength;
if (m_mediaLoaded == false)
return 0;
ma_result result = ma_sound_get_length_in_seconds(&currentSound, &pLength);
if (result != MA_SUCCESS) {
qWarning("WARNING: Failed to get total duration %s", m_currentMedia.toLatin1().constData());
return 0;
}
return pLength;
}
void AudioLayerWidget::fileLoaded(QString file)
{
QStringList list = file.split("/");
@ -245,74 +195,40 @@ void AudioLayerWidget::fileLoaded(QString file)
}
}
void AudioLayerWidget::setPlaybackStatus(Status status)
{
m_status = status;
if (status == Status::Stopped)
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(0));
QString tmp;
switch (status) {
case Status::Paused:
tmp.append("Paused");
break;
case Status::PlayingLoop:
tmp.append("Playing Loop");
break;
case Status::PlayingOnce:
tmp.append("Playing one");
break;
case Status::Stopped:
tmp.append("Stopped");
break;
}
m_statusValue->setText(tmp);
m_suspendResumeButton->setText(tmp);
}
void AudioLayerWidget::durationChanged(qint64 dur)
{
dur *= 1000;
m_progressSlider->setMaximum(dur);
m_totalTimeValue->setTime(QTime::fromMSecsSinceStartOfDay(dur));
}
void AudioLayerWidget::play(bool loop)
void AudioLayerWidget::refreshUi(float progress)
{
if (m_mediaLoaded == false)
return;
ma_sound_set_looping(&currentSound, loop);
ma_sound_start(&currentSound);
m_loopCheck->blockSignals(true);
m_loopCheck->setChecked(loop);
m_loopCheck->blockSignals(false);
}
void AudioLayerWidget::pause()
{
if (m_mediaLoaded == false)
return;
ma_sound_stop(&currentSound);
}
void AudioLayerWidget::stop()
{
if (m_mediaLoaded == false)
return;
ma_sound_stop(&currentSound);
ma_sound_seek_to_pcm_frame(&currentSound, 0);
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(0));
m_statusValue->setText(STOP_LABEL);
m_suspendResumeButton->setText(tr(STOP_LABEL));
m_progressSlider->setValue(0);
}
void AudioLayerWidget::refreshGUI() {
float progress;
if (m_mediaLoaded == false)
return;
if (currentSound.ownsDataSource == 0)
return;
switch (ma_sound_is_playing(&currentSound)) {
case true:
ma_sound_get_cursor_in_seconds(&currentSound, &progress);
progress *= 1000;
m_progressSlider->setValue(progress);
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(progress));
m_statusValue->setText(PLAY_LABEL);
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
break;
case false:
m_statusValue->setText(PAUSE_LABEL);
m_suspendResumeButton->setText(tr(RESUME_LABEL));
break;
}
}
void AudioLayerWidget::toggleSuspendResume()
{
if (m_mediaLoaded == false)
return;
if (ma_sound_is_playing(&currentSound)) {
this->pause();
} else {
if (ma_sound_at_end(&currentSound))
ma_sound_seek_to_pcm_frame(&currentSound, 0);
ma_sound_start(&currentSound);
}
progress *= 1000;
m_progressSlider->setValue(progress);
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(progress));
}