1 | /* |
2 | * Copyright (C) 2011, 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 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
16 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
17 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
18 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
19 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
22 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 | */ |
24 | |
25 | #include "config.h" |
26 | #include "WaveShaperNode.h" |
27 | |
28 | #if ENABLE(WEB_AUDIO) |
29 | |
30 | #include "AudioContext.h" |
31 | #include <wtf/IsoMallocInlines.h> |
32 | #include <wtf/MainThread.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | WTF_MAKE_ISO_ALLOCATED_IMPL(WaveShaperNode); |
37 | |
38 | WaveShaperNode::WaveShaperNode(AudioContext& context) |
39 | : AudioBasicProcessorNode(context, context.sampleRate()) |
40 | { |
41 | setNodeType(NodeTypeWaveShaper); |
42 | m_processor = std::make_unique<WaveShaperProcessor>(context.sampleRate(), 1); |
43 | |
44 | initialize(); |
45 | } |
46 | |
47 | void WaveShaperNode::setCurve(Float32Array& curve) |
48 | { |
49 | ASSERT(isMainThread()); |
50 | DEBUG_LOG(LOGIDENTIFIER); |
51 | waveShaperProcessor()->setCurve(&curve); |
52 | } |
53 | |
54 | Float32Array* WaveShaperNode::curve() |
55 | { |
56 | return waveShaperProcessor()->curve(); |
57 | } |
58 | |
59 | static inline WaveShaperProcessor::OverSampleType processorType(WaveShaperNode::OverSampleType type) |
60 | { |
61 | switch (type) { |
62 | case WaveShaperNode::OverSampleType::None: |
63 | return WaveShaperProcessor::OverSampleNone; |
64 | case WaveShaperNode::OverSampleType::_2x: |
65 | return WaveShaperProcessor::OverSample2x; |
66 | case WaveShaperNode::OverSampleType::_4x: |
67 | return WaveShaperProcessor::OverSample4x; |
68 | } |
69 | ASSERT_NOT_REACHED(); |
70 | return WaveShaperProcessor::OverSampleNone; |
71 | } |
72 | |
73 | void WaveShaperNode::setOversample(OverSampleType type) |
74 | { |
75 | ASSERT(isMainThread()); |
76 | INFO_LOG(LOGIDENTIFIER, type); |
77 | |
78 | // Synchronize with any graph changes or changes to channel configuration. |
79 | AudioContext::AutoLocker contextLocker(context()); |
80 | waveShaperProcessor()->setOversample(processorType(type)); |
81 | } |
82 | |
83 | auto WaveShaperNode::oversample() const -> OverSampleType |
84 | { |
85 | switch (const_cast<WaveShaperNode*>(this)->waveShaperProcessor()->oversample()) { |
86 | case WaveShaperProcessor::OverSampleNone: |
87 | return OverSampleType::None; |
88 | case WaveShaperProcessor::OverSample2x: |
89 | return OverSampleType::_2x; |
90 | case WaveShaperProcessor::OverSample4x: |
91 | return OverSampleType::_4x; |
92 | } |
93 | ASSERT_NOT_REACHED(); |
94 | return OverSampleType::None; |
95 | } |
96 | |
97 | } // namespace WebCore |
98 | |
99 | #endif // ENABLE(WEB_AUDIO) |
100 | |