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#pragma once
31
32#include "ExceptionOr.h"
33#include <JavaScriptCore/Float32Array.h>
34#include <wtf/Lock.h>
35#include <wtf/Vector.h>
36
37namespace WebCore {
38
39class AudioBus;
40
41class AudioBuffer : public RefCounted<AudioBuffer> {
42public:
43 static RefPtr<AudioBuffer> create(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate);
44
45 // Returns nullptr if data is not a valid audio file.
46 static RefPtr<AudioBuffer> createFromAudioFileData(const void* data, size_t dataSize, bool mixToMono, float sampleRate);
47
48 // Format
49 size_t length() const { return m_length; }
50 double duration() const { return length() / sampleRate(); }
51 float sampleRate() const { return m_sampleRate; }
52
53 // Channel data access
54 unsigned numberOfChannels() const { return m_channels.size(); }
55 ExceptionOr<Ref<Float32Array>> getChannelData(unsigned channelIndex);
56 Float32Array* channelData(unsigned channelIndex);
57 void zero();
58
59 // Scalar gain
60 double gain() const { return m_gain; }
61 void setGain(double gain) { m_gain = gain; }
62
63 // Because an AudioBuffer has a JavaScript wrapper, which will be garbage collected, it may take a while for this object to be deleted.
64 // releaseMemory() can be called when the AudioContext goes away, so we can release the memory earlier than when the garbage collection happens.
65 // Careful! Only call this when the page unloads, after the AudioContext is no longer processing.
66 void releaseMemory();
67
68 size_t memoryCost() const;
69
70private:
71 AudioBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate);
72 explicit AudioBuffer(AudioBus&);
73
74 void invalidate();
75
76 double m_gain { 1.0 }; // scalar gain
77 float m_sampleRate;
78 mutable Lock m_channelsLock;
79 size_t m_length;
80 Vector<RefPtr<Float32Array>> m_channels;
81};
82
83} // namespace WebCore
84