2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
33 #include "AudioChannel.h"
35 #include "VectorMath.h"
38 #include <wtf/OwnPtr.h>
42 using namespace VectorMath;
44 void AudioChannel::scale(double scale)
46 float s = static_cast<float>(scale);
47 vsmul(data(), 1, &s, data(), 1, length());
50 void AudioChannel::copyFrom(const AudioChannel* sourceChannel)
52 bool isSafe = (sourceChannel && sourceChannel->length() >= length());
57 memcpy(data(), sourceChannel->data(), sizeof(float) * length());
60 void AudioChannel::copyFromRange(const AudioChannel* sourceChannel, unsigned startFrame, unsigned endFrame)
62 // Check that range is safe for reading from sourceChannel.
63 bool isRangeSafe = sourceChannel && startFrame < endFrame && endFrame <= sourceChannel->length();
68 // Check that this channel has enough space.
69 size_t rangeLength = endFrame - startFrame;
70 bool isRangeLengthSafe = rangeLength <= length();
71 ASSERT(isRangeLengthSafe);
72 if (!isRangeLengthSafe)
75 const float* source = sourceChannel->data();
76 float* destination = data();
77 memcpy(destination, source + startFrame, sizeof(float) * rangeLength);
80 void AudioChannel::sumFrom(const AudioChannel* sourceChannel)
82 bool isSafe = sourceChannel && sourceChannel->length() >= length();
87 vadd(data(), 1, sourceChannel->data(), 1, data(), 1, length());
90 float AudioChannel::maxAbsValue() const
92 const float* p = data();
97 max = std::max(max, fabsf(*p++));
104 #endif // ENABLE(WEB_AUDIO)