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
14  * ANY 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
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include "third_party/blink/public/platform/web_audio_bus.h"
26 
27 #include "base/memory/scoped_refptr.h"
28 #include "third_party/blink/renderer/platform/audio/audio_bus.h"
29 
30 namespace blink {
31 
Initialize(unsigned number_of_channels,size_t length,double sample_rate)32 void WebAudioBus::Initialize(unsigned number_of_channels,
33                              size_t length,
34                              double sample_rate) {
35   scoped_refptr<AudioBus> audio_bus =
36       AudioBus::Create(number_of_channels, length);
37   audio_bus->SetSampleRate(sample_rate);
38 
39   if (private_)
40     private_->Release();
41 
42   audio_bus->AddRef();
43   private_ = audio_bus.get();
44 }
45 
ResizeSmaller(size_t new_length)46 void WebAudioBus::ResizeSmaller(size_t new_length) {
47   DCHECK(private_);
48   if (private_) {
49     DCHECK_LE(new_length, length());
50     private_->ResizeSmaller(new_length);
51   }
52 }
53 
Reset()54 void WebAudioBus::Reset() {
55   if (private_) {
56     private_->Release();
57     private_ = nullptr;
58   }
59 }
60 
NumberOfChannels() const61 unsigned WebAudioBus::NumberOfChannels() const {
62   if (!private_)
63     return 0;
64   return private_->NumberOfChannels();
65 }
66 
length() const67 size_t WebAudioBus::length() const {
68   if (!private_)
69     return 0;
70   return private_->length();
71 }
72 
SampleRate() const73 double WebAudioBus::SampleRate() const {
74   if (!private_)
75     return 0;
76   return private_->SampleRate();
77 }
78 
ChannelData(unsigned channel_index)79 float* WebAudioBus::ChannelData(unsigned channel_index) {
80   if (!private_)
81     return nullptr;
82   DCHECK_LT(channel_index, NumberOfChannels());
83   return private_->Channel(channel_index)->MutableData();
84 }
85 
Release()86 scoped_refptr<AudioBus> WebAudioBus::Release() {
87   scoped_refptr<AudioBus> audio_bus(private_);
88   private_->Release();
89   private_ = nullptr;
90   return audio_bus;
91 }
92 
93 }  // namespace blink
94