Now on pause fill the buffer with silent instead stopping.

Abandono esta rama de desarrollo:
1. Al cambiar el volumen se escucha ruido
2. QAudioOutput::reset() no funciona correctamente
3. Hay muchos pops en la reproducción
This commit is contained in:
Santi Noreña 2014-07-15 17:27:11 +02:00
parent fe4d118cfb
commit 37835e7571
3 changed files with 60 additions and 31 deletions

View file

@ -5,6 +5,8 @@
#include <QFile>
#include <QTime>
#include <time.h> // nanosleep y la chapuza de liberar el buffer
AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
QGroupBox(parent)
, m_suspendResumeButton(0)
@ -19,7 +21,8 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
, m_progressCounter(new QTime(0,0,0,1))
, m_progressMs(0)
, m_currentMedia(" ")
, m_running(false)
, m_emptyBuffer()
{
this->setTitle(name);
@ -122,6 +125,11 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
m_format.setByteOrder(QAudioFormat::LittleEndian);
m_format.setSampleType(QAudioFormat::SignedInt);
// Init the silent buffer
QAudioBuffer *abuf = new QAudioBuffer(44100, m_format, -1);
QByteArray ba((const char*)abuf->data(), abuf->byteCount());
m_emptyBuffer.append(ba);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(m_format)) {
qWarning("Default format not supported - trying to use nearest");
@ -139,7 +147,6 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
m_watchDMX->start(1000);
createAudioOutput();
pause();
}
AudioLayerWidget::~AudioLayerWidget()
@ -152,10 +159,13 @@ void AudioLayerWidget::createAudioOutput()
m_audioOutput = new QAudioOutput(m_device, m_format, this);
Q_CHECK_PTR(m_audioOutput);
m_audioOutput->setNotifyInterval(NOTIFY_INTERVAL);
m_audioOutput->setCategory("LibreMediaServer");
connect(m_audioOutput, SIGNAL(notify()), SLOT(notified()));
reset();
m_output = m_audioOutput->start();
Q_CHECK_PTR(m_output);
m_statusValue->setText(PAUSE_LABEL);
connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(handleStateChanged(QAudio::State)));
m_pullTimer->start(PULL_TIMER_INTERVAL);
}
void AudioLayerWidget::deviceChanged(int index)
@ -220,6 +230,7 @@ void AudioLayerWidget::pullTimerExpired()
if (m_audioOutput && m_audioOutput->state() != QAudio::StoppedState) {
int chunks = m_audioOutput->bytesFree() / m_audioOutput->periodSize();
while (chunks) {
if (m_running) {
const qint64 len = m_decoder->read(m_buffer.data(), m_audioOutput->periodSize());
if ( len == -1) {
stop();
@ -231,11 +242,13 @@ void AudioLayerWidget::pullTimerExpired()
}
if (len != m_audioOutput->periodSize())
break;
} else {
m_output->write(m_emptyBuffer.data(), m_audioOutput->periodSize());
}
--chunks;
}
}
}
void AudioLayerWidget::toggleSuspendResume()
{
if (m_audioOutput->state() == QAudio::SuspendedState) {
@ -246,7 +259,8 @@ void AudioLayerWidget::toggleSuspendResume()
play();
} else if (m_audioOutput->state() == QAudio::IdleState) {
qWarning("%s: IdleState", this->title().toLatin1().constData());
play();
reset();
resume();
}
}
@ -254,12 +268,16 @@ void AudioLayerWidget::handleStateChanged(QAudio::State state)
{
if (state == QAudio::SuspendedState) {
m_statusValue->setText(PAUSE_LABEL);
qDebug("Pause");
} else if (state == QAudio::ActiveState) {
m_statusValue->setText(PLAY_LABEL);
qDebug("Play");
} else if (m_audioOutput->state() == QAudio::StoppedState) {
m_statusValue->setText(STOP_LABEL);
qDebug("Stop");
} else if (state == QAudio::IdleState) {
m_statusValue->setText(IDDLE_LABEL);
qDebug("Iddle");
}
}
@ -273,10 +291,10 @@ void AudioLayerWidget::durationChanged(qint64 dur)
void AudioLayerWidget::play()
{
// reset();
setInitPosition();
m_pullTimer->start(PULL_TIMER_INTERVAL);
m_audioOutput->resume();
// m_pullTimer->start(PULL_TIMER_INTERVAL);
// m_audioOutput->resume();
m_running = true;
pullTimerExpired();
m_progressCounter->start();
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
@ -284,15 +302,17 @@ void AudioLayerWidget::play()
void AudioLayerWidget::pause()
{
m_audioOutput->suspend();
m_pullTimer->stop();
// m_pullTimer->stop();
// m_audioOutput->suspend();
m_running = false;
m_suspendResumeButton->setText(tr(RESUME_LABEL));
}
void AudioLayerWidget::resume()
{
m_pullTimer->start(PULL_TIMER_INTERVAL);
m_audioOutput->resume();
// m_pullTimer->start(PULL_TIMER_INTERVAL);
// m_audioOutput->resume();
m_running = true;
pullTimerExpired();
m_progressCounter->start();
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
@ -300,7 +320,7 @@ void AudioLayerWidget::resume()
void AudioLayerWidget::stop()
{
// reset();
m_running = false;
setInitPosition();
}
@ -310,12 +330,11 @@ void AudioLayerWidget::reset()
m_audioOutput->reset();
m_output = m_audioOutput->start();
Q_CHECK_PTR(m_output);
// pause();
}
void AudioLayerWidget::setInitPosition()
{
reset();
// reset();
m_decoder->setPos(0);
m_progressMs = 0;
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(m_progressMs));
@ -330,3 +349,8 @@ void AudioLayerWidget::updateWatchDMX(bool b)
{
m_receiveDMX->setChecked(b);
}
void AudioLayerWidget::flushBuffer()
{
// Esto es una chapuza para arreglar que reset() no limpia el buffer realmente
}

View file

@ -79,11 +79,16 @@ private:
QString m_currentMedia;
bool m_running;
QByteArray m_emptyBuffer;
void createAudioOutput();
void reset();
void setInitPosition();
void flushBuffer();
private slots:
void notified();
void pullTimerExpired();

View file

@ -7,8 +7,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>60</width>
<height>57</height>
<width>126</width>
<height>89</height>
</rect>
</property>
<property name="windowTitle">
@ -20,7 +20,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>60</width>
<width>126</width>
<height>29</height>
</rect>
</property>