diff options
| author | hibby <d@vehibberd.com> | 2018-02-16 15:18:44 +0000 |
|---|---|---|
| committer | hibby <d@vehibberd.com> | 2018-02-16 15:18:44 +0000 |
| commit | 368f5331a7641772aa7e0cd1feea57d09a394a36 (patch) | |
| tree | b421ac60cc6eb726896ea268ea24e47fc4ce7c88 /Audio | |
| parent | 71c717edc92fa771c857b3c2e5ed65f523f68f90 (diff) | |
| parent | 37f727ac0746fd85504aa46c1f023c09b3eff33a (diff) | |
Update upstream source from tag 'upstream/1.8.0+repack'
Update to upstream version '1.8.0+repack'
with Debian dir a83ef78cb70a32d1211871fdc08f3ffa295e45a8
Diffstat (limited to 'Audio')
| -rw-r--r-- | Audio/BWFFile.cpp | 1077 | ||||
| -rw-r--r-- | Audio/BWFFile.hpp | 210 |
2 files changed, 1287 insertions, 0 deletions
diff --git a/Audio/BWFFile.cpp b/Audio/BWFFile.cpp new file mode 100644 index 00000000..364fa8f2 --- /dev/null +++ b/Audio/BWFFile.cpp @@ -0,0 +1,1077 @@ +#include "BWFFile.hpp" + +#include <cstring> +#include <numeric> +#include <algorithm> + +#include <qendian.h> +#include <QAudioFormat> +#include <QDateTime> +#include <QDate> +#include <QTime> +#include <QString> +#include <QUuid> + +#include "pimpl_impl.hpp" + +#include "moc_BWFFile.cpp" + +namespace +{ + // chunk descriptor + struct Desc + { + Desc () + : size_ {0} + { + } + + explicit Desc (char const * id, quint32 size = 0) + : size_ {size} + { + set (id); + } + + void set (char const * id = nullptr) + { + if (id) + { + auto len = std::min (size_t (4), strlen (id)); + memcpy (id_.data (), id, len); + if (len < 4u) + { + memset (id_.data () + len, ' ', 4u - len); + } + } + else + { + memcpy (id_.data (), "JUNK", 4); + } + } + + void set (char const * id, quint32 size) + { + set (id); + size_ = size; + } + + char * operator & () {return reinterpret_cast<char *> (this);} + char const * operator & () const {return reinterpret_cast<char const *> (this);} + + std::array<char, 4> id_; + quint32 size_; + }; + + // "fmt " chunk contents + struct FormatChunk + { + quint16 audio_format; + quint16 num_channels; + quint32 sample_rate; + quint32 byte_rate; + quint16 block_align; + quint16 bits_per_sample; + }; + + // "bext" chunk contents + struct BroadcastAudioExtension + { + using Version = BWFFile::BextVersion; + using UMID = BWFFile::UMID; + + BroadcastAudioExtension (Version version = Version::v_0) + : version_ {static_cast<quint16> (version)} + , umid_ {{}} + { + // 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_); + auto uuid = QUuid::createUuid ().toRfc4122 (); + std::copy (uuid.cbegin (), uuid.cend (), umid_.data () + 16); + } + + char description_[256]; + char originator_[32]; + char originator_reference_[32]; + char origination_date_[10]; + char origination_time_[10]; + quint32 time_reference_low_; + quint32 time_reference_high_; + quint16 version_; + UMID umid_; // V1 zero for V0 + quint16 loudness_value_; // V2 + quint16 loudness_range_; // V2 + quint16 max_true_peak_level_; // V2 + quint16 max_momentary_loudness_; // V2 + quint16 max_short_term_loudness_; // V2 + quint8 reserved_[180]; + char coding_history_[]; + }; +} + +class BWFFile::impl final +{ +public: + using FileError = QFile::FileError; + + impl (QAudioFormat const& format) + : header_dirty_ {true} + , format_ {format} + , header_length_ {-1} + , data_size_ {-1} + , error_ {FileError::NoError} + { + } + + impl (QAudioFormat const& format, QString const& name) + : header_dirty_ {true} + , format_ {format} + , file_ {name} + , header_length_ {-1} + , data_size_ {-1} + , error_ {FileError::NoError} + { + } + + impl (QAudioFormat const& format, QString const& name, InfoDictionary const& dictionary) + : header_dirty_ {true} + , format_ {format} + , file_ {name} + , info_dictionary_ {dictionary} + , header_length_ {-1} + , data_size_ {-1} + , error_ {FileError::NoError} + { + } + + ~impl () + { + if (file_.isOpen ()) file_.close (); + } + + bool initialize (BWFFile * self, OpenMode mode); + bool read_header (); + bool write_header (QAudioFormat); + bool update_header (); + + BroadcastAudioExtension const * bext () const + { + return bext_.isEmpty () ? nullptr : reinterpret_cast<BroadcastAudioExtension const *> (bext_.constData ()); + } + + BroadcastAudioExtension * bext () + { + if (bext_.isEmpty ()) // create a "bext" chunk in place + { + bext_.data (); + bext_.fill ('\0', sizeof (BroadcastAudioExtension)); + new (bext_.data ()) BroadcastAudioExtension {}; + } + return reinterpret_cast<BroadcastAudioExtension *> (bext_.data ()); + } + + bool header_dirty_; + QAudioFormat format_; + QFile file_; + QByteArray bext_; + InfoDictionary info_dictionary_; + qint64 header_length_; + qint64 data_size_; + FileError error_; +}; + +bool BWFFile::impl::initialize (BWFFile * self, OpenMode mode) +{ + bool result {false}; + if (mode & Append) + { + result = file_.seek (file_.size ()); + if (result) result = self->seek (file_.size () - header_length_); + } + else + { + result = self->seek (0); + } + return result; +} + +bool BWFFile::impl::read_header () +{ + header_length_ = -1; + data_size_ = -1; + if (!(file_.openMode () & ReadOnly)) return false; + if (!file_.seek (0)) return false; + Desc outer_desc; + quint32 outer_offset = file_.pos (); + quint32 outer_size {0}; + bool be {false}; + while (outer_offset < sizeof outer_desc + outer_desc.size_ - 1) // allow for uncounted pad + { + if (file_.read (&outer_desc, sizeof outer_desc) != sizeof outer_desc) return false; + be = !memcmp (&outer_desc.id_, "RIFX", 4); + outer_size = be ? qFromBigEndian<quint32> (outer_desc.size_) : qFromLittleEndian<quint32> (outer_desc.size_); + if (!memcmp (&outer_desc.id_, "RIFF", 4) || be) + { + // RIFF or RIFX + char riff_item[4]; + if (file_.read (riff_item, sizeof riff_item) != sizeof riff_item) return false; + if (!memcmp (riff_item, "WAVE", 4)) + { + // WAVE + Desc wave_desc; + quint32 wave_offset = file_.pos (); + quint32 wave_size {0}; + while (wave_offset < outer_offset + sizeof outer_desc + outer_size - 1) + { + if (file_.read (&wave_desc, sizeof wave_desc) != sizeof wave_desc) return false; + wave_size = be ? qFromBigEndian<quint32> (wave_desc.size_) : qFromLittleEndian<quint32> (wave_desc.size_); + if (!memcmp (&wave_desc.id_, "bext", 4)) + { + bext_ = file_.read (wave_size); + } + if (!memcmp (&wave_desc.id_, "fmt ", 4)) + { + FormatChunk fmt; + if (file_.read (reinterpret_cast<char *> (&fmt), sizeof fmt) != sizeof fmt) return false; + auto audio_format = be ? qFromBigEndian<quint16> (fmt.audio_format) : qFromLittleEndian<quint16> (fmt.audio_format); + if (audio_format != 0 && audio_format != 1) return false; // not PCM nor undefined + format_.setByteOrder (be ? QAudioFormat::BigEndian : QAudioFormat::LittleEndian); + format_.setChannelCount (be ? qFromBigEndian<quint16> (fmt.num_channels) : qFromLittleEndian<quint16> (fmt.num_channels)); + format_.setCodec ("audio/pcm"); + format_.setSampleRate (be ? qFromBigEndian<quint32> (fmt.sample_rate) : qFromLittleEndian<quint32> (fmt.sample_rate)); + int bits_per_sample {be ? qFromBigEndian<quint16> (fmt.bits_per_sample) : qFromLittleEndian<quint16> (fmt.bits_per_sample)}; + format_.setSampleSize (bits_per_sample); + format_.setSampleType (8 == bits_per_sample ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt); + } + else if (!memcmp (&wave_desc.id_, "data", 4)) + { + data_size_ = wave_size; + header_length_ = file_.pos (); + } + else if (!memcmp (&wave_desc.id_, "LIST", 4)) + { + char list_type[4]; + if (file_.read (list_type, sizeof list_type) != sizeof list_type) return false; + if (!memcmp (list_type, "INFO", 4)) + { + Desc info_desc; + quint32 info_offset = file_.pos (); + quint32 info_size {0}; + while (info_offset < wave_offset + sizeof wave_desc + wave_size - 1) + { + if (file_.read (&info_desc, sizeof info_desc) != sizeof info_desc) return false; + info_size = be ? qFromBigEndian<quint32> (info_desc.size_) : qFromLittleEndian<quint32> (info_desc.size_); + info_dictionary_[info_desc.id_] = file_.read (info_size); + if (!file_.seek (info_offset + sizeof info_desc + (info_size + 1) / 2 * 2)) return false;; + info_offset = file_.pos (); + } + } + } + if (!file_.seek (wave_offset + sizeof wave_desc + (wave_size + 1) / 2 * 2)) return false; + wave_offset = file_.pos (); + } + } + } + if (!file_.seek (outer_offset + sizeof outer_desc + (outer_size + 1) / 2 * 2)) return false; + outer_offset = file_.pos (); + } + return data_size_ >= 0 && file_.seek (header_length_); +} + +bool BWFFile::impl::write_header (QAudioFormat format) +{ + data_size_ = -1; + if ("audio/pcm" != format.codec ()) return false; + if (!(file_.openMode () & WriteOnly)) return false; + if (!file_.seek (0)) return false; + header_length_ = 0; + bool be {QAudioFormat::BigEndian == format_.byteOrder ()}; + Desc desc {be ? "RIFX" : "RIFF"}; + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + header_dirty_ = true; + if (file_.write ("WAVE", 4) != 4) return false; + + FormatChunk fmt; + if (be) + { + fmt.audio_format = qToBigEndian<quint16> (1); // PCM + fmt.num_channels = qToBigEndian<quint16> (format.channelCount ()); + fmt.sample_rate = qToBigEndian<quint32> (format.sampleRate ()); + fmt.byte_rate = qToBigEndian<quint32> (format.bytesForDuration (1000000)); + fmt.block_align = qToBigEndian<quint16> (format.bytesPerFrame ()); + fmt.bits_per_sample = qToBigEndian<quint16> (format.sampleSize ()); + desc.set ("fmt", qToBigEndian<quint32> (sizeof fmt)); + } + else + { + fmt.audio_format = qToLittleEndian<quint16> (1); // PCM + fmt.num_channels = qToLittleEndian<quint16> (format.channelCount ()); + fmt.sample_rate = qToLittleEndian<quint32> (format.sampleRate ()); + fmt.byte_rate = qToLittleEndian<quint32> (format.bytesForDuration (1000000)); + fmt.block_align = qToLittleEndian<quint16> (format.bytesPerFrame ()); + fmt.bits_per_sample = qToLittleEndian<quint16> (format.sampleSize ()); + desc.set ("fmt", qToLittleEndian<quint32> (sizeof fmt)); + } + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + if (file_.write (reinterpret_cast<char const *> (&fmt), sizeof fmt) != sizeof fmt) return false; + + desc.set ("data"); + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + header_length_ = file_.pos (); + return true; +} + +bool BWFFile::impl::update_header () +{ + if (header_length_ < 0 || !(file_.openMode () & WriteOnly)) return false; + auto position = file_.pos (); + bool be {QAudioFormat::BigEndian == format_.byteOrder ()}; + Desc desc; + auto size = data_size_ < 0 ? file_.size () - header_length_ : data_size_; + if (!file_.seek (header_length_ - sizeof desc)) return false; + desc.set ("data", be ? qToBigEndian<quint32> (size) : qToLittleEndian<quint32> (size)); + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + + if (!bext_.isEmpty ()) + { + if (!file_.seek (file_.size ())) return false; + auto size = bext_.size (); + desc.set ("bext", be ? qToBigEndian<quint32> (size) : qToLittleEndian<quint32> (size)); + if ((file_.size () % 2) && file_.write ("\0", 1) != 1) return false; + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + auto * data = reinterpret_cast<BroadcastAudioExtension *> (bext_.data ()); + if (be) + { + data->time_reference_low_ = qToBigEndian<quint32> (data->time_reference_low_); + data->time_reference_high_ = qToBigEndian<quint32> (data->time_reference_high_); + switch (static_cast<BextVersion> (data->version_)) + { + case BextVersion::v_0: + data->version_ = qToBigEndian<quint32> (data->version_); + default: + data->loudness_value_ = qToBigEndian<quint16> (data->loudness_value_); + data->loudness_range_ = qToBigEndian<quint16> (data->loudness_range_); + data->max_true_peak_level_ = qToBigEndian<quint16> (data->max_true_peak_level_); + data->max_momentary_loudness_ = qToBigEndian<quint16> (data->max_momentary_loudness_); + data->max_short_term_loudness_ = qToBigEndian<quint16> (data->max_short_term_loudness_); + } + } + else + { + data->time_reference_low_ = qToLittleEndian<quint32> (data->time_reference_low_); + data->time_reference_high_ = qToLittleEndian<quint32> (data->time_reference_high_); + switch (static_cast<BextVersion> (data->version_)) + { + case BextVersion::v_0: + data->version_ = qToLittleEndian<quint32> (data->version_); + default: + data->loudness_value_ = qToLittleEndian<quint16> (data->loudness_value_); + data->loudness_range_ = qToLittleEndian<quint16> (data->loudness_range_); + data->max_true_peak_level_ = qToLittleEndian<quint16> (data->max_true_peak_level_); + data->max_momentary_loudness_ = qToLittleEndian<quint16> (data->max_momentary_loudness_); + data->max_short_term_loudness_ = qToLittleEndian<quint16> (data->max_short_term_loudness_); + } + } + if (file_.write (bext_) != size) return false; + } + + if (info_dictionary_.size ()) + { + if (!file_.seek (file_.size ())) return false; + desc.set ("LIST"); + if ((file_.size () % 2) && file_.write ("\0", 1) != 1) return false; + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + auto list_start = file_.pos (); + if (file_.write ("INFO", 4) != 4) return false; + for (auto iter = info_dictionary_.constBegin () + ; iter != info_dictionary_.constEnd (); ++iter) + { + auto value = iter.value (); + auto len = value.size () + 1; // include terminating null char + desc.set (iter.key ().data (), be ? qToBigEndian<quint32> (len) : qToLittleEndian<quint32> (len)); + if ((file_.size () % 2) && file_.write ("\0", 1) != 1) return false; + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + if (file_.write (value.constData (), len) != len) return false; + } + auto size = file_.pos () - list_start; + if (!file_.seek (list_start - sizeof desc)) return false; + desc.set ("LIST", be ? qToBigEndian<quint32> (size) : qToLittleEndian<quint32> (size)); + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + } + + size = file_.size () - sizeof desc; + if ((file_.size () % 2) && file_.seek (file_.size ()) && file_.write ("\0", 1) != 1) return false; + if (!file_.seek (0)) return false; + desc.set (be ? "RIFX" : "RIFF", be ? qToBigEndian<quint32> (size) : qToLittleEndian<quint32> (size)); + if (file_.write (&desc, sizeof desc) != sizeof desc) return false; + return file_.seek (position); +} + +// +// BWFFile implementation +// +BWFFile::BWFFile (QAudioFormat const& format, QObject * parent) + : QIODevice {parent} + , m_ {format} +{ +} + +BWFFile::BWFFile (QAudioFormat const& format, QString const& name, QObject * parent) + : QIODevice {parent} + , m_ {format, name} +{ +} + +BWFFile::BWFFile (QAudioFormat const& format, QString const& name + , InfoDictionary const& dictionary, QObject * parent) + : QIODevice {parent} + , m_ {format, name, dictionary} +{ +} + +BWFFile::~BWFFile () +{ + if (isOpen ()) close (); +} + +bool BWFFile::open (OpenMode mode) +{ + bool success {false}; + if (!(mode & WriteOnly)) + { + if (!(success = m_->file_.open (mode & ~Text))) + { + setErrorString (m_->file_.errorString ()); + } + else + { + if (!(success = m_->read_header ())) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to read file header"); + m_->error_ = FileError::ReadError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + } + } + else + { + if (!(success = m_->file_.open (mode & ~Text))) + { + setErrorString (m_->file_.errorString ()); + } + else + { + if (!(success = (m_->read_header () || m_->write_header (m_->format_)))) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to update file header"); + m_->error_ = FileError::WriteError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + else if (!(success = m_->file_.resize (m_->header_length_))) + { + setErrorString (m_->file_.errorString ()); + } + } + } + success && (success = QIODevice::open (mode | Unbuffered)); + if (success && !(success = m_->initialize (this, mode))) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to initialize file header"); + m_->error_ = FileError::WriteError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + if (!success) close (); + return success; +} + +bool BWFFile::open(FILE * fh, OpenMode mode, FileHandleFlags flags) +{ + bool success {false}; + if (!(mode & ReadOnly)) + { + setErrorString ("Read or read/write access must be specified"); + m_->error_ = FileError::OpenError; + return success; + } + if (!(mode & WriteOnly)) + { + if (!(success = m_->file_.open (fh, mode & ~Text, flags))) + { + setErrorString (m_->file_.errorString ()); + } + else + { + if (!(success = m_->read_header ())) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to read file header"); + m_->error_ = FileError::ReadError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + } + } + else + { + if (!(success = m_->file_.open (fh, mode & ~Text, flags))) + { + setErrorString (m_->file_.errorString ()); + } + else + { + if (!(success = (m_->read_header () || m_->write_header (m_->format_)))) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to update file header"); + m_->error_ = FileError::WriteError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + else if (!(success = m_->file_.resize (m_->header_length_))) + { + setErrorString (m_->file_.errorString ()); + } + } + } + success && (success = QIODevice::open (mode | Unbuffered)); + if (success && !(success = m_->initialize (this, mode))) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to initialize file header"); + m_->error_ = FileError::WriteError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + if (!success) close (); + return success; +} + +bool BWFFile::open (int fd, OpenMode mode, FileHandleFlags flags) +{ + bool success {false}; + if (!(mode & ReadOnly)) + { + setErrorString ("Read or read/write access must be specified"); + m_->error_ = FileError::OpenError; + return success; + } + if (!(mode & WriteOnly)) + { + if (!(success = m_->file_.open (fd, mode & ~Text, flags))) + { + setErrorString (m_->file_.errorString ()); + } + else + { + if (!(success = m_->read_header ())) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to read file header"); + m_->error_ = FileError::ReadError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + } + } + else + { + if (!(success = m_->file_.open (fd, mode & ~Text, flags))) + { + setErrorString (m_->file_.errorString ()); + } + else + { + if (!(success = (m_->read_header () || m_->write_header (m_->format_)))) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to update file header"); + m_->error_ = FileError::WriteError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + else if (!(success = m_->file_.resize (m_->header_length_))) + { + setErrorString (m_->file_.errorString ()); + } + } + } + success && (success = QIODevice::open (mode | Unbuffered)); + if (success && !(success = m_->initialize (this, mode))) + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to initialize file header"); + m_->error_ = FileError::WriteError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + } + if (!success) close (); + return success; +} + +QAudioFormat const& BWFFile::format () const {return m_->format_;} + +auto BWFFile::list_info () -> InfoDictionary& +{ + m_->header_dirty_ = true; + return m_->info_dictionary_; +} + + +// Broadcast Audio Extension fields +auto BWFFile::bext_version () const -> BextVersion +{ + return static_cast<BextVersion> (m_->bext () ? 0 : m_->bext ()->version_); +} + +void BWFFile::bext_version (BextVersion version) +{ + m_->header_dirty_ = true; + m_->bext ()->version_ = static_cast<quint16> (version); +} + +QByteArray BWFFile::bext_description () const +{ + if (!m_->bext ()) return {}; + return QByteArray::fromRawData (m_->bext ()->description_, strlen (m_->bext ()->description_)); +} + +void BWFFile::bext_description (QByteArray const& description) +{ + m_->header_dirty_ = true; + std::strncpy (m_->bext ()->description_, description.constData (), sizeof (BroadcastAudioExtension::description_)); +} + +QByteArray BWFFile::bext_originator () const +{ + if (!m_->bext ()) return {}; + return QByteArray::fromRawData (m_->bext ()->originator_, strlen (m_->bext ()->originator_)); +} + +void BWFFile::bext_originator (QByteArray const& originator) +{ + m_->header_dirty_ = true; + std::strncpy (m_->bext ()->originator_, originator.constData (), sizeof (BroadcastAudioExtension::originator_)); +} + +QByteArray BWFFile::bext_originator_reference () const +{ + if (!m_->bext ()) return {}; + return QByteArray::fromRawData (m_->bext ()->originator_reference_, strlen (m_->bext ()->originator_reference_)); +} + +void BWFFile::bext_originator_reference (QByteArray const& reference) +{ + m_->header_dirty_ = true; + std::strncpy (m_->bext ()->originator_reference_, reference.constData (), sizeof (BroadcastAudioExtension::originator_reference_)); +} + +QDateTime BWFFile::bext_origination_date_time () const +{ + if (!m_->bext ()) return {}; + return {QDate::fromString (m_->bext ()->origination_date_, "yyyy-MM-dd"), + QTime::fromString (m_->bext ()->origination_time_, "hh-mm-ss"), Qt::UTC}; +} + +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_)); +} + +quint64 BWFFile::bext_time_reference () const +{ + if (!m_->bext ()) return 0; + return (quint64 (m_->bext ()->time_reference_high_) << 32) + m_->bext ()->time_reference_low_; +} + +void BWFFile::bext_time_reference (quint64 time_code) +{ + m_->header_dirty_ = true; + m_->bext ()->time_reference_low_ = time_code & 0x00000000ffffffffll; + m_->bext ()->time_reference_high_ = time_code >> 32; +} + +auto BWFFile::bext_umid () const -> UMID +{ + UMID umid {{'\0'}}; + if (m_->bext ()) + { + umid = m_->bext ()->umid_; + } + return umid; +} + +void BWFFile::bext_umid (UMID const& umid) +{ + m_->header_dirty_ = true; + m_->bext ()->umid_ = umid; +} + +quint16 BWFFile::bext_loudness_value () const +{ + if (!m_->bext ()) return 0; + return m_->bext ()->loudness_value_; +} + +void BWFFile::bext_loudness_value (quint16 value) +{ + m_->header_dirty_ = true; + m_->bext ()->loudness_value_ = value; +} + +quint16 BWFFile::bext_loudness_range () const +{ + if (!m_->bext ()) return 0; + return m_->bext ()->loudness_range_; +} + +void BWFFile::bext_loudness_range (quint16 range) +{ + m_->header_dirty_ = true; + m_->bext ()->loudness_range_ = range; +} + +quint16 BWFFile::bext_max_true_peak_level () const +{ + if (!m_->bext ()) return 0; + return m_->bext ()->max_true_peak_level_; +} + +void BWFFile::bext_max_true_peak_level (quint16 level) +{ + m_->header_dirty_ = true; + m_->bext ()->max_true_peak_level_ = level; +} + +quint16 BWFFile::bext_max_momentary_loudness () const +{ + if (!m_->bext ()) return 0; + return m_->bext ()->max_momentary_loudness_; +} + +void BWFFile::bext_max_momentary_loudness (quint16 loudness) +{ + m_->header_dirty_ = true; + m_->bext ()->max_momentary_loudness_ = loudness; +} + +quint16 BWFFile::bext_max_short_term_loudness () const +{ + if (!m_->bext ()) return 0; + return m_->bext ()->max_short_term_loudness_; +} + +void BWFFile::bext_max_short_term_loudness (quint16 loudness) +{ + m_->header_dirty_ = true; + m_->bext ()->max_short_term_loudness_ = loudness; +} + +QByteArray BWFFile::bext_coding_history () const +{ + if (size_t (m_->bext_.size ()) <= sizeof (BroadcastAudioExtension)) return {}; + return QByteArray::fromRawData (m_->bext ()->coding_history_, + m_->bext_.size () - sizeof (BroadcastAudioExtension)); +} + +void BWFFile::bext_coding_history (QByteArray const& text) +{ + m_->header_dirty_ = true; + m_->bext (); // ensure we have a correctly + // initialized m_->bext_ + auto length = std::min (strlen (text.constData ()), size_t (text.size ())); + m_->bext_.resize (sizeof (BroadcastAudioExtension) + length); + std::strncpy (m_->bext ()->coding_history_, text.constData (), length); +} + + +bool BWFFile::reset () +{ + bool success {true}; + if (m_->file_.isOpen ()) + { + m_->info_dictionary_.clear (); + m_->bext_.clear (); + auto size = m_->data_size_ < 0 ? m_->file_.size () - m_->header_length_ : m_->data_size_; + m_->data_size_ = size; + if (m_->header_length_ > qint64 (3 * sizeof (Desc) + 4 + sizeof (FormatChunk))) + { + // we need to move the data down + auto old_pos = m_->header_length_; + if (!(success = m_->write_header (m_->format_))) + { + auto new_pos = m_->header_length_; + QByteArray buffer; + while (size) + { + success = m_->file_.seek (old_pos); + if (!success) + { + setErrorString (m_->file_.errorString ()); + } + else + { + buffer = m_->file_.read (std::min (size, qint64 (32768))); + success = m_->file_.seek (new_pos); + if (!success) + { + setErrorString (m_->file_.errorString ()); + } + else + { + qint64 bytes = m_->file_.write (buffer); + if (bytes < 0) + { + setErrorString (m_->file_.errorString ()); + success = false; + } + else + { + new_pos += buffer.size (); + old_pos += buffer.size (); + size -= buffer.size (); + } + } + } + } + } + else + { + if (FileError::NoError == m_->file_.error ()) + { + setErrorString ("Unable to update file header"); + m_->error_ = FileError::WriteError; + } + else + { + setErrorString (m_->file_.errorString ()); + } + return success; + } + } + if (success) + { + success = m_->file_.resize (m_->header_length_ + m_->data_size_); + if (!success) + { + setErrorString (m_->file_.errorString ()); + } + m_->header_dirty_ = true; + } + } + else + { + setErrorString ("File not open"); + m_->error_ = FileError::WriteError; + success = false; + } + return success && QIODevice::reset (); +} + +qint64 BWFFile::size () const +{ + return m_->data_size_ < 0 ? m_->file_.size () - m_->header_length_ : m_->data_size_; +} + +bool BWFFile::isSequential () const +{ + return m_->file_.isSequential (); +} + +void BWFFile::close () +{ + if (isOpen ()) QIODevice::close (); + if (m_->header_dirty_ || m_->data_size_ < 0) + { + m_->update_header (); + } + if (m_->file_.isOpen ()) + { + m_->file_.close (); + } +} + +bool BWFFile::seek (qint64 pos) +{ + if (pos < 0) + { + setErrorString ("Attempt to seek before beginning of data"); + m_->error_ = FileError::PositionError; + return false; + } + bool success = QIODevice::seek (pos); + if (success) + { + success = m_->file_.seek (pos + m_->header_length_); + if (!success) setErrorString (m_->file_.errorString ()); + } + return success; +} + +qint64 BWFFile::readData (char * data, qint64 max_size) +{ + return m_->file_.read (data, max_size); +} + +qint64 BWFFile::writeData (char const* data, qint64 max_size) +{ + auto bytes = m_->file_.write (data, max_size); + if (bytes > 0 && atEnd ()) + { + m_->header_dirty_ = true; + m_->data_size_ = -1; + } + return bytes; +} + +// forward to QFile +bool BWFFile::copy (QString const& new_name) +{ + close (); + return m_->file_.copy (new_name); +} + +bool BWFFile::exists () const {return m_->file_.exists ();} + +bool BWFFile::link (QString const& link_name) {return m_->file_.link (link_name);} + +bool BWFFile::remove () +{ + close (); + return m_->file_.remove (); +} + +bool BWFFile::rename (QString const& new_name) +{ + close (); + return m_->file_.rename (new_name); +} + +void BWFFile::setFileName (QString const& name) {m_->file_.setFileName (name);} + +QString BWFFile::symLinkTarget () const {return m_->file_.symLinkTarget ();} + +QString BWFFile::fileName () const {return m_->file_.fileName ();} + +auto BWFFile::permissions () const -> Permissions {return m_->file_.permissions ();} + +bool BWFFile::resize (qint64 new_size) +{ + auto size = m_->file_.size (); + if (pos () > new_size) seek (new_size); + auto result = m_->file_.resize (m_->header_length_ + new_size); + if (m_->data_size_ >= 0) + { + // set any fresh bytes to zero + auto end_of_data = m_->header_length_ + m_->data_size_; + auto length = std::min (size - end_of_data, m_->file_.size () - end_of_data); + if (length > 0) + { + auto position = m_->file_.pos (); + m_->file_.seek (m_->header_length_ + m_->data_size_); + m_->file_.write (QByteArray {int (length), '\0'}); + m_->file_.seek (position); + } + m_->data_size_ = -1; + } + m_->header_dirty_ = true; + return result; +} + +bool BWFFile::setPermissions (Permissions permissions) +{ + bool success = m_->file_.setPermissions (permissions); + if (!success) setErrorString (m_->file_.errorString ()); + return success; +} + +auto BWFFile::error () const -> FileError +{ + return m_->error_ != FileError::NoError ? m_->error_ : m_->file_.error (); +} + +bool BWFFile::flush () +{ + bool success = m_->file_.flush (); + if (!success) setErrorString (m_->file_.errorString ()); + return success; +} + +int BWFFile::handle () const +{ + int h {m_->file_.handle ()}; + if (h < 0) const_cast<BWFFile *> (this)->setErrorString (m_->file_.errorString ()); + return h; +} + +uchar * BWFFile::map (qint64 offset, qint64 size, MemoryMapFlags flags) +{ + uchar * address = m_->file_.map (offset + m_->header_length_, size, flags); + if (!address) setErrorString (m_->file_.errorString ()); + return address; +} + +bool BWFFile::unmap (uchar * address) +{ + bool success = m_->file_.unmap (address); + if (!success) setErrorString (m_->file_.errorString ()); + return success; +} + +void BWFFile::unsetError () +{ + m_->error_ = FileError::NoError; + m_->file_.unsetError (); +} diff --git a/Audio/BWFFile.hpp b/Audio/BWFFile.hpp new file mode 100644 index 00000000..a2f41b38 --- /dev/null +++ b/Audio/BWFFile.hpp @@ -0,0 +1,210 @@ +#ifndef BWF_FILE_HPP__ +#define BWF_FILE_HPP__ + +#include <array> + +#include <QFile> +#include <QMap> +#include <QByteArray> + +#include "pimpl_h.hpp" + +class QObject; +class QString; +class QAudioFormat; + +// +// BWFFile - Broadcast Wave Format File (a.k.a. WAV file) +// +// The BWF file format is a backward compatible variation of the +// Microsoft WAV file format. It contains an extra chunk with id +// 'bext' that contains metadata defined by the EBU in: +// +// https://tech.ebu.ch/docs/tech/tech3285.pdf +// +// Also relevant is the recommendation document: +// +// https://tech.ebu.ch/docs/r/r098.pdf +// +// which suggests a format to the free text coding history field. +// +// This class also supports the LIST-INFO chunk type which also allows +// metadata to be added to a WAV file, the defined INFO tag ids are +// documented here: +// +// http://bwfmetaedit.sourceforge.net/listinfo.html +// +// These ids are not enforced but they are recommended as most +// operating systems and audio applications recognize some or more of +// them. Notably Microsoft Windows is not one of the operating systems +// that does :( In fact there seems to be no documented metadata +// tagging format that Windows Explorer recognizes. +// +// Changes to the 'bext' fields and the LIST-INFO dictionary may be +// made right up until the file is closed as the relevant chunks are +// saved to the end of the file after the end of the sample data. +// +// This class emulates the QFile class, in fact it uses a QFile object +// instance internally and forwards many of its operations directly to +// it. +// +// BWFFile is a QIODevice subclass and the implementation provides +// access to the audio sample data contained in the BWF file as if +// only that data were in the file. I.e. the first sample is at file +// offset zero and the size of the file is the size of the sample +// data. The headers, trailers and metadata are hidden but can be +// accessed by the operations below. +// +class BWFFile + : public QIODevice +{ + Q_OBJECT +public: + using FileHandleFlags = QFile::FileHandleFlags; + using Permissions = QFile::Permissions; + using FileError = QFile::FileError; + using MemoryMapFlags = QFile::MemoryMapFlags; + using InfoDictionary = QMap<std::array<char, 4>, QByteArray>; + using UMID = std::array<quint8, 64>; + + explicit BWFFile (QAudioFormat const&, QObject * parent = nullptr); + explicit BWFFile (QAudioFormat const&, QString const& name, + QObject * parent = nullptr); + + // The InfoDictionary should contain valid WAV format LIST-INFO + // identifiers as keys, a list of them can be found here: + // + // http://bwfmetaedit.sourceforge.net/listinfo.html + // + // For files opened for ReadOnly access the dictionary is not + // written to the file. For files opened ReadWrite, any existing + // LIST-INFO tags will be merged into the dictionary when the file + // is opened and if the file is modified the merged dictionary will + // be written back to the file. + // + // Note that the sample data may no be in the native endian, it is + // the callers responsibility to do any required endian + // conversions. The internal data is always in native endian with + // conversions being handled automatically. Use the BWF::format() + // operation to access the format including the + // QAudioFormat::byteOrder() operation to determine the data byte + // ordering. + // + explicit BWFFile (QAudioFormat const&, QString const& name, + InfoDictionary const&, QObject * parent = nullptr); + + ~BWFFile (); + QAudioFormat const& format () const; + InfoDictionary& list_info (); + + // + // Broadcast Audio Extension fields + // + // If any of these modifiers are called then a "bext" chunk will be + // written to the file if the file is writeable and the sample data + // is modified. + // + enum class BextVersion : quint16 {v_0, v_1, v_2}; + BextVersion bext_version () const; + void bext_version (BextVersion = BextVersion::v_2); + + QByteArray bext_description () const; + void bext_description (QByteArray const&); // max 256 bytes + + QByteArray bext_originator () const; + void bext_originator (QByteArray const&); // max 32 bytes + + QByteArray bext_originator_reference () const; + void bext_originator_reference (QByteArray const&); // max 32 bytes + + QDateTime bext_origination_date_time () const; + void bext_origination_date_time (QDateTime const&); // 1s resolution + + quint64 bext_time_reference () const; + void bext_time_reference (quint64); // samples since midnight at start + + UMID bext_umid () const; // bext version >= 1 only + void bext_umid (UMID const&); + + quint16 bext_loudness_value () const; + void bext_loudness_value (quint16); // bext version >= 2 only + + quint16 bext_loudness_range () const; + void bext_loudness_range (quint16); // bext version >= 2 only + + quint16 bext_max_true_peak_level () const; + void bext_max_true_peak_level (quint16); // bext version >= 2 only + + quint16 bext_max_momentary_loudness () const; + void bext_max_momentary_loudness (quint16); // bext version >= 2 only + + quint16 bext_max_short_term_loudness () const; + void bext_max_short_term_loudness (quint16); // bext version >= 2 only + + QByteArray bext_coding_history () const; + void bext_coding_history (QByteArray const&); // See EBU R 98 + + + // Emulate QFile interface + bool open (OpenMode) override; + bool open (FILE *, OpenMode, FileHandleFlags = QFile::DontCloseHandle); + bool open (int fd, OpenMode, FileHandleFlags = QFile::DontCloseHandle); + bool copy (QString const& new_name); + bool exists () const; + bool link (QString const& link_name); + bool remove (); + bool rename (QString const& new_name); + void setFileName (QString const& name); + QString symLinkTarget () const; + QString fileName () const; + Permissions permissions () const; + + // Resize is of the sample data portion, header and trailer chunks + // are excess to the given size + bool resize (qint64 new_size); + + bool setPermissions (Permissions permissions); + FileError error () const; + bool flush (); + int handle () const; + + // The mapping offset is relative to the start of the sample data + uchar * map (qint64 offset, qint64 size, + MemoryMapFlags = QFile::NoOptions); + bool unmap (uchar * address); + + void unsetError (); + + + // + // QIODevice implementation + // + + // The size returned is of the sample data only, header and trailer + // chunks are hidden and handled internally + qint64 size () const override; + + bool isSequential () const override; + + // The reset operation clears the 'bext' and LIST-INFO as if they + // were never supplied. If the file is writable the 'bext' and + // LIST-INFO chunks will not be written making the resulting file a + // lowest common denominator WAV file. + bool reset () override; + + // Seek offsets are relative to the start of the sample data + bool seek (qint64) override; + + // this can fail due to updating header issues, errors are ignored + void close () override; + +protected: + qint64 readData (char * data, qint64 max_size) override; + qint64 writeData (char const* data, qint64 max_size) override; + +private: + class impl; + pimpl<impl> m_; +}; + +#endif |
