summaryrefslogtreecommitdiff
path: root/Audio
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2020-05-11 22:17:58 +0200
committerChristoph Berg <myon@debian.org>2020-05-11 22:17:58 +0200
commit32a41b1abe1a4aab0892c9c2051be6ba5b379f1e (patch)
treec6e107c1ee4d70610e43a7912e57c720334a08c7 /Audio
parent08299e8b2bf50ba90daf570f57def529dd29d6e7 (diff)
New upstream version 2.2.0~rc1+repackupstream/2.2.0_rc1+repack
Diffstat (limited to 'Audio')
-rw-r--r--Audio/AudioDevice.cpp10
-rw-r--r--Audio/AudioDevice.hpp98
-rw-r--r--Audio/BWFFile.cpp36
-rw-r--r--Audio/soundin.cpp172
-rw-r--r--Audio/soundin.h52
-rw-r--r--Audio/soundout.cpp207
-rw-r--r--Audio/soundout.h54
7 files changed, 612 insertions, 17 deletions
diff --git a/Audio/AudioDevice.cpp b/Audio/AudioDevice.cpp
new file mode 100644
index 00000000..2b852582
--- /dev/null
+++ b/Audio/AudioDevice.cpp
@@ -0,0 +1,10 @@
+#include "AudioDevice.hpp"
+
+bool AudioDevice::initialize (OpenMode mode, Channel channel)
+{
+ m_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
new file mode 100644
index 00000000..c4354687
--- /dev/null
+++ b/Audio/AudioDevice.hpp
@@ -0,0 +1,98 @@
+#ifndef AUDIODEVICE_HPP__
+#define AUDIODEVICE_HPP__
+
+#include <QIODevice>
+
+class QDataStream;
+
+//
+// abstract base class for audio devices
+//
+class AudioDevice : public QIODevice
+{
+public:
+ enum Channel {Mono, Left, Right, Both}; // these are mapped to combobox index so don't change
+
+ static char const * toString (Channel c)
+ {
+ return Mono == c ? "Mono" : Left == c ? "Left" : Right == c ? "Right" : "Both";
+ }
+
+ static Channel fromString (QString const& str)
+ {
+ QString s (str.toCaseFolded ().trimmed ().toLatin1 ());
+ return "both" == s ? Both : "right" == s ? Right : "left" == s ? Left : Mono;
+ }
+
+ bool initialize (OpenMode mode, Channel channel);
+
+ bool isSequential () const override {return true;}
+
+ size_t bytesPerFrame () const {return sizeof (qint16) * (Mono == m_channel ? 1 : 2);}
+
+ Channel channel () const {return m_channel;}
+
+protected:
+ AudioDevice (QObject * parent = 0)
+ : QIODevice (parent)
+ {
+ }
+
+ void store (char const * source, size_t numFrames, qint16 * dest)
+ {
+ 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;
+ }
+ }
+ }
+
+ qint16 * load (qint16 const sample, qint16 * dest)
+ {
+ switch (m_channel)
+ {
+ case Mono:
+ *dest++ = sample;
+ break;
+
+ case Left:
+ *dest++ = sample;
+ *dest++ = 0;
+ break;
+
+ case Right:
+ *dest++ = 0;
+ *dest++ = sample;
+ break;
+
+ case Both:
+ *dest++ = sample;
+ *dest++ = sample;
+ break;
+ }
+ return dest;
+ }
+
+private:
+ Channel m_channel;
+};
+
+Q_DECLARE_METATYPE (AudioDevice::Channel);
+
+#endif
diff --git a/Audio/BWFFile.cpp b/Audio/BWFFile.cpp
index e293cbb3..78d6a79f 100644
--- a/Audio/BWFFile.cpp
+++ b/Audio/BWFFile.cpp
@@ -38,7 +38,7 @@ namespace
{
auto end = reinterpret_cast<char const *> (::memchr (id, '\0', 4u));
auto len = end ? end - id : 4u;
- memcpy (id_.data (), id, len);
+ ::memcpy (id_.data (), id, len);
if (len < 4u)
{
memset (id_.data () + len, ' ', 4u - len);
@@ -46,7 +46,7 @@ namespace
}
else
{
- memcpy (id_.data (), "JUNK", 4u);
+ ::memcpy (id_.data (), "JUNK", 4u);
}
}
@@ -86,12 +86,12 @@ namespace
{
// set some sensible defaults for the "bext" fields
auto now = QDateTime::currentDateTimeUtc ();
- std::strncpy (origination_date_,
- now.date ().toString ("yyyy-MM-dd").toLocal8Bit ().constData (),
- sizeof origination_date_);
- std::strncpy (origination_time_,
- now.time ().toString ("hh-mm-ss").toLocal8Bit ().constData (),
- sizeof origination_time_);
+ ::memcpy (origination_date_,
+ now.date ().toString ("yyyy-MM-dd").toLocal8Bit ().constData (),
+ sizeof origination_date_);
+ ::memcpy (origination_time_,
+ now.time ().toString ("hh-mm-ss").toLocal8Bit ().constData (),
+ sizeof origination_time_);
auto uuid = QUuid::createUuid ().toRfc4122 ();
std::copy (uuid.cbegin (), uuid.cend (), umid_.data () + 16);
}
@@ -354,6 +354,7 @@ bool BWFFile::impl::update_header ()
{
case BextVersion::v_0:
data->version_ = qToBigEndian<quint32> (data->version_);
+ // fall through
default:
data->loudness_value_ = qToBigEndian<quint16> (data->loudness_value_);
data->loudness_range_ = qToBigEndian<quint16> (data->loudness_range_);
@@ -370,6 +371,7 @@ bool BWFFile::impl::update_header ()
{
case BextVersion::v_0:
data->version_ = qToLittleEndian<quint32> (data->version_);
+ // fall through
default:
data->loudness_value_ = qToLittleEndian<quint16> (data->loudness_value_);
data->loudness_range_ = qToLittleEndian<quint16> (data->loudness_range_);
@@ -686,7 +688,7 @@ QByteArray BWFFile::bext_description () const
void BWFFile::bext_description (QByteArray const& description)
{
m_->header_dirty_ = true;
- std::strncpy (m_->bext ()->description_, description.constData (), sizeof (BroadcastAudioExtension::description_));
+ ::memcpy (m_->bext ()->description_, description.constData (), sizeof BroadcastAudioExtension::description_);
}
QByteArray BWFFile::bext_originator () const
@@ -698,7 +700,7 @@ QByteArray BWFFile::bext_originator () const
void BWFFile::bext_originator (QByteArray const& originator)
{
m_->header_dirty_ = true;
- std::strncpy (m_->bext ()->originator_, originator.constData (), sizeof (BroadcastAudioExtension::originator_));
+ ::memcpy (m_->bext ()->originator_, originator.constData (), sizeof BroadcastAudioExtension::originator_);
}
QByteArray BWFFile::bext_originator_reference () const
@@ -710,7 +712,7 @@ QByteArray BWFFile::bext_originator_reference () const
void BWFFile::bext_originator_reference (QByteArray const& reference)
{
m_->header_dirty_ = true;
- std::strncpy (m_->bext ()->originator_reference_, reference.constData (), sizeof (BroadcastAudioExtension::originator_reference_));
+ ::memcpy (m_->bext ()->originator_reference_, reference.constData (), sizeof BroadcastAudioExtension::originator_reference_);
}
QDateTime BWFFile::bext_origination_date_time () const
@@ -723,12 +725,12 @@ QDateTime BWFFile::bext_origination_date_time () const
void BWFFile::bext_origination_date_time (QDateTime const& dt)
{
m_->header_dirty_ = true;
- std::strncpy (m_->bext ()->origination_date_,
- dt.date ().toString ("yyyy-MM-dd").toLocal8Bit ().constData (),
- sizeof (BroadcastAudioExtension::origination_date_));
- std::strncpy (m_->bext ()->origination_time_,
- dt.time ().toString ("hh-mm-ss").toLocal8Bit ().constData (),
- sizeof (BroadcastAudioExtension::origination_time_));
+ ::memcpy (m_->bext ()->origination_date_,
+ dt.date ().toString ("yyyy-MM-dd").toLocal8Bit ().constData (),
+ sizeof BroadcastAudioExtension::origination_date_);
+ ::memcpy (m_->bext ()->origination_time_,
+ dt.time ().toString ("hh-mm-ss").toLocal8Bit ().constData (),
+ sizeof BroadcastAudioExtension::origination_time_);
}
quint64 BWFFile::bext_time_reference () const
diff --git a/Audio/soundin.cpp b/Audio/soundin.cpp
new file mode 100644
index 00000000..e08d4527
--- /dev/null
+++ b/Audio/soundin.cpp
@@ -0,0 +1,172 @@
+#include "soundin.h"
+
+#include <QAudioDeviceInfo>
+#include <QAudioFormat>
+#include <QAudioInput>
+#include <QSysInfo>
+#include <QDebug>
+
+#include "moc_soundin.cpp"
+
+bool SoundInput::audioError () const
+{
+ bool result (true);
+
+ Q_ASSERT_X (m_stream, "SoundInput", "programming error");
+ if (m_stream)
+ {
+ switch (m_stream->error ())
+ {
+ case QAudio::OpenError:
+ Q_EMIT error (tr ("An error opening the audio input device has occurred."));
+ break;
+
+ case QAudio::IOError:
+ 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::FatalError:
+ Q_EMIT error (tr ("Non-recoverable error, audio input device not usable at this time."));
+ break;
+
+ case QAudio::NoError:
+ result = false;
+ break;
+ }
+ }
+ return result;
+}
+
+void SoundInput::start(QAudioDeviceInfo const& device, int framesPerBuffer, AudioDevice * sink, unsigned downSampleFactor, AudioDevice::Channel channel)
+{
+ Q_ASSERT (sink);
+
+ stop ();
+
+ m_sink = sink;
+
+ QAudioFormat format (device.preferredFormat());
+// qDebug () << "Preferred audio input format:" << format;
+ format.setChannelCount (AudioDevice::Mono == channel ? 1 : 2);
+ format.setCodec ("audio/pcm");
+ format.setSampleRate (12000 * downSampleFactor);
+ format.setSampleType (QAudioFormat::SignedInt);
+ format.setSampleSize (16);
+ format.setByteOrder (QAudioFormat::Endian (QSysInfo::ByteOrder));
+ if (!format.isValid ())
+ {
+ Q_EMIT error (tr ("Requested input audio format is not valid."));
+ return;
+ }
+
+ 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;
+
+ m_stream.reset (new QAudioInput {device, format});
+ if (audioError ())
+ {
+ return;
+ }
+
+ connect (m_stream.data(), &QAudioInput::stateChanged, this, &SoundInput::handleStateChanged);
+
+ m_stream->setBufferSize (m_stream->format ().bytesForFrames (framesPerBuffer));
+ if (sink->initialize (QIODevice::WriteOnly, channel))
+ {
+ m_stream->start (sink);
+ audioError ();
+ }
+ else
+ {
+ Q_EMIT error (tr ("Failed to initialize audio sink device"));
+ }
+}
+
+void SoundInput::suspend ()
+{
+ if (m_stream)
+ {
+ m_stream->suspend ();
+ audioError ();
+ }
+}
+
+void SoundInput::resume ()
+{
+// qDebug() << "Resume" << fmod(0.001*QDateTime::currentMSecsSinceEpoch(),6.0);
+ if (m_sink)
+ {
+ m_sink->reset ();
+ }
+
+ if (m_stream)
+ {
+ m_stream->resume ();
+ audioError ();
+ }
+}
+
+void SoundInput::handleStateChanged (QAudio::State newState) const
+{
+ // qDebug () << "SoundInput::handleStateChanged: newState:" << newState;
+
+ switch (newState)
+ {
+ case QAudio::IdleState:
+ Q_EMIT status (tr ("Idle"));
+ break;
+
+ case QAudio::ActiveState:
+ Q_EMIT status (tr ("Receiving"));
+ break;
+
+ case QAudio::SuspendedState:
+ Q_EMIT status (tr ("Suspended"));
+ break;
+
+#if QT_VERSION >= QT_VERSION_CHECK (5, 10, 0)
+ case QAudio::InterruptedState:
+ Q_EMIT status (tr ("Interrupted"));
+ break;
+#endif
+
+ case QAudio::StoppedState:
+ if (audioError ())
+ {
+ Q_EMIT status (tr ("Error"));
+ }
+ else
+ {
+ Q_EMIT status (tr ("Stopped"));
+ }
+ break;
+ }
+}
+
+void SoundInput::stop()
+{
+ if (m_stream)
+ {
+ m_stream->stop ();
+ }
+ m_stream.reset ();
+
+ if (m_sink)
+ {
+ m_sink->close ();
+ }
+}
+
+SoundInput::~SoundInput ()
+{
+ stop ();
+}
diff --git a/Audio/soundin.h b/Audio/soundin.h
new file mode 100644
index 00000000..325d5e89
--- /dev/null
+++ b/Audio/soundin.h
@@ -0,0 +1,52 @@
+// -*- Mode: C++ -*-
+#ifndef SOUNDIN_H__
+#define SOUNDIN_H__
+
+#include <QObject>
+#include <QString>
+#include <QDateTime>
+#include <QScopedPointer>
+#include <QPointer>
+#include <QAudioInput>
+
+#include "Audio/AudioDevice.hpp"
+
+class QAudioDeviceInfo;
+class QAudioInput;
+
+// Gets audio data from sound sample source and passes it to a sink device
+class SoundInput
+ : public QObject
+{
+ Q_OBJECT;
+
+public:
+ SoundInput (QObject * parent = nullptr)
+ : QObject {parent}
+ , m_sink {nullptr}
+ {
+ }
+
+ ~SoundInput ();
+
+ // sink must exist from the start call until the next start call or
+ // stop call
+ Q_SLOT void start(QAudioDeviceInfo const&, int framesPerBuffer, AudioDevice * sink, unsigned downSampleFactor, AudioDevice::Channel = AudioDevice::Mono);
+ Q_SLOT void suspend ();
+ Q_SLOT void resume ();
+ Q_SLOT void stop ();
+
+ Q_SIGNAL void error (QString message) const;
+ Q_SIGNAL void status (QString message) const;
+
+private:
+ // used internally
+ Q_SLOT void handleStateChanged (QAudio::State) const;
+
+ bool audioError () const;
+
+ QScopedPointer<QAudioInput> m_stream;
+ QPointer<AudioDevice> m_sink;
+};
+
+#endif
diff --git a/Audio/soundout.cpp b/Audio/soundout.cpp
new file mode 100644
index 00000000..0264ad91
--- /dev/null
+++ b/Audio/soundout.cpp
@@ -0,0 +1,207 @@
+#include "soundout.h"
+
+#include <QDateTime>
+#include <QAudioDeviceInfo>
+#include <QAudioOutput>
+#include <QSysInfo>
+#include <qmath.h>
+#include <QDebug>
+
+#include "moc_soundout.cpp"
+
+/*
+#if defined (WIN32)
+# define MS_BUFFERED 1000u
+#else
+# define MS_BUFFERED 2000u
+#endif
+*/
+# define MS_BUFFERED 200u
+
+bool SoundOutput::audioError () const
+{
+ bool result (true);
+
+ Q_ASSERT_X (m_stream, "SoundOutput", "programming error");
+ if (m_stream) {
+ switch (m_stream->error ())
+ {
+ case QAudio::OpenError:
+ Q_EMIT error (tr ("An error opening the audio output device has occurred."));
+ break;
+
+ case QAudio::IOError:
+ Q_EMIT error (tr ("An error occurred during write to the audio output device."));
+ break;
+
+ case QAudio::UnderrunError:
+ Q_EMIT error (tr ("Audio data not being fed to the audio output device fast enough."));
+ break;
+
+ case QAudio::FatalError:
+ Q_EMIT error (tr ("Non-recoverable error, audio output device not usable at this time."));
+ break;
+
+ case QAudio::NoError:
+ result = false;
+ break;
+ }
+ }
+ return result;
+}
+
+void SoundOutput::setFormat (QAudioDeviceInfo const& device, unsigned channels, unsigned msBuffered)
+{
+ Q_ASSERT (0 < channels && channels < 3);
+
+ 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 ())
+ {
+ Q_EMIT error (tr ("Requested output audio format is not valid."));
+ }
+ if (!device.isFormatSupported (format))
+ {
+ Q_EMIT error (tr ("Requested output audio format is not supported on device."));
+ }
+// 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();
+ m_stream->setCategory ("production");
+ m_stream->start (source);
+}
+
+void SoundOutput::suspend ()
+{
+ if (m_stream && QAudio::ActiveState == m_stream->state ())
+ {
+ m_stream->suspend ();
+ audioError ();
+ }
+}
+
+void SoundOutput::resume ()
+{
+ if (m_stream && QAudio::SuspendedState == m_stream->state ())
+ {
+ m_stream->resume ();
+ audioError ();
+ }
+}
+
+void SoundOutput::reset ()
+{
+ if (m_stream)
+ {
+ m_stream->reset ();
+ audioError ();
+ }
+}
+
+void SoundOutput::stop ()
+{
+ if (m_stream)
+ {
+ m_stream->stop ();
+ audioError ();
+ }
+}
+
+qreal SoundOutput::attenuation () const
+{
+ return -(20. * qLn (m_volume) / qLn (10.));
+}
+
+void SoundOutput::setAttenuation (qreal a)
+{
+ Q_ASSERT (0. <= a && a <= 999.);
+ m_volume = qPow(10.0, -a/20.0);
+ // qDebug () << "SoundOut: attn = " << a << ", vol = " << m_volume;
+ if (m_stream)
+ {
+ m_stream->setVolume (m_volume);
+ }
+}
+
+void SoundOutput::resetAttenuation ()
+{
+ m_volume = 1.;
+ if (m_stream)
+ {
+ m_stream->setVolume (m_volume);
+ }
+}
+
+void SoundOutput::handleStateChanged (QAudio::State newState)
+{
+ // qDebug () << "SoundOutput::handleStateChanged: newState:" << newState;
+
+ switch (newState)
+ {
+ case QAudio::IdleState:
+ Q_EMIT status (tr ("Idle"));
+ break;
+
+ case QAudio::ActiveState:
+ Q_EMIT status (tr ("Sending"));
+ break;
+
+ case QAudio::SuspendedState:
+ Q_EMIT status (tr ("Suspended"));
+ break;
+
+#if QT_VERSION >= QT_VERSION_CHECK (5, 10, 0)
+ case QAudio::InterruptedState:
+ Q_EMIT status (tr ("Interrupted"));
+ break;
+#endif
+
+ case QAudio::StoppedState:
+ if (audioError ())
+ {
+ Q_EMIT status (tr ("Error"));
+ }
+ else
+ {
+ Q_EMIT status (tr ("Stopped"));
+ }
+ break;
+ }
+}
diff --git a/Audio/soundout.h b/Audio/soundout.h
new file mode 100644
index 00000000..1e4b3a94
--- /dev/null
+++ b/Audio/soundout.h
@@ -0,0 +1,54 @@
+// -*- Mode: C++ -*-
+#ifndef SOUNDOUT_H__
+#define SOUNDOUT_H__
+
+#include <QObject>
+#include <QString>
+#include <QAudioOutput>
+#include <QAudioDeviceInfo>
+
+class QAudioDeviceInfo;
+
+// An instance of this sends audio data to a specified soundcard.
+
+class SoundOutput
+ : public QObject
+{
+ Q_OBJECT;
+
+public:
+ SoundOutput ()
+ : m_msBuffered {0u}
+ , m_volume {1.0}
+ {
+ }
+
+ qreal attenuation () const;
+
+public Q_SLOTS:
+ void setFormat (QAudioDeviceInfo const& device, unsigned channels, unsigned msBuffered = 0u);
+ void restart (QIODevice *);
+ void suspend ();
+ void resume ();
+ void reset ();
+ void stop ();
+ void setAttenuation (qreal); /* unsigned */
+ void resetAttenuation (); /* to zero */
+
+Q_SIGNALS:
+ void error (QString message) const;
+ void status (QString message) const;
+
+private:
+ bool audioError () const;
+
+private Q_SLOTS:
+ void handleStateChanged (QAudio::State);
+
+private:
+ QScopedPointer<QAudioOutput> m_stream;
+ unsigned m_msBuffered;
+ qreal m_volume;
+};
+
+#endif