Added a DMX watchdog. Changed the play methos, now play reset to 0 and stop not.

This commit is contained in:
santi 2014-07-12 21:53:07 +02:00
parent a6909f8c16
commit 69ffe79742
13 changed files with 276 additions and 154 deletions

View file

@ -9,16 +9,22 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
QGroupBox(parent)
, m_suspendResumeButton(0)
, m_deviceBox(0)
, m_watchDMX(new QTimer(this))
, m_pullTimer(new QTimer(this))
, m_device(QAudioDeviceInfo::defaultOutputDevice())
, m_audioOutput(0)
, m_output(0)
, m_buffer(BufferSize, 0)
, m_decoder(0)
, m_progressCounter(new QTime(0,0,0,1))
, m_progressMs(0)
, m_currentMedia(" ")
{
this->setTitle(name);
QVBoxLayout *layout = new QVBoxLayout;
m_deviceBox = new QComboBox(this);
const QAudioDeviceInfo &defaultDeviceInfo = QAudioDeviceInfo::defaultOutputDevice();
m_deviceBox->addItem(defaultDeviceInfo.deviceName(), qVariantFromValue(defaultDeviceInfo));
@ -26,6 +32,7 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
if (deviceInfo != defaultDeviceInfo)
m_deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo));
}
m_deviceBox->setMaximumWidth(250);
connect(m_deviceBox,SIGNAL(activated(int)),
this, SLOT(deviceChanged(int)));
layout->addWidget(m_deviceBox);
@ -34,6 +41,10 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
m_statusLabel = new QLabel;
m_statusLabel->setText(STATUS_LABEL);
m_statusValue = new QLabel;
m_receiveDMX = new QCheckBox("Receiving DMX", this);
// m_receiveDMX->setCheckable(false);
m_receiveDMX->setChecked(false);
status->addWidget(m_receiveDMX);
status->addWidget(m_statusLabel);
status->addWidget(m_statusValue);
layout->addLayout(status);
@ -54,23 +65,43 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
fileLoaded->addWidget(m_fileValue);
layout->addLayout(fileLoaded);
m_suspendResumeButton = new QPushButton(this);
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspendResume()));
layout->addWidget(m_suspendResumeButton);
QHBoxLayout *volumeBox = new QHBoxLayout;
m_volumeLabel = new QLabel;
m_volumeLabel->setText(tr(VOLUME_LABEL));
m_volumeSlider = new QSlider(Qt::Horizontal);
m_volumeSlider->setMinimum(0);
m_volumeSlider->setMaximum(100);
m_volumeSlider->setMaximum(80);
m_volumeSlider->setSingleStep(1);
connect(m_volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(volumeChanged(int)));
volumeBox->addWidget(m_volumeLabel);
volumeBox->addWidget(m_volumeSlider);
layout->addLayout(volumeBox);
QHBoxLayout *progressTime = new QHBoxLayout;
m_progressTimeLabel = new QLabel;
m_progressTimeLabel->setText(PROGRESS_TIME_LABEL);
m_progressTime = new QTimeEdit;
m_progressTime->setDisplayFormat("h:mm:ss:zzz");
m_progressTime->setReadOnly(true);
m_progressTime->setButtonSymbols(QAbstractSpinBox::NoButtons);
m_progressTime->setMaximumWidth(90);
progressTime->addWidget(m_progressTimeLabel);
progressTime->addWidget(m_progressTime);
// QHBoxLayout *totalTime = new QHBoxLayout;
m_totalTimeLabel = new QLabel;
m_totalTimeLabel->setText(TOTAL_TIME_LABEL);
m_totalTimeValue = new QTimeEdit;
m_totalTimeValue->setDisplayFormat("h:mm:ss:zzz");
m_totalTimeValue->setReadOnly(true);
m_totalTimeValue->setButtonSymbols(QAbstractSpinBox::NoButtons);
m_totalTimeValue->setMaximumWidth(90);
progressTime->addWidget(m_totalTimeLabel);
progressTime->addWidget(m_totalTimeValue);
// layout->addLayout(totalTime);
layout->addLayout(progressTime);
QHBoxLayout *progressSlider = new QHBoxLayout;
m_progressLabel = new QLabel;
m_progressLabel->setText(PROGRESS_LABEL);
@ -79,22 +110,10 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
progressSlider->addWidget(m_progressSlider);
layout->addLayout(progressSlider);
QHBoxLayout *progressTime = new QHBoxLayout;
m_progressTimeLabel = new QLabel;
m_progressTimeLabel->setText(PROGRESS_TIME_LABEL);
m_progressTimeValue = new QLabel;
m_progressTimeValue->setText("0:00:00:000");
progressTime->addWidget(m_progressTimeLabel);
progressTime->addWidget(m_progressTimeValue);
layout->addLayout(progressTime);
QHBoxLayout *totalTime = new QHBoxLayout;
m_totalTimeLabel = new QLabel;
m_totalTimeLabel->setText(TOTAL_TIME_LABEL);
m_totalTimeValue = new QLabel;
totalTime->addWidget(m_totalTimeLabel);
totalTime->addWidget(m_totalTimeValue);
layout->addLayout(totalTime);
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);
@ -107,7 +126,7 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(m_format)) {
qWarning() << "Default format not supported - trying to use nearest";
qWarning("Default format not supported - trying to use nearest");
m_format = info.nearestFormat(m_format);
}
m_decoder = new AudioDecoder(m_format, this);
@ -116,6 +135,10 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
this, SLOT(durationChanged(qint64)));
connect(m_pullTimer, SIGNAL(timeout()),
this, SLOT(pullTimerExpired()));
connect(m_watchDMX, SIGNAL(timeout()),
this, SLOT(watchDMXExpired()));
m_watchDMX->start(1000);
createAudioOutput();
}
@ -127,17 +150,17 @@ AudioLayerWidget::~AudioLayerWidget()
void AudioLayerWidget::createAudioOutput()
{
m_audioOutput = new QAudioOutput(m_device, m_format, this);
m_audioOutput->setNotifyInterval(100);
Q_CHECK_PTR(m_audioOutput);
m_audioOutput->setNotifyInterval(NOTIFY_INTERVAL);
connect(m_audioOutput, SIGNAL(notify()), SLOT(notified()));
reset();
m_statusValue->setText(PAUSE_LABEL);
connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(handleStateChanged(QAudio::State)));
m_output = m_audioOutput->start();
toggleSuspendResume();
}
void AudioLayerWidget::deviceChanged(int index)
{
m_pullTimer->stop();
m_decoder->stop();
m_audioOutput->stop();
m_audioOutput->disconnect(this);
m_device = m_deviceBox->itemData(index).value<QAudioDeviceInfo>();
@ -161,20 +184,31 @@ void AudioLayerWidget::setVol(qreal vol)
void AudioLayerWidget::loadMedia(QString file)
{
stop();
if (m_currentMedia == file)
return;
reset();
if (QFile::exists(file)){
m_decoder->loadMedia(file);
m_currentMedia = file;
fileLoaded(file);
}
}
void AudioLayerWidget::fileLoaded(QString file)
{
QStringList list = file.split("/");
int size = list.size();
if (size >= 2) {
m_folderValue->setText(list.at(size - 2));
m_fileValue->setText(list.at(size - 1));
}
}
void AudioLayerWidget::notified()
{
QTime real(0,0,0,0);
qint64 ms = m_audioOutput->processedUSecs() / 1000 ;
real = real.addMSecs(ms);
m_progressTimeValue->setText(real.toString("h:mm:ss:zzz"));
m_progressSlider->setValue(ms);
m_progressMs += m_progressCounter->restart();
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(m_progressMs));
m_progressSlider->setValue(m_progressMs);
/* qDebug() << "bytesFree = " << m_audioOutput->bytesFree()
<< ", " << "elapsedUSecs = " << m_audioOutput->elapsedUSecs()
<< ", " << "processedUSecs = " << m_audioOutput->processedUSecs();
@ -189,7 +223,7 @@ void AudioLayerWidget::pullTimerExpired()
const qint64 len = m_decoder->read(m_buffer.data(), m_audioOutput->periodSize());
if ( len == -1) {
stop();
qDebug()<< this->title() << " End of file";
qDebug("End of file %s", this->title().toLatin1().constData());
break;
}
if (len) {
@ -205,72 +239,92 @@ void AudioLayerWidget::pullTimerExpired()
void AudioLayerWidget::toggleSuspendResume()
{
if (m_audioOutput->state() == QAudio::SuspendedState) {
// qDebug() << "status: Suspended, resume()";
m_audioOutput->resume();
m_pullTimer->start(20);
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
m_statusValue->setText(PLAY_LABEL);
resume();
} else if (m_audioOutput->state() == QAudio::ActiveState) {
// qDebug() << "status: Active, suspend()";
m_audioOutput->suspend();
m_pullTimer->stop();
m_suspendResumeButton->setText(tr(RESUME_LABEL));
m_statusValue->setText(PAUSE_LABEL);
pause();
} else if (m_audioOutput->state() == QAudio::StoppedState) {
// qDebug() << "status: Stopped, resume()";
play();
} else if (m_audioOutput->state() == QAudio::IdleState) {
// qDebug() << "status: IdleState";
m_statusValue->setText(IDDLE_LABEL);
qWarning("%s: IdleState", this->title().toLatin1().constData());
play();
}
}
void AudioLayerWidget::handleStateChanged(QAudio::State state)
{
qDebug() << this->title() << " state = " << state;
if (state == QAudio::SuspendedState) {
m_statusValue->setText(PAUSE_LABEL);
} else if (state == QAudio::ActiveState) {
m_statusValue->setText(PLAY_LABEL);
} else if (m_audioOutput->state() == QAudio::StoppedState) {
m_statusValue->setText(STOP_LABEL);
} else if (state == QAudio::IdleState) {
m_statusValue->setText(IDDLE_LABEL);
}
}
void AudioLayerWidget::durationChanged(qint64 dur)
{
if (dur == -1)
return;
QTime temp(0,0,0,0);
QTime real = temp.addMSecs(dur);
m_totalTimeValue->setText(real.toString("h:mm:ss:zzz"));
m_totalTimeValue->setTime(QTime::fromMSecsSinceStartOfDay(dur));
m_progressSlider->setMaximum(dur);
qDebug() << this->title() << " Duration Changed: " << dur;
}
void AudioLayerWidget::fileLoaded(QString file)
{
QStringList list = file.split("/");
int size = list.size();
if (size >= 2) {
m_folderValue->setText(list.at(size - 2));
m_fileValue->setText(list.at(size - 1));
}
}
void AudioLayerWidget::play()
{
// m_decoder->setPos(0);
m_pullTimer->start(20);
setInitPosition();
m_pullTimer->start(PULL_TIMER_INTERVAL);
m_audioOutput->resume();
m_statusValue->setText(PLAY_LABEL);
pullTimerExpired();
m_progressCounter->start();
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
}
void AudioLayerWidget::pause()
{
toggleSuspendResume();
m_audioOutput->suspend();
m_pullTimer->stop();
m_suspendResumeButton->setText(tr(RESUME_LABEL));
}
void AudioLayerWidget::resume()
{
m_pullTimer->start(PULL_TIMER_INTERVAL);
m_audioOutput->resume();
pullTimerExpired();
m_progressCounter->start();
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
}
void AudioLayerWidget::stop()
{
m_audioOutput->suspend();
m_pullTimer->stop();
// m_audioOutput->reset();
m_decoder->setPos(44);
m_statusValue->setText(STOP_LABEL);
m_suspendResumeButton->setText(tr(RESUME_LABEL));
reset();
setInitPosition();
}
void AudioLayerWidget::reset()
{
m_pullTimer->stop();
m_audioOutput->reset();
m_output = m_audioOutput->start();
Q_CHECK_PTR(m_output);
pause();
}
void AudioLayerWidget::setInitPosition()
{
m_decoder->setPos(0);
m_progressMs = 0;
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(m_progressMs));
m_progressSlider->setValue(m_progressMs);
}
void AudioLayerWidget::watchDMXExpired() {
m_receiveDMX->setChecked(false);
}
void AudioLayerWidget::updateWatchDMX(bool b)
{
m_receiveDMX->setChecked(b);
}