summaryrefslogtreecommitdiff
path: root/Audio
diff options
context:
space:
mode:
Diffstat (limited to 'Audio')
-rw-r--r--Audio/Audio.pri5
-rw-r--r--Audio/AudioDevice.cpp1
-rw-r--r--Audio/AudioDevice.hpp60
-rw-r--r--Audio/soundin.cpp84
-rw-r--r--Audio/soundin.h10
-rw-r--r--Audio/soundout.cpp130
-rw-r--r--Audio/soundout.h13
7 files changed, 175 insertions, 128 deletions
diff --git a/Audio/Audio.pri b/Audio/Audio.pri
new file mode 100644
index 00000000..e2237bbd
--- /dev/null
+++ b/Audio/Audio.pri
@@ -0,0 +1,5 @@
+SOURCES += Audio/AudioDevice.cpp Audio/BWFFile.cpp Audio/soundin.cpp \
+ Audio/soundout.cpp
+
+HEADERS += Audio/AudioDevice.hpp Audio/BWFFile.hpp Audio/soundin.h \
+ Audio/soundout.h
diff --git a/Audio/AudioDevice.cpp b/Audio/AudioDevice.cpp
index 2b852582..16fec314 100644
--- a/Audio/AudioDevice.cpp
+++ b/Audio/AudioDevice.cpp
@@ -7,4 +7,3 @@ bool AudioDevice::initialize (OpenMode mode, Channel channel)
// open and ensure we are unbuffered if possible
return QIODevice::open (mode | QIODevice::Unbuffered);
}
-
diff --git a/Audio/AudioDevice.hpp b/Audio/AudioDevice.hpp
index c4354687..2d47af89 100644
--- a/Audio/AudioDevice.hpp
+++ b/Audio/AudioDevice.hpp
@@ -33,8 +33,8 @@ public:
Channel channel () const {return m_channel;}
protected:
- AudioDevice (QObject * parent = 0)
- : QIODevice (parent)
+ AudioDevice (QObject * parent = nullptr)
+ : QIODevice {parent}
{
}
@@ -43,23 +43,23 @@ protected:
qint16 const * begin (reinterpret_cast<qint16 const *> (source));
for ( qint16 const * i = begin; i != begin + numFrames * (bytesPerFrame () / sizeof (qint16)); i += bytesPerFrame () / sizeof (qint16))
{
- switch (m_channel)
- {
- case Mono:
- *dest++ = *i;
- break;
-
- case Right:
- *dest++ = *(i + 1);
- break;
-
- case Both: // should be able to happen but if it
- // does we'll take left
- Q_ASSERT (Both == m_channel);
- case Left:
- *dest++ = *i;
- break;
- }
+ switch (m_channel)
+ {
+ case Mono:
+ *dest++ = *i;
+ break;
+
+ case Right:
+ *dest++ = *(i + 1);
+ break;
+
+ case Both: // should be able to happen but if it
+ // does we'll take left
+ Q_ASSERT (Both == m_channel);
+ case Left:
+ *dest++ = *i;
+ break;
+ }
}
}
@@ -68,23 +68,23 @@ protected:
switch (m_channel)
{
case Mono:
- *dest++ = sample;
- break;
+ *dest++ = sample;
+ break;
case Left:
- *dest++ = sample;
- *dest++ = 0;
- break;
+ *dest++ = sample;
+ *dest++ = 0;
+ break;
case Right:
- *dest++ = 0;
- *dest++ = sample;
- break;
+ *dest++ = 0;
+ *dest++ = sample;
+ break;
case Both:
- *dest++ = sample;
- *dest++ = sample;
- break;
+ *dest++ = sample;
+ *dest++ = sample;
+ break;
}
return dest;
}
diff --git a/Audio/soundin.cpp b/Audio/soundin.cpp
index e08d4527..53779b34 100644
--- a/Audio/soundin.cpp
+++ b/Audio/soundin.cpp
@@ -1,5 +1,7 @@
#include "soundin.h"
+#include <cstdlib>
+#include <cmath>
#include <QAudioDeviceInfo>
#include <QAudioFormat>
#include <QAudioInput>
@@ -8,11 +10,9 @@
#include "moc_soundin.cpp"
-bool SoundInput::audioError () const
+bool SoundInput::checkStream ()
{
- bool result (true);
-
- Q_ASSERT_X (m_stream, "SoundInput", "programming error");
+ bool result (false);
if (m_stream)
{
switch (m_stream->error ())
@@ -25,23 +25,26 @@ bool SoundInput::audioError () const
Q_EMIT error (tr ("An error occurred during read from the audio input device."));
break;
- case QAudio::UnderrunError:
- Q_EMIT error (tr ("Audio data not being fed to the audio input device fast enough."));
- break;
+ // case QAudio::UnderrunError:
+ // Q_EMIT error (tr ("Audio data not being fed to the audio input device fast enough."));
+ // break;
case QAudio::FatalError:
Q_EMIT error (tr ("Non-recoverable error, audio input device not usable at this time."));
break;
+ case QAudio::UnderrunError: // TODO G4WJS: stop ignoring this
+ // when we find the cause on macOS
case QAudio::NoError:
- result = false;
+ result = true;
break;
}
}
return result;
}
-void SoundInput::start(QAudioDeviceInfo const& device, int framesPerBuffer, AudioDevice * sink, unsigned downSampleFactor, AudioDevice::Channel channel)
+void SoundInput::start(QAudioDeviceInfo const& device, int framesPerBuffer, AudioDevice * sink
+ , unsigned downSampleFactor, AudioDevice::Channel channel)
{
Q_ASSERT (sink);
@@ -62,28 +65,37 @@ void SoundInput::start(QAudioDeviceInfo const& device, int framesPerBuffer, Audi
Q_EMIT error (tr ("Requested input audio format is not valid."));
return;
}
-
- if (!device.isFormatSupported (format))
+ else if (!device.isFormatSupported (format))
{
// qDebug () << "Nearest supported audio format:" << device.nearestFormat (format);
Q_EMIT error (tr ("Requested input audio format is not supported on device."));
return;
}
-// qDebug () << "Selected audio input format:" << format;
+ // qDebug () << "Selected audio input format:" << format;
m_stream.reset (new QAudioInput {device, format});
- if (audioError ())
+ if (!checkStream ())
{
return;
}
connect (m_stream.data(), &QAudioInput::stateChanged, this, &SoundInput::handleStateChanged);
-
- m_stream->setBufferSize (m_stream->format ().bytesForFrames (framesPerBuffer));
- if (sink->initialize (QIODevice::WriteOnly, channel))
+ connect (m_stream.data(), &QAudioInput::notify, [this] () {checkStream ();});
+
+ //qDebug () << "SoundIn default buffer size (bytes):" << m_stream->bufferSize () << "period size:" << m_stream->periodSize ();
+ // the Windows MME version of QAudioInput uses 1/5 of the buffer
+ // size for period size other platforms seem to optimize themselves
+#if defined (Q_OS_WIN)
+ m_stream->setBufferSize (m_stream->format ().bytesForFrames (framesPerBuffer * 5));
+#else
+ Q_UNUSED (framesPerBuffer);
+#endif
+ if (m_sink->initialize (QIODevice::WriteOnly, channel))
{
m_stream->start (sink);
- audioError ();
+ checkStream ();
+ cummulative_lost_usec_ = -1;
+ //qDebug () << "SoundIn selected buffer size (bytes):" << m_stream->bufferSize () << "peirod size:" << m_stream->periodSize ();
}
else
{
@@ -96,7 +108,7 @@ void SoundInput::suspend ()
if (m_stream)
{
m_stream->suspend ();
- audioError ();
+ checkStream ();
}
}
@@ -111,14 +123,12 @@ void SoundInput::resume ()
if (m_stream)
{
m_stream->resume ();
- audioError ();
+ checkStream ();
}
}
-void SoundInput::handleStateChanged (QAudio::State newState) const
+void SoundInput::handleStateChanged (QAudio::State newState)
{
- // qDebug () << "SoundInput::handleStateChanged: newState:" << newState;
-
switch (newState)
{
case QAudio::IdleState:
@@ -126,6 +136,7 @@ void SoundInput::handleStateChanged (QAudio::State newState) const
break;
case QAudio::ActiveState:
+ reset (false);
Q_EMIT status (tr ("Receiving"));
break;
@@ -140,7 +151,7 @@ void SoundInput::handleStateChanged (QAudio::State newState) const
#endif
case QAudio::StoppedState:
- if (audioError ())
+ if (!checkStream ())
{
Q_EMIT status (tr ("Error"));
}
@@ -152,18 +163,35 @@ void SoundInput::handleStateChanged (QAudio::State newState) const
}
}
-void SoundInput::stop()
+void SoundInput::reset (bool report_dropped_frames)
{
if (m_stream)
{
- m_stream->stop ();
+ auto elapsed_usecs = m_stream->elapsedUSecs ();
+ while (std::abs (elapsed_usecs - m_stream->processedUSecs ())
+ > 24 * 60 * 60 * 500000ll) // half day
+ {
+ // QAudioInput::elapsedUSecs() wraps after 24 hours
+ elapsed_usecs += 24 * 60 * 60 * 1000000ll;
+ }
+ // don't report first time as we don't yet known latency
+ if (cummulative_lost_usec_ != std::numeric_limits<qint64>::min () && report_dropped_frames)
+ {
+ auto lost_usec = elapsed_usecs - m_stream->processedUSecs () - cummulative_lost_usec_;
+ Q_EMIT dropped_frames (m_stream->format ().framesForDuration (lost_usec), lost_usec);
+ //qDebug () << "SoundInput::reset: frames dropped:" << m_stream->format ().framesForDuration (lost_usec) << "sec:" << lost_usec / 1.e6;
+ }
+ cummulative_lost_usec_ = elapsed_usecs - m_stream->processedUSecs ();
}
- m_stream.reset ();
+}
- if (m_sink)
+void SoundInput::stop()
+{
+ if (m_stream)
{
- m_sink->close ();
+ m_stream->stop ();
}
+ m_stream.reset ();
}
SoundInput::~SoundInput ()
diff --git a/Audio/soundin.h b/Audio/soundin.h
index 325d5e89..c35b3d7d 100644
--- a/Audio/soundin.h
+++ b/Audio/soundin.h
@@ -2,6 +2,7 @@
#ifndef SOUNDIN_H__
#define SOUNDIN_H__
+#include <limits>
#include <QObject>
#include <QString>
#include <QDateTime>
@@ -23,7 +24,7 @@ class SoundInput
public:
SoundInput (QObject * parent = nullptr)
: QObject {parent}
- , m_sink {nullptr}
+ , cummulative_lost_usec_ {std::numeric_limits<qint64>::min ()}
{
}
@@ -35,18 +36,21 @@ public:
Q_SLOT void suspend ();
Q_SLOT void resume ();
Q_SLOT void stop ();
+ Q_SLOT void reset (bool report_dropped_frames);
Q_SIGNAL void error (QString message) const;
Q_SIGNAL void status (QString message) const;
+ Q_SIGNAL void dropped_frames (qint32 dropped, qint64 usec);
private:
// used internally
- Q_SLOT void handleStateChanged (QAudio::State) const;
+ Q_SLOT void handleStateChanged (QAudio::State);
- bool audioError () const;
+ bool checkStream ();
QScopedPointer<QAudioInput> m_stream;
QPointer<AudioDevice> m_sink;
+ qint64 cummulative_lost_usec_;
};
#endif
diff --git a/Audio/soundout.cpp b/Audio/soundout.cpp
index 0264ad91..fe7c58fb 100644
--- a/Audio/soundout.cpp
+++ b/Audio/soundout.cpp
@@ -7,20 +7,13 @@
#include <qmath.h>
#include <QDebug>
-#include "moc_soundout.cpp"
+#include "Audio/AudioDevice.hpp"
-/*
-#if defined (WIN32)
-# define MS_BUFFERED 1000u
-#else
-# define MS_BUFFERED 2000u
-#endif
-*/
-# define MS_BUFFERED 200u
+#include "moc_soundout.cpp"
-bool SoundOutput::audioError () const
+bool SoundOutput::checkStream () const
{
- bool result (true);
+ bool result {false};
Q_ASSERT_X (m_stream, "SoundOutput", "programming error");
if (m_stream) {
@@ -43,69 +36,83 @@ bool SoundOutput::audioError () const
break;
case QAudio::NoError:
- result = false;
+ result = true;
break;
}
}
return result;
}
-void SoundOutput::setFormat (QAudioDeviceInfo const& device, unsigned channels, unsigned msBuffered)
+void SoundOutput::setFormat (QAudioDeviceInfo const& device, unsigned channels, int frames_buffered)
{
Q_ASSERT (0 < channels && channels < 3);
+ m_device = device;
+ m_channels = channels;
+ m_framesBuffered = frames_buffered;
+}
+
+void SoundOutput::restart (QIODevice * source)
+{
+ if (!m_device.isNull ())
+ {
+ QAudioFormat format (m_device.preferredFormat ());
+ // qDebug () << "Preferred audio output format:" << format;
+ format.setChannelCount (m_channels);
+ format.setCodec ("audio/pcm");
+ format.setSampleRate (48000);
+ format.setSampleType (QAudioFormat::SignedInt);
+ format.setSampleSize (16);
+ format.setByteOrder (QAudioFormat::Endian (QSysInfo::ByteOrder));
+ if (!format.isValid ())
+ {
+ Q_EMIT error (tr ("Requested output audio format is not valid."));
+ }
+ else if (!m_device.isFormatSupported (format))
+ {
+ Q_EMIT error (tr ("Requested output audio format is not supported on device."));
+ }
+ else
+ {
+ // qDebug () << "Selected audio output format:" << format;
+ m_stream.reset (new QAudioOutput (m_device, format));
+ checkStream ();
+ m_stream->setVolume (m_volume);
+ m_stream->setNotifyInterval(1000);
+ error_ = false;
+
+ connect (m_stream.data(), &QAudioOutput::stateChanged, this, &SoundOutput::handleStateChanged);
+ connect (m_stream.data(), &QAudioOutput::notify, [this] () {checkStream ();});
- m_msBuffered = msBuffered;
-
- QAudioFormat format (device.preferredFormat ());
-// qDebug () << "Preferred audio output format:" << format;
- format.setChannelCount (channels);
- format.setCodec ("audio/pcm");
- format.setSampleRate (48000);
- format.setSampleType (QAudioFormat::SignedInt);
- format.setSampleSize (16);
- format.setByteOrder (QAudioFormat::Endian (QSysInfo::ByteOrder));
- if (!format.isValid ())
+ // qDebug() << "A" << m_volume << m_stream->notifyInterval();
+ }
+ }
+ if (!m_stream)
{
- Q_EMIT error (tr ("Requested output audio format is not valid."));
+ if (!error_)
+ {
+ error_ = true; // only signal error once
+ Q_EMIT error (tr ("No audio output device configured."));
+ }
+ return;
}
- if (!device.isFormatSupported (format))
+ else
{
- Q_EMIT error (tr ("Requested output audio format is not supported on device."));
+ error_ = false;
}
-// qDebug () << "Selected audio output format:" << format;
-
- m_stream.reset (new QAudioOutput (device, format));
- audioError ();
- m_stream->setVolume (m_volume);
- m_stream->setNotifyInterval(100);
-
- connect (m_stream.data(), &QAudioOutput::stateChanged, this, &SoundOutput::handleStateChanged);
-
- // qDebug() << "A" << m_volume << m_stream->notifyInterval();
-}
-void SoundOutput::restart (QIODevice * source)
-{
- Q_ASSERT (m_stream);
-
- //
- // This buffer size is critical since for proper sound streaming. If
- // it is too short; high activity levels on the machine can starve
- // the audio buffer. On the other hand the Windows implementation
- // seems to take the length of the buffer in time to stop the audio
- // stream even if reset() is used.
- //
- // 2 seconds seems a reasonable compromise except for Windows
- // where things are probably broken.
- //
// we have to set this before every start on the stream because the
// Windows implementation seems to forget the buffer size after a
// stop.
- m_stream->setBufferSize (m_stream->format().bytesForDuration((m_msBuffered ? m_msBuffered : MS_BUFFERED) * 1000));
- // qDebug() << "B" << m_stream->bufferSize() <<
- // m_stream->periodSize() << m_stream->notifyInterval();
+ //qDebug () << "SoundOut default buffer size (bytes):" << m_stream->bufferSize () << "period size:" << m_stream->periodSize ();
+ if (m_framesBuffered)
+ {
+#if defined (Q_OS_WIN)
+ m_stream->setBufferSize (m_stream->format().bytesForFrames (m_framesBuffered));
+#endif
+ }
m_stream->setCategory ("production");
m_stream->start (source);
+ // qDebug () << "SoundOut selected buffer size (bytes):" << m_stream->bufferSize () << "period size:" << m_stream->periodSize ();
}
void SoundOutput::suspend ()
@@ -113,7 +120,7 @@ void SoundOutput::suspend ()
if (m_stream && QAudio::ActiveState == m_stream->state ())
{
m_stream->suspend ();
- audioError ();
+ checkStream ();
}
}
@@ -122,7 +129,7 @@ void SoundOutput::resume ()
if (m_stream && QAudio::SuspendedState == m_stream->state ())
{
m_stream->resume ();
- audioError ();
+ checkStream ();
}
}
@@ -131,7 +138,7 @@ void SoundOutput::reset ()
if (m_stream)
{
m_stream->reset ();
- audioError ();
+ checkStream ();
}
}
@@ -139,9 +146,10 @@ void SoundOutput::stop ()
{
if (m_stream)
{
+ m_stream->reset ();
m_stream->stop ();
- audioError ();
}
+ m_stream.reset ();
}
qreal SoundOutput::attenuation () const
@@ -171,8 +179,6 @@ void SoundOutput::resetAttenuation ()
void SoundOutput::handleStateChanged (QAudio::State newState)
{
- // qDebug () << "SoundOutput::handleStateChanged: newState:" << newState;
-
switch (newState)
{
case QAudio::IdleState:
@@ -194,7 +200,7 @@ void SoundOutput::handleStateChanged (QAudio::State newState)
#endif
case QAudio::StoppedState:
- if (audioError ())
+ if (!checkStream ())
{
Q_EMIT status (tr ("Error"));
}
diff --git a/Audio/soundout.h b/Audio/soundout.h
index 1e4b3a94..76711660 100644
--- a/Audio/soundout.h
+++ b/Audio/soundout.h
@@ -7,6 +7,7 @@
#include <QAudioOutput>
#include <QAudioDeviceInfo>
+class QIODevice;
class QAudioDeviceInfo;
// An instance of this sends audio data to a specified soundcard.
@@ -18,15 +19,16 @@ class SoundOutput
public:
SoundOutput ()
- : m_msBuffered {0u}
+ : m_framesBuffered {0}
, m_volume {1.0}
+ , error_ {false}
{
}
qreal attenuation () const;
public Q_SLOTS:
- void setFormat (QAudioDeviceInfo const& device, unsigned channels, unsigned msBuffered = 0u);
+ void setFormat (QAudioDeviceInfo const& device, unsigned channels, int frames_buffered = 0);
void restart (QIODevice *);
void suspend ();
void resume ();
@@ -40,15 +42,18 @@ Q_SIGNALS:
void status (QString message) const;
private:
- bool audioError () const;
+ bool checkStream () const;
private Q_SLOTS:
void handleStateChanged (QAudio::State);
private:
+ QAudioDeviceInfo m_device;
+ unsigned m_channels;
QScopedPointer<QAudioOutput> m_stream;
- unsigned m_msBuffered;
+ int m_framesBuffered;
qreal m_volume;
+ bool error_;
};
#endif