Home | History | Annotate | Download | only in audio
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef AudioChannel_h
     30 #define AudioChannel_h
     31 
     32 #include "AudioArray.h"
     33 #include <wtf/PassOwnPtr.h>
     34 
     35 namespace WebCore {
     36 
     37 // An AudioChannel represents a buffer of non-interleaved floating-point audio samples.
     38 // The PCM samples are normally assumed to be in a nominal range -1.0 -> +1.0
     39 class AudioChannel {
     40     WTF_MAKE_NONCOPYABLE(AudioChannel);
     41 public:
     42     // Memory can be externally referenced, or can be internally allocated with an AudioFloatArray.
     43 
     44     // Reference an external buffer.
     45     AudioChannel(float* storage, size_t length)
     46         : m_length(length), m_rawPointer(storage) { }
     47 
     48     // Manage storage for us.
     49     explicit AudioChannel(size_t length)
     50         : m_length(length)
     51         , m_rawPointer(0)
     52     {
     53         m_memBuffer = adoptPtr(new AudioFloatArray(length));
     54     }
     55 
     56     // A "blank" audio channel -- must call set() before it's useful...
     57     AudioChannel()
     58         : m_length(0)
     59         , m_rawPointer(0)
     60     {
     61     }
     62 
     63     // Redefine the memory for this channel.
     64     // storage represents external memory not managed by this object.
     65     void set(float* storage, size_t length)
     66     {
     67         m_memBuffer.clear(); // cleanup managed storage
     68         m_rawPointer = storage;
     69         m_length = length;
     70     }
     71 
     72     // How many sample-frames do we contain?
     73     size_t length() const { return m_length; }
     74 
     75     // Direct access to PCM sample data
     76     float* data() { return m_rawPointer ? m_rawPointer : m_memBuffer->data(); }
     77     const float* data() const { return m_rawPointer ? m_rawPointer : m_memBuffer->data(); }
     78 
     79     // Zeroes out all sample values in buffer.
     80     void zero()
     81     {
     82         if (m_memBuffer.get())
     83             m_memBuffer->zero();
     84         else
     85             memset(m_rawPointer, 0, sizeof(float) * m_length);
     86     }
     87 
     88     // Scales all samples by the same amount.
     89     void scale(double scale);
     90 
     91     // A simple memcpy() from the source channel
     92     void copyFrom(const AudioChannel* sourceChannel);
     93 
     94     // Copies the given range from the source channel.
     95     void copyFromRange(const AudioChannel* sourceChannel, unsigned startFrame, unsigned endFrame);
     96 
     97     // Sums (with unity gain) from the source channel.
     98     void sumFrom(const AudioChannel* sourceChannel);
     99 
    100     // Returns maximum absolute value (useful for normalization).
    101     float maxAbsValue() const;
    102 
    103 private:
    104     size_t m_length;
    105 
    106     float* m_rawPointer;
    107     OwnPtr<AudioFloatArray> m_memBuffer;
    108 };
    109 
    110 } // WebCore
    111 
    112 #endif // AudioChannel_h
    113