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 * 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 "public/platform/WebAudioBus.h"
27
28#if ENABLE(WEB_AUDIO)
29#include "platform/audio/AudioBus.h"
30#else
31#include "wtf/ThreadSafeRefCounted.h"
32
33namespace WebCore {
34class AudioBus : public ThreadSafeRefCounted<AudioBus> {
35};
36} // namespace WebCore
37#endif
38
39#include "wtf/PassRefPtr.h"
40#include "wtf/RefPtr.h"
41
42using namespace WebCore;
43
44namespace blink {
45
46class WebAudioBusPrivate : public AudioBus {
47};
48
49void WebAudioBus::initialize(unsigned numberOfChannels, size_t length, double sampleRate)
50{
51#if ENABLE(WEB_AUDIO)
52    RefPtr<AudioBus> audioBus = AudioBus::create(numberOfChannels, length);
53    audioBus->setSampleRate(sampleRate);
54
55    if (m_private)
56        (static_cast<AudioBus*>(m_private))->deref();
57
58    audioBus->ref();
59    m_private = static_cast<WebAudioBusPrivate*>(audioBus.get());
60#else
61    ASSERT_NOT_REACHED();
62#endif
63}
64
65void WebAudioBus::resizeSmaller(size_t newLength)
66{
67#if ENABLE(WEB_AUDIO)
68    ASSERT(m_private);
69    if (m_private) {
70        ASSERT(newLength <= length());
71        m_private->resizeSmaller(newLength);
72    }
73#else
74    ASSERT_NOT_REACHED();
75#endif
76}
77
78void WebAudioBus::reset()
79{
80#if ENABLE(WEB_AUDIO)
81    if (m_private) {
82        (static_cast<AudioBus*>(m_private))->deref();
83        m_private = 0;
84    }
85#else
86    ASSERT_NOT_REACHED();
87#endif
88}
89
90unsigned WebAudioBus::numberOfChannels() const
91{
92#if ENABLE(WEB_AUDIO)
93    if (!m_private)
94        return 0;
95    return m_private->numberOfChannels();
96#else
97    ASSERT_NOT_REACHED();
98    return 0;
99#endif
100}
101
102size_t WebAudioBus::length() const
103{
104#if ENABLE(WEB_AUDIO)
105    if (!m_private)
106        return 0;
107    return m_private->length();
108#else
109    ASSERT_NOT_REACHED();
110    return 0;
111#endif
112}
113
114double WebAudioBus::sampleRate() const
115{
116#if ENABLE(WEB_AUDIO)
117    if (!m_private)
118        return 0;
119    return m_private->sampleRate();
120#else
121    ASSERT_NOT_REACHED();
122    return 0;
123#endif
124}
125
126float* WebAudioBus::channelData(unsigned channelIndex)
127{
128#if ENABLE(WEB_AUDIO)
129    if (!m_private)
130        return 0;
131    ASSERT(channelIndex < numberOfChannels());
132    return m_private->channel(channelIndex)->mutableData();
133#else
134    ASSERT_NOT_REACHED();
135    return 0;
136#endif
137}
138
139PassRefPtr<AudioBus> WebAudioBus::release()
140{
141#if ENABLE(WEB_AUDIO)
142    RefPtr<AudioBus> audioBus(adoptRef(static_cast<AudioBus*>(m_private)));
143    m_private = 0;
144    return audioBus;
145#else
146    ASSERT_NOT_REACHED();
147    return 0;
148#endif
149}
150
151} // namespace blink
152