1 | /* |
2 | * Copyright (C) 2010 Google Inc. All rights reserved. |
3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
15 | * its contributors may be used to endorse or promote products derived |
16 | * from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #include "config.h" |
31 | |
32 | #if ENABLE(WEB_AUDIO) |
33 | |
34 | #include "AudioBuffer.h" |
35 | |
36 | #include "AudioContext.h" |
37 | #include "AudioFileReader.h" |
38 | #include <JavaScriptCore/JSCInlines.h> |
39 | #include <JavaScriptCore/TypedArrayInlines.h> |
40 | |
41 | namespace WebCore { |
42 | |
43 | RefPtr<AudioBuffer> AudioBuffer::create(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate) |
44 | { |
45 | if (sampleRate < 22050 || sampleRate > 96000 || numberOfChannels > AudioContext::maxNumberOfChannels() || !numberOfFrames) |
46 | return nullptr; |
47 | |
48 | auto buffer = adoptRef(*new AudioBuffer(numberOfChannels, numberOfFrames, sampleRate)); |
49 | if (!buffer->m_length) |
50 | return nullptr; |
51 | |
52 | return buffer; |
53 | } |
54 | |
55 | RefPtr<AudioBuffer> AudioBuffer::createFromAudioFileData(const void* data, size_t dataSize, bool mixToMono, float sampleRate) |
56 | { |
57 | RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(data, dataSize, mixToMono, sampleRate); |
58 | if (!bus) |
59 | return nullptr; |
60 | return adoptRef(*new AudioBuffer(*bus)); |
61 | } |
62 | |
63 | AudioBuffer::AudioBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate) |
64 | : m_sampleRate(sampleRate) |
65 | , m_length(numberOfFrames) |
66 | { |
67 | m_channels.reserveCapacity(numberOfChannels); |
68 | |
69 | for (unsigned i = 0; i < numberOfChannels; ++i) { |
70 | auto channelDataArray = Float32Array::tryCreate(m_length); |
71 | if (!channelDataArray) { |
72 | invalidate(); |
73 | break; |
74 | } |
75 | |
76 | channelDataArray->setNeuterable(false); |
77 | m_channels.append(WTFMove(channelDataArray)); |
78 | } |
79 | } |
80 | |
81 | AudioBuffer::AudioBuffer(AudioBus& bus) |
82 | : m_sampleRate(bus.sampleRate()) |
83 | , m_length(bus.length()) |
84 | { |
85 | // Copy audio data from the bus to the Float32Arrays we manage. |
86 | unsigned numberOfChannels = bus.numberOfChannels(); |
87 | m_channels.reserveCapacity(numberOfChannels); |
88 | for (unsigned i = 0; i < numberOfChannels; ++i) { |
89 | auto channelDataArray = Float32Array::tryCreate(m_length); |
90 | if (!channelDataArray) { |
91 | invalidate(); |
92 | break; |
93 | } |
94 | |
95 | channelDataArray->setNeuterable(false); |
96 | channelDataArray->setRange(bus.channel(i)->data(), m_length, 0); |
97 | m_channels.append(WTFMove(channelDataArray)); |
98 | } |
99 | } |
100 | |
101 | void AudioBuffer::invalidate() |
102 | { |
103 | releaseMemory(); |
104 | m_length = 0; |
105 | } |
106 | |
107 | void AudioBuffer::releaseMemory() |
108 | { |
109 | auto locker = holdLock(m_channelsLock); |
110 | m_channels.clear(); |
111 | } |
112 | |
113 | ExceptionOr<Ref<Float32Array>> AudioBuffer::getChannelData(unsigned channelIndex) |
114 | { |
115 | if (channelIndex >= m_channels.size()) |
116 | return Exception { SyntaxError }; |
117 | auto& channelData = *m_channels[channelIndex]; |
118 | return Float32Array::create(channelData.unsharedBuffer(), channelData.byteOffset(), channelData.length()); |
119 | } |
120 | |
121 | Float32Array* AudioBuffer::channelData(unsigned channelIndex) |
122 | { |
123 | if (channelIndex >= m_channels.size()) |
124 | return nullptr; |
125 | return m_channels[channelIndex].get(); |
126 | } |
127 | |
128 | void AudioBuffer::zero() |
129 | { |
130 | for (auto& channel : m_channels) |
131 | channel->zeroRange(0, length()); |
132 | } |
133 | |
134 | size_t AudioBuffer::memoryCost() const |
135 | { |
136 | // memoryCost() may be invoked concurrently from a GC thread, and we need to be careful |
137 | // about what data we access here and how. We need to hold a lock to prevent m_channels |
138 | // from being changed while we iterate it, but calling channel->byteLength() is safe |
139 | // because it doesn't involve chasing any pointers that can be nullified while the |
140 | // AudioBuffer is alive. |
141 | auto locker = holdLock(m_channelsLock); |
142 | size_t cost = 0; |
143 | for (auto& channel : m_channels) |
144 | cost += channel->byteLength(); |
145 | return cost; |
146 | } |
147 | |
148 | } // namespace WebCore |
149 | |
150 | #endif // ENABLE(WEB_AUDIO) |
151 | |