Make sound asset language optional.
[libdcp.git] / src / sound_asset_writer.cc
index 5c5bcdec71165e6e6a472596f15e4f5b288918be..0d7d20744b0f1c961f6ab8633d8032511bdaa6bf 100644 (file)
     files in the program, then also delete it here.
 */
 
+
+/** @file  src/sound_asset_writer.cc
+ *  @brief SoundAssetWriter class
+ */
+
+
 #include "bitstream.h"
 #include "sound_asset_writer.h"
 #include "sound_asset.h"
@@ -42,6 +48,7 @@
 #include <asdcp/Metadata.h>
 #include <iostream>
 
+
 using std::min;
 using std::max;
 using std::cout;
@@ -58,14 +65,12 @@ struct SoundAssetWriter::ASDCPState
        ASDCP::PCM::AudioDescriptor desc;
 };
 
-SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, vector<Channel> active_channels, bool sync)
+
+SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, bool sync)
        : AssetWriter (asset, file)
        , _state (new SoundAssetWriter::ASDCPState)
        , _asset (asset)
-       , _frame_buffer_offset (0)
        , _sync (sync)
-       , _sync_packet (0)
-       , _active_channels (active_channels)
 {
        DCP_ASSERT (!_sync || _asset->channels() >= 14);
        DCP_ASSERT (!_sync || _asset->standard() == Standard::SMPTE);
@@ -74,7 +79,7 @@ SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path f
        _state->desc.EditRate = ASDCP::Rational (_asset->edit_rate().numerator, _asset->edit_rate().denominator);
        _state->desc.AudioSamplingRate = ASDCP::Rational (_asset->sampling_rate(), 1);
        _state->desc.Locked = 0;
-       _state->desc.ChannelCount = _asset->channels ();
+       _state->desc.ChannelCount = _asset->channels();
        _state->desc.QuantizationBits = 24;
        _state->desc.BlockAlign = 3 * _asset->channels();
        _state->desc.AvgBps = _asset->sampling_rate() * _state->desc.BlockAlign;
@@ -82,9 +87,7 @@ SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path f
        if (asset->standard() == Standard::INTEROP) {
                _state->desc.ChannelFormat = ASDCP::PCM::CF_NONE;
        } else {
-               /* Just use WTF ("wild track format") for SMPTE for now; searches suggest that this
-                  uses the same assignment as Interop.
-               */
+               /* As required by Bv2.1 */
                _state->desc.ChannelFormat = ASDCP::PCM::CF_CFG_4;
        }
 
@@ -108,23 +111,25 @@ SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path f
 void
 SoundAssetWriter::start ()
 {
-       Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->desc);
-       if (ASDCP_FAILURE (r)) {
-               boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r));
+       auto r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->desc);
+       if (ASDCP_FAILURE(r)) {
+               boost::throw_exception (FileError("could not open audio MXF for writing", _file.string(), r));
        }
 
-       if (_asset->standard() == Standard::SMPTE && !_active_channels.empty()) {
+       if (_asset->standard() == Standard::SMPTE) {
 
-               ASDCP::MXF::WaveAudioDescriptor* essence_descriptor = 0;
+               ASDCP::MXF::WaveAudioDescriptor* essence_descriptor = nullptr;
                _state->mxf_writer.OP1aHeader().GetMDObjectByType(
                        asdcp_smpte_dict->ul(ASDCP::MDD_WaveAudioDescriptor), reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&essence_descriptor)
                        );
                DCP_ASSERT (essence_descriptor);
                essence_descriptor->ChannelAssignment = asdcp_smpte_dict->ul(ASDCP::MDD_DCAudioChannelCfg_MCA);
 
-               ASDCP::MXF::SoundfieldGroupLabelSubDescriptor* soundfield = new ASDCP::MXF::SoundfieldGroupLabelSubDescriptor(asdcp_smpte_dict);
+               auto soundfield = new ASDCP::MXF::SoundfieldGroupLabelSubDescriptor(asdcp_smpte_dict);
                GenRandomValue (soundfield->MCALinkID);
-               soundfield->RFC5646SpokenLanguage = _asset->language();
+               if (auto lang = _asset->language()) {
+                       soundfield->RFC5646SpokenLanguage = *lang;
+               }
 
                const MCASoundField field = _asset->channels() > 10 ? MCASoundField::SEVEN_POINT_ONE : MCASoundField::FIVE_POINT_ONE;
 
@@ -141,15 +146,28 @@ SoundAssetWriter::start ()
                _state->mxf_writer.OP1aHeader().AddChildObject(soundfield);
                essence_descriptor->SubDescriptors.push_back(soundfield->InstanceUID);
 
-               for (auto i: _active_channels) {
-                       ASDCP::MXF::AudioChannelLabelSubDescriptor* channel = new ASDCP::MXF::AudioChannelLabelSubDescriptor(asdcp_smpte_dict);
+               /* We must describe at least the number of channels in `field', even if they aren't
+                * in the asset (I think)
+                */
+               int descriptors = max(_asset->channels(), field == MCASoundField::FIVE_POINT_ONE ? 6 : 8);
+
+               auto const used = used_audio_channels();
+
+               for (auto i = 0; i < descriptors; ++i) {
+                       auto dcp_channel = static_cast<dcp::Channel>(i);
+                       if (find(used.begin(), used.end(), dcp_channel) == used.end()) {
+                               continue;
+                       }
+                       auto channel = new ASDCP::MXF::AudioChannelLabelSubDescriptor(asdcp_smpte_dict);
                        GenRandomValue (channel->MCALinkID);
                        channel->SoundfieldGroupLinkID = soundfield->MCALinkID;
-                       channel->MCAChannelID = static_cast<int>(i) + 1;
-                       channel->MCATagSymbol = "ch" + channel_to_mca_id(i, field);
-                       channel->MCATagName = channel_to_mca_name(i, field);
-                       channel->RFC5646SpokenLanguage = _asset->language();
-                       channel->MCALabelDictionaryID = channel_to_mca_universal_label(i, field, asdcp_smpte_dict);
+                       channel->MCAChannelID = i + 1;
+                       channel->MCATagSymbol = "ch" + channel_to_mca_id(dcp_channel, field);
+                       channel->MCATagName = channel_to_mca_name(dcp_channel, field);
+                       if (auto lang = _asset->language()) {
+                               channel->RFC5646SpokenLanguage = *lang;
+                       }
+                       channel->MCALabelDictionaryID = channel_to_mca_universal_label(dcp_channel, field, asdcp_smpte_dict);
                        _state->mxf_writer.OP1aHeader().AddChildObject(channel);
                        essence_descriptor->SubDescriptors.push_back(channel->InstanceUID);
                }
@@ -160,9 +178,6 @@ SoundAssetWriter::start ()
 }
 
 
-/** @param data Pointer an array of float pointers, one for each channel.
- *  @param frames Number of frames i.e. number of floats that are given for each channel.
- */
 void
 SoundAssetWriter::write (float const * const * data, int frames)
 {
@@ -202,7 +217,7 @@ SoundAssetWriter::write (float const * const * data, int frames)
                }
                _frame_buffer_offset += 3 * ch;
 
-               DCP_ASSERT (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
+               DCP_ASSERT (_frame_buffer_offset <= int(_state->frame_buffer.Capacity()));
 
                /* Finish the MXF frame if required */
                if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
@@ -216,9 +231,9 @@ SoundAssetWriter::write (float const * const * data, int frames)
 void
 SoundAssetWriter::write_current_frame ()
 {
-       ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
-       if (ASDCP_FAILURE (r)) {
-               boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
+       auto const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
+       if (ASDCP_FAILURE(r)) {
+               boost::throw_exception (MiscError(String::compose("could not write audio MXF frame (%1)", static_cast<int>(r))));
        }
 
        ++_frames_written;
@@ -237,9 +252,9 @@ SoundAssetWriter::finalize ()
        }
 
        if (_started) {
-               ASDCP::Result_t const r = _state->mxf_writer.Finalize();
+               auto const r = _state->mxf_writer.Finalize();
                if (ASDCP_FAILURE(r)) {
-                       boost::throw_exception (MiscError (String::compose ("could not finalise audio MXF (%1)", int(r))));
+                       boost::throw_exception (MiscError(String::compose ("could not finalise audio MXF (%1)", static_cast<int>(r))));
                }
        }
 
@@ -261,7 +276,7 @@ SoundAssetWriter::create_sync_packets ()
        int remaining_bits = 0;
        /* How many packets in this edit unit (i.e. "frame") */
        int packets = 0;
-       Fraction const edit_rate = _asset->edit_rate ();
+       auto const edit_rate = _asset->edit_rate ();
        if (edit_rate == Fraction(24, 1)) {
                edit_rate_code = 0;
                remaining_bits = 25;