pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_BusReadChannel.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_BUSREADCHANNEL_H
00040 #define PLONK_BUSREADCHANNEL_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "../plonk_GraphForwardDeclarations.h"
00044 
00047 template<class SampleType>
00048 class BusReadChannelInternal 
00049 :   public ChannelInternal<SampleType, ChannelInternalCore::Data>
00050 {
00051 public:
00052     typedef ChannelInternalCore::Data                           Data;
00053     typedef ChannelBase<SampleType>                             ChannelType;
00054     typedef BusReadChannelInternal<SampleType>                  BusReadInternal;
00055     typedef ChannelInternal<SampleType,Data>                    Internal;
00056     typedef ChannelInternalBase<SampleType>                     InternalBase;
00057     typedef UnitBase<SampleType>                                UnitType;
00058     typedef InputDictionary                                     Inputs;
00059     typedef NumericalArray<SampleType>                          Buffer;
00060     typedef BusBuffer<SampleType>                               Bus;
00061     typedef PLONK_BUSARRAYBASETYPE<Bus>                         Busses;
00062     
00063     BusReadChannelInternal (Inputs const& inputs, 
00064                             Data const& data, 
00065                             BlockSize const& blockSize,
00066                             SampleRate const& sampleRate) throw()
00067     :   Internal (inputs, data, blockSize, sampleRate),
00068         nextValidReadTime (TimeStamp::getSentinel()),
00069         latency (0.0)
00070     {
00071     }
00072     
00073     Text getName() const throw()
00074     {
00075         const Busses& busses (this->getInputAsBusses (IOKey::Busses));
00076         const int numBusses = busses.length();
00077 
00078         Text name = "Bus Read (";
00079             
00080         for (int i = 0; i < numBusses; ++i)
00081         {
00082             name += busses.atUnchecked (i).getName();
00083             
00084             if (i < (numBusses - 1))
00085                 name += ", ";
00086         }
00087         
00088         name += ")";
00089         
00090         return name;
00091     }        
00092     
00093     IntArray getInputKeys() const throw()
00094     {
00095         const IntArray keys (IOKey::Busses);
00096         return keys;
00097     }    
00098     
00099     InternalBase* getChannel (const int index) throw()
00100     {
00101         const Inputs channelInputs = this->getInputs().getChannel (index);
00102         return new BusReadInternal (channelInputs, 
00103                                     this->getState(), 
00104                                     this->getBlockSize(),
00105                                     this->getSampleRate());
00106     }        
00107     
00108     void initChannel (const int channel) throw()
00109     {
00110         const Busses& busses (this->getInputAsBusses (IOKey::Busses));
00111         this->setSampleRate (busses.wrapAt (channel).getSampleRate());
00112         this->initValue (SampleType (0));
00113     }    
00114     
00115     double getLatency() const throw()
00116     {
00117         return latency;
00118     }
00119     
00120     void process (ProcessInfo& info, const int channel) throw()
00121     {                
00122         SampleType* const outputSamples = this->getOutputSamples();
00123         const int outputBufferLength = this->getOutputBuffer().length();
00124         
00125         Busses& busses (this->getInputAsBusses (IOKey::Busses));
00126         Bus& bus = busses.wrapAt (channel);
00127         
00128         plonk_assert (bus.getSampleRate() == this->getSampleRate());
00129         
00130         bus.read (nextValidReadTime, 
00131                   outputBufferLength, 
00132                   outputSamples);
00133         
00134         latency = (info.getTimeStamp() - nextValidReadTime).getValue();
00135     }
00136     
00137 
00138 private:
00139     TimeStamp nextValidReadTime;
00140     double latency;
00141 };
00142 
00143 
00144 
00145 //------------------------------------------------------------------------------
00146 
00158 template<class SampleType>
00159 class BusReadUnit
00160 {
00161 public:    
00162     typedef BusReadChannelInternal<SampleType>      BusReadInternal;
00163     typedef typename BusReadInternal::Data          Data;
00164     typedef ChannelBase<SampleType>                 ChannelType;
00165     typedef ChannelInternal<SampleType,Data>        Internal;
00166     typedef ChannelInternalBase<SampleType>         InternaBase;
00167     typedef UnitBase<SampleType>                    UnitType;
00168     typedef InputDictionary                         Inputs;
00169     typedef NumericalArray<SampleType>              Buffer;
00170     typedef BusBuffer<SampleType>                   Bus;
00171     typedef PLONK_BUSARRAYBASETYPE<Bus>             Busses;
00172     
00173     static inline UnitInfos getInfo() throw()
00174     {
00175         const double blockSize = (double)BlockSize::getDefault().getValue();
00176 
00177         return UnitInfo ("BusRead", "Reads samples from a bus.",
00178                          
00179                          // output
00180                          ChannelCount::VariableChannelCount, 
00181                          IOKey::Generic,    Measure::None,      IOInfo::NoDefault,   IOLimit::None, 
00182                          IOKey::End,
00183                          
00184                          // inputs
00185                          IOKey::Busses,     Measure::None,
00186                          IOKey::BlockSize,  Measure::Samples,   blockSize,           IOLimit::Minimum, Measure::Samples, 1.0,
00187                          IOKey::End);
00188     }    
00189     
00191     static UnitType ar (Busses const& busses,
00192                         BlockSize const& preferredBlockSize = BlockSize::getDefault()) throw()
00193     {        
00194         Inputs inputs;
00195         inputs.put (IOKey::Busses, busses);
00196         
00197         Data data = { -1.0, -1.0 };
00198         
00199         return UnitType::template createFromInputs<BusReadInternal> (inputs, 
00200                                                                      data, 
00201                                                                      preferredBlockSize, 
00202                                                                      SampleRate::noPreference());
00203     }
00204         
00205     static inline UnitType kr (Busses const& busses) throw()
00206     {
00207         return ar (busses, BlockSize::getControlRateBlockSize());
00208     }
00209       
00210 };
00211 
00212 typedef BusReadUnit<PLONK_TYPE_DEFAULT> BusRead;
00213 
00214 
00215 
00216 
00217 #endif // PLONK_BUSREADCHANNEL_H
00218 
00219 
 All Classes Functions Typedefs Enumerations Enumerator Properties