Cinder

  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

include/cinder/audio/PcmBuffer.h

Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2009, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/Cinder.h"
00026 #include "cinder/Exception.h"
00027 #include <vector>
00028 #include <boost/preprocessor/seq.hpp>
00029 
00030 namespace cinder { namespace audio {
00031 
00032 typedef enum ChannelIdentifier { 
00033     CHANNEL_FRONT_LEFT = 0, 
00034     CHANNEL_FRONT_RIGHT = 1
00035 } ChannelIdentifier;
00036 
00037 template<typename T>
00038 struct BufferT {
00039     uint32_t    mNumberChannels; 
00040     uint32_t    mDataByteSize;
00041     uint32_t    mSampleCount;
00042     T           * mData;
00043 };
00044 
00045 typedef BufferT<void> BufferGeneric; //used for internally handing around data
00046 typedef BufferT<uint8_t> Buffer8u;
00047 typedef BufferT<int8_t> Buffer8i;
00048 typedef BufferT<uint16_t> Buffer16u;
00049 typedef BufferT<int16_t> Buffer16i;
00050 typedef BufferT<int32_t> Buffer32i;
00051 typedef BufferT<uint32_t> Buffer32u;
00052 typedef BufferT<float> Buffer32f;
00053 
00054 typedef shared_ptr<BufferT<float> > Buffer32fRef;
00055 
00056 template<typename T>
00057 struct BufferListT { 
00058     uint32_t    mNumberBuffers; 
00059     BufferT<T>  * mBuffers; 
00060 };
00061 
00062 typedef BufferListT<void> BufferList;
00063 typedef BufferListT<float> BufferList32f;
00064 
00065 typedef shared_ptr<BufferList32f> BufferList32fRef;
00066 
00067 template<typename T>
00068 class PcmBufferT {
00069  public:
00070     PcmBufferT( uint32_t aMaxSampleCount, uint16_t aChannelCount, bool isInterleaved );
00071     ~PcmBufferT();
00072     
00073     uint32_t    getSampleCount( ChannelIdentifier channelId = CHANNEL_FRONT_LEFT ) const { return mBufferSampleCounts[channelId]; }
00074     uint32_t    getMaxSampleCount() const { return mMaxSampleCount; }
00075     uint16_t    getChannelCount() const { return mChannelCount; }
00076     bool        isInterleaved() const  { return mIsInterleaved; }
00077     
00078     shared_ptr<BufferT<T> >     getChannelData( ChannelIdentifier channelId ) const;
00079     shared_ptr<BufferT<T> >     getInterleavedData() const;
00080     
00081     //TODO: add support for adding/editing data at arbitrary positions in the buffer
00082     //TODO: add support for an appendData method that just accepts a Buffer or BufferList and interprets interleaving accordingly
00083     void        appendInterleavedData( T * aData, uint32_t aSampleCount );
00084     void        appendChannelData( T * aData, uint32_t aSampleCount, ChannelIdentifier channelId );
00085  private:
00086     std::vector<shared_ptr<BufferT<T> > > mBuffers;
00087     
00088     uint16_t        mBufferCount;
00089     uint32_t        * mBufferSampleCounts;
00090     uint32_t        mMaxSampleCount;
00091     uint16_t        mChannelCount;
00092     bool            mIsInterleaved;
00093 };
00094 
00095 typedef PcmBufferT<float> PcmBuffer32f;
00096 
00097 typedef shared_ptr<PcmBuffer32f> PcmBuffer32fRef;
00098 
00099 class PcmBufferException : public Exception {
00100 };
00101 
00102 class InvalidChannelPcmBufferException : public PcmBufferException {
00103 };
00104 
00105 class OutOfRangePcmBufferException : public PcmBufferException {
00106 };
00107 
00108 inline void silenceBuffers( BufferList * aBufferList )
00109 {
00110     for( uint32_t i = 0; i < aBufferList->mNumberBuffers; i++ ) {
00111         memset( aBufferList->mBuffers[i].mData, 0, aBufferList->mBuffers[i].mDataByteSize );
00112     }
00113 }
00114 
00115 template<typename T> 
00116 void deleteBuffer( BufferT<T> * aBuffer );
00117 
00118 template<typename T>
00119 shared_ptr<BufferListT<T> > createBufferList( uint32_t sampleCount, uint16_t channelCount, bool isInterleaved );
00120 
00121 template<typename T> 
00122 void deleteBufferList( BufferListT<T> * aBufferList );
00123 
00124 #define AUDIO_DATA_TYPES (uint8_t)(int16_t)(int32_t)(float)
00125 
00126 }} //namespace