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 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 | #include "config.h" |
30 | |
31 | #if ENABLE(WEB_AUDIO) |
32 | |
33 | #include "AudioChannel.h" |
34 | |
35 | #include "VectorMath.h" |
36 | #include <algorithm> |
37 | #include <math.h> |
38 | |
39 | namespace WebCore { |
40 | |
41 | using namespace VectorMath; |
42 | |
43 | void AudioChannel::resizeSmaller(size_t newLength) |
44 | { |
45 | ASSERT(newLength <= m_length); |
46 | if (newLength <= m_length) |
47 | m_length = newLength; |
48 | } |
49 | |
50 | void AudioChannel::scale(float scale) |
51 | { |
52 | if (isSilent()) |
53 | return; |
54 | |
55 | vsmul(data(), 1, &scale, mutableData(), 1, length()); |
56 | } |
57 | |
58 | void AudioChannel::copyFrom(const AudioChannel* sourceChannel) |
59 | { |
60 | bool isSafe = (sourceChannel && sourceChannel->length() >= length()); |
61 | ASSERT(isSafe); |
62 | |
63 | if (!isSafe || sourceChannel->isSilent()) { |
64 | zero(); |
65 | return; |
66 | } |
67 | memcpy(mutableData(), sourceChannel->data(), sizeof(float) * length()); |
68 | } |
69 | |
70 | void AudioChannel::copyFromRange(const AudioChannel* sourceChannel, unsigned startFrame, unsigned endFrame) |
71 | { |
72 | // Check that range is safe for reading from sourceChannel. |
73 | bool isRangeSafe = sourceChannel && startFrame < endFrame && endFrame <= sourceChannel->length(); |
74 | ASSERT(isRangeSafe); |
75 | if (!isRangeSafe) |
76 | return; |
77 | |
78 | if (sourceChannel->isSilent() && isSilent()) |
79 | return; |
80 | |
81 | // Check that this channel has enough space. |
82 | size_t rangeLength = endFrame - startFrame; |
83 | bool isRangeLengthSafe = rangeLength <= length(); |
84 | ASSERT(isRangeLengthSafe); |
85 | if (!isRangeLengthSafe) |
86 | return; |
87 | |
88 | const float* source = sourceChannel->data(); |
89 | float* destination = mutableData(); |
90 | |
91 | if (sourceChannel->isSilent()) { |
92 | if (rangeLength == length()) |
93 | zero(); |
94 | else |
95 | memset(destination, 0, sizeof(float) * rangeLength); |
96 | } else |
97 | memcpy(destination, source + startFrame, sizeof(float) * rangeLength); |
98 | } |
99 | |
100 | void AudioChannel::sumFrom(const AudioChannel* sourceChannel) |
101 | { |
102 | bool isSafe = sourceChannel && sourceChannel->length() >= length(); |
103 | ASSERT(isSafe); |
104 | if (!isSafe) |
105 | return; |
106 | |
107 | if (sourceChannel->isSilent()) |
108 | return; |
109 | |
110 | if (isSilent()) |
111 | copyFrom(sourceChannel); |
112 | else |
113 | vadd(data(), 1, sourceChannel->data(), 1, mutableData(), 1, length()); |
114 | } |
115 | |
116 | float AudioChannel::maxAbsValue() const |
117 | { |
118 | if (isSilent()) |
119 | return 0; |
120 | |
121 | float max = 0; |
122 | |
123 | vmaxmgv(data(), 1, &max, length()); |
124 | |
125 | return max; |
126 | } |
127 | |
128 | } // WebCore |
129 | |
130 | #endif // ENABLE(WEB_AUDIO) |
131 | |