summaryrefslogtreecommitdiff
path: root/src/FSKSyncChannel_Mixer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/FSKSyncChannel_Mixer.cpp')
-rw-r--r--src/FSKSyncChannel_Mixer.cpp368
1 files changed, 368 insertions, 0 deletions
diff --git a/src/FSKSyncChannel_Mixer.cpp b/src/FSKSyncChannel_Mixer.cpp
new file mode 100644
index 0000000..c4e8d87
--- /dev/null
+++ b/src/FSKSyncChannel_Mixer.cpp
@@ -0,0 +1,368 @@
+/*
+Copyright (c) 2013-2014, John Hurst
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/*! \file FSKSyncChannel_Mixer.cpp
+ \version $Id$
+ \brief Read WAV files(s), multiplex multiple PCM frame buffers including FSK Sync into one
+*/
+
+#include <FSKSyncChannel_Mixer.h>
+
+#include <algorithm>
+
+#include <AS_DCP.h>
+#include <KM_log.h>
+#include <PCMDataProviders.h>
+
+using namespace ASDCP;
+using namespace Kumu;
+
+//
+ASDCP::FSKSyncChannelMixer::FSKSyncChannelMixer(const byte_t * trackUUID)
+ : m_inputs(), m_outputs(), m_trackUUID(), m_ADesc(), m_ChannelCount(0), m_FramesRead(0)
+{
+ ::memcpy(m_trackUUID, trackUUID, UUIDlen);
+}
+
+ASDCP::FSKSyncChannelMixer::~FSKSyncChannelMixer()
+{
+ clear();
+}
+
+void
+ASDCP::FSKSyncChannelMixer::clear()
+{
+ m_outputs.clear();
+ std::for_each(m_inputs.begin(), m_inputs.end(), delete_input());
+ m_inputs.clear();
+}
+
+//
+Result_t
+ASDCP::FSKSyncChannelMixer::OpenRead(ui32_t argc, const char** argv, const Rational& PictureRate)
+{
+ ASDCP_TEST_NULL(argv);
+ PathList_t TmpFileList;
+
+ for ( ui32_t i = 0; i < argc; ++i )
+ {
+ ASDCP_TEST_NULL_STR(argv[i]);
+ TmpFileList.push_back(argv[i]);
+ }
+
+ return OpenRead(TmpFileList, PictureRate);
+}
+
+//
+Result_t
+ASDCP::FSKSyncChannelMixer::OpenRead(const Kumu::PathList_t& argv, const Rational& PictureRate)
+{
+ Result_t result = RESULT_OK;
+ PathList_t::iterator fi;
+ Kumu::PathList_t file_list;
+ PCM::AudioDescriptor tmpDesc;
+
+ if ( argv.size() == 1 && PathIsDirectory(argv.front()) )
+ {
+ DirScanner Dir;
+ char name_buf[MaxFilePath];
+ result = Dir.Open(argv.front().c_str());
+
+ if ( KM_SUCCESS(result) )
+ result = Dir.GetNext(name_buf);
+
+ while ( KM_SUCCESS(result) )
+ {
+ if ( name_buf[0] != '.' ) // no hidden files
+ {
+ std::string tmp_path = argv.front() + "/" + name_buf;
+ file_list.push_back(tmp_path);
+ }
+
+ result = Dir.GetNext(name_buf);
+ }
+
+ if ( result == RESULT_ENDOFFILE )
+ {
+ result = RESULT_OK;
+ file_list.sort();
+ }
+ }
+ else
+ {
+ file_list = argv;
+ }
+
+ for ( fi = file_list.begin(); KM_SUCCESS(result) && fi != file_list.end(); ++fi )
+ {
+ result = OpenRead(*fi, PictureRate);
+ }
+
+ if ( ASDCP_SUCCESS(result) && (m_ChannelCount < IAB::SYNC_CHANNEL))
+ {
+ // fsk sync channel has not been added
+ result = MixInSilenceChannels();
+ if ( ASDCP_SUCCESS(result) )
+ result = MixInFSKSyncChannel();
+ }
+
+ if ( ASDCP_SUCCESS(result) )
+ {
+ m_ADesc.ChannelCount = m_ChannelCount;
+ m_ADesc.AvgBps = (ui32_t)(ceil(m_ADesc.AudioSamplingRate.Quotient()) * m_ADesc.BlockAlign);
+ }
+ else
+ {
+ clear();
+ }
+
+ return result;
+}
+
+//
+Result_t
+ASDCP::FSKSyncChannelMixer::OpenRead(const std::string& file, const Rational& PictureRate)
+{
+ Result_t result = RESULT_OK;
+ PCM::AudioDescriptor tmpDesc;
+ ui32_t numChannels = 0;
+ mem_ptr<WAVDataProvider> I = new WAVDataProvider;
+ result = I->OpenRead(file.c_str(), PictureRate);
+
+ if ( ASDCP_SUCCESS(result))
+ {
+ result = I->FillAudioDescriptor(tmpDesc);
+ }
+
+ if ( ASDCP_SUCCESS(result) )
+ {
+
+ if ( m_ChannelCount == 0 )
+ {
+ m_ADesc = tmpDesc;
+ }
+ else
+ {
+
+ if ( tmpDesc.AudioSamplingRate != m_ADesc.AudioSamplingRate )
+ {
+ DefaultLogSink().Error("AudioSamplingRate mismatch in PCM parser list.");
+ return RESULT_FORMAT;
+ }
+
+ if ( tmpDesc.QuantizationBits != m_ADesc.QuantizationBits )
+ {
+ DefaultLogSink().Error("QuantizationBits mismatch in PCM parser list.");
+ return RESULT_FORMAT;
+ }
+
+ if ( tmpDesc.ContainerDuration < m_ADesc.ContainerDuration )
+ m_ADesc.ContainerDuration = tmpDesc.ContainerDuration;
+
+ m_ADesc.BlockAlign += tmpDesc.BlockAlign;
+ }
+ }
+
+
+ if ( ASDCP_SUCCESS(result) )
+ {
+ numChannels = tmpDesc.ChannelCount; // default to all channels
+ if ((m_ChannelCount < IAB::SYNC_CHANNEL) && (m_ChannelCount + numChannels) > (IAB::SYNC_CHANNEL - 1))
+ {
+ // need to insert an fsk channel between the channels of this file.
+ numChannels = IAB::SYNC_CHANNEL - m_ChannelCount - 1;
+ m_outputs.push_back(std::make_pair(numChannels, I.get()));
+ m_ChannelCount += numChannels;
+ MixInFSKSyncChannel();
+ numChannels = tmpDesc.ChannelCount - numChannels;
+ }
+ m_outputs.push_back(std::make_pair(numChannels, I.get()));
+ m_inputs.push_back(I);
+ I.release();
+ m_ChannelCount += numChannels;
+ }
+ return result;
+}
+
+Result_t
+ASDCP::FSKSyncChannelMixer::MixInSilenceChannels()
+{
+ Result_t result = RESULT_OK;
+ PCM::AudioDescriptor tmpDesc;
+ ui32_t numSilenceChannels = IAB::SYNC_CHANNEL - m_ChannelCount - 1;
+ if (numSilenceChannels > 0)
+ {
+ mem_ptr<SilenceDataProvider> I = new SilenceDataProvider(numSilenceChannels,
+ m_ADesc.QuantizationBits,
+ m_ADesc.AudioSamplingRate.Numerator,
+ m_ADesc.EditRate);
+ result = I->FillAudioDescriptor(tmpDesc);
+ if ( ASDCP_SUCCESS(result) )
+ {
+ m_ADesc.BlockAlign += tmpDesc.BlockAlign;
+ m_ChannelCount += tmpDesc.ChannelCount;
+ m_outputs.push_back(std::make_pair(numSilenceChannels, I.get()));
+ m_inputs.push_back(I);
+ I.release();
+ assert(m_ChannelCount == (IAB::SYNC_CHANNEL - 1));
+ }
+ }
+ return result;
+}
+
+//
+Result_t
+ASDCP::FSKSyncChannelMixer::MixInFSKSyncChannel()
+{
+ Result_t result = RESULT_OK;
+ PCM::AudioDescriptor tmpDesc;
+ mem_ptr<FSKSyncDataProvider> I = new FSKSyncDataProvider(m_ADesc.QuantizationBits,
+ m_ADesc.AudioSamplingRate.Numerator,
+ m_ADesc.EditRate, m_trackUUID);
+ result = I->FillAudioDescriptor(tmpDesc);
+ if ( ASDCP_SUCCESS(result) )
+ {
+ m_ADesc.BlockAlign += tmpDesc.BlockAlign;
+ m_ChannelCount += tmpDesc.ChannelCount;
+ m_outputs.push_back(std::make_pair(tmpDesc.ChannelCount, I.get()));
+ m_inputs.push_back(I);
+ I.release();
+ assert(m_ChannelCount == IAB::SYNC_CHANNEL);
+ }
+ return result;
+}
+
+//
+Result_t
+ASDCP::FSKSyncChannelMixer::AppendSilenceChannels(const ui32_t& channel_count)
+{
+ if ( m_ADesc.QuantizationBits == 0 )
+ {
+ DefaultLogSink().Error("Mixer object contains no channels, call OpenRead() first.\n");
+ return RESULT_PARAM;
+ }
+
+ Result_t result = RESULT_OK;
+ PCM::AudioDescriptor tmpDesc;
+
+ if ( channel_count > 0 )
+ {
+ Kumu::mem_ptr<SilenceDataProvider> I =
+ new SilenceDataProvider(channel_count,
+ m_ADesc.QuantizationBits,
+ m_ADesc.AudioSamplingRate.Numerator,
+ m_ADesc.EditRate);
+
+ result = I->FillAudioDescriptor(tmpDesc);
+
+ if ( ASDCP_SUCCESS(result) )
+ {
+ m_ADesc.BlockAlign += tmpDesc.BlockAlign;
+ m_ChannelCount += tmpDesc.ChannelCount;
+ m_ADesc.ChannelCount = m_ChannelCount;
+ m_ADesc.AvgBps = (ui32_t)(ceil(m_ADesc.AudioSamplingRate.Quotient()) * m_ADesc.BlockAlign);
+
+ m_outputs.push_back(std::make_pair(channel_count, I.get()));
+ m_inputs.push_back(I);
+ I.release();
+ }
+ }
+
+ return result;
+}
+
+//
+Result_t
+ASDCP::FSKSyncChannelMixer::FillAudioDescriptor(PCM::AudioDescriptor& ADesc) const
+{
+ ADesc = m_ADesc;
+ return RESULT_OK;
+}
+
+//
+Result_t
+ASDCP::FSKSyncChannelMixer::Reset()
+{
+ Result_t result = RESULT_OK;
+ SourceList::iterator it;
+ SourceList::iterator lastInput = m_inputs.end();
+
+ for ( it = m_inputs.begin(); it != lastInput && ASDCP_SUCCESS(result) ; ++it )
+ result = (*it)->Reset();
+
+ return result;
+}
+
+
+//2
+Result_t
+ASDCP::FSKSyncChannelMixer::ReadFrame(PCM::FrameBuffer& OutFB)
+{
+
+
+ Result_t result = RESULT_OK;
+ SourceList::iterator iter;
+ SourceList::iterator lastInput = m_inputs.end();
+ ui32_t bufSize = PCM::CalcFrameBufferSize(m_ADesc);
+ assert( bufSize <= OutFB.Capacity());
+
+ for ( iter = m_inputs.begin(); iter != lastInput && ASDCP_SUCCESS(result) ; ++iter )
+ result = (*iter)->ReadFrame();
+
+ if ( ASDCP_SUCCESS(result) )
+ {
+ OutFB.Size(bufSize);
+ byte_t* Out_p = OutFB.Data();
+ byte_t* End_p = Out_p + OutFB.Size();
+ ui32_t bytesWritten = 0;
+ OutputList::iterator iter;
+ OutputList::iterator lastOutput = m_outputs.end();
+
+ while ( Out_p < End_p && ASDCP_SUCCESS(result) )
+ {
+ iter = m_outputs.begin();
+ while ( iter != lastOutput && ASDCP_SUCCESS(result) )
+ {
+ result = ((*iter).second)->PutSample((*iter).first, Out_p, &bytesWritten);
+ Out_p += bytesWritten;
+ ++iter;
+ }
+ }
+
+ if ( ASDCP_SUCCESS(result) )
+ {
+ assert(Out_p == End_p);
+ OutFB.FrameNumber(m_FramesRead++);
+ }
+ }
+
+ return result;
+}
+
+
+//
+// end FSKSyncChannel_Mixer.cpp
+//